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

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

E-mail: