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

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

E-mail: