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

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.204   ! moko       23: volatile const char * IDENT_STRING_C="$Id: string.C,v 1.203 2012-03-16 09:24:08 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.204   ! moko      537:                if(HashStringValue* options=params.as_hash(1)) {
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]
                    606:                Table* table=params.as_no_junction(0, PARAM_MUST_NOT_BE_CODE).get_table();
                    607:                if(!table)
                    608:                        throw Exception(PARSER_RUNTIME,
                    609:                                0,
                    610:                                "parameter must be table");
                    611:                Dictionary dict(*table);
                    612:                r.write_assign_lang(src.replace(dict));
                    613:        } else {
                    614:                // ^string.replace[from-string;to-string]
                    615:                Dictionary dict(
                    616:                                                params.as_string(0, "from must be string"),
                    617:                                                params.as_string(1, "to must be string")
                    618:                                        );
                    619:                r.write_assign_lang(src.replace(dict));
                    620:        }
1.71      parser    621: 
                    622: }
1.79      parser    623: 
1.126     paf       624: static void _save(Request& r, MethodParams& params) {
1.189     misha     625:        bool do_append=false;
                    626:        Charset* asked_charset=0;
                    627: 
                    628:        size_t file_name_index=0;
                    629:        if(params.count()>1)
                    630:                if(HashStringValue* options=params.as_no_junction(1, "second parameter should be string or hash").get_hash()){
1.190     misha     631:                        int valid_options=0;
1.189     misha     632:                        if(Value* vcharset_name=options->get(PA_CHARSET_NAME)){
                    633:                                asked_charset=&::charsets.get(vcharset_name->as_string().change_case(r.charsets.source(), String::CC_UPPER));
                    634:                                valid_options++;
                    635:                        }
                    636:                        if(Value* vappend=options->get(MODE_APPEND)){
                    637:                                do_append=vappend->as_bool();
                    638:                                valid_options++;
                    639:                        }
                    640:                        if(valid_options != options->count())
1.194     misha     641:                                throw Exception(PARSER_RUNTIME, 0, CALLED_WITH_INVALID_OPTION);
1.189     misha     642:                } else {
                    643:                        const String& mode=params.as_string(0, "mode must be string");
                    644:                        if(mode==MODE_APPEND){
                    645:                                do_append=true;
                    646:                                file_name_index++;
                    647:                        } else
                    648:                                throw Exception(PARSER_RUNTIME,
                    649:                                        &mode,
                    650:                                        "unknown mode, must be 'append'");
                    651:                }
1.79      parser    652: 
1.189     misha     653:        const String& file_name=params.as_string(file_name_index, FILE_NAME_MUST_BE_STRING);
1.126     paf       654:        const String& src=GET_SELF(r, VString).string();
1.79      parser    655: 
1.189     misha     656:        String::Body sbody=src.cstr_to_string_body_untaint(String::L_AS_IS, r.connection(false/*no error if none*/));
1.87      paf       657: 
1.79      parser    658:        // write
1.189     misha     659:        file_write(r.charsets, r.absolute(file_name), sbody.cstr(), sbody.length(), true, do_append, asked_charset);
1.79      parser    660: }
                    661: 
1.126     paf       662: static void _normalize(Request& r, MethodParams&) {
                    663:        const String& src=GET_SELF(r, VString).string();
                    664: 
                    665:        r.write_assign_lang(src);
1.109     paf       666: }
                    667: 
1.133     paf       668: static void _trim(Request& r, MethodParams& params) {
                    669:        const String& src=GET_SELF(r, VString).string();
                    670: 
                    671:        String::Trim_kind kind=String::TRIM_BOTH;
1.176     misha     672:        size_t params_count=params.count();
1.133     paf       673:        const char* chars=0;
1.176     misha     674:        if(params_count>0) {
                    675:                const String& skind=params.as_string(0, "'where' must be string");
1.178     misha     676:                if(!skind.is_empty())
1.162     misha     677:                        if(skind==TRIM_BOTH_OPTION)
                    678:                                kind=String::TRIM_BOTH;
                    679:                        else if(skind==TRIM_START_OPTION || skind=="start")
1.137     paf       680:                                kind=String::TRIM_START;
1.162     misha     681:                        else if(skind==TRIM_END_OPTION || skind=="end")
1.137     paf       682:                                kind=String::TRIM_END;
                    683:                        else
1.153     misha     684:                                throw Exception(PARSER_RUNTIME,
1.137     paf       685:                                        &skind,
                    686:                                        "'kind' must be one of "TRIM_START_OPTION", "TRIM_BOTH_OPTION", "TRIM_END_OPTION);
1.133     paf       687: 
1.176     misha     688:                if(params_count>1) {
1.136     paf       689:                        const String& schars=params.as_string(1, "'chars' must be string");
1.178     misha     690:                        if(!schars.is_empty())
1.137     paf       691:                                chars=schars.cstr();
1.136     paf       692:                }
1.133     paf       693:        }
                    694: 
1.182     misha     695:        r.write_assign_lang(src.trim(kind, chars, &r.charsets.source()));
1.133     paf       696: }
                    697: 
1.146     paf       698: static void _base64(Request& r, MethodParams& params) {
                    699:        if(params.count()) {
1.202     misha     700:                // decode: ^string:base64[encoded[;$.strict(true|false)]]
1.155     misha     701:                const char* cstr=params.as_string(0, PARAMETER_MUST_BE_STRING).cstr();
1.169     misha     702:                char* decoded=0;
                    703:                size_t length=0;
1.202     misha     704: 
                    705:                bool strict=false;
                    706:                if(params.count() > 1)
                    707:                        if(HashStringValue* options=params.as_hash(1)) {
                    708:                                int valid_options=0;
                    709:                                if(Value* vstrict=options->get(BASE64_STRICT_OPTION_NAME)) {
                    710:                                        strict=r.process_to_value(*vstrict).as_bool();
                    711:                                        valid_options++;
                    712:                                }
                    713:                                if(valid_options!=options->count())
                    714:                                        throw Exception(PARSER_RUNTIME, 0, CALLED_WITH_INVALID_OPTION);
                    715:                        }
                    716: 
                    717:                pa_base64_decode(cstr, strlen(cstr), decoded, length, strict);
1.169     misha     718:                if(decoded && length){
                    719:                        if(memchr((const char*)decoded, 0, length))
                    720:                                throw Exception(PARSER_RUNTIME,
                    721:                                        0,
                    722:                                        "Invalid \\x00 character found while decode to string. Decode it to file instead.");
                    723: 
                    724:                        fix_line_breaks(decoded, length);
1.179     misha     725:                        if(length)
1.180     misha     726:                                r.write_assign_lang(*new String(decoded, String::L_TAINTED));
1.169     misha     727:                }
1.147     paf       728:        } else {
1.169     misha     729:                // encode: ^str.base64[]
1.148     paf       730:                VString& self=GET_SELF(r, VString);
                    731:                const char* cstr=self.string().cstr();
1.147     paf       732:                const char* encoded=pa_base64_encode(cstr, strlen(cstr));
1.180     misha     733:                r.write_assign_lang(*new String(encoded, String::L_TAINTED/*once ?param=base64(something) was needed*/));
1.146     paf       734:        }
                    735: }
                    736: 
1.167     misha     737: static void _escape(Request& r, MethodParams&){
                    738:        const String& src=GET_SELF(r, VString).string();
                    739:        r.write_assign_lang(src.escape(r.charsets.source()));
                    740: }
                    741: 
                    742: static void _unescape(Request& r, MethodParams& params){
                    743:        const String& src=params.as_string(0, PARAMETER_MUST_BE_STRING);
1.193     misha     744:        if(const char* result=unescape_chars(src.cstr(), src.length(), &r.charsets.source(), true))
1.167     misha     745:                r.write_assign_lang(*new String(result));
                    746: }
                    747: 
1.41      paf       748: // constructor
                    749: 
1.126     paf       750: MString::MString(): Methoded("string") {
1.1       paf       751:        // ^string.length[]
1.41      paf       752:        add_native_method("length", Method::CT_DYNAMIC, _length, 0, 0);
1.6       paf       753:        
1.1       paf       754:        // ^string.int[]
1.72      parser    755:        // ^string.int(default)
                    756:        add_native_method("int", Method::CT_DYNAMIC, _int, 0, 1);
1.1       paf       757:        // ^string.double[]
1.72      parser    758:        // ^string.double(default)
                    759:        add_native_method("double", Method::CT_DYNAMIC, _double, 0, 1);
1.151     misha     760:        // ^void.bool[]
                    761:        // ^void.bool(default)
                    762:        add_native_method("bool", Method::CT_DYNAMIC, _bool, 0, 1);
1.9       paf       763: 
1.165     misha     764:        // ^string.format[format]
1.41      paf       765:        add_native_method("format", Method::CT_DYNAMIC, _string_format, 1, 1);
1.14      paf       766: 
1.15      paf       767:        // ^string.left(n)
1.41      paf       768:        add_native_method("left", Method::CT_DYNAMIC, _left, 1, 1);
1.15      paf       769:        // ^string.right(n)
1.41      paf       770:        add_native_method("right", Method::CT_DYNAMIC, _right, 1, 1);
1.165     misha     771:        // ^string.mid(p)
1.15      paf       772:        // ^string.mid(p;n)
1.82      parser    773:        add_native_method("mid", Method::CT_DYNAMIC, _mid, 1, 2);
1.16      paf       774: 
                    775:        // ^string.pos[substr]
1.165     misha     776:        // ^string.pos[substr](n)
                    777:        add_native_method("pos", Method::CT_DYNAMIC, _pos, 1, 2);
1.17      paf       778: 
1.118     paf       779:        // ^string.split[delim]
                    780:        // ^string.split[delim][options]
1.156     misha     781:        // ^string.split[delim][options][column name]
                    782:        add_native_method("split", Method::CT_DYNAMIC, _split, 1, 3);
1.118     paf       783:                // old names for backward compatibility
                    784:                // ^string.lsplit[delim]
                    785:                add_native_method("lsplit", Method::CT_DYNAMIC, _lsplit, 1, 1);
                    786:                // ^string.rsplit[delim]
                    787:                add_native_method("rsplit", Method::CT_DYNAMIC, _rsplit, 1, 1);
                    788:        
1.32      paf       789:        // ^string.match[regexp][options]
                    790:        // ^string.match[regexp][options]{replacement-code}
1.192     misha     791:        // ^string.match[regexp][options]{replacement-code}{code-if-nothing-is-found}
                    792:        add_native_method("match", Method::CT_DYNAMIC, _match, 1, 4);
1.49      parser    793: 
1.165     misha     794:        // ^string.upper[]
1.49      parser    795:        add_native_method("upper", Method::CT_DYNAMIC, _upper, 0, 0);
1.165     misha     796:        // ^string.lower[]
1.49      parser    797:        add_native_method("lower", Method::CT_DYNAMIC, _lower, 0, 0);
1.53      parser    798: 
1.189     misha     799:        // ^string:sql{query}
                    800:        // ^string:sql{query}[options hash]
1.67      parser    801:        add_native_method("sql", Method::CT_STATIC, _sql, 1, 2);
1.71      parser    802: 
                    803:        // ^string.replace[table]
1.201     misha     804:        add_native_method("replace", Method::CT_DYNAMIC, _replace, 1, 2);
1.79      parser    805: 
1.189     misha     806:        // ^string.save[append][file]
                    807:        // ^string.save[file]
                    808:        // ^string.save[file][$.append(true) $.charset[...]]
1.87      paf       809:        add_native_method("save", Method::CT_DYNAMIC, _save, 1, 2);
1.109     paf       810: 
1.112     paf       811:        // ^string.normalize[]  
                    812:        add_native_method("normalize", Method::CT_DYNAMIC, _normalize, 0, 0);
1.133     paf       813: 
                    814:        // ^string.trim[[start|both|end][;chars]]
                    815:        add_native_method("trim", Method::CT_DYNAMIC, _trim, 0, 2);
1.139     paf       816: 
1.146     paf       817:        // ^string.base64[] << encode
1.147     paf       818:        // ^string:base64[encoded string] << decode     
1.202     misha     819:        add_native_method("base64", Method::CT_ANY, _base64, 0, 2);
1.167     misha     820: 
1.168     misha     821:        // ^string.js-escape[]
1.189     misha     822:        add_native_method("js-escape", Method::CT_ANY, _escape, 0, 0);
                    823: 
1.168     misha     824:        // ^string:js-unescape[escaped%uXXXXstring]
                    825:        add_native_method("js-unescape", Method::CT_STATIC, _unescape, 1, 1);
1.2       paf       826: }      

E-mail: