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

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

E-mail: