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

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

E-mail: