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

1.24      paf         1: /** @file
                      2:        Parser: @b string parser class.
                      3: 
1.4       paf         4:        Copyright (c) 2001 ArtLebedev Group (http://www.artlebedev.com)
1.24      paf         5: 
1.5       paf         6:        Author: Alexander Petrosyan <paf@design.ru> (http://design.ru/paf)
1.1       paf         7: */
1.60    ! parser      8: static const char *RCSId="$Id: string.C,v 1.59 2001/07/07 16:38:01 parser Exp $"; 
1.1       paf         9: 
1.43      paf        10: #include "classes.h"
1.1       paf        11: #include "pa_request.h"
                     12: #include "pa_vdouble.h"
                     13: #include "pa_vint.h"
1.17      paf        14: #include "pa_vtable.h"
1.25      paf        15: #include "pa_vbool.h"
1.27      paf        16: #include "pa_string.h"
1.53      parser     17: #include "pa_sql_connection.h"
1.1       paf        18: 
1.41      paf        19: // defines
1.1       paf        20: 
1.41      paf        21: #define STRING_CLASS_NAME "string"
                     22: 
                     23: // class
                     24: 
                     25: class MString : public Methoded {
                     26: public:
                     27:        MString(Pool& pool);
1.44      paf        28: public: // Methoded
1.53      parser     29:        bool used_directly() { return true; }
1.41      paf        30: };
1.1       paf        31: 
                     32: // methods
                     33: 
1.47      paf        34: static void _length(Request& r, const String& method_name, MethodParams *) {
1.1       paf        35:        Pool& pool=r.pool();
1.47      paf        36:        Value& result=*new(pool) VDouble(pool, r.self->get_string()->size());
                     37:        result.set_name(method_name);
                     38:        r.write_no_lang(result);
1.1       paf        39: }
                     40: 
1.47      paf        41: static void _int(Request& r, const String& method_name, MethodParams *) {
1.1       paf        42:        Pool& pool=r.pool();
1.48      parser     43:        Value& result=*new(pool) VInt(pool, r.self->as_int());
1.47      paf        44:        result.set_name(method_name);
                     45:        r.write_no_lang(result);
1.1       paf        46: }
                     47: 
1.47      paf        48: static void _double(Request& r, const String& method_name, MethodParams *) {
1.1       paf        49:        Pool& pool=r.pool();
1.47      paf        50:        Value& result=*new(pool) VDouble(pool, r.self->as_double());
                     51:        result.set_name(method_name);
                     52:        r.write_no_lang(result);
1.1       paf        53: }
                     54: 
1.38      paf        55: /*not static*/void _string_format(Request& r, const String& method_name, MethodParams *params) {
1.9       paf        56:        Pool& pool=r.pool();
                     57: 
1.59      parser     58:        Value& fmt=params->as_junction(0, "fmt must be code");
1.9       paf        59: 
1.24      paf        60:        Temp_lang temp_lang(r, String::UL_PASS_APPENDED);
                     61:        char *buf=format(pool, r.self->as_double(), r.process(fmt).as_string().cstr());
1.9       paf        62:        
1.12      paf        63:        r.write_no_lang(String(pool, buf));
1.9       paf        64: }
1.11      paf        65: 
1.38      paf        66: static void _left(Request& r, const String&, MethodParams *params) {
1.15      paf        67:        Pool& pool=r.pool();
                     68: 
1.38      paf        69:        size_t n=(size_t)r.process(params->get(0)).as_double();
1.15      paf        70:        
                     71:        const String& string=*static_cast<VString *>(r.self)->get_string();
1.35      paf        72:        r.write_assign_lang(*new(pool) VString(string.mid(0, n)));
1.15      paf        73: }
                     74: 
1.38      paf        75: static void _right(Request& r, const String&, MethodParams *params) {
1.15      paf        76:        Pool& pool=r.pool();
                     77: 
1.38      paf        78:        size_t n=(size_t)r.process(params->get(0)).as_double();
1.15      paf        79:        
                     80:        const String& string=*static_cast<VString *>(r.self)->get_string();
1.35      paf        81:        r.write_assign_lang(*new(pool) VString(string.mid(string.size()-n, string.size())));
1.15      paf        82: }
                     83: 
1.38      paf        84: static void _mid(Request& r, const String&, MethodParams *params) {
1.15      paf        85:        Pool& pool=r.pool();
                     86: 
1.38      paf        87:        size_t p=(size_t)r.process(params->get(0)).as_double();
                     88:        size_t n=(size_t)r.process(params->get(1)).as_double();
1.15      paf        89:        
                     90:        const String& string=*static_cast<VString *>(r.self)->get_string();
1.35      paf        91:        r.write_assign_lang(*new(pool) VString(string.mid(p, p+n)));
1.15      paf        92: }
                     93: 
1.38      paf        94: static void _pos(Request& r, const String& method_name, MethodParams *params) {
1.16      paf        95:        Pool& pool=r.pool();
                     96: 
1.59      parser     97:        Value& substr=params->as_no_junction(0, "substr must not be code");
1.16      paf        98:        
                     99:        const String& string=*static_cast<VString *>(r.self)->get_string();
                    100:        r.write_assign_lang(*new(pool) VInt(pool, string.pos(substr.as_string())));
                    101: }
                    102: 
1.38      paf       103: static void split_list(Request& r, const String& method_name, MethodParams *params,
1.23      paf       104:                                           const String& string, 
                    105:                                           Array& result) {
1.59      parser    106:        Value& delim_value=params->as_no_junction(0, "delimiter must not be code");
1.23      paf       107: 
1.45      paf       108:        string.split(result, 0, delim_value.as_string());
1.19      paf       109: }
                    110: 
1.38      paf       111: static void _lsplit(Request& r, const String& method_name, MethodParams *params) {
1.19      paf       112:        Pool& pool=r.pool();
                    113:        const String& string=*static_cast<VString *>(r.self)->get_string();
                    114: 
1.21      paf       115:        Array& row=*new(pool) Array(pool);
                    116:        split_list(r, method_name, params, string, row);
1.19      paf       117: 
1.21      paf       118:        Table& table=*new(pool) Table(pool, &string, 
                    119:                0/*nameless*/, 1/*row preallocate(and only)*/);
                    120:        table+=&row;
1.19      paf       121: 
                    122:        r.write_no_lang(*new(pool) VTable(pool, &table));
                    123: }
                    124: 
1.38      paf       125: static void _rsplit(Request& r, const String& method_name, MethodParams *params) {
1.19      paf       126:        Pool& pool=r.pool();
                    127:        const String& string=*static_cast<VString *>(r.self)->get_string();
                    128: 
                    129:        Array list(pool);
                    130:        split_list(r, method_name, params, string, list);
                    131: 
1.21      paf       132:        Array& row=*new(pool) Array(pool);
                    133:        for(int i=list.size(); --i>=0; )
                    134:                row+=list.get(i);
                    135: 
                    136:        Table& table=*new(pool) Table(pool, &string, 
                    137:                0/*nameless*/, 1/*row preallocate(and only)*/);
                    138:        table+=&row;
1.17      paf       139: 
1.19      paf       140:        r.write_no_lang(*new(pool) VTable(pool, &table));
1.17      paf       141: }
                    142: 
1.33      paf       143: static void search_action(Table& table, Array *row, int, int, void *) {
1.28      paf       144:        if(row)
                    145:                table+=row;
1.27      paf       146: }
                    147: 
1.40      paf       148: /// used by string: _match / replace_action
1.27      paf       149: struct Replace_action_info {
1.30      paf       150:        Request *request;  const String *origin;
1.31      paf       151:        const String *src;  String *dest;
1.27      paf       152:        Value *replacement_code;
1.29      paf       153:        const String *post_match;
1.27      paf       154: };
1.33      paf       155: static void replace_action(Table& table, Array *row, int start, int finish, 
1.31      paf       156:                                                           void *info) {
1.27      paf       157:        Replace_action_info& ai=*static_cast<Replace_action_info *>(info);
1.31      paf       158:        if(row) { // begin&middle
                    159:                // piece from last match['start'] to beginning of this match['finish']
                    160:                if(start!=finish)
1.37      paf       161:                        *ai.dest << ai.src->mid(start, finish);//ai.dest->APPEND_CONST("-");
1.51      parser    162:                // store found parts in one-record VTable
1.32      paf       163:                if(table.size()) // middle
                    164:                        table.put(0, row);
                    165:                else // begin
1.30      paf       166:                        table+=row;
                    167:                { // execute 'replacement_code' in 'table' context
                    168:                        VTable& vtable=*new(table.pool()) VTable(table.pool(), &table);
                    169:                        vtable.set_name(*ai.origin);
                    170: 
                    171:                        Junction *junction=ai.replacement_code->get_junction();
1.52      parser    172:                        junction->rcontext=/*must be some way to get to 
                    173:                                                           outside world junction->root=*/&vtable;
1.30      paf       174:                        Value& replaced=ai.request->process(*ai.replacement_code, ai.origin, false);
                    175: 
1.34      paf       176:                        /*
1.31      paf       177:                        ai.dest->APPEND_CONST("(");
1.37      paf       178:                                *ai.dest << *(String *)row->get(1/*match* /);
1.31      paf       179:                        ai.dest->APPEND_CONST(")");
1.34      paf       180:                        */
1.37      paf       181:                        *ai.dest << replaced.as_string();
1.29      paf       182:                }
                    183:                ai.post_match=(String *)row->get(2/*post_match*/);
                    184:        } else // end
1.37      paf       185:                *ai.dest << *ai.post_match;
1.27      paf       186: }
                    187: 
1.50      parser    188: /// @todo use pcre:study somehow
1.38      paf       189: static void _match(Request& r, const String& method_name, MethodParams *params) {
1.24      paf       190:        Pool& pool=r.pool();
1.28      paf       191:        const String& src=*static_cast<VString *>(r.self)->get_string();
1.24      paf       192: 
1.59      parser    193:        Value& regexp=params->as_no_junction(0, "regexp must not be code");
1.38      paf       194: 
                    195:        const String *options=
                    196:                params->size()>1?
1.59      parser    197:                &params->as_no_junction(1, "options must not be code").as_string():0;
1.24      paf       198: 
1.27      paf       199:        Value *result;
1.24      paf       200:        Temp_lang temp_lang(r, String::UL_PASS_APPENDED);
1.25      paf       201:        Table *table;
1.27      paf       202:        if(params->size()<3) { // search
1.60    ! parser    203:                bool matched=src.match(r.pcre_tables,
1.39      paf       204:                        &method_name, 
1.32      paf       205:                        regexp.as_string(), options,
1.27      paf       206:                        &table,
1.60    ! parser    207:                        search_action, 0);
        !           208:                // matched
        !           209:                if(table->columns()->size()==3 && // just matched[3=pre/match/post], no substrings
        !           210:                        table->size()<=1)  // just one row, not /g_lobal search
        !           211:                        result=new(pool) VBool(pool, matched);
        !           212:                else // table of pre/match/post+substrings
        !           213:                        result=new(pool) VTable(pool, table);
1.27      paf       214:        } else { // replace
1.59      parser    215:                Value& replacement_code=params->as_junction(2, "replacement code must be code");
1.24      paf       216: 
1.28      paf       217:                String& dest=*new(pool) String(pool);
1.27      paf       218:                Replace_action_info replace_action_info={
1.30      paf       219:                        &r, &method_name,
1.31      paf       220:                        &src, &dest,
1.28      paf       221:                        &replacement_code,
1.29      paf       222:                        &src
1.27      paf       223:                };
1.39      paf       224:                src.match(r.pcre_tables,
                    225:                        &method_name, 
1.27      paf       226:                        r.process(regexp).as_string(), options,
                    227:                        &table,
1.33      paf       228:                        replace_action, &replace_action_info);
1.28      paf       229:                result=new(pool) VString(dest);
1.27      paf       230:        }
1.26      paf       231:        result->set_name(method_name);
1.34      paf       232:        r.write_assign_lang(*result);
1.24      paf       233: }
                    234: 
1.49      parser    235: static void change_case(Request& r, const String& method_name, MethodParams *params, 
                    236:                                                String::Change_case_kind kind) {
                    237:        Pool& pool=r.pool();
                    238:        const String& src=*static_cast<VString *>(r.self)->get_string();
                    239: 
                    240:        r.write_assign_lang(*new(pool) VString(src.change_case(pool, r.pcre_tables,
                    241:                kind)));
                    242: }
                    243: static void _upper(Request& r, const String& method_name, MethodParams *params) {
                    244:        change_case(r, method_name, params, String::CC_UPPER);
                    245: }
                    246: static void _lower(Request& r, const String& method_name, MethodParams *params) {
                    247:        change_case(r, method_name, params, String::CC_LOWER);
                    248: }
                    249: 
1.53      parser    250: String& sql_result_string(Request& r, const String& method_name, MethodParams *params) {
                    251:        Pool& pool=r.pool();
                    252: 
                    253:        if(!r.connection)
                    254:                PTHROW(0, 0,
                    255:                        &method_name,
                    256:                        "without connect");
                    257: 
1.59      parser    258:        Value& statement=params->as_junction(0, "statement must be code");
1.53      parser    259: 
                    260:        ulong offset=0;
                    261:        if(params->size()>1) {
1.59      parser    262:                Value& offset_code=params->as_junction(1, "offset must be expression");
1.53      parser    263:                offset=(ulong)r.process(offset_code).as_double();
                    264:        }
                    265: 
                    266:        Temp_lang temp_lang(r, String::UL_SQL);
                    267:        const String& statement_string=r.process(statement).as_string();
                    268:        const char *statement_cstr=
                    269:                statement_string.cstr(String::UL_UNSPECIFIED, r.connection);
                    270:        unsigned int sql_column_count; SQL_Driver::Cell *sql_columns;
                    271:        unsigned long sql_row_count; SQL_Driver::Cell **sql_rows;
                    272:        bool need_rethrow=false; Exception rethrow_me;
                    273:        PTRY {
                    274:                r.connection->query(
                    275:                        statement_cstr, offset, 0,
                    276:                        &sql_column_count, &sql_columns,
                    277:                        &sql_row_count, &sql_rows);
                    278:        }
                    279:        PCATCH(e) { // query problem
                    280:                rethrow_me=e;  need_rethrow=true;
                    281:        }
                    282:        PEND_CATCH
                    283:        if(need_rethrow)
                    284:                PTHROW(rethrow_me.type(), rethrow_me.code(),
                    285:                        &statement_string, // setting more specific source [were url]
                    286:                        rethrow_me.comment());
                    287:        
                    288:        if(sql_column_count!=1)
                    289:                PTHROW(0, 0,
                    290:                        &statement_string,
1.56      parser    291:                        "result must contain exactly one column");
1.53      parser    292: 
                    293:        if(sql_row_count!=1)
                    294:                PTHROW(0, 0,
                    295:                        &statement_string,
1.56      parser    296:                        "result must contain exactly one row");
1.53      parser    297: 
                    298:        String& result=*new(pool) String(pool);
                    299:        SQL_Driver::Cell& cell=sql_rows[0][0];
                    300:        result.APPEND_TAINTED(
                    301:                (const char *)cell.ptr, cell.size,
                    302:                statement_cstr, 0);
                    303:        
                    304:        return result;
                    305: }
                    306: 
                    307: static void _sql(Request& r, const String& method_name, MethodParams *params) {
                    308:        Pool& pool=r.pool();
                    309: 
                    310:        VString& result=*new(pool) VString(sql_result_string(r, method_name, params));
                    311:        result.set_name(method_name);
                    312:        r.write_assign_lang(result);
                    313: }
                    314: 
1.41      paf       315: // constructor
                    316: 
                    317: MString::MString(Pool& apool) : Methoded(apool) {
                    318:        set_name(*NEW String(pool(), STRING_CLASS_NAME));
                    319: 
1.9       paf       320: 
1.1       paf       321:        // ^string.length[]
1.41      paf       322:        add_native_method("length", Method::CT_DYNAMIC, _length, 0, 0);
1.6       paf       323:        
1.1       paf       324:        // ^string.int[]
1.41      paf       325:        add_native_method("int", Method::CT_DYNAMIC, _int, 0, 0);
1.6       paf       326:        
1.1       paf       327:        // ^string.double[]
1.41      paf       328:        add_native_method("double", Method::CT_DYNAMIC, _double, 0, 0);
1.9       paf       329: 
1.24      paf       330:        // ^string.format{format}
1.41      paf       331:        add_native_method("format", Method::CT_DYNAMIC, _string_format, 1, 1);
1.14      paf       332: 
1.15      paf       333:        // ^string.left(n)
1.41      paf       334:        add_native_method("left", Method::CT_DYNAMIC, _left, 1, 1);
1.15      paf       335:        // ^string.right(n)
1.41      paf       336:        add_native_method("right", Method::CT_DYNAMIC, _right, 1, 1);
1.15      paf       337:        // ^string.mid(p;n)
1.41      paf       338:        add_native_method("mid", Method::CT_DYNAMIC, _mid, 2, 2);
1.16      paf       339: 
                    340:        // ^string.pos[substr]
1.41      paf       341:        add_native_method("pos", Method::CT_DYNAMIC, _pos, 1, 1);
1.17      paf       342: 
                    343:        // ^string.lsplit[delim]
1.41      paf       344:        add_native_method("lsplit", Method::CT_DYNAMIC, _lsplit, 1, 1);
1.19      paf       345:        // ^string.rsplit[delim]
1.41      paf       346:        add_native_method("rsplit", Method::CT_DYNAMIC, _rsplit, 1, 1);
1.24      paf       347: 
1.32      paf       348:        // ^string.match[regexp][options]
                    349:        // ^string.match[regexp][options]{replacement-code}
1.41      paf       350:        add_native_method("match", Method::CT_DYNAMIC, _match, 1, 3);
1.49      parser    351: 
                    352:        // ^string.toupper[]
                    353:        add_native_method("upper", Method::CT_DYNAMIC, _upper, 0, 0);
                    354:        // ^string.tolower[]
                    355:        add_native_method("lower", Method::CT_DYNAMIC, _lower, 0, 0);
1.53      parser    356: 
                    357:        // ^string:sql[query]
                    358:        // ^string:sql[query](offset)
1.54      parser    359:        add_native_method("sql", Method::CT_STATIC, _sql, 1, 2);
1.2       paf       360: }      
1.1       paf       361: 
1.41      paf       362: // global variable
                    363: 
                    364: Methoded *string_class;
                    365: 
                    366: // creator
                    367: 
                    368: Methoded *MString_create(Pool& pool) {
                    369:        return string_class=new(pool) MString(pool);
                    370: }

E-mail: