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

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.24    ! paf         8:        $Id: string.C,v 1.23 2001/04/03 05:23:37 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.1       paf        16: 
                     17: // global var
                     18: 
1.10      paf        19: VStateless_class *string_class;
1.1       paf        20: 
                     21: // methods
                     22: 
1.7       paf        23: static void _length(Request& r, const String&, Array *) {
1.1       paf        24:        Pool& pool=r.pool();
1.15      paf        25:        Value& value=*new(pool) VDouble(pool, r.self->get_string()->size());
1.11      paf        26:        r.write_no_lang(value);
1.1       paf        27: }
                     28: 
1.7       paf        29: static void _int(Request& r, const String&, Array *) {
1.1       paf        30:        Pool& pool=r.pool();
1.13      paf        31:        Value& value=*new(pool) VInt(pool, (int)r.self->as_double());
1.11      paf        32:        r.write_no_lang(value);
1.1       paf        33: }
                     34: 
1.7       paf        35: static void _double(Request& r, const String&, Array *) {
1.1       paf        36:        Pool& pool=r.pool();
1.13      paf        37:        Value& value=*new(pool) VDouble(pool, r.self->as_double());
1.11      paf        38:        r.write_no_lang(value);
1.1       paf        39: }
                     40: 
1.24    ! paf        41: /// ^string.format{format}
1.18      paf        42: /*not static*/void _string_format(Request& r, const String& method_name, Array *params) {
1.9       paf        43:        Pool& pool=r.pool();
                     44: 
                     45:        Value& fmt=*static_cast<Value *>(params->get(0));
1.24    ! paf        46:        // forcing {this param type}
        !            47:        r.fail_if_junction_(false, fmt, method_name, "fmt must be junction");
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.18      paf        55: static void _left(Request& r, const String&, Array *params) {
1.15      paf        56:        Pool& pool=r.pool();
                     57: 
                     58:        size_t n=(size_t)r.process(*static_cast<Value *>(params->get(0))).as_double();
                     59:        
                     60:        const String& string=*static_cast<VString *>(r.self)->get_string();
                     61:        r.write_assign_lang(*new(pool) VString(string.piece(0, n)));
                     62: }
                     63: 
1.18      paf        64: static void _right(Request& r, const String&, Array *params) {
1.15      paf        65:        Pool& pool=r.pool();
                     66: 
                     67:        size_t n=(size_t)r.process(*static_cast<Value *>(params->get(0))).as_double();
                     68:        
                     69:        const String& string=*static_cast<VString *>(r.self)->get_string();
                     70:        r.write_assign_lang(*new(pool) VString(string.piece(string.size()-n, string.size())));
                     71: }
                     72: 
1.18      paf        73: static void _mid(Request& r, const String&, Array *params) {
1.15      paf        74:        Pool& pool=r.pool();
                     75: 
                     76:        size_t p=(size_t)r.process(*static_cast<Value *>(params->get(0))).as_double();
                     77:        size_t n=(size_t)r.process(*static_cast<Value *>(params->get(1))).as_double();
                     78:        
                     79:        const String& string=*static_cast<VString *>(r.self)->get_string();
                     80:        r.write_assign_lang(*new(pool) VString(string.piece(p, p+n)));
                     81: }
                     82: 
1.18      paf        83: static void _pos(Request& r, const String& method_name, Array *params) {
1.16      paf        84:        Pool& pool=r.pool();
                     85: 
                     86:        Value& substr=*static_cast<Value *>(params->get(0));
1.17      paf        87:        // forcing [this param type]
1.16      paf        88:        r.fail_if_junction_(true, substr, method_name, "substr must not be junction");
                     89:        
                     90:        const String& string=*static_cast<VString *>(r.self)->get_string();
                     91:        r.write_assign_lang(*new(pool) VInt(pool, string.pos(substr.as_string())));
                     92: }
                     93: 
1.19      paf        94: static void split_list(Request& r, const String& method_name, Array *params,
1.23      paf        95:                                           const String& string, 
                     96:                                           Array& result) {
1.17      paf        97:        Pool& pool=r.pool();
                     98: 
                     99:        Value& delim_value=*static_cast<Value *>(params->get(0));
                    100:        // forcing [this param type]
                    101:        r.fail_if_junction_(true, delim_value, method_name, "delimiter must not be junction");
1.23      paf       102: 
                    103:        string.split(result, 0, delim_value.as_string(), String::UL_CLEAN, -1);
1.19      paf       104: }
                    105: 
                    106: static void _lsplit(Request& r, const String& method_name, Array *params) {
                    107:        Pool& pool=r.pool();
                    108:        const String& string=*static_cast<VString *>(r.self)->get_string();
                    109: 
1.21      paf       110:        Array& row=*new(pool) Array(pool);
                    111:        split_list(r, method_name, params, string, row);
1.19      paf       112: 
1.21      paf       113:        Table& table=*new(pool) Table(pool, &string, 
                    114:                0/*nameless*/, 1/*row preallocate(and only)*/);
                    115:        table+=&row;
1.19      paf       116: 
                    117:        r.write_no_lang(*new(pool) VTable(pool, &table));
                    118: }
                    119: 
                    120: static void _rsplit(Request& r, const String& method_name, Array *params) {
                    121:        Pool& pool=r.pool();
                    122:        const String& string=*static_cast<VString *>(r.self)->get_string();
                    123: 
                    124:        Array list(pool);
                    125:        split_list(r, method_name, params, string, list);
                    126: 
1.21      paf       127:        Array& row=*new(pool) Array(pool);
                    128:        for(int i=list.size(); --i>=0; )
                    129:                row+=list.get(i);
                    130: 
                    131:        Table& table=*new(pool) Table(pool, &string, 
                    132:                0/*nameless*/, 1/*row preallocate(and only)*/);
                    133:        table+=&row;
1.17      paf       134: 
1.19      paf       135:        r.write_no_lang(*new(pool) VTable(pool, &table));
1.17      paf       136: }
                    137: 
1.24    ! paf       138: /// ^string.match{regexp}[options]
        !           139: static void _match(Request& r, const String& method_name, Array *params) {
        !           140:        Pool& pool=r.pool();
        !           141:        const String& string=*static_cast<VString *>(r.self)->get_string();
        !           142: 
        !           143:        Value& regexp=*static_cast<Value *>(params->get(0));
        !           144:        // forcing {this param type}
        !           145:        r.fail_if_junction_(false, regexp, method_name, "regexp must be junction");
        !           146: 
        !           147:        Value& options=*static_cast<Value *>(params->get(1));
        !           148:        // forcing {this param type}
        !           149:        r.fail_if_junction_(true, options, method_name, "options must not be junction");
        !           150: 
        !           151:        Temp_lang temp_lang(r, String::UL_PASS_APPENDED);
        !           152:        Table& table=string.match(&method_name, 
        !           153:                r.process(regexp).as_string(), options.as_string());
        !           154: 
        !           155:        r.write_no_lang(*new(pool) VTable(pool, &table));
        !           156: }
        !           157: 
1.11      paf       158: // initialize
1.9       paf       159: 
1.10      paf       160: void initialize_string_class(Pool& pool, VStateless_class& vclass) {
1.1       paf       161:        // ^string.length[]
1.22      paf       162:        vclass.add_native_method("length", Method::CT_DYNAMIC, _length, 0, 0);
1.6       paf       163:        
1.1       paf       164:        // ^string.int[]
1.22      paf       165:        vclass.add_native_method("int", Method::CT_DYNAMIC, _int, 0, 0);
1.6       paf       166:        
1.1       paf       167:        // ^string.double[]
1.22      paf       168:        vclass.add_native_method("double", Method::CT_DYNAMIC, _double, 0, 0);
1.9       paf       169: 
1.24    ! paf       170:        // ^string.format{format}
1.22      paf       171:        vclass.add_native_method("format", Method::CT_DYNAMIC, _string_format, 1, 1);
1.14      paf       172: 
1.15      paf       173:        // ^string.left(n)
1.22      paf       174:        vclass.add_native_method("left", Method::CT_DYNAMIC, _left, 1, 1);
1.15      paf       175:        // ^string.right(n)
1.22      paf       176:        vclass.add_native_method("right", Method::CT_DYNAMIC, _right, 1, 1);
1.15      paf       177:        // ^string.mid(p;n)
1.22      paf       178:        vclass.add_native_method("mid", Method::CT_DYNAMIC, _mid, 2, 2);
1.16      paf       179: 
                    180:        // ^string.pos[substr]
1.22      paf       181:        vclass.add_native_method("pos", Method::CT_DYNAMIC, _pos, 1, 1);
1.17      paf       182: 
                    183:        // ^string.lsplit[delim]
1.22      paf       184:        vclass.add_native_method("lsplit", Method::CT_DYNAMIC, _lsplit, 1, 1);
1.19      paf       185:        // ^string.rsplit[delim]
1.22      paf       186:        vclass.add_native_method("rsplit", Method::CT_DYNAMIC, _rsplit, 1, 1);
1.24    ! paf       187: 
        !           188:        // ^string.match{regexp}[options]
        !           189:        vclass.add_native_method("match", Method::CT_DYNAMIC, _match, 2, 2);
1.2       paf       190: }      
1.1       paf       191: 

E-mail: