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

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.194   ! misha       8: static const char * const IDENT_STRING_C="$Date: 2010-05-17 22:57:12 $";
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.194   ! misha     549:                                        throw Exception(PARSER_RUNTIME, 0, CALLED_WITH_INVALID_OPTION);
1.73      parser    550:                        } else
1.194   ! misha     551:                                throw Exception(PARSER_RUNTIME, 0, OPTIONS_MUST_BE_HASH);
1.70      parser    552:        } else
                    553:                options=0;
                    554: 
1.141     paf       555:        SQL_Driver::Placeholder* placeholders=0;
                    556:        uint placeholders_count=0;
                    557:        if(bind)
                    558:                placeholders_count=marshal_binds(*bind, placeholders);
                    559: 
1.126     paf       560:        Temp_lang temp_lang(r, String::L_SQL);
1.99      paf       561:        const String& statement_string=r.process_to_string(statement);
1.185     misha     562:        const char* statement_cstr=statement_string.untaint_cstr(r.flang, r.connection());
1.183     misha     563: 
1.126     paf       564:        String_sql_event_handlers handlers(statement_string, statement_cstr);
1.160     misha     565: 
1.126     paf       566:        r.connection()->query(
1.140     paf       567:                statement_cstr, 
1.142     paf       568:                placeholders_count, placeholders,
1.140     paf       569:                offset, limit, 
1.117     paf       570:                handlers,
                    571:                statement_string);
1.53      parser    572:        
1.141     paf       573:        if(bind)
                    574:                unmarshal_bind_updates(*bind, placeholders_count, placeholders);
                    575: 
1.65      parser    576:        if(!handlers.got_cell)
                    577:                return 0; // no lines, caller should return second param[default value]
1.62      parser    578: 
1.126     paf       579:        return &handlers.result;
1.53      parser    580: }
                    581: 
1.126     paf       582: static void _sql(Request& r, MethodParams& params) {
1.53      parser    583: 
1.126     paf       584:        HashStringValue* options;
                    585:        Value* default_code;
                    586:        const String* string=sql_result_string(r, params, options, default_code);
1.62      parser    587:        if(!string) {
1.81      parser    588:                if(default_code) {
1.99      paf       589:                        string=&r.process_to_string(*default_code);
1.68      parser    590:                } else
1.153     misha     591:                        throw Exception(PARSER_RUNTIME,
1.126     paf       592:                                0,
1.81      parser    593:                                "produced no result, but no default option specified");
1.62      parser    594:        }
1.100     paf       595: 
1.102     paf       596:        r.write_assign_lang(*string);
1.53      parser    597: }
                    598: 
1.126     paf       599: static void _replace(Request& r, MethodParams& params) {
                    600:        const String& src=GET_SELF(r, VString).string();
1.71      parser    601: 
1.157     misha     602:        Table* table=params.as_no_junction(0, PARAM_MUST_NOT_BE_CODE).get_table();
1.71      parser    603:        if(!table)
1.153     misha     604:                throw Exception(PARSER_RUNTIME,
1.126     paf       605:                        0,
1.71      parser    606:                        "parameter must be table");
                    607: 
                    608:        Dictionary dict(*table);
1.126     paf       609:        r.write_assign_lang(src.replace(dict));
1.71      parser    610: }
1.79      parser    611: 
1.126     paf       612: static void _save(Request& r, MethodParams& params) {
1.189     misha     613:        bool do_append=false;
                    614:        Charset* asked_charset=0;
                    615: 
                    616:        size_t file_name_index=0;
                    617:        if(params.count()>1)
                    618:                if(HashStringValue* options=params.as_no_junction(1, "second parameter should be string or hash").get_hash()){
1.190     misha     619:                        int valid_options=0;
1.189     misha     620:                        if(Value* vcharset_name=options->get(PA_CHARSET_NAME)){
                    621:                                asked_charset=&::charsets.get(vcharset_name->as_string().change_case(r.charsets.source(), String::CC_UPPER));
                    622:                                valid_options++;
                    623:                        }
                    624:                        if(Value* vappend=options->get(MODE_APPEND)){
                    625:                                do_append=vappend->as_bool();
                    626:                                valid_options++;
                    627:                        }
                    628:                        if(valid_options != options->count())
1.194   ! misha     629:                                throw Exception(PARSER_RUNTIME, 0, CALLED_WITH_INVALID_OPTION);
1.189     misha     630:                } else {
                    631:                        const String& mode=params.as_string(0, "mode must be string");
                    632:                        if(mode==MODE_APPEND){
                    633:                                do_append=true;
                    634:                                file_name_index++;
                    635:                        } else
                    636:                                throw Exception(PARSER_RUNTIME,
                    637:                                        &mode,
                    638:                                        "unknown mode, must be 'append'");
                    639:                }
1.79      parser    640: 
1.189     misha     641:        const String& file_name=params.as_string(file_name_index, FILE_NAME_MUST_BE_STRING);
1.126     paf       642:        const String& src=GET_SELF(r, VString).string();
1.79      parser    643: 
1.189     misha     644:        String::Body sbody=src.cstr_to_string_body_untaint(String::L_AS_IS, r.connection(false/*no error if none*/));
1.87      paf       645: 
1.79      parser    646:        // write
1.189     misha     647:        file_write(r.charsets, r.absolute(file_name), sbody.cstr(), sbody.length(), true, do_append, asked_charset);
1.79      parser    648: }
                    649: 
1.126     paf       650: static void _normalize(Request& r, MethodParams&) {
                    651:        const String& src=GET_SELF(r, VString).string();
                    652: 
                    653:        r.write_assign_lang(src);
1.109     paf       654: }
                    655: 
1.133     paf       656: static void _trim(Request& r, MethodParams& params) {
                    657:        const String& src=GET_SELF(r, VString).string();
                    658: 
                    659:        String::Trim_kind kind=String::TRIM_BOTH;
1.176     misha     660:        size_t params_count=params.count();
1.133     paf       661:        const char* chars=0;
1.176     misha     662:        if(params_count>0) {
                    663:                const String& skind=params.as_string(0, "'where' must be string");
1.178     misha     664:                if(!skind.is_empty())
1.162     misha     665:                        if(skind==TRIM_BOTH_OPTION)
                    666:                                kind=String::TRIM_BOTH;
                    667:                        else if(skind==TRIM_START_OPTION || skind=="start")
1.137     paf       668:                                kind=String::TRIM_START;
1.162     misha     669:                        else if(skind==TRIM_END_OPTION || skind=="end")
1.137     paf       670:                                kind=String::TRIM_END;
                    671:                        else
1.153     misha     672:                                throw Exception(PARSER_RUNTIME,
1.137     paf       673:                                        &skind,
                    674:                                        "'kind' must be one of "TRIM_START_OPTION", "TRIM_BOTH_OPTION", "TRIM_END_OPTION);
1.133     paf       675: 
1.176     misha     676:                if(params_count>1) {
1.136     paf       677:                        const String& schars=params.as_string(1, "'chars' must be string");
1.178     misha     678:                        if(!schars.is_empty())
1.137     paf       679:                                chars=schars.cstr();
1.136     paf       680:                }
1.133     paf       681:        }
                    682: 
1.182     misha     683:        r.write_assign_lang(src.trim(kind, chars, &r.charsets.source()));
1.133     paf       684: }
                    685: 
1.139     paf       686: static void _append(Request& r, MethodParams& params) {
                    687:        // c=a+b
                    688:        VString& va=GET_SELF(r, VString);
                    689:        const String& a=va.string();
1.155     misha     690:        const String& b=params.as_string(0, PARAMETER_MUST_BE_STRING);
1.139     paf       691:        String& c=*new String(a);
                    692:        c.append(b, String::L_PASS_APPENDED);
                    693:        va.set_string(c);
                    694: }
                    695: 
1.146     paf       696: static void _base64(Request& r, MethodParams& params) {
                    697:        if(params.count()) {
1.169     misha     698:                // decode: ^string:base64[encoded]
1.155     misha     699:                const char* cstr=params.as_string(0, PARAMETER_MUST_BE_STRING).cstr();
1.169     misha     700:                char* decoded=0;
                    701:                size_t length=0;
                    702:                pa_base64_decode(cstr, strlen(cstr), decoded, length);
                    703:                if(decoded && length){
                    704:                        if(memchr((const char*)decoded, 0, length))
                    705:                                throw Exception(PARSER_RUNTIME,
                    706:                                        0,
                    707:                                        "Invalid \\x00 character found while decode to string. Decode it to file instead.");
                    708: 
                    709:                        fix_line_breaks(decoded, length);
1.179     misha     710:                        if(length)
1.180     misha     711:                                r.write_assign_lang(*new String(decoded, String::L_TAINTED));
1.169     misha     712:                }
1.147     paf       713:        } else {
1.169     misha     714:                // encode: ^str.base64[]
1.148     paf       715:                VString& self=GET_SELF(r, VString);
                    716:                const char* cstr=self.string().cstr();
1.147     paf       717:                const char* encoded=pa_base64_encode(cstr, strlen(cstr));
1.180     misha     718:                r.write_assign_lang(*new String(encoded, String::L_TAINTED/*once ?param=base64(something) was needed*/));
1.146     paf       719:        }
                    720: }
                    721: 
1.167     misha     722: static void _escape(Request& r, MethodParams&){
                    723:        const String& src=GET_SELF(r, VString).string();
                    724:        r.write_assign_lang(src.escape(r.charsets.source()));
                    725: }
                    726: 
                    727: static void _unescape(Request& r, MethodParams& params){
                    728:        const String& src=params.as_string(0, PARAMETER_MUST_BE_STRING);
1.193     misha     729:        if(const char* result=unescape_chars(src.cstr(), src.length(), &r.charsets.source(), true))
1.167     misha     730:                r.write_assign_lang(*new String(result));
                    731: }
                    732: 
1.41      paf       733: // constructor
                    734: 
1.126     paf       735: MString::MString(): Methoded("string") {
1.1       paf       736:        // ^string.length[]
1.41      paf       737:        add_native_method("length", Method::CT_DYNAMIC, _length, 0, 0);
1.6       paf       738:        
1.1       paf       739:        // ^string.int[]
1.72      parser    740:        // ^string.int(default)
                    741:        add_native_method("int", Method::CT_DYNAMIC, _int, 0, 1);
1.1       paf       742:        // ^string.double[]
1.72      parser    743:        // ^string.double(default)
                    744:        add_native_method("double", Method::CT_DYNAMIC, _double, 0, 1);
1.151     misha     745:        // ^void.bool[]
                    746:        // ^void.bool(default)
                    747:        add_native_method("bool", Method::CT_DYNAMIC, _bool, 0, 1);
1.9       paf       748: 
1.165     misha     749:        // ^string.format[format]
1.41      paf       750:        add_native_method("format", Method::CT_DYNAMIC, _string_format, 1, 1);
1.14      paf       751: 
1.15      paf       752:        // ^string.left(n)
1.41      paf       753:        add_native_method("left", Method::CT_DYNAMIC, _left, 1, 1);
1.15      paf       754:        // ^string.right(n)
1.41      paf       755:        add_native_method("right", Method::CT_DYNAMIC, _right, 1, 1);
1.165     misha     756:        // ^string.mid(p)
1.15      paf       757:        // ^string.mid(p;n)
1.82      parser    758:        add_native_method("mid", Method::CT_DYNAMIC, _mid, 1, 2);
1.16      paf       759: 
                    760:        // ^string.pos[substr]
1.165     misha     761:        // ^string.pos[substr](n)
                    762:        add_native_method("pos", Method::CT_DYNAMIC, _pos, 1, 2);
1.17      paf       763: 
1.118     paf       764:        // ^string.split[delim]
                    765:        // ^string.split[delim][options]
1.156     misha     766:        // ^string.split[delim][options][column name]
                    767:        add_native_method("split", Method::CT_DYNAMIC, _split, 1, 3);
1.118     paf       768:                // old names for backward compatibility
                    769:                // ^string.lsplit[delim]
                    770:                add_native_method("lsplit", Method::CT_DYNAMIC, _lsplit, 1, 1);
                    771:                // ^string.rsplit[delim]
                    772:                add_native_method("rsplit", Method::CT_DYNAMIC, _rsplit, 1, 1);
                    773:        
1.32      paf       774:        // ^string.match[regexp][options]
                    775:        // ^string.match[regexp][options]{replacement-code}
1.192     misha     776:        // ^string.match[regexp][options]{replacement-code}{code-if-nothing-is-found}
                    777:        add_native_method("match", Method::CT_DYNAMIC, _match, 1, 4);
1.49      parser    778: 
1.165     misha     779:        // ^string.upper[]
1.49      parser    780:        add_native_method("upper", Method::CT_DYNAMIC, _upper, 0, 0);
1.165     misha     781:        // ^string.lower[]
1.49      parser    782:        add_native_method("lower", Method::CT_DYNAMIC, _lower, 0, 0);
1.53      parser    783: 
1.189     misha     784:        // ^string:sql{query}
                    785:        // ^string:sql{query}[options hash]
1.67      parser    786:        add_native_method("sql", Method::CT_STATIC, _sql, 1, 2);
1.71      parser    787: 
                    788:        // ^string.replace[table]
                    789:        add_native_method("replace", Method::CT_DYNAMIC, _replace, 1, 1);
1.79      parser    790: 
1.189     misha     791:        // ^string.save[append][file]
                    792:        // ^string.save[file]
                    793:        // ^string.save[file][$.append(true) $.charset[...]]
1.87      paf       794:        add_native_method("save", Method::CT_DYNAMIC, _save, 1, 2);
1.109     paf       795: 
1.112     paf       796:        // ^string.normalize[]  
                    797:        add_native_method("normalize", Method::CT_DYNAMIC, _normalize, 0, 0);
1.133     paf       798: 
                    799:        // ^string.trim[[start|both|end][;chars]]
                    800:        add_native_method("trim", Method::CT_DYNAMIC, _trim, 0, 2);
1.139     paf       801: 
                    802:        // ^string.append[string]
                    803:        add_native_method("append", Method::CT_DYNAMIC, _append, 1, 1);
1.146     paf       804: 
                    805:        // ^string.base64[] << encode
1.147     paf       806:        // ^string:base64[encoded string] << decode     
1.146     paf       807:        add_native_method("base64", Method::CT_ANY, _base64, 0, 1);
1.167     misha     808: 
1.168     misha     809:        // ^string.js-escape[]
1.189     misha     810:        add_native_method("js-escape", Method::CT_ANY, _escape, 0, 0);
                    811: 
1.168     misha     812:        // ^string:js-unescape[escaped%uXXXXstring]
                    813:        add_native_method("js-unescape", Method::CT_STATIC, _unescape, 1, 1);
1.2       paf       814: }      

E-mail: