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

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

E-mail: