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

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

E-mail: