Annotation of parser3/src/classes/string.C, revision 1.132
1.24 paf 1: /** @file
2: Parser: @b string parser class.
3:
1.132 ! paf 4: Copyright (c) 2001-2004 ArtLebedev Group (http://www.artlebedev.com)
1.97 paf 5: Author: Alexandr Petrosian <paf@design.ru> (http://paf.design.ru)
1.113 paf 6: */
1.24 paf 7:
1.132 ! paf 8: static const char * const IDENT_STRING_C="$Date: 2003/11/20 17:46:01 $";
1.1 paf 9:
1.43 paf 10: #include "classes.h"
1.126 paf 11: #include "pa_vmethod_frame.h"
12:
1.1 paf 13: #include "pa_request.h"
14: #include "pa_vdouble.h"
15: #include "pa_vint.h"
1.17 paf 16: #include "pa_vtable.h"
1.25 paf 17: #include "pa_vbool.h"
1.27 paf 18: #include "pa_string.h"
1.53 parser 19: #include "pa_sql_connection.h"
1.71 parser 20: #include "pa_dictionary.h"
1.119 paf 21: #include "pa_vmethod_frame.h"
1.1 paf 22:
1.41 paf 23: // class
24:
1.126 paf 25: class MString: public Methoded {
1.41 paf 26: public:
1.126 paf 27: MString();
1.44 paf 28: public: // Methoded
1.53 parser 29: bool used_directly() { return true; }
1.41 paf 30: };
1.1 paf 31:
1.126 paf 32: // global variable
33:
34: DECLARE_CLASS_VAR(string, new MString, 0);
35:
36: // defines for statics
37:
38: #define MATCH_VAR_NAME "match"
39:
40: // statics
41:
42: static const String match_var_name(MATCH_VAR_NAME);
43:
1.1 paf 44: // methods
45:
1.126 paf 46: static void _length(Request& r, MethodParams&) {
47: double result=GET_SELF(r, VString).string().length();
48: r.write_no_lang(*new VDouble(result));
1.1 paf 49: }
50:
1.126 paf 51: static void _int(Request& r, MethodParams& params) {
52: const String& self_string=GET_SELF(r, VString).string();
1.72 parser 53: int converted;
1.126 paf 54: Value* default_code=params.count()>0?¶ms.as_junction(0, "default must be int")
55: :0; // (default)
1.84 parser 56: try {
1.126 paf 57: if(self_string.is_empty())
1.121 paf 58: throw Exception("parser.runtime",
1.126 paf 59: 0,
1.121 paf 60: "parameter is empty string, error converting");
1.126 paf 61: converted=self_string.as_int();
1.84 parser 62: } catch(...) { // convert problem
1.126 paf 63: if(default_code)
64: converted=r.process_to_value(*default_code).as_int();
1.84 parser 65: else
1.126 paf 66: rethrow; // we have a problem when no default
1.72 parser 67: }
1.126 paf 68: r.write_no_lang(*new VInt(converted));
1.1 paf 69: }
70:
1.126 paf 71: static void _double(Request& r, MethodParams& params) {
72: const String& self_string=GET_SELF(r, VString).string();
1.72 parser 73: double converted;
1.126 paf 74: Value* default_code=params.count()>0?¶ms.as_junction(0, "default must be double")
75: :0; // (default)
1.84 parser 76: try {
1.126 paf 77: if(self_string.is_empty())
1.121 paf 78: throw Exception("parser.runtime",
1.126 paf 79: 0,
1.121 paf 80: "parameter is empty string, error converting");
1.126 paf 81: converted=self_string.as_double();
1.84 parser 82: } catch(...) { // convert problem
1.126 paf 83: if(default_code)
84: converted=r.process_to_value(*default_code).as_double();
1.84 parser 85: else
1.126 paf 86: rethrow; // we have a problem when no default
1.72 parser 87: }
88:
1.126 paf 89: r.write_no_lang(*new VDouble(converted));
1.1 paf 90: }
91:
1.126 paf 92: /*not static*/void _string_format(Request& r, MethodParams& params) {
1.9 paf 93:
1.126 paf 94: Value& fmt_maybe_code=params[0];
1.95 paf 95: // for some time due to stupid {} in original design
1.99 paf 96: const String& fmt=r.process_to_string(fmt_maybe_code);
1.9 paf 97:
1.126 paf 98: const char* buf=format(r.get_self().as_double(), fmt.cstrm());
1.63 parser 99:
1.126 paf 100: r.write_no_lang(String(buf));
1.9 paf 101: }
1.11 paf 102:
1.126 paf 103: static void _left(Request& r, MethodParams& params) {
104: size_t n=(size_t)params.as_int(0, "n must be int", r);
1.15 paf 105:
1.126 paf 106: const String& string=GET_SELF(r, VString).string();
1.102 paf 107: r.write_assign_lang(string.mid(0, n));
1.15 paf 108: }
109:
1.126 paf 110: static void _right(Request& r, MethodParams& params) {
111: size_t n=(size_t)params.as_int(0, "n must be int", r);
1.15 paf 112:
1.126 paf 113: const String& string=GET_SELF(r, VString).string();
1.127 paf 114: size_t length=string.length();
115: r.write_assign_lang(n<length?string.mid(length-n, string.length()):string);
1.15 paf 116: }
117:
1.126 paf 118: static void _mid(Request& r, MethodParams& params) {
119: const String& string=GET_SELF(r, VString).string();
1.83 parser 120:
1.126 paf 121: size_t p=(size_t)max(0, params.as_int(0, "p must be int", r));
122: size_t n=params.count()>1?
123: (size_t)max(0, params.as_int(1, "n must be int", r)):string.length();
1.15 paf 124:
1.102 paf 125: r.write_assign_lang(string.mid(p, p+n));
1.15 paf 126: }
127:
1.126 paf 128: static void _pos(Request& r, MethodParams& params) {
129: Value& substr=params.as_no_junction(0, "substr must not be code");
1.16 paf 130:
1.126 paf 131: const String& string=GET_SELF(r, VString).string();
132: r.write_assign_lang(*new VInt((int)string.pos(substr.as_string())));
1.16 paf 133: }
134:
1.128 paf 135: static void split_list(MethodParams& params, int paramIndex,
1.126 paf 136: const String& string,
137: ArrayString& result) {
138: Value& delim_value=params.as_no_junction(paramIndex, "delimiter must not be code");
1.23 paf 139:
1.126 paf 140: size_t pos_after=0;
141: string.split(result, pos_after, delim_value.as_string());
1.19 paf 142: }
143:
1.118 paf 144: #define SPLIT_LEFT 0x0001
145: #define SPLIT_RIGHT 0x0010
146: #define SPLIT_HORIZONTAL 0x0100
147: #define SPLIT_VERTICAL 0x1000
148:
1.126 paf 149: static int split_options(const String* options) {
1.118 paf 150: struct Split_option {
1.126 paf 151: const char* keyL;
152: const char* keyU;
1.118 paf 153: int setBit;
154: int checkBit;
155: } split_option[]={
156: {"l", "L", SPLIT_LEFT, SPLIT_RIGHT}, // 0xVHRL
157: {"r", "R", SPLIT_RIGHT, SPLIT_LEFT},
158: {"h", "H", SPLIT_HORIZONTAL, SPLIT_VERTICAL},
159: {"v", "V", SPLIT_VERTICAL, SPLIT_HORIZONTAL},
1.130 paf 160: {0, 0, 0, 0}
1.118 paf 161: };
162:
163: int result=0;
1.126 paf 164: if(options) {
1.118 paf 165: for(Split_option *o=split_option; o->keyL; o++)
1.126 paf 166: if(options->pos(o->keyL)!=STRING_NOT_FOUND
167: || (o->keyU && options->pos(o->keyU)!=STRING_NOT_FOUND)) {
1.118 paf 168: if(result & o->checkBit)
169: throw Exception("parser.runtime",
170: options,
171: "conflicting split options");
172: result |= o->setBit;
173: }
174: }
175:
176: return result;
177: }
178:
1.128 paf 179: static Table& split_vertical(ArrayString& pieces, bool right) {
1.126 paf 180: Table::columns_type columns(new ArrayString);
181: *columns+=new String("piece");
1.19 paf 182:
1.126 paf 183: Table& table=*new Table(columns, pieces.count());
1.118 paf 184: if(right) { // right
1.126 paf 185: for(int i=pieces.count(); --i>=0; ) {
186: Table::element_type row(new ArrayString);
187: *row+=pieces[i];
188: table+=row;
1.118 paf 189: }
190: } else { // left
1.126 paf 191: Array_iterator<const String*> i(pieces);
1.118 paf 192: while(i.has_next()) {
1.126 paf 193: Table::element_type row(new ArrayString);
194: *row+=i.next();
195: table+=row;
1.118 paf 196: }
1.61 parser 197: }
1.118 paf 198:
1.126 paf 199: return table;
1.19 paf 200: }
201:
1.128 paf 202: static Table& split_horizontal(ArrayString& pieces, bool right) {
1.126 paf 203: Table& table=*new Table(Table::columns_type(0) /* nameless */);
204: Table::element_type row(new ArrayString(pieces.count()));
1.118 paf 205: if(right) { // right
1.131 paf 206: for(int i=pieces.count(); --i>=0; )
1.126 paf 207: *row+=pieces[i];
1.118 paf 208: } else { // left
1.126 paf 209: for(Array_iterator<const String*> i(pieces); i.has_next(); )
210: *row+=i.next();
1.118 paf 211: }
1.126 paf 212: table+=row;
1.118 paf 213:
1.126 paf 214: return table;
1.118 paf 215: }
216:
1.126 paf 217: static void split_with_options(Request& r, MethodParams& params,
1.118 paf 218: int bits) {
1.126 paf 219: const String& string=GET_SELF(r, VString).string();
1.19 paf 220:
1.126 paf 221: ArrayString pieces;
1.128 paf 222: split_list(params, 0, string, pieces);
1.19 paf 223:
1.118 paf 224: if(!bits) {
1.126 paf 225: const String* options=0;
226: if(params.count()>1)
227: options=¶ms.as_string(1, "options must not be code");
228:
1.118 paf 229: bits=split_options(options);
230: }
1.21 paf 231:
1.118 paf 232: bool right=(bits & SPLIT_RIGHT) != 0;
233: bool horizontal=(bits & SPLIT_HORIZONTAL) !=0;
1.128 paf 234: Table& table=horizontal?split_horizontal(pieces, right)
235: :split_vertical(pieces, right);
1.17 paf 236:
1.126 paf 237: r.write_no_lang(*new VTable(&table));
1.118 paf 238: }
1.126 paf 239: static void _split(Request& r, MethodParams& params) {
240: split_with_options(r, params, 0 /* maybe-determine from param #2 */);
1.118 paf 241: }
1.126 paf 242: static void _lsplit(Request& r, MethodParams& params) {
243: split_with_options(r, params, SPLIT_LEFT);
1.118 paf 244: }
1.126 paf 245: static void _rsplit(Request& r, MethodParams& params) {
246: split_with_options(r, params, SPLIT_RIGHT);
1.17 paf 247: }
248:
1.126 paf 249: static void search_action(Table& table, Table::element_type row, int, int, int, int, void *) {
1.28 paf 250: if(row)
251: table+=row;
1.27 paf 252: }
253:
1.74 parser 254: #ifndef DOXYGEN
1.27 paf 255: struct Replace_action_info {
1.126 paf 256: Request* request;
257: const String* src; String* dest;
258: VTable* vtable;
259: Value* replacement_code;
1.27 paf 260: };
1.74 parser 261: #endif
1.105 paf 262: /// @todo they can do $global[$result] there, getting pointer to later-invalid local var, kill this
1.126 paf 263: static void replace_action(Table& table, ArrayString* row,
264: int prestart, int prefinish,
265: int poststart, int postfinish,
266: void *info) {
1.27 paf 267: Replace_action_info& ai=*static_cast<Replace_action_info *>(info);
1.31 paf 268: if(row) { // begin&middle
1.104 paf 269: // piece from last match['prestart'] to beginning of this match['prefinish']
270: if(prestart!=prefinish)
271: *ai.dest << ai.src->mid(prestart, prefinish);//ai.dest->APPEND_CONST("-");
1.51 parser 272: // store found parts in one-record VTable
1.126 paf 273: if(table.count()) // middle
1.32 paf 274: table.put(0, row);
275: else // begin
1.30 paf 276: table+=row;
277: { // execute 'replacement_code' in 'table' context
1.105 paf 278: ai.vtable->set_table(table);
1.30 paf 279:
1.105 paf 280: *ai.dest << ai.request->process_to_string(*ai.replacement_code);
1.29 paf 281: }
282: } else // end
1.104 paf 283: *ai.dest << ai.src->mid(poststart, postfinish);
1.27 paf 284: }
285:
1.50 parser 286: /// @todo use pcre:study somehow
1.126 paf 287: static void _match(Request& r, MethodParams& params) {
288: Value& regexp=params.as_no_junction(0, "regexp must not be code");
289:
290: const String* options=
291: params.count()>1?
292: ¶ms.as_no_junction(1, "options must not be code").as_string():0;
293:
294: Temp_lang temp_lang(r, String::L_PASS_APPENDED);
295: const String& src=GET_SELF(r, VString).string();
296: bool just_matched;
297: if(params.count()<3) { // search
298: Table* table=src.match(r.charsets.source(),
1.32 paf 299: regexp.as_string(), options,
1.64 parser 300: search_action, 0,
1.126 paf 301: just_matched);
302: Value* result;
303: if(table)
304: result=new VTable(table); // table of pre/match/post+substrings
1.64 parser 305: else
1.126 paf 306: result=new VBool(just_matched);
1.102 paf 307: r.write_assign_lang(*result);
1.27 paf 308: } else { // replace
1.126 paf 309: Value& replacement_code=params.as_junction(2, "replacement param must be code");
1.106 paf 310:
1.126 paf 311: String result;
312: VTable* vtable=new VTable;
1.130 paf 313: Replace_action_info info={
314: &r,
315: &src,
316: &result,
317: vtable,
318: &replacement_code
319: };
1.105 paf 320: Temp_value_element temp_match_var(
1.119 paf 321: *replacement_code.get_junction()->method_frame,
1.126 paf 322: match_var_name, vtable);
323: src.match(r.charsets.source(),
1.99 paf 324: r.process_to_string(regexp), options,
1.126 paf 325: replace_action, &info,
326: just_matched);
1.102 paf 327: r.write_assign_lang(result);
1.27 paf 328: }
1.24 paf 329: }
330:
1.128 paf 331: static void change_case(Request& r, MethodParams&,
1.49 parser 332: String::Change_case_kind kind) {
1.126 paf 333: const String& src=GET_SELF(r, VString).string();
1.49 parser 334:
1.126 paf 335: r.write_assign_lang(src.change_case(r.charsets.source(), kind));
1.49 parser 336: }
1.126 paf 337: static void _upper(Request& r, MethodParams& params) {
338: change_case(r, params, String::CC_UPPER);
1.49 parser 339: }
1.126 paf 340: static void _lower(Request& r, MethodParams& params) {
341: change_case(r, params, String::CC_LOWER);
1.49 parser 342: }
343:
1.65 parser 344: #ifndef DOXYGEN
1.126 paf 345: class String_sql_event_handlers: public SQL_Driver_query_event_handlers {
346: const String& statement_string; const char* statement_cstr;
347: bool got_column;
1.65 parser 348: public:
1.126 paf 349: bool got_cell;
350: String& result;
351: public:
352: String_sql_event_handlers(
353: const String& astatement_string, const char* astatement_cstr):
354: statement_string(astatement_string), statement_cstr(astatement_cstr),
355: got_column(false),
356: got_cell(false),
357: result(*new String) {}
1.65 parser 358:
1.128 paf 359: bool add_column(SQL_Error& error, const char* /*str*/, size_t /*length*/) {
1.124 paf 360: if(got_column) {
361: error=SQL_Error("parser.runtime",
1.126 paf 362: //statement_string,
1.65 parser 363: "result must contain exactly one column");
1.124 paf 364: return true;
365: }
1.65 parser 366: got_column=true;
1.124 paf 367: return false;
1.65 parser 368: }
1.124 paf 369: bool before_rows(SQL_Error& /*error*/ ) { /* ignore */ return false; }
370: bool add_row(SQL_Error& /*error*/) { /* ignore */ return false; }
1.126 paf 371: bool add_row_cell(SQL_Error& error, const char* str, size_t length) {
1.124 paf 372: if(got_cell) {
373: error=SQL_Error("parser.runtime",
1.126 paf 374: //statement_string,
1.65 parser 375: "result must not contain more then one row");
1.124 paf 376: return true;
377: }
1.65 parser 378:
1.124 paf 379: try {
380: got_cell=true;
1.126 paf 381: result.append_know_length(str, length, String::L_TAINTED);
1.124 paf 382: return false;
383: } catch(...) {
384: error=SQL_Error("exception occured in String_sql_event_handlers::add_row_cell");
385: return true;
386: }
1.65 parser 387: }
388: };
389: #endif
1.126 paf 390: extern String sql_limit_name;
391: extern String sql_offset_name;
392: extern String sql_default_name;
393: extern String sql_distinct_name;
394: const String* sql_result_string(Request& r, MethodParams& params,
395: HashStringValue*& options, Value*& default_code) {
396: Value& statement=params.as_junction(0, "statement must be code");
1.53 parser 397:
1.70 parser 398: ulong limit=0;
399: ulong offset=0;
1.81 parser 400: default_code=0;
1.126 paf 401: if(params.count()>1) {
402: Value& voptions=params.as_no_junction(1, "options must be hash, not code");
1.115 paf 403: if(!voptions.is_string())
1.130 paf 404: if((options=voptions.get_hash())) {
1.126 paf 405: if(Value* vlimit=options->get(sql_limit_name))
1.99 paf 406: limit=(ulong)r.process_to_value(*vlimit).as_double();
1.126 paf 407: if(Value* voffset=options->get(sql_offset_name))
1.99 paf 408: offset=(ulong)r.process_to_value(*voffset).as_double();
1.130 paf 409: if((default_code=options->get(sql_default_name))) {
410: if(!default_code->get_junction())
1.98 paf 411: throw Exception("parser.runtime",
1.126 paf 412: 0,
1.81 parser 413: "default option must be code");
414: }
1.73 parser 415: } else
1.98 paf 416: throw Exception("parser.runtime",
1.126 paf 417: 0,
1.73 parser 418: "options must be hash");
1.70 parser 419: } else
420: options=0;
421:
1.126 paf 422: Temp_lang temp_lang(r, String::L_SQL);
1.99 paf 423: const String& statement_string=r.process_to_string(statement);
1.126 paf 424: const char* statement_cstr=
425: statement_string.cstr(String::L_UNSPECIFIED, r.connection());
426: String_sql_event_handlers handlers(statement_string, statement_cstr);
427: r.connection()->query(
1.117 paf 428: statement_cstr, offset, limit,
429: handlers,
430: statement_string);
1.53 parser 431:
1.65 parser 432: if(!handlers.got_cell)
433: return 0; // no lines, caller should return second param[default value]
1.62 parser 434:
1.126 paf 435: return &handlers.result;
1.53 parser 436: }
437:
1.126 paf 438: static void _sql(Request& r, MethodParams& params) {
1.53 parser 439:
1.126 paf 440: HashStringValue* options;
441: Value* default_code;
442: const String* string=sql_result_string(r, params, options, default_code);
1.62 parser 443: if(!string) {
1.81 parser 444: if(default_code) {
1.99 paf 445: string=&r.process_to_string(*default_code);
1.68 parser 446: } else
1.98 paf 447: throw Exception("parser.runtime",
1.126 paf 448: 0,
1.81 parser 449: "produced no result, but no default option specified");
1.62 parser 450: }
1.100 paf 451:
1.102 paf 452: r.write_assign_lang(*string);
1.53 parser 453: }
454:
1.126 paf 455: static void _replace(Request& r, MethodParams& params) {
456: const String& src=GET_SELF(r, VString).string();
1.71 parser 457:
1.126 paf 458: Table* table=params.as_no_junction(0, "parameter must not be code").get_table();
1.71 parser 459: if(!table)
1.98 paf 460: throw Exception("parser.runtime",
1.126 paf 461: 0,
1.71 parser 462: "parameter must be table");
463:
464: Dictionary dict(*table);
1.126 paf 465: r.write_assign_lang(src.replace(dict));
1.71 parser 466: }
1.79 parser 467:
1.126 paf 468: static void _save(Request& r, MethodParams& params) {
469: const String& file_name=params.as_string(params.count()-1,
1.87 paf 470: "file name must be string");
1.79 parser 471:
1.126 paf 472: const String& src=GET_SELF(r, VString).string();
1.79 parser 473:
1.87 paf 474: bool do_append=false;
1.126 paf 475: if(params.count()>1) {
476: const String& mode=params.as_string(0, "mode must be string");
1.87 paf 477: if(mode=="append")
478: do_append=true;
479: else
1.98 paf 480: throw Exception("parser.runtime",
1.87 paf 481: &mode,
482: "unknown mode, must be 'append'");
483: }
484:
1.79 parser 485: // write
1.126 paf 486: const char* buf=src.cstr(String::L_UNSPECIFIED, r.connection(false/*no error if none*/));
1.94 paf 487: file_write(r.absolute(file_name),
1.89 paf 488: buf, strlen(buf), true, do_append);
1.79 parser 489: }
490:
1.126 paf 491: static void _normalize(Request& r, MethodParams&) {
492: const String& src=GET_SELF(r, VString).string();
493:
494: r.write_assign_lang(src);
1.109 paf 495: }
496:
1.41 paf 497: // constructor
498:
1.126 paf 499: MString::MString(): Methoded("string") {
1.1 paf 500: // ^string.length[]
1.41 paf 501: add_native_method("length", Method::CT_DYNAMIC, _length, 0, 0);
1.6 paf 502:
1.1 paf 503: // ^string.int[]
1.72 parser 504: // ^string.int(default)
505: add_native_method("int", Method::CT_DYNAMIC, _int, 0, 1);
1.1 paf 506: // ^string.double[]
1.72 parser 507: // ^string.double(default)
508: add_native_method("double", Method::CT_DYNAMIC, _double, 0, 1);
1.9 paf 509:
1.24 paf 510: // ^string.format{format}
1.41 paf 511: add_native_method("format", Method::CT_DYNAMIC, _string_format, 1, 1);
1.14 paf 512:
1.15 paf 513: // ^string.left(n)
1.41 paf 514: add_native_method("left", Method::CT_DYNAMIC, _left, 1, 1);
1.15 paf 515: // ^string.right(n)
1.41 paf 516: add_native_method("right", Method::CT_DYNAMIC, _right, 1, 1);
1.15 paf 517: // ^string.mid(p;n)
1.82 parser 518: add_native_method("mid", Method::CT_DYNAMIC, _mid, 1, 2);
1.16 paf 519:
520: // ^string.pos[substr]
1.41 paf 521: add_native_method("pos", Method::CT_DYNAMIC, _pos, 1, 1);
1.17 paf 522:
1.118 paf 523: // ^string.split[delim]
524: // ^string.split[delim][options]
525: add_native_method("split", Method::CT_DYNAMIC, _split, 1, 2);
526: // old names for backward compatibility
527: // ^string.lsplit[delim]
528: add_native_method("lsplit", Method::CT_DYNAMIC, _lsplit, 1, 1);
529: // ^string.rsplit[delim]
530: add_native_method("rsplit", Method::CT_DYNAMIC, _rsplit, 1, 1);
531:
1.32 paf 532: // ^string.match[regexp][options]
533: // ^string.match[regexp][options]{replacement-code}
1.41 paf 534: add_native_method("match", Method::CT_DYNAMIC, _match, 1, 3);
1.49 parser 535:
536: // ^string.toupper[]
537: add_native_method("upper", Method::CT_DYNAMIC, _upper, 0, 0);
538: // ^string.tolower[]
539: add_native_method("lower", Method::CT_DYNAMIC, _lower, 0, 0);
1.53 parser 540:
1.70 parser 541: // ^sql[query]
542: // ^sql[query][$.limit(1) $.offset(2) $.default[n/a]]
1.67 parser 543: add_native_method("sql", Method::CT_STATIC, _sql, 1, 2);
1.71 parser 544:
545: // ^string.replace[table]
546: add_native_method("replace", Method::CT_DYNAMIC, _replace, 1, 1);
1.79 parser 547:
548: // ^string.save[file]
1.87 paf 549: add_native_method("save", Method::CT_DYNAMIC, _save, 1, 2);
1.109 paf 550:
1.112 paf 551: // ^string.normalize[]
552: add_native_method("normalize", Method::CT_DYNAMIC, _normalize, 0, 0);
1.2 paf 553: }
E-mail: