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

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.4       paf         7: 
1.39    ! paf         8:        $Id: string.C,v 1.38 2001/04/15 13:12:18 paf Exp $
1.1       paf         9: */
                     10: 
                     11: #include "pa_request.h"
                     12: #include "_string.h"
                     13: #include "pa_vdouble.h"
                     14: #include "pa_vint.h"
1.17      paf        15: #include "pa_vtable.h"
1.25      paf        16: #include "pa_vbool.h"
1.27      paf        17: #include "pa_string.h"
1.1       paf        18: 
                     19: // global var
                     20: 
1.10      paf        21: VStateless_class *string_class;
1.1       paf        22: 
                     23: // methods
                     24: 
1.38      paf        25: static void _length(Request& r, const String&, MethodParams *) {
1.1       paf        26:        Pool& pool=r.pool();
1.15      paf        27:        Value& value=*new(pool) VDouble(pool, r.self->get_string()->size());
1.11      paf        28:        r.write_no_lang(value);
1.1       paf        29: }
                     30: 
1.38      paf        31: static void _int(Request& r, const String&, MethodParams *) {
1.1       paf        32:        Pool& pool=r.pool();
1.13      paf        33:        Value& value=*new(pool) VInt(pool, (int)r.self->as_double());
1.11      paf        34:        r.write_no_lang(value);
1.1       paf        35: }
                     36: 
1.38      paf        37: static void _double(Request& r, const String&, MethodParams *) {
1.1       paf        38:        Pool& pool=r.pool();
1.13      paf        39:        Value& value=*new(pool) VDouble(pool, r.self->as_double());
1.11      paf        40:        r.write_no_lang(value);
1.1       paf        41: }
                     42: 
1.24      paf        43: /// ^string.format{format}
1.38      paf        44: /*not static*/void _string_format(Request& r, const String& method_name, MethodParams *params) {
1.9       paf        45:        Pool& pool=r.pool();
                     46: 
1.38      paf        47:        Value& fmt=params->get_junction(0, "fmt must be code");
1.9       paf        48: 
1.24      paf        49:        Temp_lang temp_lang(r, String::UL_PASS_APPENDED);
                     50:        char *buf=format(pool, r.self->as_double(), r.process(fmt).as_string().cstr());
1.9       paf        51:        
1.12      paf        52:        r.write_no_lang(String(pool, buf));
1.9       paf        53: }
1.11      paf        54: 
1.38      paf        55: static void _left(Request& r, const String&, MethodParams *params) {
1.15      paf        56:        Pool& pool=r.pool();
                     57: 
1.38      paf        58:        size_t n=(size_t)r.process(params->get(0)).as_double();
1.15      paf        59:        
                     60:        const String& string=*static_cast<VString *>(r.self)->get_string();
1.35      paf        61:        r.write_assign_lang(*new(pool) VString(string.mid(0, n)));
1.15      paf        62: }
                     63: 
1.38      paf        64: static void _right(Request& r, const String&, MethodParams *params) {
1.15      paf        65:        Pool& pool=r.pool();
                     66: 
1.38      paf        67:        size_t n=(size_t)r.process(params->get(0)).as_double();
1.15      paf        68:        
                     69:        const String& string=*static_cast<VString *>(r.self)->get_string();
1.35      paf        70:        r.write_assign_lang(*new(pool) VString(string.mid(string.size()-n, string.size())));
1.15      paf        71: }
                     72: 
1.38      paf        73: static void _mid(Request& r, const String&, MethodParams *params) {
1.15      paf        74:        Pool& pool=r.pool();
                     75: 
1.38      paf        76:        size_t p=(size_t)r.process(params->get(0)).as_double();
                     77:        size_t n=(size_t)r.process(params->get(1)).as_double();
1.15      paf        78:        
                     79:        const String& string=*static_cast<VString *>(r.self)->get_string();
1.35      paf        80:        r.write_assign_lang(*new(pool) VString(string.mid(p, p+n)));
1.15      paf        81: }
                     82: 
1.38      paf        83: static void _pos(Request& r, const String& method_name, MethodParams *params) {
1.16      paf        84:        Pool& pool=r.pool();
                     85: 
1.38      paf        86:        Value& substr=params->get_no_junction(0, "substr must not be code");
1.16      paf        87:        
                     88:        const String& string=*static_cast<VString *>(r.self)->get_string();
                     89:        r.write_assign_lang(*new(pool) VInt(pool, string.pos(substr.as_string())));
                     90: }
                     91: 
1.38      paf        92: static void split_list(Request& r, const String& method_name, MethodParams *params,
1.23      paf        93:                                           const String& string, 
                     94:                                           Array& result) {
1.17      paf        95:        Pool& pool=r.pool();
                     96: 
1.38      paf        97:        Value& delim_value=params->get_no_junction(0, "delimiter must not be code");
1.23      paf        98: 
                     99:        string.split(result, 0, delim_value.as_string(), String::UL_CLEAN, -1);
1.19      paf       100: }
                    101: 
1.38      paf       102: static void _lsplit(Request& r, const String& method_name, MethodParams *params) {
1.19      paf       103:        Pool& pool=r.pool();
                    104:        const String& string=*static_cast<VString *>(r.self)->get_string();
                    105: 
1.21      paf       106:        Array& row=*new(pool) Array(pool);
                    107:        split_list(r, method_name, params, string, row);
1.19      paf       108: 
1.21      paf       109:        Table& table=*new(pool) Table(pool, &string, 
                    110:                0/*nameless*/, 1/*row preallocate(and only)*/);
                    111:        table+=&row;
1.19      paf       112: 
                    113:        r.write_no_lang(*new(pool) VTable(pool, &table));
                    114: }
                    115: 
1.38      paf       116: static void _rsplit(Request& r, const String& method_name, MethodParams *params) {
1.19      paf       117:        Pool& pool=r.pool();
                    118:        const String& string=*static_cast<VString *>(r.self)->get_string();
                    119: 
                    120:        Array list(pool);
                    121:        split_list(r, method_name, params, string, list);
                    122: 
1.21      paf       123:        Array& row=*new(pool) Array(pool);
                    124:        for(int i=list.size(); --i>=0; )
                    125:                row+=list.get(i);
                    126: 
                    127:        Table& table=*new(pool) Table(pool, &string, 
                    128:                0/*nameless*/, 1/*row preallocate(and only)*/);
                    129:        table+=&row;
1.17      paf       130: 
1.19      paf       131:        r.write_no_lang(*new(pool) VTable(pool, &table));
1.17      paf       132: }
                    133: 
1.33      paf       134: static void search_action(Table& table, Array *row, int, int, void *) {
1.28      paf       135:        if(row)
                    136:                table+=row;
1.27      paf       137: }
                    138: 
                    139: struct Replace_action_info {
1.30      paf       140:        Request *request;  const String *origin;
1.31      paf       141:        const String *src;  String *dest;
1.27      paf       142:        Value *replacement_code;
1.29      paf       143:        const String *post_match;
1.27      paf       144: };
1.33      paf       145: static void replace_action(Table& table, Array *row, int start, int finish, 
1.31      paf       146:                                                           void *info) {
1.27      paf       147:        Replace_action_info& ai=*static_cast<Replace_action_info *>(info);
1.31      paf       148:        if(row) { // begin&middle
                    149:                // piece from last match['start'] to beginning of this match['finish']
                    150:                if(start!=finish)
1.37      paf       151:                        *ai.dest << ai.src->mid(start, finish);//ai.dest->APPEND_CONST("-");
1.30      paf       152:                // store found parts in one-record Vtable
1.32      paf       153:                if(table.size()) // middle
                    154:                        table.put(0, row);
                    155:                else // begin
1.30      paf       156:                        table+=row;
                    157:                { // execute 'replacement_code' in 'table' context
                    158:                        VTable& vtable=*new(table.pool()) VTable(table.pool(), &table);
                    159:                        vtable.set_name(*ai.origin);
                    160: 
                    161:                        Junction *junction=ai.replacement_code->get_junction();
1.34      paf       162:                        junction->rcontext=junction->root=&vtable;
1.30      paf       163:                        Value& replaced=ai.request->process(*ai.replacement_code, ai.origin, false);
                    164: 
1.34      paf       165:                        /*
1.31      paf       166:                        ai.dest->APPEND_CONST("(");
1.37      paf       167:                                *ai.dest << *(String *)row->get(1/*match* /);
1.31      paf       168:                        ai.dest->APPEND_CONST(")");
1.34      paf       169:                        */
1.37      paf       170:                        *ai.dest << replaced.as_string();
1.29      paf       171:                }
                    172:                ai.post_match=(String *)row->get(2/*post_match*/);
                    173:        } else // end
1.37      paf       174:                *ai.dest << *ai.post_match;
1.27      paf       175: }
                    176: 
                    177: /** search/replace
1.32      paf       178:        ^string.match[regexp][options]
                    179:        ^string.match[regexp][options]{replacement-code}
1.27      paf       180: */
1.38      paf       181: static void _match(Request& r, const String& method_name, MethodParams *params) {
1.24      paf       182:        Pool& pool=r.pool();
1.28      paf       183:        const String& src=*static_cast<VString *>(r.self)->get_string();
1.24      paf       184: 
1.38      paf       185:        Value& regexp=params->get_no_junction(0, "regexp must not be code");
                    186: 
                    187:        const String *options=
                    188:                params->size()>1?
                    189:                &params->get_no_junction(1, "options must not be code").as_string():0;
1.24      paf       190: 
1.27      paf       191:        Value *result;
1.24      paf       192:        Temp_lang temp_lang(r, String::UL_PASS_APPENDED);
1.25      paf       193:        Table *table;
1.27      paf       194:        if(params->size()<3) { // search
1.39    ! paf       195:                if(src.match(r.pcre_tables,
        !           196:                        &method_name, 
1.32      paf       197:                        regexp.as_string(), options,
1.27      paf       198:                        &table,
1.33      paf       199:                        search_action, 0)) {
1.27      paf       200:                        // matched
                    201:                        if(table->columns()->size()==3 && // just matched[3=pre/match/post], no substrings
                    202:                                table->size()==1)  // just one row, not /g_lobal search
                    203:                                result=new(pool) VBool(pool, true);
                    204:                        else // table of pre/match/post+substrings
                    205:                                result=new(pool) VTable(pool, table);
                    206:                } else // not matched [not global]
                    207:                        result=new(pool) VBool(pool, false);
                    208:        } else { // replace
1.38      paf       209:                Value& replacement_code=params->get_junction(2, "replacement code must be code");
1.24      paf       210: 
1.28      paf       211:                String& dest=*new(pool) String(pool);
1.27      paf       212:                Replace_action_info replace_action_info={
1.30      paf       213:                        &r, &method_name,
1.31      paf       214:                        &src, &dest,
1.28      paf       215:                        &replacement_code,
1.29      paf       216:                        &src
1.27      paf       217:                };
1.39    ! paf       218:                src.match(r.pcre_tables,
        !           219:                        &method_name, 
1.27      paf       220:                        r.process(regexp).as_string(), options,
                    221:                        &table,
1.33      paf       222:                        replace_action, &replace_action_info);
1.28      paf       223:                result=new(pool) VString(dest);
1.27      paf       224:        }
1.26      paf       225:        result->set_name(method_name);
1.34      paf       226:        r.write_assign_lang(*result);
1.24      paf       227: }
                    228: 
1.11      paf       229: // initialize
1.9       paf       230: 
1.10      paf       231: void initialize_string_class(Pool& pool, VStateless_class& vclass) {
1.1       paf       232:        // ^string.length[]
1.22      paf       233:        vclass.add_native_method("length", Method::CT_DYNAMIC, _length, 0, 0);
1.6       paf       234:        
1.1       paf       235:        // ^string.int[]
1.22      paf       236:        vclass.add_native_method("int", Method::CT_DYNAMIC, _int, 0, 0);
1.6       paf       237:        
1.1       paf       238:        // ^string.double[]
1.22      paf       239:        vclass.add_native_method("double", Method::CT_DYNAMIC, _double, 0, 0);
1.9       paf       240: 
1.24      paf       241:        // ^string.format{format}
1.22      paf       242:        vclass.add_native_method("format", Method::CT_DYNAMIC, _string_format, 1, 1);
1.14      paf       243: 
1.15      paf       244:        // ^string.left(n)
1.22      paf       245:        vclass.add_native_method("left", Method::CT_DYNAMIC, _left, 1, 1);
1.15      paf       246:        // ^string.right(n)
1.22      paf       247:        vclass.add_native_method("right", Method::CT_DYNAMIC, _right, 1, 1);
1.15      paf       248:        // ^string.mid(p;n)
1.22      paf       249:        vclass.add_native_method("mid", Method::CT_DYNAMIC, _mid, 2, 2);
1.16      paf       250: 
                    251:        // ^string.pos[substr]
1.22      paf       252:        vclass.add_native_method("pos", Method::CT_DYNAMIC, _pos, 1, 1);
1.17      paf       253: 
                    254:        // ^string.lsplit[delim]
1.22      paf       255:        vclass.add_native_method("lsplit", Method::CT_DYNAMIC, _lsplit, 1, 1);
1.19      paf       256:        // ^string.rsplit[delim]
1.22      paf       257:        vclass.add_native_method("rsplit", Method::CT_DYNAMIC, _rsplit, 1, 1);
1.24      paf       258: 
1.32      paf       259:        // ^string.match[regexp][options]
                    260:        // ^string.match[regexp][options]{replacement-code}
1.28      paf       261:        vclass.add_native_method("match", Method::CT_DYNAMIC, _match, 1, 3);
1.2       paf       262: }      
1.1       paf       263: 

E-mail: