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