Annotation of parser3/src/classes/string.C, revision 1.71
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.1 paf 7: */
1.71 ! parser 8: static const char *RCSId="$Id: string.C,v 1.70 2001/08/07 13:54:13 parser Exp $";
1.1 paf 9:
1.43 paf 10: #include "classes.h"
1.1 paf 11: #include "pa_request.h"
12: #include "pa_vdouble.h"
13: #include "pa_vint.h"
1.17 paf 14: #include "pa_vtable.h"
1.25 paf 15: #include "pa_vbool.h"
1.27 paf 16: #include "pa_string.h"
1.53 parser 17: #include "pa_sql_connection.h"
1.71 ! parser 18: #include "pa_dictionary.h"
1.1 paf 19:
1.41 paf 20: // defines
1.1 paf 21:
1.41 paf 22: #define STRING_CLASS_NAME "string"
23:
24: // class
25:
26: class MString : public Methoded {
27: public:
28: MString(Pool& pool);
1.44 paf 29: public: // Methoded
1.53 parser 30: bool used_directly() { return true; }
1.41 paf 31: };
1.1 paf 32:
33: // methods
34:
1.47 paf 35: static void _length(Request& r, const String& method_name, MethodParams *) {
1.1 paf 36: Pool& pool=r.pool();
1.47 paf 37: Value& result=*new(pool) VDouble(pool, r.self->get_string()->size());
38: result.set_name(method_name);
39: r.write_no_lang(result);
1.1 paf 40: }
41:
1.47 paf 42: static void _int(Request& r, const String& method_name, MethodParams *) {
1.1 paf 43: Pool& pool=r.pool();
1.48 parser 44: Value& result=*new(pool) VInt(pool, r.self->as_int());
1.47 paf 45: result.set_name(method_name);
46: r.write_no_lang(result);
1.1 paf 47: }
48:
1.47 paf 49: static void _double(Request& r, const String& method_name, MethodParams *) {
1.1 paf 50: Pool& pool=r.pool();
1.47 paf 51: Value& result=*new(pool) VDouble(pool, r.self->as_double());
52: result.set_name(method_name);
53: r.write_no_lang(result);
1.1 paf 54: }
55:
1.38 paf 56: /*not static*/void _string_format(Request& r, const String& method_name, MethodParams *params) {
1.9 paf 57: Pool& pool=r.pool();
58:
1.59 parser 59: Value& fmt=params->as_junction(0, "fmt must be code");
1.9 paf 60:
1.24 paf 61: Temp_lang temp_lang(r, String::UL_PASS_APPENDED);
62: char *buf=format(pool, r.self->as_double(), r.process(fmt).as_string().cstr());
1.63 parser 63:
64: String result(pool);
65: result.APPEND_CLEAN(buf, 0,
66: method_name.origin().file,
67: method_name.origin().line);
68: r.write_no_lang(result);
1.9 paf 69: }
1.11 paf 70:
1.38 paf 71: static void _left(Request& r, const String&, MethodParams *params) {
1.15 paf 72: Pool& pool=r.pool();
73:
1.38 paf 74: size_t n=(size_t)r.process(params->get(0)).as_double();
1.15 paf 75:
76: const String& string=*static_cast<VString *>(r.self)->get_string();
1.35 paf 77: r.write_assign_lang(*new(pool) VString(string.mid(0, n)));
1.15 paf 78: }
79:
1.38 paf 80: static void _right(Request& r, const String&, MethodParams *params) {
1.15 paf 81: Pool& pool=r.pool();
82:
1.38 paf 83: size_t n=(size_t)r.process(params->get(0)).as_double();
1.15 paf 84:
85: const String& string=*static_cast<VString *>(r.self)->get_string();
1.35 paf 86: r.write_assign_lang(*new(pool) VString(string.mid(string.size()-n, string.size())));
1.15 paf 87: }
88:
1.38 paf 89: static void _mid(Request& r, const String&, MethodParams *params) {
1.15 paf 90: Pool& pool=r.pool();
91:
1.38 paf 92: size_t p=(size_t)r.process(params->get(0)).as_double();
93: size_t n=(size_t)r.process(params->get(1)).as_double();
1.15 paf 94:
95: const String& string=*static_cast<VString *>(r.self)->get_string();
1.35 paf 96: r.write_assign_lang(*new(pool) VString(string.mid(p, p+n)));
1.15 paf 97: }
98:
1.38 paf 99: static void _pos(Request& r, const String& method_name, MethodParams *params) {
1.16 paf 100: Pool& pool=r.pool();
101:
1.59 parser 102: Value& substr=params->as_no_junction(0, "substr must not be code");
1.16 paf 103:
104: const String& string=*static_cast<VString *>(r.self)->get_string();
105: r.write_assign_lang(*new(pool) VInt(pool, string.pos(substr.as_string())));
106: }
107:
1.38 paf 108: static void split_list(Request& r, const String& method_name, MethodParams *params,
1.23 paf 109: const String& string,
110: Array& result) {
1.59 parser 111: Value& delim_value=params->as_no_junction(0, "delimiter must not be code");
1.23 paf 112:
1.45 paf 113: string.split(result, 0, delim_value.as_string());
1.19 paf 114: }
115:
1.38 paf 116: static void _lsplit(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:
1.61 parser 120: Array pieces(pool);
121: split_list(r, method_name, params, string, pieces);
122:
123: Array& columns=*new(pool) Array(pool);
124: columns+=new(pool) String(pool, "piece");
1.19 paf 125:
1.21 paf 126: Table& table=*new(pool) Table(pool, &string,
1.61 parser 127: &columns, pieces.size());
1.69 parser 128: Array_iter i(pieces);
129: while(i.has_next()) {
1.61 parser 130: Array& row=*new(pool) Array(pool);
1.69 parser 131: row+=i.next();
1.61 parser 132: table+=&row;
133: }
1.19 paf 134: r.write_no_lang(*new(pool) VTable(pool, &table));
135: }
136:
1.38 paf 137: static void _rsplit(Request& r, const String& method_name, MethodParams *params) {
1.19 paf 138: Pool& pool=r.pool();
139: const String& string=*static_cast<VString *>(r.self)->get_string();
140:
1.61 parser 141: Array pieces(pool);
142: split_list(r, method_name, params, string, pieces);
1.19 paf 143:
1.61 parser 144: Array& columns=*new(pool) Array(pool);
145: columns+=new(pool) String(pool, "piece");
1.21 paf 146:
147: Table& table=*new(pool) Table(pool, &string,
1.61 parser 148: &columns, pieces.size());
149: for(int i=pieces.size(); --i>=0; ) {
150: Array& row=*new(pool) Array(pool);
151: row+=pieces.get(i);
152: table+=&row;
153: }
1.17 paf 154:
1.19 paf 155: r.write_no_lang(*new(pool) VTable(pool, &table));
1.17 paf 156: }
157:
1.33 paf 158: static void search_action(Table& table, Array *row, int, int, void *) {
1.28 paf 159: if(row)
160: table+=row;
1.27 paf 161: }
162:
1.40 paf 163: /// used by string: _match / replace_action
1.27 paf 164: struct Replace_action_info {
1.30 paf 165: Request *request; const String *origin;
1.31 paf 166: const String *src; String *dest;
1.27 paf 167: Value *replacement_code;
1.29 paf 168: const String *post_match;
1.27 paf 169: };
1.33 paf 170: static void replace_action(Table& table, Array *row, int start, int finish,
1.31 paf 171: void *info) {
1.27 paf 172: Replace_action_info& ai=*static_cast<Replace_action_info *>(info);
1.31 paf 173: if(row) { // begin&middle
174: // piece from last match['start'] to beginning of this match['finish']
175: if(start!=finish)
1.37 paf 176: *ai.dest << ai.src->mid(start, finish);//ai.dest->APPEND_CONST("-");
1.51 parser 177: // store found parts in one-record VTable
1.32 paf 178: if(table.size()) // middle
179: table.put(0, row);
180: else // begin
1.30 paf 181: table+=row;
182: { // execute 'replacement_code' in 'table' context
183: VTable& vtable=*new(table.pool()) VTable(table.pool(), &table);
184: vtable.set_name(*ai.origin);
185:
186: Junction *junction=ai.replacement_code->get_junction();
1.66 parser 187: Value *saved_match_var_value=junction->root->get_element(*match_var_name);
188: junction->root->put_element(*match_var_name, &vtable);
1.30 paf 189: Value& replaced=ai.request->process(*ai.replacement_code, ai.origin, false);
1.66 parser 190: junction->root->put_element(*match_var_name, saved_match_var_value);
1.30 paf 191:
1.34 paf 192: /*
1.31 paf 193: ai.dest->APPEND_CONST("(");
1.37 paf 194: *ai.dest << *(String *)row->get(1/*match* /);
1.31 paf 195: ai.dest->APPEND_CONST(")");
1.34 paf 196: */
1.37 paf 197: *ai.dest << replaced.as_string();
1.29 paf 198: }
199: ai.post_match=(String *)row->get(2/*post_match*/);
200: } else // end
1.37 paf 201: *ai.dest << *ai.post_match;
1.27 paf 202: }
203:
1.50 parser 204: /// @todo use pcre:study somehow
1.38 paf 205: static void _match(Request& r, const String& method_name, MethodParams *params) {
1.24 paf 206: Pool& pool=r.pool();
1.28 paf 207: const String& src=*static_cast<VString *>(r.self)->get_string();
1.24 paf 208:
1.59 parser 209: Value& regexp=params->as_no_junction(0, "regexp must not be code");
1.38 paf 210:
211: const String *options=
212: params->size()>1?
1.59 parser 213: ¶ms->as_no_junction(1, "options must not be code").as_string():0;
1.24 paf 214:
1.27 paf 215: Value *result;
1.24 paf 216: Temp_lang temp_lang(r, String::UL_PASS_APPENDED);
1.25 paf 217: Table *table;
1.27 paf 218: if(params->size()<3) { // search
1.64 parser 219: bool was_global;
1.60 parser 220: bool matched=src.match(r.pcre_tables,
1.39 paf 221: &method_name,
1.32 paf 222: regexp.as_string(), options,
1.27 paf 223: &table,
1.64 parser 224: search_action, 0,
225: &was_global);
1.60 parser 226: // matched
1.64 parser 227: // not (just matched[3=pre/match/post], no substrings) or Global search
228: if(table->columns()->size()>3 || was_global)
229: result=new(pool) VTable(pool, table); // table of pre/match/post+substrings
230: else
231: result=new(pool) VBool(pool, matched);
1.27 paf 232: } else { // replace
1.59 parser 233: Value& replacement_code=params->as_junction(2, "replacement code must be code");
1.24 paf 234:
1.28 paf 235: String& dest=*new(pool) String(pool);
1.27 paf 236: Replace_action_info replace_action_info={
1.30 paf 237: &r, &method_name,
1.31 paf 238: &src, &dest,
1.28 paf 239: &replacement_code,
1.29 paf 240: &src
1.27 paf 241: };
1.39 paf 242: src.match(r.pcre_tables,
243: &method_name,
1.27 paf 244: r.process(regexp).as_string(), options,
245: &table,
1.33 paf 246: replace_action, &replace_action_info);
1.28 paf 247: result=new(pool) VString(dest);
1.27 paf 248: }
1.26 paf 249: result->set_name(method_name);
1.34 paf 250: r.write_assign_lang(*result);
1.24 paf 251: }
252:
1.49 parser 253: static void change_case(Request& r, const String& method_name, MethodParams *params,
254: String::Change_case_kind kind) {
255: Pool& pool=r.pool();
256: const String& src=*static_cast<VString *>(r.self)->get_string();
257:
258: r.write_assign_lang(*new(pool) VString(src.change_case(pool, r.pcre_tables,
259: kind)));
260: }
261: static void _upper(Request& r, const String& method_name, MethodParams *params) {
262: change_case(r, method_name, params, String::CC_UPPER);
263: }
264: static void _lower(Request& r, const String& method_name, MethodParams *params) {
265: change_case(r, method_name, params, String::CC_LOWER);
266: }
267:
1.65 parser 268: #ifndef DOXYGEN
269: class String_sql_event_handlers : public SQL_Driver_query_event_handlers {
270: public:
271: String_sql_event_handlers(Pool& apool,
272: const String& astatement_string, const char *astatement_cstr) :
273: pool(apool),
274: statement_string(astatement_string),
275: statement_cstr(astatement_cstr),
276: got_column(false), got_cell(false) {
277: result=new(pool) String(pool);
278: }
279:
280: void add_column(void *ptr, size_t size) {
281: if(got_column)
282: PTHROW(0, 0,
283: &statement_string,
284: "result must contain exactly one column");
285: got_column=true;
286: }
287: void before_rows() { /* ignore */ }
288: void add_row() { /* ignore */ }
289: void add_row_cell(void *ptr, size_t size) {
290: if(got_cell)
291: PTHROW(0, 0,
292: &statement_string,
293: "result must not contain more then one row");
294: got_cell=true;
295:
296: result->APPEND_TAINTED((const char *)ptr, size, statement_cstr, 0);
297: }
298:
299: private:
300: Pool& pool;
301: const String& statement_string; const char *statement_cstr;
302: bool got_column;
303: public:
304: bool got_cell;
305: String *result;
306: };
307: #endif
1.70 parser 308: const String* sql_result_string(Request& r, const String& method_name, MethodParams *params,
309: Hash *&options) {
1.53 parser 310: Pool& pool=r.pool();
311:
312: if(!r.connection)
313: PTHROW(0, 0,
314: &method_name,
315: "without connect");
316:
1.59 parser 317: Value& statement=params->as_junction(0, "statement must be code");
1.53 parser 318:
1.70 parser 319: ulong limit=0;
320: ulong offset=0;
321: if(params->size()>1) {
322: Value& options_param=params->as_no_junction(1, "options must be hash, not code");
323: if(options=options_param.get_hash()) {
324: if(Value *vlimit=(Value *)options->get(*sql_limit_name))
325: limit=(ulong)r.process(*vlimit).as_double();
326: if(Value *voffset=(Value *)options->get(*sql_offset_name))
327: offset=(ulong)r.process(*voffset).as_double();
328: } else
329: PTHROW(0, 0,
330: &method_name,
331: "options must be hash");
332: } else
333: options=0;
334:
1.53 parser 335: Temp_lang temp_lang(r, String::UL_SQL);
336: const String& statement_string=r.process(statement).as_string();
337: const char *statement_cstr=
338: statement_string.cstr(String::UL_UNSPECIFIED, r.connection);
1.65 parser 339: String_sql_event_handlers handlers(pool, statement_string, statement_cstr);
1.53 parser 340: bool need_rethrow=false; Exception rethrow_me;
341: PTRY {
342: r.connection->query(
1.70 parser 343: statement_cstr, offset, limit,
1.65 parser 344: handlers);
1.53 parser 345: }
346: PCATCH(e) { // query problem
347: rethrow_me=e; need_rethrow=true;
348: }
349: PEND_CATCH
350: if(need_rethrow)
351: PTHROW(rethrow_me.type(), rethrow_me.code(),
352: &statement_string, // setting more specific source [were url]
353: rethrow_me.comment());
354:
1.65 parser 355: if(!handlers.got_cell)
356: return 0; // no lines, caller should return second param[default value]
1.62 parser 357:
1.65 parser 358: return handlers.result;
1.53 parser 359: }
360:
361: static void _sql(Request& r, const String& method_name, MethodParams *params) {
362: Pool& pool=r.pool();
363:
1.70 parser 364: Hash *options;
365: const String *string=sql_result_string(r, method_name, params, options);
1.62 parser 366: if(!string) {
1.70 parser 367: if(options) {
368: if(Value *vdefault=(Value *)options->get(*sql_default_name)) {
369: if(!vdefault->get_junction())
370: PTHROW(0, 0,
371: &method_name,
372: "default option must be code");
373: string=r.process(*vdefault).get_string();
374: if(!string)
375: string=empty_string;
376: } else
377: PTHROW(0, 0,
378: &method_name,
379: "produced no result, but no default option specified");
1.68 parser 380: } else
381: PTHROW(0, 0,
382: &method_name,
1.70 parser 383: "produced no result, but no options (no default) specified");
1.62 parser 384: }
385: VString& result=*new(pool) VString(*string);
1.53 parser 386: result.set_name(method_name);
387: r.write_assign_lang(result);
388: }
389:
1.71 ! parser 390: static void _replace(Request& r, const String& method_name, MethodParams *params) {
! 391: Pool& pool=r.pool();
! 392: const String& src=*static_cast<VString *>(r.self)->get_string();
! 393:
! 394: Table *table=params->as_no_junction(0, "parameter must not be code").get_table();
! 395: if(!table)
! 396: PTHROW(0, 0,
! 397: &method_name,
! 398: "parameter must be table");
! 399:
! 400: Dictionary dict(*table);
! 401: r.write_assign_lang(*new(pool) VString(src.replace(pool, dict)));
! 402: }
1.41 paf 403: // constructor
404:
405: MString::MString(Pool& apool) : Methoded(apool) {
406: set_name(*NEW String(pool(), STRING_CLASS_NAME));
407:
1.9 paf 408:
1.1 paf 409: // ^string.length[]
1.41 paf 410: add_native_method("length", Method::CT_DYNAMIC, _length, 0, 0);
1.6 paf 411:
1.1 paf 412: // ^string.int[]
1.41 paf 413: add_native_method("int", Method::CT_DYNAMIC, _int, 0, 0);
1.6 paf 414:
1.1 paf 415: // ^string.double[]
1.41 paf 416: add_native_method("double", Method::CT_DYNAMIC, _double, 0, 0);
1.9 paf 417:
1.24 paf 418: // ^string.format{format}
1.41 paf 419: add_native_method("format", Method::CT_DYNAMIC, _string_format, 1, 1);
1.14 paf 420:
1.15 paf 421: // ^string.left(n)
1.41 paf 422: add_native_method("left", Method::CT_DYNAMIC, _left, 1, 1);
1.15 paf 423: // ^string.right(n)
1.41 paf 424: add_native_method("right", Method::CT_DYNAMIC, _right, 1, 1);
1.15 paf 425: // ^string.mid(p;n)
1.41 paf 426: add_native_method("mid", Method::CT_DYNAMIC, _mid, 2, 2);
1.16 paf 427:
428: // ^string.pos[substr]
1.41 paf 429: add_native_method("pos", Method::CT_DYNAMIC, _pos, 1, 1);
1.17 paf 430:
431: // ^string.lsplit[delim]
1.41 paf 432: add_native_method("lsplit", Method::CT_DYNAMIC, _lsplit, 1, 1);
1.19 paf 433: // ^string.rsplit[delim]
1.41 paf 434: add_native_method("rsplit", Method::CT_DYNAMIC, _rsplit, 1, 1);
1.24 paf 435:
1.32 paf 436: // ^string.match[regexp][options]
437: // ^string.match[regexp][options]{replacement-code}
1.41 paf 438: add_native_method("match", Method::CT_DYNAMIC, _match, 1, 3);
1.49 parser 439:
440: // ^string.toupper[]
441: add_native_method("upper", Method::CT_DYNAMIC, _upper, 0, 0);
442: // ^string.tolower[]
443: add_native_method("lower", Method::CT_DYNAMIC, _lower, 0, 0);
1.53 parser 444:
1.70 parser 445: // ^sql[query]
446: // ^sql[query][$.limit(1) $.offset(2) $.default[n/a]]
1.67 parser 447: add_native_method("sql", Method::CT_STATIC, _sql, 1, 2);
1.71 ! parser 448:
! 449: // ^string.replace[table]
! 450: add_native_method("replace", Method::CT_DYNAMIC, _replace, 1, 1);
1.2 paf 451: }
1.1 paf 452:
1.41 paf 453: // global variable
454:
455: Methoded *string_class;
456:
457: // creator
458:
459: Methoded *MString_create(Pool& pool) {
460: return string_class=new(pool) MString(pool);
461: }
E-mail: