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

1.1       paf         1: /*
1.4       paf         2:        Parser
                      3:        Copyright (c) 2001 ArtLebedev Group (http://www.artlebedev.com)
1.5       paf         4:        Author: Alexander Petrosyan <paf@design.ru> (http://design.ru/paf)
1.4       paf         5: 
1.17    ! paf         6:        $Id: string.C,v 1.16 2001/03/29 15:36:14 paf Exp $
1.1       paf         7: */
                      8: 
                      9: #include "pa_request.h"
                     10: #include "_string.h"
                     11: #include "pa_vdouble.h"
                     12: #include "pa_vint.h"
1.17    ! paf        13: #include "pa_vtable.h"
1.1       paf        14: 
                     15: // global var
                     16: 
1.10      paf        17: VStateless_class *string_class;
1.1       paf        18: 
                     19: // methods
                     20: 
1.7       paf        21: static void _length(Request& r, const String&, Array *) {
1.1       paf        22:        Pool& pool=r.pool();
1.15      paf        23:        Value& value=*new(pool) VDouble(pool, r.self->get_string()->size());
1.11      paf        24:        r.write_no_lang(value);
1.1       paf        25: }
                     26: 
1.7       paf        27: static void _int(Request& r, const String&, Array *) {
1.1       paf        28:        Pool& pool=r.pool();
1.13      paf        29:        Value& value=*new(pool) VInt(pool, (int)r.self->as_double());
1.11      paf        30:        r.write_no_lang(value);
1.1       paf        31: }
                     32: 
1.7       paf        33: static void _double(Request& r, const String&, Array *) {
1.1       paf        34:        Pool& pool=r.pool();
1.13      paf        35:        Value& value=*new(pool) VDouble(pool, r.self->as_double());
1.11      paf        36:        r.write_no_lang(value);
1.1       paf        37: }
                     38: 
1.9       paf        39: void _string_format(Request& r, const String& method_name, Array *params) {
                     40:        Pool& pool=r.pool();
                     41: 
                     42:        Value& fmt=*static_cast<Value *>(params->get(0));
1.17    ! paf        43:        // forcing [this param type]
1.16      paf        44:        r.fail_if_junction_(true, fmt, method_name, "fmt must not be junction");
1.9       paf        45: 
1.13      paf        46:        char *buf=format(pool, r.self->as_double(), fmt.as_string().cstr());
1.9       paf        47:        
1.12      paf        48:        r.write_no_lang(String(pool, buf));
1.9       paf        49: }
1.11      paf        50: 
1.16      paf        51: void _left(Request& r, const String&, Array *params) {
1.15      paf        52:        Pool& pool=r.pool();
                     53: 
                     54:        size_t n=(size_t)r.process(*static_cast<Value *>(params->get(0))).as_double();
                     55:        
                     56:        const String& string=*static_cast<VString *>(r.self)->get_string();
                     57:        r.write_assign_lang(*new(pool) VString(string.piece(0, n)));
                     58: }
                     59: 
1.16      paf        60: void _right(Request& r, const String&, Array *params) {
1.15      paf        61:        Pool& pool=r.pool();
                     62: 
                     63:        size_t n=(size_t)r.process(*static_cast<Value *>(params->get(0))).as_double();
                     64:        
                     65:        const String& string=*static_cast<VString *>(r.self)->get_string();
                     66:        r.write_assign_lang(*new(pool) VString(string.piece(string.size()-n, string.size())));
                     67: }
                     68: 
1.16      paf        69: void _mid(Request& r, const String&, Array *params) {
1.15      paf        70:        Pool& pool=r.pool();
                     71: 
                     72:        size_t p=(size_t)r.process(*static_cast<Value *>(params->get(0))).as_double();
                     73:        size_t n=(size_t)r.process(*static_cast<Value *>(params->get(1))).as_double();
                     74:        
                     75:        const String& string=*static_cast<VString *>(r.self)->get_string();
                     76:        r.write_assign_lang(*new(pool) VString(string.piece(p, p+n)));
                     77: }
                     78: 
1.16      paf        79: void _pos(Request& r, const String& method_name, Array *params) {
                     80:        Pool& pool=r.pool();
                     81: 
                     82:        Value& substr=*static_cast<Value *>(params->get(0));
1.17    ! paf        83:        // forcing [this param type]
1.16      paf        84:        r.fail_if_junction_(true, substr, method_name, "substr must not be junction");
                     85:        
                     86:        const String& string=*static_cast<VString *>(r.self)->get_string();
                     87:        r.write_assign_lang(*new(pool) VInt(pool, string.pos(substr.as_string())));
                     88: }
                     89: 
1.17    ! paf        90: void _lsplit(Request& r, const String& method_name, Array *params) {
        !            91:        Pool& pool=r.pool();
        !            92: 
        !            93:        Value& delim_value=*static_cast<Value *>(params->get(0));
        !            94:        // forcing [this param type]
        !            95:        r.fail_if_junction_(true, delim_value, method_name, "delimiter must not be junction");
        !            96:        const String& delim=delim_value.as_string();
        !            97:        
        !            98:        const String& string=*static_cast<VString *>(r.self)->get_string();
        !            99: 
        !           100:        Table& result=*new(pool) Table(pool, &string, 0);
        !           101: 
        !           102:        int pos_before=string.pos(delim);
        !           103:        if(pos_before>=0) {
        !           104:                {
        !           105:                        Array& row=*new(pool) Array(pool, 1);
        !           106:                        result+=&(row+=&string.piece(0, pos_before));
        !           107:                }
        !           108:                size_t pos_after=pos_before+delim.size();
        !           109:                if(pos_after<string.size()) {
        !           110:                        Array& row=*new(pool) Array(pool, 1);
        !           111:                        result+=&(row+=&string.piece(pos_after, string.size()));
        !           112:                }
        !           113:        } else {
        !           114:                Array& row=*new(pool) Array(pool, 1);
        !           115:                result+=&(row+=&string);
        !           116:        }
        !           117: 
        !           118:        r.write_no_lang(*new(pool) VTable(pool, &result));
        !           119: }
        !           120: 
1.11      paf       121: // initialize
1.9       paf       122: 
1.10      paf       123: void initialize_string_class(Pool& pool, VStateless_class& vclass) {
1.1       paf       124:        // ^string.length[]
1.6       paf       125:        vclass.add_native_method("length", _length, 0, 0);
                    126:        
1.1       paf       127:        // ^string.int[]
1.6       paf       128:        vclass.add_native_method("int", _int, 0, 0);
                    129:        
1.1       paf       130:        // ^string.double[]
1.6       paf       131:        vclass.add_native_method("double", _double, 0, 0);
1.9       paf       132: 
                    133:        // ^string.format[]
                    134:        vclass.add_native_method("format", _string_format, 1, 1);
1.14      paf       135: 
1.15      paf       136:        // ^string.left(n)
                    137:        vclass.add_native_method("left", _left, 1, 1);
                    138:        // ^string.right(n)
                    139:        vclass.add_native_method("right", _right, 1, 1);
                    140:        // ^string.mid(p;n)
                    141:        vclass.add_native_method("mid", _mid, 2, 2);
1.16      paf       142: 
                    143:        // ^string.pos[substr]
                    144:        vclass.add_native_method("pos", _pos, 1, 1);
1.17    ! paf       145: 
        !           146:        // ^string.lsplit[delim]
        !           147:        vclass.add_native_method("lsplit", _lsplit, 1, 1);
1.2       paf       148: }      
1.1       paf       149: 

E-mail: