Annotation of parser3/src/classes/string.C, revision 1.22
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.22 ! paf 6: $Id: string.C,v 1.21 2001/03/29 17:44:38 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.18 paf 39: /*not static*/void _string_format(Request& r, const String& method_name, Array *params) {
1.9 paf 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.18 paf 51: static 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.18 paf 60: static 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.18 paf 69: static 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.18 paf 79: static void _pos(Request& r, const String& method_name, Array *params) {
1.16 paf 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.19 paf 90: static void split_list(Request& r, const String& method_name, Array *params,
91: const String& string, Array& list) {
1.17 paf 92: Pool& pool=r.pool();
93:
94: Value& delim_value=*static_cast<Value *>(params->get(0));
95: // forcing [this param type]
96: r.fail_if_junction_(true, delim_value, method_name, "delimiter must not be junction");
97: const String& delim=delim_value.as_string();
98:
1.18 paf 99: if(delim.size()) {
100: size_t pos_after=0;
101: int pos_before;
1.20 paf 102: // while we have 'delim' in 'string'...
103: while((pos_before=string.pos(delim, pos_after))>=0) {
1.19 paf 104: list+=&string.piece(pos_after, pos_before);
1.18 paf 105: pos_after=pos_before+delim.size();
1.17 paf 106: }
1.18 paf 107: // last piece
1.19 paf 108: if(pos_after<string.size())
109: list+=&string.piece(pos_after, string.size());
1.18 paf 110: } else { // empty delim
1.19 paf 111: list+=&string;
112: }
113: }
114:
115: static void _lsplit(Request& r, const String& method_name, Array *params) {
116: Pool& pool=r.pool();
117: const String& string=*static_cast<VString *>(r.self)->get_string();
118:
1.21 paf 119: Array& row=*new(pool) Array(pool);
120: split_list(r, method_name, params, string, row);
1.19 paf 121:
1.21 paf 122: Table& table=*new(pool) Table(pool, &string,
123: 0/*nameless*/, 1/*row preallocate(and only)*/);
124: table+=&row;
1.19 paf 125:
126: r.write_no_lang(*new(pool) VTable(pool, &table));
127: }
128:
129: static void _rsplit(Request& r, const String& method_name, Array *params) {
130: Pool& pool=r.pool();
131: const String& string=*static_cast<VString *>(r.self)->get_string();
132:
133: Array list(pool);
134: split_list(r, method_name, params, string, list);
135:
1.21 paf 136: Array& row=*new(pool) Array(pool);
137: for(int i=list.size(); --i>=0; )
138: row+=list.get(i);
139:
140: Table& table=*new(pool) Table(pool, &string,
141: 0/*nameless*/, 1/*row preallocate(and only)*/);
142: table+=&row;
1.17 paf 143:
1.19 paf 144: r.write_no_lang(*new(pool) VTable(pool, &table));
1.17 paf 145: }
146:
1.11 paf 147: // initialize
1.9 paf 148:
1.10 paf 149: void initialize_string_class(Pool& pool, VStateless_class& vclass) {
1.1 paf 150: // ^string.length[]
1.22 ! paf 151: vclass.add_native_method("length", Method::CT_DYNAMIC, _length, 0, 0);
1.6 paf 152:
1.1 paf 153: // ^string.int[]
1.22 ! paf 154: vclass.add_native_method("int", Method::CT_DYNAMIC, _int, 0, 0);
1.6 paf 155:
1.1 paf 156: // ^string.double[]
1.22 ! paf 157: vclass.add_native_method("double", Method::CT_DYNAMIC, _double, 0, 0);
1.9 paf 158:
159: // ^string.format[]
1.22 ! paf 160: vclass.add_native_method("format", Method::CT_DYNAMIC, _string_format, 1, 1);
1.14 paf 161:
1.15 paf 162: // ^string.left(n)
1.22 ! paf 163: vclass.add_native_method("left", Method::CT_DYNAMIC, _left, 1, 1);
1.15 paf 164: // ^string.right(n)
1.22 ! paf 165: vclass.add_native_method("right", Method::CT_DYNAMIC, _right, 1, 1);
1.15 paf 166: // ^string.mid(p;n)
1.22 ! paf 167: vclass.add_native_method("mid", Method::CT_DYNAMIC, _mid, 2, 2);
1.16 paf 168:
169: // ^string.pos[substr]
1.22 ! paf 170: vclass.add_native_method("pos", Method::CT_DYNAMIC, _pos, 1, 1);
1.17 paf 171:
172: // ^string.lsplit[delim]
1.22 ! paf 173: vclass.add_native_method("lsplit", Method::CT_DYNAMIC, _lsplit, 1, 1);
1.19 paf 174: // ^string.rsplit[delim]
1.22 ! paf 175: vclass.add_native_method("rsplit", Method::CT_DYNAMIC, _rsplit, 1, 1);
1.2 paf 176: }
1.1 paf 177:
E-mail: