Annotation of parser3/src/classes/string.C, revision 1.175
1.24 paf 1: /** @file
2: Parser: @b string parser class.
3:
1.170 misha 4: Copyright (c) 2001-2009 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.175 ! misha 8: static const char * const IDENT_STRING_C="$Date: 2009-04-22 04:38:36 $";
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.174 misha 22: #include "pa_vregex.h"
1.1 paf 23:
1.41 paf 24: // class
25:
1.126 paf 26: class MString: public Methoded {
1.41 paf 27: public:
1.126 paf 28: MString();
1.44 paf 29: public: // Methoded
1.53 parser 30: bool used_directly() { return true; }
1.41 paf 31: };
1.1 paf 32:
1.126 paf 33: // global variable
34:
35: DECLARE_CLASS_VAR(string, new MString, 0);
36:
37: // defines for statics
38:
39: #define MATCH_VAR_NAME "match"
1.162 misha 40: #define TRIM_START_OPTION "left"
41: #define TRIM_END_OPTION "right"
1.133 paf 42: #define TRIM_BOTH_OPTION "both"
1.126 paf 43:
44: // statics
45:
46: static const String match_var_name(MATCH_VAR_NAME);
47:
1.1 paf 48: // methods
49:
1.126 paf 50: static void _length(Request& r, MethodParams&) {
1.163 misha 51: double result=GET_SELF(r, VString).string().length(r.charsets.source());
1.126 paf 52: r.write_no_lang(*new VDouble(result));
1.1 paf 53: }
54:
1.126 paf 55: static void _int(Request& r, MethodParams& params) {
56: const String& self_string=GET_SELF(r, VString).string();
1.72 parser 57: int converted;
1.84 parser 58: try {
1.126 paf 59: if(self_string.is_empty())
1.153 misha 60: throw Exception(PARSER_RUNTIME,
1.126 paf 61: 0,
1.121 paf 62: "parameter is empty string, error converting");
1.126 paf 63: converted=self_string.as_int();
1.84 parser 64: } catch(...) { // convert problem
1.144 paf 65: if(params.count()>0)
66: converted=params.as_int(0, "default must be int", r); // (default)
1.84 parser 67: else
1.126 paf 68: rethrow; // we have a problem when no default
1.72 parser 69: }
1.126 paf 70: r.write_no_lang(*new VInt(converted));
1.1 paf 71: }
72:
1.126 paf 73: static void _double(Request& r, MethodParams& params) {
74: const String& self_string=GET_SELF(r, VString).string();
1.72 parser 75: double converted;
1.84 parser 76: try {
1.126 paf 77: if(self_string.is_empty())
1.153 misha 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.144 paf 83: if(params.count()>0)
84: converted=params.as_double(0, "default must be double", r); // (default)
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.151 misha 92: static void _bool(Request& r, MethodParams& params) {
93: const String& self_string=GET_SELF(r, VString).string();
94: bool converted;
95: try {
96: if(self_string.is_empty())
1.153 misha 97: throw Exception(PARSER_RUNTIME,
1.151 misha 98: 0,
99: "parameter is empty string, error converting");
1.158 misha 100:
101: try {
102: converted=self_string.as_bool();
103: } catch(...) {
104: const String& lower_string=self_string.change_case(r.charsets.source(), String::CC_LOWER);
105: if(lower_string == "true"){
106: converted=true;
107: } else if (lower_string == "false"){
108: converted=false;
109: } else {
110: rethrow;
111: }
112: }
1.151 misha 113: } catch(...) { // convert problem
114: if(params.count()>0)
115: converted=params.as_bool(0, "default must be bool", r); // (default)
116: else
117: rethrow; // we have a problem when no default
118: }
119:
1.172 misha 120: r.write_no_lang(VBool::get(converted));
1.151 misha 121: }
122:
1.126 paf 123: /*not static*/void _string_format(Request& r, MethodParams& params) {
1.9 paf 124:
1.126 paf 125: Value& fmt_maybe_code=params[0];
1.95 paf 126: // for some time due to stupid {} in original design
1.99 paf 127: const String& fmt=r.process_to_string(fmt_maybe_code);
1.9 paf 128:
1.159 misha 129: const char* buf=format(r.get_self().as_double(), fmt.trim().cstrm());
1.63 parser 130:
1.126 paf 131: r.write_no_lang(String(buf));
1.9 paf 132: }
1.11 paf 133:
1.126 paf 134: static void _left(Request& r, MethodParams& params) {
1.138 paf 135: ssize_t sn=params.as_int(0, "n must be int", r);
136: if(sn<0)
1.153 misha 137: throw Exception(PARSER_RUNTIME,
1.138 paf 138: 0,
139: "n(%d) must be >=0", sn);
140: size_t n=(size_t)sn;
141:
1.126 paf 142: const String& string=GET_SELF(r, VString).string();
1.163 misha 143: r.write_assign_lang(string.mid(r.charsets.source(), 0, n));
1.15 paf 144: }
145:
1.126 paf 146: static void _right(Request& r, MethodParams& params) {
1.138 paf 147: ssize_t sn=(size_t)params.as_int(0, "n must be int", r);
148: if(sn<0)
1.153 misha 149: throw Exception(PARSER_RUNTIME,
1.138 paf 150: 0,
151: "n(%d) must be >=0", sn);
152: size_t n=(size_t)sn;
1.159 misha 153:
1.126 paf 154: const String& string=GET_SELF(r, VString).string();
1.163 misha 155: size_t length=string.length(r.charsets.source());
1.164 misha 156: r.write_assign_lang(n<length?string.mid(r.charsets.source(), length-n, length, length):string);
1.15 paf 157: }
158:
1.126 paf 159: static void _mid(Request& r, MethodParams& params) {
160: const String& string=GET_SELF(r, VString).string();
1.83 parser 161:
1.138 paf 162: ssize_t sbegin=params.as_int(0, "p must be int", r);
163: if(sbegin<0)
1.153 misha 164: throw Exception(PARSER_RUNTIME,
1.138 paf 165: 0,
166: "p(%d) must be >=0", sbegin);
167: size_t begin=(size_t)sbegin;
168:
169: size_t end;
1.164 misha 170: size_t length=0;
1.138 paf 171: if(params.count()>1) {
172: ssize_t sn=params.as_int(1, "n must be int", r);
173: if(sn<0)
1.153 misha 174: throw Exception(PARSER_RUNTIME,
1.138 paf 175: 0,
176: "n(%d) must be >=0", sn);
177: end=begin+(size_t)sn;
1.164 misha 178: } else {
179: length=string.length(r.charsets.source());
180: end=length;
181: }
1.163 misha 182:
1.164 misha 183: r.write_assign_lang(string.mid(r.charsets.source(), begin, end, length));
1.15 paf 184: }
185:
1.126 paf 186: static void _pos(Request& r, MethodParams& params) {
187: Value& substr=params.as_no_junction(0, "substr must not be code");
1.16 paf 188:
1.126 paf 189: const String& string=GET_SELF(r, VString).string();
1.165 misha 190: ssize_t offset=0;
191: if(params.count()>1){
192: offset=params.as_int(1, "n must be int", r);
193: if(offset<0)
194: throw Exception(PARSER_RUNTIME,
195: 0,
196: "n(%d) must be >=0", offset);
197: }
198:
1.166 misha 199: r.write_no_lang(*new VInt((int)string.pos(r.charsets.source(), substr.as_string(), (size_t)offset)));
1.16 paf 200: }
201:
1.128 paf 202: static void split_list(MethodParams& params, int paramIndex,
1.126 paf 203: const String& string,
204: ArrayString& result) {
205: Value& delim_value=params.as_no_junction(paramIndex, "delimiter must not be code");
1.23 paf 206:
1.126 paf 207: size_t pos_after=0;
208: string.split(result, pos_after, delim_value.as_string());
1.19 paf 209: }
210:
1.118 paf 211: #define SPLIT_LEFT 0x0001
212: #define SPLIT_RIGHT 0x0010
213: #define SPLIT_HORIZONTAL 0x0100
214: #define SPLIT_VERTICAL 0x1000
215:
1.126 paf 216: static int split_options(const String* options) {
1.118 paf 217: struct Split_option {
1.126 paf 218: const char* keyL;
219: const char* keyU;
1.118 paf 220: int setBit;
221: int checkBit;
222: } split_option[]={
223: {"l", "L", SPLIT_LEFT, SPLIT_RIGHT}, // 0xVHRL
224: {"r", "R", SPLIT_RIGHT, SPLIT_LEFT},
225: {"h", "H", SPLIT_HORIZONTAL, SPLIT_VERTICAL},
226: {"v", "V", SPLIT_VERTICAL, SPLIT_HORIZONTAL},
1.130 paf 227: {0, 0, 0, 0}
1.118 paf 228: };
229:
230: int result=0;
1.126 paf 231: if(options) {
1.118 paf 232: for(Split_option *o=split_option; o->keyL; o++)
1.126 paf 233: if(options->pos(o->keyL)!=STRING_NOT_FOUND
234: || (o->keyU && options->pos(o->keyU)!=STRING_NOT_FOUND)) {
1.118 paf 235: if(result & o->checkBit)
1.153 misha 236: throw Exception(PARSER_RUNTIME,
1.118 paf 237: options,
238: "conflicting split options");
239: result |= o->setBit;
240: }
241: }
242:
243: return result;
244: }
245:
1.156 misha 246: static Table& split_vertical(ArrayString& pieces, bool right, const String* column_name) {
1.126 paf 247: Table::columns_type columns(new ArrayString);
1.156 misha 248: *columns+=column_name;
1.19 paf 249:
1.126 paf 250: Table& table=*new Table(columns, pieces.count());
1.118 paf 251: if(right) { // right
1.126 paf 252: for(int i=pieces.count(); --i>=0; ) {
253: Table::element_type row(new ArrayString);
254: *row+=pieces[i];
255: table+=row;
1.118 paf 256: }
257: } else { // left
1.126 paf 258: Array_iterator<const String*> i(pieces);
1.118 paf 259: while(i.has_next()) {
1.126 paf 260: Table::element_type row(new ArrayString);
261: *row+=i.next();
262: table+=row;
1.118 paf 263: }
1.61 parser 264: }
1.118 paf 265:
1.126 paf 266: return table;
1.19 paf 267: }
268:
1.128 paf 269: static Table& split_horizontal(ArrayString& pieces, bool right) {
1.126 paf 270: Table& table=*new Table(Table::columns_type(0) /* nameless */);
271: Table::element_type row(new ArrayString(pieces.count()));
1.118 paf 272: if(right) { // right
1.131 paf 273: for(int i=pieces.count(); --i>=0; )
1.126 paf 274: *row+=pieces[i];
1.118 paf 275: } else { // left
1.126 paf 276: for(Array_iterator<const String*> i(pieces); i.has_next(); )
277: *row+=i.next();
1.118 paf 278: }
1.126 paf 279: table+=row;
1.118 paf 280:
1.126 paf 281: return table;
1.118 paf 282: }
283:
1.126 paf 284: static void split_with_options(Request& r, MethodParams& params,
1.118 paf 285: int bits) {
1.126 paf 286: const String& string=GET_SELF(r, VString).string();
1.19 paf 287:
1.126 paf 288: ArrayString pieces;
1.128 paf 289: split_list(params, 0, string, pieces);
1.19 paf 290:
1.118 paf 291: if(!bits) {
1.126 paf 292: const String* options=0;
293: if(params.count()>1)
294: options=¶ms.as_string(1, "options must not be code");
295:
1.118 paf 296: bits=split_options(options);
297: }
1.21 paf 298:
1.118 paf 299: bool right=(bits & SPLIT_RIGHT) != 0;
300: bool horizontal=(bits & SPLIT_HORIZONTAL) !=0;
1.156 misha 301:
302: const String* column_name=0;
303: if(params.count()>2){
304: column_name=¶ms.as_string(2, COLUMN_NAME_MUST_BE_STRING);
305: if (horizontal && column_name->length())
306: throw Exception(PARSER_RUNTIME,
307: column_name,
308: "column name can't be specified with horisontal split");
309: }
310: if(!column_name || !column_name->length())
311: column_name=new String("piece");
312:
313: Table& table=horizontal?split_horizontal(pieces, right):split_vertical(pieces, right, column_name);
1.17 paf 314:
1.126 paf 315: r.write_no_lang(*new VTable(&table));
1.118 paf 316: }
1.126 paf 317: static void _split(Request& r, MethodParams& params) {
318: split_with_options(r, params, 0 /* maybe-determine from param #2 */);
1.118 paf 319: }
1.126 paf 320: static void _lsplit(Request& r, MethodParams& params) {
321: split_with_options(r, params, SPLIT_LEFT);
1.118 paf 322: }
1.126 paf 323: static void _rsplit(Request& r, MethodParams& params) {
324: split_with_options(r, params, SPLIT_RIGHT);
1.17 paf 325: }
326:
1.126 paf 327: static void search_action(Table& table, Table::element_type row, int, int, int, int, void *) {
1.28 paf 328: if(row)
329: table+=row;
1.27 paf 330: }
331:
1.74 parser 332: #ifndef DOXYGEN
1.27 paf 333: struct Replace_action_info {
1.126 paf 334: Request* request;
335: const String* src; String* dest;
336: VTable* vtable;
337: Value* replacement_code;
1.27 paf 338: };
1.74 parser 339: #endif
1.105 paf 340: /// @todo they can do $global[$result] there, getting pointer to later-invalid local var, kill this
1.126 paf 341: static void replace_action(Table& table, ArrayString* row,
342: int prestart, int prefinish,
343: int poststart, int postfinish,
344: void *info) {
1.27 paf 345: Replace_action_info& ai=*static_cast<Replace_action_info *>(info);
1.31 paf 346: if(row) { // begin&middle
1.104 paf 347: // piece from last match['prestart'] to beginning of this match['prefinish']
348: if(prestart!=prefinish)
349: *ai.dest << ai.src->mid(prestart, prefinish);//ai.dest->APPEND_CONST("-");
1.51 parser 350: // store found parts in one-record VTable
1.126 paf 351: if(table.count()) // middle
1.32 paf 352: table.put(0, row);
353: else // begin
1.30 paf 354: table+=row;
355: { // execute 'replacement_code' in 'table' context
1.105 paf 356: ai.vtable->set_table(table);
1.30 paf 357:
1.105 paf 358: *ai.dest << ai.request->process_to_string(*ai.replacement_code);
1.29 paf 359: }
360: } else // end
1.104 paf 361: *ai.dest << ai.src->mid(poststart, postfinish);
1.27 paf 362: }
363:
1.126 paf 364: static void _match(Request& r, MethodParams& params) {
1.174 misha 365: size_t params_count=params.count();
366:
1.126 paf 367: Value& regexp=params.as_no_junction(0, "regexp must not be code");
1.174 misha 368: Value* options=(params_count>1)?¶ms.as_no_junction(1, "options must not be code"):0;
369:
370: VRegex* vregex;
371: VRegexCleaner vrcleaner;
1.126 paf 372:
1.174 misha 373: if(Value* value=regexp.as(VREGEX_TYPE, false)){
374: if(options && options->is_defined())
375: throw Exception(PARSER_RUNTIME,
376: 0,
377: "you can not specify regex-object and options together"
378: );
379: vregex=static_cast<VRegex*>(value);
380: } else {
381: vregex=new VRegex(r.charsets.source(),
382: ®exp.as_string(),
383: (options)?(&options->as_string()):0);
1.175 ! misha 384: vregex->study();
1.174 misha 385: vrcleaner.vregex=vregex;
386: }
1.126 paf 387:
388: Temp_lang temp_lang(r, String::L_PASS_APPENDED);
389: const String& src=GET_SELF(r, VString).string();
1.152 misha 390: int matches_count=0;
1.174 misha 391:
392: if(params_count<3) { // search
393: Table* table=src.match(vregex,
1.64 parser 394: search_action, 0,
1.152 misha 395: matches_count);
1.174 misha 396:
1.152 misha 397: if(table){
1.159 misha 398: r.write_assign_lang(*new VTable(table));
1.152 misha 399: } else {
1.173 misha 400: r.write_no_lang(*new VInt(matches_count));
1.152 misha 401: }
402:
1.27 paf 403: } else { // replace
1.126 paf 404: Value& replacement_code=params.as_junction(2, "replacement param must be code");
1.106 paf 405:
1.126 paf 406: String result;
407: VTable* vtable=new VTable;
1.130 paf 408: Replace_action_info info={
409: &r,
410: &src,
411: &result,
412: vtable,
413: &replacement_code
414: };
1.105 paf 415: Temp_value_element temp_match_var(
1.119 paf 416: *replacement_code.get_junction()->method_frame,
1.126 paf 417: match_var_name, vtable);
1.174 misha 418:
419: src.match(vregex,
1.126 paf 420: replace_action, &info,
1.152 misha 421: matches_count);
1.174 misha 422:
1.102 paf 423: r.write_assign_lang(result);
1.27 paf 424: }
1.24 paf 425: }
426:
1.128 paf 427: static void change_case(Request& r, MethodParams&,
1.49 parser 428: String::Change_case_kind kind) {
1.126 paf 429: const String& src=GET_SELF(r, VString).string();
1.49 parser 430:
1.126 paf 431: r.write_assign_lang(src.change_case(r.charsets.source(), kind));
1.49 parser 432: }
1.126 paf 433: static void _upper(Request& r, MethodParams& params) {
434: change_case(r, params, String::CC_UPPER);
1.49 parser 435: }
1.126 paf 436: static void _lower(Request& r, MethodParams& params) {
437: change_case(r, params, String::CC_LOWER);
1.49 parser 438: }
439:
1.65 parser 440: #ifndef DOXYGEN
1.126 paf 441: class String_sql_event_handlers: public SQL_Driver_query_event_handlers {
442: const String& statement_string; const char* statement_cstr;
443: bool got_column;
1.65 parser 444: public:
1.126 paf 445: bool got_cell;
446: String& result;
447: public:
448: String_sql_event_handlers(
449: const String& astatement_string, const char* astatement_cstr):
450: statement_string(astatement_string), statement_cstr(astatement_cstr),
451: got_column(false),
452: got_cell(false),
453: result(*new String) {}
1.65 parser 454:
1.128 paf 455: bool add_column(SQL_Error& error, const char* /*str*/, size_t /*length*/) {
1.124 paf 456: if(got_column) {
1.153 misha 457: error=SQL_Error(PARSER_RUNTIME,
1.126 paf 458: //statement_string,
1.65 parser 459: "result must contain exactly one column");
1.124 paf 460: return true;
461: }
1.65 parser 462: got_column=true;
1.124 paf 463: return false;
1.65 parser 464: }
1.124 paf 465: bool before_rows(SQL_Error& /*error*/ ) { /* ignore */ return false; }
466: bool add_row(SQL_Error& /*error*/) { /* ignore */ return false; }
1.126 paf 467: bool add_row_cell(SQL_Error& error, const char* str, size_t length) {
1.124 paf 468: if(got_cell) {
1.153 misha 469: error=SQL_Error(PARSER_RUNTIME,
1.126 paf 470: //statement_string,
1.65 parser 471: "result must not contain more then one row");
1.124 paf 472: return true;
473: }
1.65 parser 474:
1.124 paf 475: try {
476: got_cell=true;
1.126 paf 477: result.append_know_length(str, length, String::L_TAINTED);
1.124 paf 478: return false;
479: } catch(...) {
480: error=SQL_Error("exception occured in String_sql_event_handlers::add_row_cell");
481: return true;
482: }
1.65 parser 483: }
484: };
485: #endif
1.141 paf 486: extern String sql_bind_name;
1.126 paf 487: extern String sql_limit_name;
488: extern String sql_offset_name;
489: extern String sql_default_name;
490: extern String sql_distinct_name;
1.141 paf 491: extern int marshal_binds(HashStringValue& hash, SQL_Driver::Placeholder*& placeholders);
492: extern void unmarshal_bind_updates(HashStringValue& hash, int placeholder_count, SQL_Driver::Placeholder* placeholders);
493:
1.126 paf 494: const String* sql_result_string(Request& r, MethodParams& params,
495: HashStringValue*& options, Value*& default_code) {
496: Value& statement=params.as_junction(0, "statement must be code");
1.53 parser 497:
1.141 paf 498: HashStringValue* bind=0;
1.160 misha 499: ulong limit=SQL_NO_LIMIT;
1.70 parser 500: ulong offset=0;
1.81 parser 501: default_code=0;
1.126 paf 502: if(params.count()>1) {
503: Value& voptions=params.as_no_junction(1, "options must be hash, not code");
1.145 paf 504: if(voptions.is_defined() && !voptions.is_string())
1.130 paf 505: if((options=voptions.get_hash())) {
1.141 paf 506: int valid_options=0;
507: if(Value* vbind=options->get(sql_bind_name)) {
508: valid_options++;
509: bind=vbind->get_hash();
510: }
511: if(Value* vlimit=options->get(sql_limit_name)) {
1.161 misha 512: valid_options++;
513: limit=(ulong)r.process_to_value(*vlimit).as_double();
1.141 paf 514: }
515: if(Value* voffset=options->get(sql_offset_name)) {
516: valid_options++;
1.99 paf 517: offset=(ulong)r.process_to_value(*voffset).as_double();
1.141 paf 518: }
1.130 paf 519: if((default_code=options->get(sql_default_name))) {
1.141 paf 520: valid_options++;
1.81 parser 521: }
1.141 paf 522: if(valid_options!=options->count())
1.153 misha 523: throw Exception(PARSER_RUNTIME,
1.141 paf 524: 0,
525: "called with invalid option");
1.73 parser 526: } else
1.153 misha 527: throw Exception(PARSER_RUNTIME,
1.126 paf 528: 0,
1.73 parser 529: "options must be hash");
1.70 parser 530: } else
531: options=0;
532:
1.141 paf 533: SQL_Driver::Placeholder* placeholders=0;
534: uint placeholders_count=0;
535: if(bind)
536: placeholders_count=marshal_binds(*bind, placeholders);
537:
1.126 paf 538: Temp_lang temp_lang(r, String::L_SQL);
1.99 paf 539: const String& statement_string=r.process_to_string(statement);
1.126 paf 540: const char* statement_cstr=
541: statement_string.cstr(String::L_UNSPECIFIED, r.connection());
542: String_sql_event_handlers handlers(statement_string, statement_cstr);
1.160 misha 543:
1.126 paf 544: r.connection()->query(
1.140 paf 545: statement_cstr,
1.142 paf 546: placeholders_count, placeholders,
1.140 paf 547: offset, limit,
1.117 paf 548: handlers,
549: statement_string);
1.53 parser 550:
1.141 paf 551: if(bind)
552: unmarshal_bind_updates(*bind, placeholders_count, placeholders);
553:
1.65 parser 554: if(!handlers.got_cell)
555: return 0; // no lines, caller should return second param[default value]
1.62 parser 556:
1.126 paf 557: return &handlers.result;
1.53 parser 558: }
559:
1.126 paf 560: static void _sql(Request& r, MethodParams& params) {
1.53 parser 561:
1.126 paf 562: HashStringValue* options;
563: Value* default_code;
564: const String* string=sql_result_string(r, params, options, default_code);
1.62 parser 565: if(!string) {
1.81 parser 566: if(default_code) {
1.99 paf 567: string=&r.process_to_string(*default_code);
1.68 parser 568: } else
1.153 misha 569: throw Exception(PARSER_RUNTIME,
1.126 paf 570: 0,
1.81 parser 571: "produced no result, but no default option specified");
1.62 parser 572: }
1.100 paf 573:
1.102 paf 574: r.write_assign_lang(*string);
1.53 parser 575: }
576:
1.126 paf 577: static void _replace(Request& r, MethodParams& params) {
578: const String& src=GET_SELF(r, VString).string();
1.71 parser 579:
1.157 misha 580: Table* table=params.as_no_junction(0, PARAM_MUST_NOT_BE_CODE).get_table();
1.71 parser 581: if(!table)
1.153 misha 582: throw Exception(PARSER_RUNTIME,
1.126 paf 583: 0,
1.71 parser 584: "parameter must be table");
585:
586: Dictionary dict(*table);
1.126 paf 587: r.write_assign_lang(src.replace(dict));
1.71 parser 588: }
1.79 parser 589:
1.126 paf 590: static void _save(Request& r, MethodParams& params) {
1.154 misha 591: const String& file_name=params.as_string(params.count()-1, FILE_NAME_MUST_BE_STRING);
1.79 parser 592:
1.126 paf 593: const String& src=GET_SELF(r, VString).string();
1.79 parser 594:
1.87 paf 595: bool do_append=false;
1.126 paf 596: if(params.count()>1) {
597: const String& mode=params.as_string(0, "mode must be string");
1.87 paf 598: if(mode=="append")
599: do_append=true;
600: else
1.153 misha 601: throw Exception(PARSER_RUNTIME,
1.87 paf 602: &mode,
603: "unknown mode, must be 'append'");
604: }
605:
1.79 parser 606: // write
1.126 paf 607: const char* buf=src.cstr(String::L_UNSPECIFIED, r.connection(false/*no error if none*/));
1.94 paf 608: file_write(r.absolute(file_name),
1.89 paf 609: buf, strlen(buf), true, do_append);
1.79 parser 610: }
611:
1.126 paf 612: static void _normalize(Request& r, MethodParams&) {
613: const String& src=GET_SELF(r, VString).string();
614:
615: r.write_assign_lang(src);
1.109 paf 616: }
617:
1.133 paf 618: static void _trim(Request& r, MethodParams& params) {
619: const String& src=GET_SELF(r, VString).string();
620:
621: String::Trim_kind kind=String::TRIM_BOTH;
622: const char* chars=0;
623: if(params.count()>0) {
1.135 paf 624: const String& skind=params.as_string(0,
625: "'where' must be string");
1.137 paf 626: if(skind.length())
1.162 misha 627: if(skind==TRIM_BOTH_OPTION)
628: kind=String::TRIM_BOTH;
629: else if(skind==TRIM_START_OPTION || skind=="start")
1.137 paf 630: kind=String::TRIM_START;
1.162 misha 631: else if(skind==TRIM_END_OPTION || skind=="end")
1.137 paf 632: kind=String::TRIM_END;
633: else
1.153 misha 634: throw Exception(PARSER_RUNTIME,
1.137 paf 635: &skind,
636: "'kind' must be one of "TRIM_START_OPTION", "TRIM_BOTH_OPTION", "TRIM_END_OPTION);
1.133 paf 637:
1.136 paf 638: if(params.count()>1) {
639: const String& schars=params.as_string(1, "'chars' must be string");
1.137 paf 640: if(schars.length())
641: chars=schars.cstr();
1.136 paf 642: }
1.133 paf 643: }
644:
645: r.write_assign_lang(src.trim(kind, chars));
646: }
647:
1.139 paf 648: static void _append(Request& r, MethodParams& params) {
649: // c=a+b
650: VString& va=GET_SELF(r, VString);
651: const String& a=va.string();
1.155 misha 652: const String& b=params.as_string(0, PARAMETER_MUST_BE_STRING);
1.139 paf 653: String& c=*new String(a);
654: c.append(b, String::L_PASS_APPENDED);
655: va.set_string(c);
656: }
657:
1.146 paf 658: static void _base64(Request& r, MethodParams& params) {
659: if(params.count()) {
1.169 misha 660: // decode: ^string:base64[encoded]
1.155 misha 661: const char* cstr=params.as_string(0, PARAMETER_MUST_BE_STRING).cstr();
1.169 misha 662: char* decoded=0;
663: size_t length=0;
664: pa_base64_decode(cstr, strlen(cstr), decoded, length);
665: if(decoded && length){
666: if(memchr((const char*)decoded, 0, length))
667: throw Exception(PARSER_RUNTIME,
668: 0,
669: "Invalid \\x00 character found while decode to string. Decode it to file instead.");
670:
671: fix_line_breaks(decoded, length);
672: if(length){
673: r.write_assign_lang(*new String(decoded, length, true/*tainted*/));
674: }
675: }
1.147 paf 676: } else {
1.169 misha 677: // encode: ^str.base64[]
1.148 paf 678: VString& self=GET_SELF(r, VString);
679: const char* cstr=self.string().cstr();
1.147 paf 680: const char* encoded=pa_base64_encode(cstr, strlen(cstr));
1.169 misha 681: r.write_assign_lang(*new String(encoded, 0, true/*tainted. once ?param=base64(something) was needed*/));
1.146 paf 682: }
683: }
684:
1.167 misha 685: static void _escape(Request& r, MethodParams&){
686: const String& src=GET_SELF(r, VString).string();
687: r.write_assign_lang(src.escape(r.charsets.source()));
688: }
689:
690: static void _unescape(Request& r, MethodParams& params){
691: const String& src=params.as_string(0, PARAMETER_MUST_BE_STRING);
692: if(const char* result=unescape_chars(src.cstr(), src.length(), &r.charsets.source(), true/* don't unescape '+' char */))
693: r.write_assign_lang(*new String(result));
694: }
695:
1.41 paf 696: // constructor
697:
1.126 paf 698: MString::MString(): Methoded("string") {
1.1 paf 699: // ^string.length[]
1.41 paf 700: add_native_method("length", Method::CT_DYNAMIC, _length, 0, 0);
1.6 paf 701:
1.1 paf 702: // ^string.int[]
1.72 parser 703: // ^string.int(default)
704: add_native_method("int", Method::CT_DYNAMIC, _int, 0, 1);
1.1 paf 705: // ^string.double[]
1.72 parser 706: // ^string.double(default)
707: add_native_method("double", Method::CT_DYNAMIC, _double, 0, 1);
1.151 misha 708: // ^void.bool[]
709: // ^void.bool(default)
710: add_native_method("bool", Method::CT_DYNAMIC, _bool, 0, 1);
1.9 paf 711:
1.165 misha 712: // ^string.format[format]
1.41 paf 713: add_native_method("format", Method::CT_DYNAMIC, _string_format, 1, 1);
1.14 paf 714:
1.15 paf 715: // ^string.left(n)
1.41 paf 716: add_native_method("left", Method::CT_DYNAMIC, _left, 1, 1);
1.15 paf 717: // ^string.right(n)
1.41 paf 718: add_native_method("right", Method::CT_DYNAMIC, _right, 1, 1);
1.165 misha 719: // ^string.mid(p)
1.15 paf 720: // ^string.mid(p;n)
1.82 parser 721: add_native_method("mid", Method::CT_DYNAMIC, _mid, 1, 2);
1.16 paf 722:
723: // ^string.pos[substr]
1.165 misha 724: // ^string.pos[substr](n)
725: add_native_method("pos", Method::CT_DYNAMIC, _pos, 1, 2);
1.17 paf 726:
1.118 paf 727: // ^string.split[delim]
728: // ^string.split[delim][options]
1.156 misha 729: // ^string.split[delim][options][column name]
730: add_native_method("split", Method::CT_DYNAMIC, _split, 1, 3);
1.118 paf 731: // old names for backward compatibility
732: // ^string.lsplit[delim]
733: add_native_method("lsplit", Method::CT_DYNAMIC, _lsplit, 1, 1);
734: // ^string.rsplit[delim]
735: add_native_method("rsplit", Method::CT_DYNAMIC, _rsplit, 1, 1);
736:
1.32 paf 737: // ^string.match[regexp][options]
738: // ^string.match[regexp][options]{replacement-code}
1.41 paf 739: add_native_method("match", Method::CT_DYNAMIC, _match, 1, 3);
1.49 parser 740:
1.165 misha 741: // ^string.upper[]
1.49 parser 742: add_native_method("upper", Method::CT_DYNAMIC, _upper, 0, 0);
1.165 misha 743: // ^string.lower[]
1.49 parser 744: add_native_method("lower", Method::CT_DYNAMIC, _lower, 0, 0);
1.53 parser 745:
1.70 parser 746: // ^sql[query]
1.141 paf 747: // ^sql[query][options hash]
1.67 parser 748: add_native_method("sql", Method::CT_STATIC, _sql, 1, 2);
1.71 parser 749:
750: // ^string.replace[table]
751: add_native_method("replace", Method::CT_DYNAMIC, _replace, 1, 1);
1.79 parser 752:
753: // ^string.save[file]
1.87 paf 754: add_native_method("save", Method::CT_DYNAMIC, _save, 1, 2);
1.109 paf 755:
1.112 paf 756: // ^string.normalize[]
757: add_native_method("normalize", Method::CT_DYNAMIC, _normalize, 0, 0);
1.133 paf 758:
759: // ^string.trim[[start|both|end][;chars]]
760: add_native_method("trim", Method::CT_DYNAMIC, _trim, 0, 2);
1.139 paf 761:
762: // ^string.append[string]
763: add_native_method("append", Method::CT_DYNAMIC, _append, 1, 1);
1.146 paf 764:
765: // ^string.base64[] << encode
1.147 paf 766: // ^string:base64[encoded string] << decode
1.146 paf 767: add_native_method("base64", Method::CT_ANY, _base64, 0, 1);
1.167 misha 768:
1.168 misha 769: // ^string.js-escape[]
770: // ^string:js-unescape[escaped%uXXXXstring]
771: add_native_method("js-escape", Method::CT_ANY, _escape, 0, 0);
772: add_native_method("js-unescape", Method::CT_STATIC, _unescape, 1, 1);
1.2 paf 773: }
E-mail: