Annotation of parser3/src/classes/string.C, revision 1.268

1.24      paf         1: /** @file
                      2:        Parser: @b string parser class.
                      3: 
1.267     moko        4:        Copyright (c) 2001-2026 Art. Lebedev Studio (https://www.artlebedev.com)
1.251     moko        5:        Authors: Konstantin Morshnev <moko@design.ru>, Alexandr Petrosian <paf@design.ru>
1.113     paf         6: */
1.24      paf         7: 
1.43      paf         8: #include "classes.h"
1.126     paf         9: #include "pa_vmethod_frame.h"
1.241     moko       10: #include "pa_base64.h"
1.126     paf        11: 
1.1       paf        12: #include "pa_request.h"
                     13: #include "pa_vdouble.h"
                     14: #include "pa_vint.h"
1.17      paf        15: #include "pa_vtable.h"
1.25      paf        16: #include "pa_vbool.h"
1.27      paf        17: #include "pa_string.h"
1.257     moko       18: #include "pa_varray.h"
1.53      parser     19: #include "pa_sql_connection.h"
1.71      parser     20: #include "pa_dictionary.h"
1.119     paf        21: #include "pa_vmethod_frame.h"
1.174     misha      22: #include "pa_vregex.h"
1.189     misha      23: #include "pa_charsets.h"
1.1       paf        24: 
1.268   ! moko       25: volatile const char * IDENT_STRING_C="$Id: string.C,v 1.267 2026/04/25 13:38:46 moko Exp $";
1.203     moko       26: 
1.41      paf        27: // class
                     28: 
1.126     paf        29: class MString: public Methoded {
1.41      paf        30: public:
1.126     paf        31:        MString();
1.41      paf        32: };
1.1       paf        33: 
1.126     paf        34: // global variable
                     35: 
1.224     moko       36: DECLARE_CLASS_VAR(string, new MString);
1.126     paf        37: 
1.196     moko       38: // void class, inherited from string and thus should be inited afterwards
                     39: 
                     40: class MVoid: public Methoded {
                     41: public:
                     42:        MVoid();
                     43: };
                     44: 
                     45: // void global variable should be after string global variable
                     46: 
1.224     moko       47: DECLARE_CLASS_VAR(void, new MVoid);
1.196     moko       48: 
1.126     paf        49: // defines for statics
                     50: 
                     51: #define MATCH_VAR_NAME "match"
1.162     misha      52: #define TRIM_START_OPTION "left"
                     53: #define TRIM_END_OPTION "right"
1.133     paf        54: #define TRIM_BOTH_OPTION "both"
1.126     paf        55: 
1.189     misha      56: #define MODE_APPEND "append"
                     57: 
1.220     moko       58: #define UNESCAPE_MODE_JS "js"
                     59: #define UNESCAPE_MODE_URI "uri"
                     60: 
1.126     paf        61: // statics
                     62: 
                     63: static const String match_var_name(MATCH_VAR_NAME);
                     64: 
1.1       paf        65: // methods
                     66: 
1.126     paf        67: static void _length(Request& r, MethodParams&) {
1.266     moko       68:        double result=(double)GET_SELF(r, VString).string().length(r.charsets.source());
1.233     moko       69:        r.write(*new VDouble(result));
1.1       paf        70: }
                     71: 
1.126     paf        72: static void _int(Request& r, MethodParams& params) {
                     73:        const String& self_string=GET_SELF(r, VString).string();
1.206     moko       74: 
                     75:        if(self_string.is_empty()) {
1.144     paf        76:                if(params.count()>0)
1.265     moko       77:                        r.write(*new VInt(params.as_wint(0, "default must be int", r))); // (default)
1.84      parser     78:                else
1.206     moko       79:                        throw Exception(PARSER_RUNTIME, 0, "unable to convert empty string without default specified");
                     80:        } else {
                     81:                try {
1.265     moko       82:                        r.write(*new VInt(self_string.as_wint()));
1.206     moko       83:                } catch(...) { // convert problem
                     84:                        if(params.count()>0)
1.265     moko       85:                                r.write(*new VInt(params.as_wint(0, "default must be int", r))); // (default)
1.206     moko       86:                        else
                     87:                                rethrow; // we have a problem when no default
                     88:                }
1.72      parser     89:        }
1.1       paf        90: }
                     91: 
1.126     paf        92: static void _double(Request& r, MethodParams& params) {
                     93:        const String& self_string=GET_SELF(r, VString).string();
1.206     moko       94: 
                     95:        if(self_string.is_empty()) {
1.144     paf        96:                if(params.count()>0)
1.233     moko       97:                        r.write(*new VDouble(params.as_double(0, "default must be double", r))); // (default)
1.84      parser     98:                else
1.206     moko       99:                        throw Exception(PARSER_RUNTIME, 0, "unable to convert empty string without default specified");
                    100:        } else {
                    101:                try {
1.233     moko      102:                        r.write(*new VDouble(self_string.as_double()));
1.206     moko      103:                } catch(...) { // convert problem
                    104:                        if(params.count()>0)
1.233     moko      105:                                r.write(*new VDouble(params.as_double(0, "default must be double", r))); // (default)
1.206     moko      106:                        else
                    107:                                rethrow; // we have a problem when no default
                    108:                }
1.72      parser    109:        }
1.1       paf       110: }
                    111: 
1.151     misha     112: static void _bool(Request& r, MethodParams& params) {
                    113:        const String& self_string=GET_SELF(r, VString).string();
                    114:        bool converted;
1.206     moko      115:        const char *str=self_string.cstr();
                    116: 
                    117:        if(self_string.is_empty()) {
                    118:                if(params.count()>0)
                    119:                        converted=params.as_bool(0, "default must be bool", r); // (default)
                    120:                else
                    121:                        throw Exception(PARSER_RUNTIME, 0, "unable to convert empty string without default specified");
1.207     moko      122:        } else if( (str[0]=='T' || str[0]=='t') && (str[1]=='R' || str[1]=='r') && (str[2]=='U' || str[2]=='u') &&
1.206     moko      123:                   (str[3]=='E' || str[3]=='e') && str[4]==0 ) { // "true"
                    124:                converted=true;
1.207     moko      125:        } else if( (str[0]=='F' || str[0]=='f') && (str[1]=='A' || str[1]=='a') && (str[2]=='L' || str[2]=='l') &&
1.206     moko      126:                   (str[3]=='S' || str[3]=='s') && (str[4]=='E' || str[4]=='e') && str[5]==0 ) { // "false"
                    127:                converted=false;
                    128:        } else {
1.158     misha     129:                try {
                    130:                        converted=self_string.as_bool();
1.206     moko      131:                } catch(...) { // convert problem
                    132:                        if(params.count()>0)
                    133:                                converted=params.as_bool(0, "default must be bool", r); // (default)
                    134:                        else
                    135:                                rethrow; // we have a problem when no default
1.158     misha     136:                }
1.151     misha     137:        }
                    138: 
1.233     moko      139:        r.write(VBool::get(converted));
1.151     misha     140: }
                    141: 
1.126     paf       142: /*not static*/void _string_format(Request& r, MethodParams& params) {
1.9       paf       143: 
1.126     paf       144:        Value& fmt_maybe_code=params[0];
1.95      paf       145:        // for some time due to stupid {} in original design
1.99      paf       146:        const String& fmt=r.process_to_string(fmt_maybe_code);
1.9       paf       147: 
1.256     moko      148:        const char* buf=format_double(r.get_self().as_double(), fmt.trim().cstrm());
1.63      parser    149: 
1.233     moko      150:        r.write(String(buf));
1.9       paf       151: }
1.11      paf       152: 
1.126     paf       153: static void _left(Request& r, MethodParams& params) {
1.244     moko      154:        int sn=params.as_int(0, "n must be int", r);
1.126     paf       155:        const String& string=GET_SELF(r, VString).string();
1.244     moko      156:        r.write(sn<0 ? string : string.mid(r.charsets.source(), 0, sn));
1.15      paf       157: }
                    158: 
1.126     paf       159: static void _right(Request& r, MethodParams& params) {
1.244     moko      160:        int sn=params.as_int(0, "n must be int", r);
1.216     moko      161:        if(sn>0){
1.244     moko      162:                size_t n=sn;
1.216     moko      163:                const String& string=GET_SELF(r, VString).string();
                    164:                size_t length=string.length(r.charsets.source());
1.233     moko      165:                r.write(n<length ? string.mid(r.charsets.source(), length-n, length, length) : string);
1.216     moko      166:        }
1.15      paf       167: }
                    168: 
1.126     paf       169: static void _mid(Request& r, MethodParams& params) {
                    170:        const String& string=GET_SELF(r, VString).string();
1.83      parser    171: 
1.245     moko      172:        int begin=params.as_int(0, "p must be int", r);
                    173:        if(begin<0)
                    174:                throw Exception(PARSER_RUNTIME, 0,  "p(%d) must be >=0", begin);
1.138     paf       175: 
                    176:        size_t end;
1.164     misha     177:        size_t length=0;
1.245     moko      178: 
1.138     paf       179:        if(params.count()>1) {
1.245     moko      180:                int n=params.as_int(1, "n must be int", r);
                    181:                if(n<0)
                    182:                        throw Exception(PARSER_RUNTIME, 0, "n(%d) must be >=0", n);
                    183:                end=begin+n;
1.164     misha     184:        } else {
                    185:                length=string.length(r.charsets.source());
                    186:                end=length;
                    187:        }
1.163     misha     188: 
1.233     moko      189:        r.write(string.mid(r.charsets.source(), begin, end, length));
1.15      paf       190: }
                    191: 
1.126     paf       192: static void _pos(Request& r, MethodParams& params) {
                    193:        Value& substr=params.as_no_junction(0, "substr must not be code");
1.16      paf       194:        
1.126     paf       195:        const String& string=GET_SELF(r, VString).string();
1.245     moko      196:        int offset=0;
1.165     misha     197:        if(params.count()>1){
                    198:                offset=params.as_int(1, "n must be int", r);
                    199:                if(offset<0)
1.227     moko      200:                        throw Exception(PARSER_RUNTIME, 0, "n(%d) must be >=0", offset);
1.165     misha     201:        }
                    202: 
1.245     moko      203:        r.write(*new VInt((int)string.pos(r.charsets.source(), substr.as_string(), offset)));
1.16      paf       204: }
                    205: 
1.268   ! moko      206: static void _rpos(Request& r, MethodParams& params) {
        !           207:        Value& substr=params.as_no_junction(0, "substr must not be code");
        !           208: 
        !           209:        const String& string=GET_SELF(r, VString).string();
        !           210:        size_t offset=string.length(); // from the very end
        !           211:        if(params.count()>1){
        !           212:                int n=params.as_int(1, "n must be int", r);
        !           213:                if(n<0)
        !           214:                        throw Exception(PARSER_RUNTIME, 0, "n(%d) must be >=0", n);
        !           215:                offset=n;
        !           216:        }
        !           217: 
        !           218:        r.write(*new VInt((int)string.rpos(r.charsets.source(), substr.as_string(), offset)));
        !           219: }
        !           220: 
1.250     moko      221: struct Split_action_info {
                    222:        const String& src;
                    223:        ArrayString &result;
1.258     moko      224:        Split_action_info(const String& s, ArrayString& r) : src(s), result(r) {}
1.250     moko      225: };
                    226: 
                    227: static void split_action(Table& , ArrayString* row, int prestart, int prefinish, int poststart, int postfinish, void *info) {
                    228:        Split_action_info& ai=*static_cast<Split_action_info *>(info);
                    229:        if(row) { // begin&middle
                    230:                // piece from last match['prestart'] to beginning of this match['prefinish']
                    231:                ai.result += &ai.src.mid(prestart, prefinish);
                    232:        } else // end
                    233:                if(poststart != postfinish)
                    234:                        ai.result += &ai.src.mid(poststart, postfinish);
                    235: }
                    236: 
1.249     moko      237: static void split_list(Value& delim_value, const String& string, ArrayString& result) {
1.254     moko      238:        if(VRegex *vregex=dynamic_cast<VRegex*>(&delim_value)){
1.250     moko      239:                vregex->study();
                    240: 
                    241:                int matches_count=0;
1.258     moko      242:                Split_action_info ai(string, result);
1.250     moko      243: 
                    244:                string.match(vregex, split_action, &ai, matches_count);
                    245:        } else
                    246:                string.split(result, 0, delim_value.as_string());
1.19      paf       247: }
                    248: 
1.118     paf       249: #define SPLIT_LEFT 0x0001
                    250: #define SPLIT_RIGHT 0x0010
                    251: #define SPLIT_HORIZONTAL 0x0100
                    252: #define SPLIT_VERTICAL 0x1000
1.257     moko      253: #define SPLIT_ARRAY 0x10000
1.118     paf       254: 
1.126     paf       255: static int split_options(const String* options) {
1.118     paf       256:     struct Split_option {
1.126     paf       257:                const char* keyL;
                    258:                const char* keyU;
1.118     paf       259:                int setBit;
                    260:                int checkBit;
                    261:     } split_option[]={
1.257     moko      262:                {"l", "L", SPLIT_LEFT, SPLIT_RIGHT}, // 0xAVHRL
1.118     paf       263:                {"r", "R", SPLIT_RIGHT, SPLIT_LEFT},
1.257     moko      264:                {"h", "H", SPLIT_HORIZONTAL, SPLIT_VERTICAL | SPLIT_ARRAY},
                    265:                {"v", "V", SPLIT_VERTICAL, SPLIT_HORIZONTAL | SPLIT_ARRAY},
                    266:                {"a", "A", SPLIT_ARRAY, SPLIT_VERTICAL | SPLIT_HORIZONTAL},
1.130     paf       267:                {0, 0, 0, 0}
1.118     paf       268:     };
                    269: 
                    270:        int result=0;
1.126     paf       271:        if(options) {
1.118     paf       272:                for(Split_option *o=split_option; o->keyL; o++) 
1.126     paf       273:                        if(options->pos(o->keyL)!=STRING_NOT_FOUND 
                    274:                                || (o->keyU && options->pos(o->keyU)!=STRING_NOT_FOUND)) {
1.118     paf       275:                                if(result & o->checkBit)
1.238     moko      276:                                        throw Exception(PARSER_RUNTIME, options, "conflicting split options");
1.118     paf       277:                                result |= o->setBit;
                    278:                        }
                    279:        }
                    280: 
                    281:        return result;
                    282: }
                    283: 
1.156     misha     284: static Table& split_vertical(ArrayString& pieces, bool right, const String* column_name) {
1.126     paf       285:        Table::columns_type columns(new ArrayString);
1.156     misha     286:        *columns+=column_name;
1.19      paf       287: 
1.126     paf       288:        Table& table=*new Table(columns, pieces.count());
1.118     paf       289:        if(right) { // right
1.126     paf       290:                for(int i=pieces.count(); --i>=0; ) {
                    291:                        Table::element_type row(new ArrayString);
                    292:                        *row+=pieces[i];
                    293:                        table+=row;
1.118     paf       294:                }
                    295:        } else { // left
1.253     moko      296:                for(ArrayString::Iterator i(pieces); i; ) {
1.126     paf       297:                        Table::element_type row(new ArrayString);
                    298:                        *row+=i.next();
                    299:                        table+=row;
1.118     paf       300:                }
1.61      parser    301:        }
1.118     paf       302: 
1.126     paf       303:        return table;
1.19      paf       304: }
                    305: 
1.128     paf       306: static Table& split_horizontal(ArrayString& pieces, bool right) {
1.126     paf       307:        Table& table=*new Table(Table::columns_type(0) /* nameless */);
                    308:        Table::element_type row(new ArrayString(pieces.count()));
1.118     paf       309:        if(right) { // right
1.131     paf       310:                for(int i=pieces.count(); --i>=0; )
1.126     paf       311:                        *row+=pieces[i];
1.118     paf       312:        } else { // left
1.253     moko      313:                for(ArrayString::Iterator i(pieces); i; )
1.126     paf       314:                        *row+=i.next();
1.118     paf       315:        }
1.126     paf       316:        table+=row;
1.118     paf       317: 
1.126     paf       318:        return table;
1.118     paf       319: }
                    320: 
1.257     moko      321: static VArray& split_array(ArrayString& pieces, bool right) {
                    322:        VArray& result=*new VArray(pieces.count());
                    323:        ArrayValue &array=result.array();
                    324:        if(right) { // right
                    325:                for(int i=pieces.count(); --i>=0; )
                    326:                        array+=new VString(*pieces[i]);
                    327:        } else { // left
                    328:                for(ArrayString::Iterator i(pieces); i; )
                    329:                        array+=new VString(*i.next());
                    330:        }
                    331:        return result;
                    332: }
                    333: 
1.238     moko      334: static void split_with_options(Request& r, MethodParams& params, int bits) {
1.126     paf       335:        const String& string=GET_SELF(r, VString).string();
1.176     misha     336:        size_t params_count=params.count();
1.19      paf       337: 
1.126     paf       338:        ArrayString pieces;
1.249     moko      339:        split_list(params.as_no_junction(0, "delimiter must not be code"), string, pieces);
1.19      paf       340: 
1.118     paf       341:        if(!bits) {
1.126     paf       342:                const String* options=0;
1.176     misha     343:                if(params_count>1)
1.187     misha     344:                        options=&params.as_string(1, OPTIONS_MUST_NOT_BE_CODE);
1.118     paf       345:                bits=split_options(options);
                    346:        }
1.21      paf       347: 
1.118     paf       348:        bool right=(bits & SPLIT_RIGHT) != 0;
                    349:        bool horizontal=(bits & SPLIT_HORIZONTAL) !=0;
1.257     moko      350:        bool array=(bits & SPLIT_ARRAY) !=0;
1.156     misha     351: 
                    352:        const String* column_name=0;
1.176     misha     353:        if(params_count>2){
1.156     misha     354:                column_name=&params.as_string(2, COLUMN_NAME_MUST_BE_STRING);
1.257     moko      355:                if (horizontal && !column_name->is_empty())
                    356:                        throw Exception(PARSER_RUNTIME, column_name, "column name cannot be specified when splitting into a table horizontal row");
                    357:                if (array && !column_name->is_empty())
                    358:                        throw Exception(PARSER_RUNTIME, column_name, "column name cannot be specified when splitting into an array");
                    359:        }
1.156     misha     360: 
1.257     moko      361:        if(array){
                    362:                r.write(split_array(pieces, right));
                    363:        } else {
                    364:                if(!column_name || column_name->is_empty())
                    365:                        column_name=new String("piece");
                    366:                Table& table=horizontal ? split_horizontal(pieces, right) : split_vertical(pieces, right, column_name);
                    367:                r.write(*new VTable(&table));
                    368:        }
1.118     paf       369: }
1.126     paf       370: static void _split(Request& r, MethodParams& params) {
                    371:        split_with_options(r, params, 0 /* maybe-determine from param #2 */);
1.118     paf       372: }
1.126     paf       373: static void _lsplit(Request& r, MethodParams& params) {
                    374:        split_with_options(r, params, SPLIT_LEFT);
1.118     paf       375: }
1.126     paf       376: static void _rsplit(Request& r, MethodParams& params) {
                    377:        split_with_options(r, params, SPLIT_RIGHT);
1.17      paf       378: }
                    379: 
1.126     paf       380: static void search_action(Table& table, Table::element_type row, int, int, int, int, void *) {
1.28      paf       381:        if(row)
                    382:                table+=row;
1.27      paf       383: }
                    384: 
1.74      parser    385: #ifndef DOXYGEN
1.27      paf       386: struct Replace_action_info {
1.181     misha     387:        Request* request;
                    388:        const String* src;
                    389:        String* dest;
1.126     paf       390:        VTable* vtable;
                    391:        Value* replacement_code;
1.27      paf       392: };
1.74      parser    393: #endif
1.105     paf       394: /// @todo they can do $global[$result] there, getting pointer to later-invalid local var, kill this
1.238     moko      395: static void replace_action(Table& table, ArrayString* row, int prestart, int prefinish, int poststart, int postfinish, void *info) {
1.27      paf       396:        Replace_action_info& ai=*static_cast<Replace_action_info *>(info);
1.31      paf       397:        if(row) { // begin&middle
1.104     paf       398:                // piece from last match['prestart'] to beginning of this match['prefinish']
                    399:                if(prestart!=prefinish)
                    400:                        *ai.dest << ai.src->mid(prestart, prefinish);//ai.dest->APPEND_CONST("-");
1.51      parser    401:                // store found parts in one-record VTable
1.126     paf       402:                if(table.count()) // middle
1.32      paf       403:                        table.put(0, row);
                    404:                else // begin
1.30      paf       405:                        table+=row;
1.181     misha     406: 
1.30      paf       407:                { // execute 'replacement_code' in 'table' context
1.181     misha     408:                        if(ai.replacement_code){
                    409:                                ai.vtable->set_table(table);
                    410:                                *ai.dest << ai.request->process_to_string(*ai.replacement_code);
                    411:                        }
1.29      paf       412:                }
                    413:        } else // end
1.104     paf       414:                *ai.dest << ai.src->mid(poststart, postfinish);
1.27      paf       415: }
                    416: 
1.126     paf       417: static void _match(Request& r, MethodParams& params) {
1.174     misha     418:        size_t params_count=params.count();
                    419: 
1.126     paf       420:        Value& regexp=params.as_no_junction(0, "regexp must not be code");
1.187     misha     421:        Value* options=(params_count>1)?&params.as_no_junction(1, OPTIONS_MUST_NOT_BE_CODE):0;
1.174     misha     422: 
1.254     moko      423:        VRegex* vregex=dynamic_cast<VRegex*>(&regexp);
1.174     misha     424:        VRegexCleaner vrcleaner;
1.126     paf       425: 
1.254     moko      426:        if(vregex){
1.174     misha     427:                if(options && options->is_defined())
1.255     moko      428:                        throw Exception(PARSER_RUNTIME, 0, "you cannot specify regex-object and options together");
1.174     misha     429:        } else {
1.237     moko      430:                vregex=new VRegex(r.charsets.source(), &regexp.as_string(), (options) ? (&options->as_string()) : 0);
1.175     misha     431:                vregex->study();
1.174     misha     432:                vrcleaner.vregex=vregex;
                    433:        }
1.126     paf       434: 
                    435:        const String& src=GET_SELF(r, VString).string();
1.152     misha     436:        int matches_count=0;
1.174     misha     437: 
                    438:        if(params_count<3) { // search
1.237     moko      439:                Table* table=src.match(vregex, search_action, 0, matches_count);
1.174     misha     440: 
1.152     misha     441:                if(table){
1.233     moko      442:                        r.write(*new VTable(table));
1.152     misha     443:                } else {
1.233     moko      444:                        r.write(*new VInt(matches_count));
1.152     misha     445:                }
                    446: 
1.27      paf       447:        } else { // replace
1.181     misha     448: 
                    449:                Value* replacement_code=0;
                    450:                bool is_junction=false;
                    451: 
                    452:                Value* replacement=&params[2];
                    453:                if(replacement->get_junction()){
                    454:                        replacement_code=replacement;
                    455:                        is_junction=true;
                    456:                } else if(replacement->is_string()){
                    457:                        if(replacement->is_defined())
                    458:                                replacement_code=replacement;
                    459:                } else if(!replacement->is_void())
1.238     moko      460:                        throw Exception(PARSER_RUNTIME, 0, "replacement option should be junction or string");
1.106     paf       461: 
1.238     moko      462:                Value* default_code=(params_count==4) ? &params.as_junction(3, "default value must be code") : 0;
1.192     misha     463: 
1.126     paf       464:                String result;
                    465:                VTable* vtable=new VTable;
1.130     paf       466:                Replace_action_info info={
                    467:                        &r,
                    468:                        &src,
                    469:                        &result,
                    470:                        vtable,
1.181     misha     471:                        replacement_code
1.130     paf       472:                };
1.181     misha     473: 
1.191     misha     474:                if(is_junction){
1.211     moko      475:                        Temp_value_element temp(r, *replacement_code->get_junction()->method_frame, match_var_name, vtable);
1.191     misha     476:                        src.match(vregex, replace_action, &info, matches_count);
                    477:                } else {
                    478:                        src.match(vregex, replace_action, &info, matches_count);
                    479:                }
1.181     misha     480: 
1.192     misha     481:                if(!matches_count && default_code)
                    482:                        r.process_write(*default_code);
                    483:                else
1.233     moko      484:                        r.write(result);
1.27      paf       485:        }
1.24      paf       486: }
                    487: 
1.238     moko      488: static void change_case(Request& r, MethodParams&, String::Change_case_kind kind) {
1.126     paf       489:        const String& src=GET_SELF(r, VString).string();
1.49      parser    490: 
1.233     moko      491:        r.write(src.change_case(r.charsets.source(), kind));
1.49      parser    492: }
1.126     paf       493: static void _upper(Request& r, MethodParams& params) {
                    494:        change_case(r, params, String::CC_UPPER);
1.49      parser    495: }
1.126     paf       496: static void _lower(Request& r, MethodParams& params) {
                    497:        change_case(r, params, String::CC_LOWER);
1.49      parser    498: }
                    499: 
1.65      parser    500: #ifndef DOXYGEN
1.126     paf       501: class String_sql_event_handlers: public SQL_Driver_query_event_handlers {
                    502:        bool got_column;
1.65      parser    503: public:
1.126     paf       504:        bool got_cell;
1.208     moko      505:        const String* result;
1.126     paf       506: public:
1.240     moko      507:        String_sql_event_handlers():
1.126     paf       508:                got_column(false),
                    509:                got_cell(false),
1.208     moko      510:                result(&String::Empty) {}
1.65      parser    511: 
1.128     paf       512:        bool add_column(SQL_Error& error, const char* /*str*/, size_t /*length*/) {
1.124     paf       513:                if(got_column) {
1.240     moko      514:                        error=SQL_Error("result must contain exactly one column");
1.124     paf       515:                        return true;
                    516:                }
1.65      parser    517:                got_column=true;
1.124     paf       518:                return false;
1.65      parser    519:        }
1.124     paf       520:        bool before_rows(SQL_Error& /*error*/ ) { /* ignore */ return false; }
                    521:        bool add_row(SQL_Error& /*error*/) { /* ignore */ return false; }
1.210     moko      522:        bool add_row_cell(SQL_Error& error, const char* str, size_t) {
1.124     paf       523:                if(got_cell) {
1.255     moko      524:                        error=SQL_Error("result must contain no more than one row");
1.124     paf       525:                        return true;
                    526:                }
                    527:                try {
                    528:                        got_cell=true;
1.208     moko      529:                        result=new String(str, String::L_TAINTED /* no length as 0x00 can be inside */ );
1.124     paf       530:                        return false;
                    531:                } catch(...) {
1.235     moko      532:                        error=SQL_Error("exception occurred in String_sql_event_handlers::add_row_cell");
1.124     paf       533:                        return true;
                    534:                }
1.65      parser    535:        }
                    536: };
                    537: #endif
1.239     moko      538: 
1.141     paf       539: extern int marshal_binds(HashStringValue& hash, SQL_Driver::Placeholder*& placeholders);
                    540: extern void unmarshal_bind_updates(HashStringValue& hash, int placeholder_count, SQL_Driver::Placeholder* placeholders);
                    541: 
1.204     moko      542: const String* sql_result_string(Request& r, MethodParams& params, Value*& default_code) {
1.126     paf       543:        Value& statement=params.as_junction(0, "statement must be code");
1.53      parser    544: 
1.141     paf       545:        HashStringValue* bind=0;
1.160     misha     546:        ulong limit=SQL_NO_LIMIT;
1.70      parser    547:        ulong offset=0;
1.81      parser    548:        default_code=0;
1.199     misha     549:        if(params.count()>1)
1.205     misha     550:                if(HashStringValue* options=params.as_hash(1, "sql options")) {
1.199     misha     551:                        int valid_options=0;
                    552:                        if(Value* vbind=options->get(sql_bind_name)) {
                    553:                                valid_options++;
                    554:                                bind=vbind->get_hash();
                    555:                        }
                    556:                        if(Value* vlimit=options->get(sql_limit_name)) {
                    557:                                valid_options++;
1.230     moko      558:                                limit=(ulong)r.process(*vlimit).as_double();
1.199     misha     559:                        }
                    560:                        if(Value* voffset=options->get(sql_offset_name)) {
                    561:                                valid_options++;
1.230     moko      562:                                offset=(ulong)r.process(*voffset).as_double();
1.199     misha     563:                        }
                    564:                        if((default_code=options->get(sql_default_name))) {
                    565:                                valid_options++;
                    566:                        }
                    567:                        if(valid_options!=options->count())
                    568:                                throw Exception(PARSER_RUNTIME, 0, CALLED_WITH_INVALID_OPTION);
1.200     misha     569:                }
1.70      parser    570: 
1.141     paf       571:        SQL_Driver::Placeholder* placeholders=0;
                    572:        uint placeholders_count=0;
                    573:        if(bind)
                    574:                placeholders_count=marshal_binds(*bind, placeholders);
                    575: 
1.99      paf       576:        const String& statement_string=r.process_to_string(statement);
1.232     moko      577:        const char* statement_cstr=statement_string.untaint_cstr(String::L_SQL, r.connection());
1.183     misha     578: 
1.240     moko      579:        String_sql_event_handlers handlers;
1.160     misha     580: 
1.240     moko      581:        r.connection()->query(statement_cstr, placeholders_count, placeholders, offset, limit, handlers, statement_string);
1.53      parser    582:        
1.141     paf       583:        if(bind)
                    584:                unmarshal_bind_updates(*bind, placeholders_count, placeholders);
                    585: 
1.65      parser    586:        if(!handlers.got_cell)
                    587:                return 0; // no lines, caller should return second param[default value]
1.62      parser    588: 
1.208     moko      589:        return handlers.result;
1.53      parser    590: }
                    591: 
1.126     paf       592: static void _sql(Request& r, MethodParams& params) {
1.53      parser    593: 
1.126     paf       594:        Value* default_code;
1.204     moko      595:        const String* string=sql_result_string(r, params, default_code);
1.62      parser    596:        if(!string) {
1.81      parser    597:                if(default_code) {
1.99      paf       598:                        string=&r.process_to_string(*default_code);
1.68      parser    599:                } else
1.238     moko      600:                        throw Exception(PARSER_RUNTIME, 0, "produced no result, but no default option specified");
1.62      parser    601:        }
1.100     paf       602: 
1.233     moko      603:        r.write(*string);
1.53      parser    604: }
                    605: 
1.126     paf       606: static void _replace(Request& r, MethodParams& params) {
                    607:        const String& src=GET_SELF(r, VString).string();
1.71      parser    608: 
1.201     misha     609:        if(params.count()==1) {
                    610:                // ^string.replace[table]
1.248     moko      611:                if(Table* table=params.as_table(0, "param")){
                    612:                        Dictionary dict(*table);
                    613:                        r.write(src.replace(dict));
                    614:                } else
                    615:                        r.write(src);
1.201     misha     616:        } else {
                    617:                // ^string.replace[from-string;to-string]
1.238     moko      618:                Dictionary dict(params.as_string(0, "from must be string"), params.as_string(1, "to must be string"));
1.233     moko      619:                r.write(src.replace(dict));
1.201     misha     620:        }
1.71      parser    621: 
                    622: }
1.79      parser    623: 
1.126     paf       624: static void _save(Request& r, MethodParams& params) {
1.189     misha     625:        bool do_append=false;
                    626:        Charset* asked_charset=0;
                    627: 
                    628:        size_t file_name_index=0;
1.214     moko      629:        if(params.count()>1) {
1.189     misha     630:                if(HashStringValue* options=params.as_no_junction(1, "second parameter should be string or hash").get_hash()){
1.205     misha     631:                        // ^file.save[filespec;$.charset[] $.append(true)]
1.190     misha     632:                        int valid_options=0;
1.189     misha     633:                        if(Value* vcharset_name=options->get(PA_CHARSET_NAME)){
1.229     moko      634:                                asked_charset=&pa_charsets.get(vcharset_name->as_string());
1.189     misha     635:                                valid_options++;
                    636:                        }
                    637:                        if(Value* vappend=options->get(MODE_APPEND)){
                    638:                                do_append=vappend->as_bool();
                    639:                                valid_options++;
                    640:                        }
                    641:                        if(valid_options != options->count())
1.194     misha     642:                                throw Exception(PARSER_RUNTIME, 0, CALLED_WITH_INVALID_OPTION);
1.189     misha     643:                } else {
1.205     misha     644:                        // ^file.save[append;filespec]
1.189     misha     645:                        const String& mode=params.as_string(0, "mode must be string");
                    646:                        if(mode==MODE_APPEND){
                    647:                                do_append=true;
                    648:                                file_name_index++;
                    649:                        } else
                    650:                                throw Exception(PARSER_RUNTIME,
                    651:                                        &mode,
                    652:                                        "unknown mode, must be 'append'");
                    653:                }
1.214     moko      654:        }
1.79      parser    655: 
1.261     moko      656:        const String& file_name=params.as_file_name(file_name_index);
1.126     paf       657:        const String& src=GET_SELF(r, VString).string();
1.79      parser    658: 
1.209     moko      659:        String::Body sbody=src.cstr_to_string_body_untaint(String::L_AS_IS, r.connection(false), &r.charsets);
1.87      paf       660: 
1.79      parser    661:        // write
1.246     moko      662:        file_write(r.charsets, r.full_disk_path(file_name), sbody.cstr(), sbody.length(), true, do_append, asked_charset);
1.79      parser    663: }
                    664: 
1.126     paf       665: static void _normalize(Request& r, MethodParams&) {
                    666:        const String& src=GET_SELF(r, VString).string();
                    667: 
1.233     moko      668:        r.write(src);
1.109     paf       669: }
                    670: 
1.133     paf       671: static void _trim(Request& r, MethodParams& params) {
                    672:        const String& src=GET_SELF(r, VString).string();
                    673: 
                    674:        String::Trim_kind kind=String::TRIM_BOTH;
1.176     misha     675:        size_t params_count=params.count();
1.133     paf       676:        const char* chars=0;
1.176     misha     677:        if(params_count>0) {
                    678:                const String& skind=params.as_string(0, "'where' must be string");
1.214     moko      679:                if(!skind.is_empty()) {
1.162     misha     680:                        if(skind==TRIM_BOTH_OPTION)
                    681:                                kind=String::TRIM_BOTH;
                    682:                        else if(skind==TRIM_START_OPTION || skind=="start")
1.137     paf       683:                                kind=String::TRIM_START;
1.162     misha     684:                        else if(skind==TRIM_END_OPTION || skind=="end")
1.137     paf       685:                                kind=String::TRIM_END;
1.218     moko      686:                        else if(params_count==1)
                    687:                                chars=skind.cstr();
1.137     paf       688:                        else
1.238     moko      689:                                throw Exception(PARSER_RUNTIME, &skind, "'kind' must be one of " TRIM_START_OPTION ", " TRIM_BOTH_OPTION ", " TRIM_END_OPTION);
1.214     moko      690:                }
1.133     paf       691: 
1.176     misha     692:                if(params_count>1) {
1.136     paf       693:                        const String& schars=params.as_string(1, "'chars' must be string");
1.178     misha     694:                        if(!schars.is_empty())
1.137     paf       695:                                chars=schars.cstr();
1.136     paf       696:                }
1.133     paf       697:        }
                    698: 
1.233     moko      699:        r.write(src.trim(kind, chars, &r.charsets.source()));
1.133     paf       700: }
                    701: 
1.241     moko      702: Base64Options base64_encode_options(Request& r, HashStringValue* options) {
                    703:        Base64Options result;
                    704:        if(options) {
                    705:                for(HashStringValue::Iterator i(*options); i; i.next()) {
                    706:                        String::Body key=i.key();
                    707:                        Value* value=i.value();
                    708:                        if(key == "pad") {
                    709:                                result.pad=r.process(*value).as_bool();
                    710:                        } else if(key == "wrap") {
                    711:                                result.wrap=r.process(*value).as_bool();
                    712:                        } else if(key == "url-safe") {
                    713:                                if(r.process(*value).as_bool())
                    714:                                        result.set_url_safe_abc();
1.263     moko      715:                        } else
                    716:                                throw Exception(PARSER_RUNTIME, 0, CALLED_WITH_INVALID_OPTION);
1.241     moko      717:                }
                    718:        }
                    719:        return result;
                    720: }
                    721: 
                    722: Base64Options base64_decode_options(Request& r, HashStringValue* options) {
                    723:        Base64Options result;
                    724:        if(options) {
                    725:                for(HashStringValue::Iterator i(*options); i; i.next() ) {
                    726:                        String::Body key=i.key();
                    727:                        Value* value=i.value();
                    728:                        if(key == "pad") {
                    729:                                result.pad=r.process(*value).as_bool();
                    730:                        } else if(key == "strict") {
                    731:                                result.strict=r.process(*value).as_bool();
                    732:                        } else if(key == "url-safe") {
                    733:                                if(r.process(*value).as_bool())
                    734:                                        result.set_url_safe_abc();
1.263     moko      735:                        } else
                    736:                                throw Exception(PARSER_RUNTIME, 0, CALLED_WITH_INVALID_OPTION);
1.241     moko      737:                }
                    738:        }
                    739:        return result;
                    740: }
                    741: 
1.146     paf       742: static void _base64(Request& r, MethodParams& params) {
1.219     moko      743:        if(&r.get_self() == string_class) {
1.241     moko      744:                // decode: ^string:base64[encoded[;options]]
1.219     moko      745:                const char* cstr=params.count() ? params.as_string(0, PARAMETER_MUST_BE_STRING).cstr() : "";
1.241     moko      746:                Base64Options options = base64_decode_options(r, params.count() > 1 ? params.as_hash(1) : NULL);
                    747: 
1.169     misha     748:                char* decoded=0;
1.243     moko      749:                size_t length=pa_base64_decode(cstr, strlen(cstr), decoded, options);
1.202     misha     750: 
1.169     misha     751:                if(decoded && length){
1.242     moko      752:                        if(memchr(decoded, 0, length))
                    753:                                throw Exception(PARSER_RUNTIME, 0, "Invalid \\x00 character found while decoding to string. Decode to file instead.");
1.169     misha     754: 
                    755:                        fix_line_breaks(decoded, length);
1.242     moko      756: 
1.179     misha     757:                        if(length)
1.233     moko      758:                                r.write(*new String(decoded, String::L_TAINTED));
1.169     misha     759:                }
1.147     paf       760:        } else {
1.241     moko      761:                // encode: ^str.base64[options]
1.148     paf       762:                VString& self=GET_SELF(r, VString);
                    763:                const char* cstr=self.string().cstr();
1.241     moko      764:                Base64Options options = base64_encode_options(r, params.count() ? params.as_hash(0) : NULL);
                    765:                const char* encoded=pa_base64_encode(cstr, strlen(cstr), options);
                    766:                r.write(*new String(encoded, String::L_TAINTED /*once ?param=base64(something) was needed*/ ));
1.146     paf       767:        }
                    768: }
                    769: 
1.215     moko      770: static void _idna(Request& r, MethodParams& params) {
1.219     moko      771:        if(&r.get_self() == string_class) {
1.215     moko      772:                // decode: ^string:idna[encoded]
1.219     moko      773:                const char* cstr=params.count() ? params.as_string(0, PARAMETER_MUST_BE_STRING).cstr() : "";
1.233     moko      774:                r.write(*new String(pa_idna_decode(cstr, r.charsets.source()), String::L_TAINTED));
1.215     moko      775:        } else {
                    776:                // encode: ^str.idna[]
                    777:                VString& self=GET_SELF(r, VString);
                    778:                const char* cstr=self.string().cstr();
1.233     moko      779:                r.write(*new String(pa_idna_encode(cstr, r.charsets.source()), String::L_TAINTED));
1.215     moko      780:        }
                    781: }
                    782: 
1.220     moko      783: static void _js_escape(Request& r, MethodParams&){
1.167     misha     784:        const String& src=GET_SELF(r, VString).string();
1.233     moko      785:        r.write(src.escape(r.charsets.source()));
1.167     misha     786: }
                    787: 
1.220     moko      788: static void _js_unescape(Request& r, MethodParams& params){
1.167     misha     789:        const String& src=params.as_string(0, PARAMETER_MUST_BE_STRING);
1.193     misha     790:        if(const char* result=unescape_chars(src.cstr(), src.length(), &r.charsets.source(), true))
1.233     moko      791:                r.write(*new String(result, String::L_TAINTED));
1.167     misha     792: }
                    793: 
1.220     moko      794: static void _unescape(Request& r, MethodParams& params){
1.262     moko      795:        const String& mode=params.as_string(0, MODE_MUST_BE_STRING);
1.220     moko      796:        const String& src=params.as_string(1, PARAMETER_MUST_BE_STRING);
                    797: 
                    798:        Charset* from_charset=&r.charsets.client();
                    799: 
                    800:        if(params.count() > 2)
                    801:                if(HashStringValue* options=params.as_hash(2)) {
                    802:                        int valid_options=0;
                    803:                        if(Value* vcharset_name=options->get(PA_CHARSET_NAME)){
1.229     moko      804:                                from_charset=&pa_charsets.get(vcharset_name->as_string());
1.220     moko      805:                                valid_options++;
                    806:                        }
                    807:                        if(valid_options!=options->count())
                    808:                                throw Exception(PARSER_RUNTIME, 0, CALLED_WITH_INVALID_OPTION);
                    809:                }
                    810: 
                    811:        bool mode_js;
                    812:        if(mode==UNESCAPE_MODE_JS){
                    813:                mode_js=true;
                    814:        } else if(mode==UNESCAPE_MODE_URI){
                    815:                mode_js=false;
                    816:        } else {
1.259     moko      817:                throw Exception(PARSER_RUNTIME, &mode, "is an invalid mode, must be either '" UNESCAPE_MODE_JS "' or '" UNESCAPE_MODE_URI "'");
1.220     moko      818:        }
                    819: 
                    820:        const char* unescaped=unescape_chars(src.cstr(), src.length(), from_charset, mode_js);
                    821:        if(*unescaped){
1.221     moko      822:                const String* result=new String(Charset::transcode(unescaped, *from_charset, r.charsets.source()), String::L_TAINTED);
1.233     moko      823:                r.write(*result);
1.220     moko      824:        }
                    825: }
                    826: 
1.225     moko      827: static void _contains(Request& r, MethodParams& params) {
                    828:        // empty or whitespace string is hash compatible
                    829:        GET_SELF(r, VString).get_element(params.as_string(0, "key must be string"));
                    830:        // ignoring result as it allways null
1.233     moko      831:        r.write(VBool::get(false));
1.225     moko      832: }
                    833: 
1.41      paf       834: // constructor
                    835: 
1.126     paf       836: MString::MString(): Methoded("string") {
1.1       paf       837:        // ^string.length[]
1.41      paf       838:        add_native_method("length", Method::CT_DYNAMIC, _length, 0, 0);
1.6       paf       839:        
1.1       paf       840:        // ^string.int[]
1.72      parser    841:        // ^string.int(default)
                    842:        add_native_method("int", Method::CT_DYNAMIC, _int, 0, 1);
1.1       paf       843:        // ^string.double[]
1.72      parser    844:        // ^string.double(default)
                    845:        add_native_method("double", Method::CT_DYNAMIC, _double, 0, 1);
1.151     misha     846:        // ^void.bool[]
                    847:        // ^void.bool(default)
                    848:        add_native_method("bool", Method::CT_DYNAMIC, _bool, 0, 1);
1.9       paf       849: 
1.165     misha     850:        // ^string.format[format]
1.41      paf       851:        add_native_method("format", Method::CT_DYNAMIC, _string_format, 1, 1);
1.14      paf       852: 
1.15      paf       853:        // ^string.left(n)
1.41      paf       854:        add_native_method("left", Method::CT_DYNAMIC, _left, 1, 1);
1.15      paf       855:        // ^string.right(n)
1.41      paf       856:        add_native_method("right", Method::CT_DYNAMIC, _right, 1, 1);
1.165     misha     857:        // ^string.mid(p)
1.15      paf       858:        // ^string.mid(p;n)
1.82      parser    859:        add_native_method("mid", Method::CT_DYNAMIC, _mid, 1, 2);
1.16      paf       860: 
                    861:        // ^string.pos[substr]
1.268   ! moko      862:        // ^string.pos[substr](n) n is the position from which to search
1.165     misha     863:        add_native_method("pos", Method::CT_DYNAMIC, _pos, 1, 2);
1.268   ! moko      864:        add_native_method("lpos", Method::CT_DYNAMIC, _pos, 1, 2);
        !           865: 
        !           866:        // ^string.rpos[substr]
        !           867:        // ^string.rpos[substr](n) n is the maximum position of the substr start, not substr end
        !           868:        add_native_method("rpos", Method::CT_DYNAMIC, _rpos, 1, 2);
1.17      paf       869: 
1.118     paf       870:        // ^string.split[delim]
                    871:        // ^string.split[delim][options]
1.156     misha     872:        // ^string.split[delim][options][column name]
                    873:        add_native_method("split", Method::CT_DYNAMIC, _split, 1, 3);
1.236     moko      874:        // old names for backward compatibility
                    875:        // ^string.lsplit[delim]
                    876:        add_native_method("lsplit", Method::CT_DYNAMIC, _lsplit, 1, 1);
                    877:        // ^string.rsplit[delim]
                    878:        add_native_method("rsplit", Method::CT_DYNAMIC, _rsplit, 1, 1);
                    879: 
1.32      paf       880:        // ^string.match[regexp][options]
                    881:        // ^string.match[regexp][options]{replacement-code}
1.192     misha     882:        // ^string.match[regexp][options]{replacement-code}{code-if-nothing-is-found}
                    883:        add_native_method("match", Method::CT_DYNAMIC, _match, 1, 4);
1.49      parser    884: 
1.165     misha     885:        // ^string.upper[]
1.49      parser    886:        add_native_method("upper", Method::CT_DYNAMIC, _upper, 0, 0);
1.165     misha     887:        // ^string.lower[]
1.49      parser    888:        add_native_method("lower", Method::CT_DYNAMIC, _lower, 0, 0);
1.53      parser    889: 
1.189     misha     890:        // ^string:sql{query}
                    891:        // ^string:sql{query}[options hash]
1.67      parser    892:        add_native_method("sql", Method::CT_STATIC, _sql, 1, 2);
1.71      parser    893: 
                    894:        // ^string.replace[table]
1.201     misha     895:        add_native_method("replace", Method::CT_DYNAMIC, _replace, 1, 2);
1.79      parser    896: 
1.189     misha     897:        // ^string.save[append][file]
                    898:        // ^string.save[file]
                    899:        // ^string.save[file][$.append(true) $.charset[...]]
1.87      paf       900:        add_native_method("save", Method::CT_DYNAMIC, _save, 1, 2);
1.109     paf       901: 
1.112     paf       902:        // ^string.normalize[]  
                    903:        add_native_method("normalize", Method::CT_DYNAMIC, _normalize, 0, 0);
1.133     paf       904: 
                    905:        // ^string.trim[[start|both|end][;chars]]
                    906:        add_native_method("trim", Method::CT_DYNAMIC, _trim, 0, 2);
1.139     paf       907: 
1.146     paf       908:        // ^string.base64[] << encode
1.215     moko      909:        // ^string:base64[encoded string] << decode
1.202     misha     910:        add_native_method("base64", Method::CT_ANY, _base64, 0, 2);
1.167     misha     911: 
1.215     moko      912:        // ^string.idna[] << encode
                    913:        // ^string:idna[encoded string] << decode
                    914:        add_native_method("idna", Method::CT_ANY, _idna, 0, 1);
                    915: 
1.168     misha     916:        // ^string.js-escape[]
1.220     moko      917:        add_native_method("js-escape", Method::CT_DYNAMIC, _js_escape, 0, 0);
1.189     misha     918: 
1.168     misha     919:        // ^string:js-unescape[escaped%uXXXXstring]
1.220     moko      920:        add_native_method("js-unescape", Method::CT_STATIC, _js_unescape, 1, 1);
                    921: 
                    922:        // ^string:unescape[js|uri;escaped;$.charset[...]]
                    923:        add_native_method("unescape", Method::CT_STATIC, _unescape, 2, 3);
1.225     moko      924: 
                    925:        // ^string.contains[key] for hash compatibility
                    926:        add_native_method("contains", Method::CT_DYNAMIC, _contains, 1, 1);
1.220     moko      927: }

E-mail: