Annotation of parser3/src/classes/string.C, revision 1.188
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.188 ! misha 8: static const char * const IDENT_STRING_C="$Date: 2009-07-15 12:59:19 $";
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.176 misha 287: size_t params_count=params.count();
1.19 paf 288:
1.126 paf 289: ArrayString pieces;
1.128 paf 290: split_list(params, 0, string, pieces);
1.19 paf 291:
1.118 paf 292: if(!bits) {
1.126 paf 293: const String* options=0;
1.176 misha 294: if(params_count>1)
1.187 misha 295: options=¶ms.as_string(1, OPTIONS_MUST_NOT_BE_CODE);
1.126 paf 296:
1.118 paf 297: bits=split_options(options);
298: }
1.21 paf 299:
1.118 paf 300: bool right=(bits & SPLIT_RIGHT) != 0;
301: bool horizontal=(bits & SPLIT_HORIZONTAL) !=0;
1.156 misha 302:
303: const String* column_name=0;
1.176 misha 304: if(params_count>2){
1.156 misha 305: column_name=¶ms.as_string(2, COLUMN_NAME_MUST_BE_STRING);
1.178 misha 306: if (horizontal && !column_name->is_empty())
1.156 misha 307: throw Exception(PARSER_RUNTIME,
308: column_name,
309: "column name can't be specified with horisontal split");
310: }
1.178 misha 311: if(!column_name || column_name->is_empty())
1.156 misha 312: column_name=new String("piece");
313:
314: Table& table=horizontal?split_horizontal(pieces, right):split_vertical(pieces, right, column_name);
1.17 paf 315:
1.126 paf 316: r.write_no_lang(*new VTable(&table));
1.118 paf 317: }
1.126 paf 318: static void _split(Request& r, MethodParams& params) {
319: split_with_options(r, params, 0 /* maybe-determine from param #2 */);
1.118 paf 320: }
1.126 paf 321: static void _lsplit(Request& r, MethodParams& params) {
322: split_with_options(r, params, SPLIT_LEFT);
1.118 paf 323: }
1.126 paf 324: static void _rsplit(Request& r, MethodParams& params) {
325: split_with_options(r, params, SPLIT_RIGHT);
1.17 paf 326: }
327:
1.126 paf 328: static void search_action(Table& table, Table::element_type row, int, int, int, int, void *) {
1.28 paf 329: if(row)
330: table+=row;
1.27 paf 331: }
332:
1.74 parser 333: #ifndef DOXYGEN
1.27 paf 334: struct Replace_action_info {
1.181 misha 335: Request* request;
336: const String* src;
337: String* dest;
1.126 paf 338: VTable* vtable;
339: Value* replacement_code;
1.27 paf 340: };
1.74 parser 341: #endif
1.105 paf 342: /// @todo they can do $global[$result] there, getting pointer to later-invalid local var, kill this
1.126 paf 343: static void replace_action(Table& table, ArrayString* row,
1.181 misha 344: int prestart, int prefinish,
345: int poststart, int postfinish,
346: void *info) {
1.27 paf 347: Replace_action_info& ai=*static_cast<Replace_action_info *>(info);
1.31 paf 348: if(row) { // begin&middle
1.104 paf 349: // piece from last match['prestart'] to beginning of this match['prefinish']
350: if(prestart!=prefinish)
351: *ai.dest << ai.src->mid(prestart, prefinish);//ai.dest->APPEND_CONST("-");
1.51 parser 352: // store found parts in one-record VTable
1.126 paf 353: if(table.count()) // middle
1.32 paf 354: table.put(0, row);
355: else // begin
1.30 paf 356: table+=row;
1.181 misha 357:
1.30 paf 358: { // execute 'replacement_code' in 'table' context
1.181 misha 359: if(ai.replacement_code){
360: ai.vtable->set_table(table);
361: *ai.dest << ai.request->process_to_string(*ai.replacement_code);
362: }
1.29 paf 363: }
364: } else // end
1.104 paf 365: *ai.dest << ai.src->mid(poststart, postfinish);
1.27 paf 366: }
367:
1.126 paf 368: static void _match(Request& r, MethodParams& params) {
1.174 misha 369: size_t params_count=params.count();
370:
1.126 paf 371: Value& regexp=params.as_no_junction(0, "regexp must not be code");
1.187 misha 372: Value* options=(params_count>1)?¶ms.as_no_junction(1, OPTIONS_MUST_NOT_BE_CODE):0;
1.174 misha 373:
374: VRegex* vregex;
375: VRegexCleaner vrcleaner;
1.126 paf 376:
1.188 ! misha 377: if(Value* value=regexp.as(VREGEX_TYPE)){
1.174 misha 378: if(options && options->is_defined())
379: throw Exception(PARSER_RUNTIME,
380: 0,
381: "you can not specify regex-object and options together"
382: );
383: vregex=static_cast<VRegex*>(value);
384: } else {
385: vregex=new VRegex(r.charsets.source(),
386: ®exp.as_string(),
387: (options)?(&options->as_string()):0);
1.175 misha 388: vregex->study();
1.174 misha 389: vrcleaner.vregex=vregex;
390: }
1.126 paf 391:
392: Temp_lang temp_lang(r, String::L_PASS_APPENDED);
393: const String& src=GET_SELF(r, VString).string();
1.152 misha 394: int matches_count=0;
1.174 misha 395:
396: if(params_count<3) { // search
397: Table* table=src.match(vregex,
1.64 parser 398: search_action, 0,
1.152 misha 399: matches_count);
1.174 misha 400:
1.152 misha 401: if(table){
1.176 misha 402: r.write_no_lang(*new VTable(table));
1.152 misha 403: } else {
1.173 misha 404: r.write_no_lang(*new VInt(matches_count));
1.152 misha 405: }
406:
1.27 paf 407: } else { // replace
1.181 misha 408:
409: Value* replacement_code=0;
410: bool is_junction=false;
411:
412: Value* replacement=¶ms[2];
413: if(replacement->get_junction()){
414: replacement_code=replacement;
415: is_junction=true;
416: } else if(replacement->is_string()){
417: if(replacement->is_defined())
418: replacement_code=replacement;
419: } else if(!replacement->is_void())
420: throw Exception(PARSER_RUNTIME,
421: 0,
422: "replacement option should be junction or string");
1.106 paf 423:
1.126 paf 424: String result;
425: VTable* vtable=new VTable;
1.130 paf 426: Replace_action_info info={
427: &r,
428: &src,
429: &result,
430: vtable,
1.181 misha 431: replacement_code
1.130 paf 432: };
1.181 misha 433:
434: Temp_value_element* temp_match_var=0;
435:
436: if(is_junction)
437: temp_match_var=new Temp_value_element(
438: *replacement_code->get_junction()->method_frame,
439: match_var_name, vtable);
1.174 misha 440:
441: src.match(vregex,
1.126 paf 442: replace_action, &info,
1.152 misha 443: matches_count);
1.174 misha 444:
1.181 misha 445: if(temp_match_var)
446: delete temp_match_var;
447:
1.102 paf 448: r.write_assign_lang(result);
1.27 paf 449: }
1.24 paf 450: }
451:
1.128 paf 452: static void change_case(Request& r, MethodParams&,
1.49 parser 453: String::Change_case_kind kind) {
1.126 paf 454: const String& src=GET_SELF(r, VString).string();
1.49 parser 455:
1.126 paf 456: r.write_assign_lang(src.change_case(r.charsets.source(), kind));
1.49 parser 457: }
1.126 paf 458: static void _upper(Request& r, MethodParams& params) {
459: change_case(r, params, String::CC_UPPER);
1.49 parser 460: }
1.126 paf 461: static void _lower(Request& r, MethodParams& params) {
462: change_case(r, params, String::CC_LOWER);
1.49 parser 463: }
464:
1.65 parser 465: #ifndef DOXYGEN
1.126 paf 466: class String_sql_event_handlers: public SQL_Driver_query_event_handlers {
467: const String& statement_string; const char* statement_cstr;
468: bool got_column;
1.65 parser 469: public:
1.126 paf 470: bool got_cell;
471: String& result;
472: public:
473: String_sql_event_handlers(
474: const String& astatement_string, const char* astatement_cstr):
475: statement_string(astatement_string), statement_cstr(astatement_cstr),
476: got_column(false),
477: got_cell(false),
478: result(*new String) {}
1.65 parser 479:
1.128 paf 480: bool add_column(SQL_Error& error, const char* /*str*/, size_t /*length*/) {
1.124 paf 481: if(got_column) {
1.153 misha 482: error=SQL_Error(PARSER_RUNTIME,
1.126 paf 483: //statement_string,
1.65 parser 484: "result must contain exactly one column");
1.124 paf 485: return true;
486: }
1.65 parser 487: got_column=true;
1.124 paf 488: return false;
1.65 parser 489: }
1.124 paf 490: bool before_rows(SQL_Error& /*error*/ ) { /* ignore */ return false; }
491: bool add_row(SQL_Error& /*error*/) { /* ignore */ return false; }
1.126 paf 492: bool add_row_cell(SQL_Error& error, const char* str, size_t length) {
1.124 paf 493: if(got_cell) {
1.153 misha 494: error=SQL_Error(PARSER_RUNTIME,
1.126 paf 495: //statement_string,
1.65 parser 496: "result must not contain more then one row");
1.124 paf 497: return true;
498: }
1.65 parser 499:
1.124 paf 500: try {
501: got_cell=true;
1.126 paf 502: result.append_know_length(str, length, String::L_TAINTED);
1.124 paf 503: return false;
504: } catch(...) {
505: error=SQL_Error("exception occured in String_sql_event_handlers::add_row_cell");
506: return true;
507: }
1.65 parser 508: }
509: };
510: #endif
1.141 paf 511: extern String sql_bind_name;
1.126 paf 512: extern String sql_limit_name;
513: extern String sql_offset_name;
514: extern String sql_default_name;
515: extern String sql_distinct_name;
1.141 paf 516: extern int marshal_binds(HashStringValue& hash, SQL_Driver::Placeholder*& placeholders);
517: extern void unmarshal_bind_updates(HashStringValue& hash, int placeholder_count, SQL_Driver::Placeholder* placeholders);
518:
1.126 paf 519: const String* sql_result_string(Request& r, MethodParams& params,
520: HashStringValue*& options, Value*& default_code) {
521: Value& statement=params.as_junction(0, "statement must be code");
1.53 parser 522:
1.141 paf 523: HashStringValue* bind=0;
1.160 misha 524: ulong limit=SQL_NO_LIMIT;
1.70 parser 525: ulong offset=0;
1.81 parser 526: default_code=0;
1.126 paf 527: if(params.count()>1) {
528: Value& voptions=params.as_no_junction(1, "options must be hash, not code");
1.145 paf 529: if(voptions.is_defined() && !voptions.is_string())
1.130 paf 530: if((options=voptions.get_hash())) {
1.141 paf 531: int valid_options=0;
532: if(Value* vbind=options->get(sql_bind_name)) {
533: valid_options++;
534: bind=vbind->get_hash();
535: }
536: if(Value* vlimit=options->get(sql_limit_name)) {
1.161 misha 537: valid_options++;
538: limit=(ulong)r.process_to_value(*vlimit).as_double();
1.141 paf 539: }
540: if(Value* voffset=options->get(sql_offset_name)) {
541: valid_options++;
1.99 paf 542: offset=(ulong)r.process_to_value(*voffset).as_double();
1.141 paf 543: }
1.130 paf 544: if((default_code=options->get(sql_default_name))) {
1.141 paf 545: valid_options++;
1.81 parser 546: }
1.141 paf 547: if(valid_options!=options->count())
1.153 misha 548: throw Exception(PARSER_RUNTIME,
1.141 paf 549: 0,
550: "called with invalid option");
1.73 parser 551: } else
1.153 misha 552: throw Exception(PARSER_RUNTIME,
1.126 paf 553: 0,
1.73 parser 554: "options must be hash");
1.70 parser 555: } else
556: options=0;
557:
1.141 paf 558: SQL_Driver::Placeholder* placeholders=0;
559: uint placeholders_count=0;
560: if(bind)
561: placeholders_count=marshal_binds(*bind, placeholders);
562:
1.126 paf 563: Temp_lang temp_lang(r, String::L_SQL);
1.99 paf 564: const String& statement_string=r.process_to_string(statement);
1.185 misha 565: const char* statement_cstr=statement_string.untaint_cstr(r.flang, r.connection());
1.183 misha 566:
1.126 paf 567: String_sql_event_handlers handlers(statement_string, statement_cstr);
1.160 misha 568:
1.126 paf 569: r.connection()->query(
1.140 paf 570: statement_cstr,
1.142 paf 571: placeholders_count, placeholders,
1.140 paf 572: offset, limit,
1.117 paf 573: handlers,
574: statement_string);
1.53 parser 575:
1.141 paf 576: if(bind)
577: unmarshal_bind_updates(*bind, placeholders_count, placeholders);
578:
1.65 parser 579: if(!handlers.got_cell)
580: return 0; // no lines, caller should return second param[default value]
1.62 parser 581:
1.126 paf 582: return &handlers.result;
1.53 parser 583: }
584:
1.126 paf 585: static void _sql(Request& r, MethodParams& params) {
1.53 parser 586:
1.126 paf 587: HashStringValue* options;
588: Value* default_code;
589: const String* string=sql_result_string(r, params, options, default_code);
1.62 parser 590: if(!string) {
1.81 parser 591: if(default_code) {
1.99 paf 592: string=&r.process_to_string(*default_code);
1.68 parser 593: } else
1.153 misha 594: throw Exception(PARSER_RUNTIME,
1.126 paf 595: 0,
1.81 parser 596: "produced no result, but no default option specified");
1.62 parser 597: }
1.100 paf 598:
1.102 paf 599: r.write_assign_lang(*string);
1.53 parser 600: }
601:
1.126 paf 602: static void _replace(Request& r, MethodParams& params) {
603: const String& src=GET_SELF(r, VString).string();
1.71 parser 604:
1.157 misha 605: Table* table=params.as_no_junction(0, PARAM_MUST_NOT_BE_CODE).get_table();
1.71 parser 606: if(!table)
1.153 misha 607: throw Exception(PARSER_RUNTIME,
1.126 paf 608: 0,
1.71 parser 609: "parameter must be table");
610:
611: Dictionary dict(*table);
1.126 paf 612: r.write_assign_lang(src.replace(dict));
1.71 parser 613: }
1.79 parser 614:
1.126 paf 615: static void _save(Request& r, MethodParams& params) {
1.176 misha 616: size_t params_count=params.count();
617: const String& file_name=params.as_string(params_count-1, FILE_NAME_MUST_BE_STRING);
1.79 parser 618:
1.126 paf 619: const String& src=GET_SELF(r, VString).string();
1.79 parser 620:
1.87 paf 621: bool do_append=false;
1.176 misha 622: if(params_count>1) {
1.126 paf 623: const String& mode=params.as_string(0, "mode must be string");
1.87 paf 624: if(mode=="append")
625: do_append=true;
626: else
1.153 misha 627: throw Exception(PARSER_RUNTIME,
1.87 paf 628: &mode,
629: "unknown mode, must be 'append'");
630: }
631:
1.79 parser 632: // write
1.186 misha 633: String::Body sbody=src.cstr_to_string_body_untaint(String::L_AS_IS, r.connection(false/*no error if none*/));
634: file_write(r.absolute(file_name), sbody.cstr(), sbody.length(), true, do_append);
1.79 parser 635: }
636:
1.126 paf 637: static void _normalize(Request& r, MethodParams&) {
638: const String& src=GET_SELF(r, VString).string();
639:
640: r.write_assign_lang(src);
1.109 paf 641: }
642:
1.133 paf 643: static void _trim(Request& r, MethodParams& params) {
644: const String& src=GET_SELF(r, VString).string();
645:
646: String::Trim_kind kind=String::TRIM_BOTH;
1.176 misha 647: size_t params_count=params.count();
1.133 paf 648: const char* chars=0;
1.176 misha 649: if(params_count>0) {
650: const String& skind=params.as_string(0, "'where' must be string");
1.178 misha 651: if(!skind.is_empty())
1.162 misha 652: if(skind==TRIM_BOTH_OPTION)
653: kind=String::TRIM_BOTH;
654: else if(skind==TRIM_START_OPTION || skind=="start")
1.137 paf 655: kind=String::TRIM_START;
1.162 misha 656: else if(skind==TRIM_END_OPTION || skind=="end")
1.137 paf 657: kind=String::TRIM_END;
658: else
1.153 misha 659: throw Exception(PARSER_RUNTIME,
1.137 paf 660: &skind,
661: "'kind' must be one of "TRIM_START_OPTION", "TRIM_BOTH_OPTION", "TRIM_END_OPTION);
1.133 paf 662:
1.176 misha 663: if(params_count>1) {
1.136 paf 664: const String& schars=params.as_string(1, "'chars' must be string");
1.178 misha 665: if(!schars.is_empty())
1.137 paf 666: chars=schars.cstr();
1.136 paf 667: }
1.133 paf 668: }
669:
1.182 misha 670: r.write_assign_lang(src.trim(kind, chars, &r.charsets.source()));
1.133 paf 671: }
672:
1.139 paf 673: static void _append(Request& r, MethodParams& params) {
674: // c=a+b
675: VString& va=GET_SELF(r, VString);
676: const String& a=va.string();
1.155 misha 677: const String& b=params.as_string(0, PARAMETER_MUST_BE_STRING);
1.139 paf 678: String& c=*new String(a);
679: c.append(b, String::L_PASS_APPENDED);
680: va.set_string(c);
681: }
682:
1.146 paf 683: static void _base64(Request& r, MethodParams& params) {
684: if(params.count()) {
1.169 misha 685: // decode: ^string:base64[encoded]
1.155 misha 686: const char* cstr=params.as_string(0, PARAMETER_MUST_BE_STRING).cstr();
1.169 misha 687: char* decoded=0;
688: size_t length=0;
689: pa_base64_decode(cstr, strlen(cstr), decoded, length);
690: if(decoded && length){
691: if(memchr((const char*)decoded, 0, length))
692: throw Exception(PARSER_RUNTIME,
693: 0,
694: "Invalid \\x00 character found while decode to string. Decode it to file instead.");
695:
696: fix_line_breaks(decoded, length);
1.179 misha 697: if(length)
1.180 misha 698: r.write_assign_lang(*new String(decoded, String::L_TAINTED));
1.169 misha 699: }
1.147 paf 700: } else {
1.169 misha 701: // encode: ^str.base64[]
1.148 paf 702: VString& self=GET_SELF(r, VString);
703: const char* cstr=self.string().cstr();
1.147 paf 704: const char* encoded=pa_base64_encode(cstr, strlen(cstr));
1.180 misha 705: r.write_assign_lang(*new String(encoded, String::L_TAINTED/*once ?param=base64(something) was needed*/));
1.146 paf 706: }
707: }
708:
1.167 misha 709: static void _escape(Request& r, MethodParams&){
710: const String& src=GET_SELF(r, VString).string();
711: r.write_assign_lang(src.escape(r.charsets.source()));
712: }
713:
714: static void _unescape(Request& r, MethodParams& params){
715: const String& src=params.as_string(0, PARAMETER_MUST_BE_STRING);
716: if(const char* result=unescape_chars(src.cstr(), src.length(), &r.charsets.source(), true/* don't unescape '+' char */))
717: r.write_assign_lang(*new String(result));
718: }
719:
1.41 paf 720: // constructor
721:
1.126 paf 722: MString::MString(): Methoded("string") {
1.1 paf 723: // ^string.length[]
1.41 paf 724: add_native_method("length", Method::CT_DYNAMIC, _length, 0, 0);
1.6 paf 725:
1.1 paf 726: // ^string.int[]
1.72 parser 727: // ^string.int(default)
728: add_native_method("int", Method::CT_DYNAMIC, _int, 0, 1);
1.1 paf 729: // ^string.double[]
1.72 parser 730: // ^string.double(default)
731: add_native_method("double", Method::CT_DYNAMIC, _double, 0, 1);
1.151 misha 732: // ^void.bool[]
733: // ^void.bool(default)
734: add_native_method("bool", Method::CT_DYNAMIC, _bool, 0, 1);
1.9 paf 735:
1.165 misha 736: // ^string.format[format]
1.41 paf 737: add_native_method("format", Method::CT_DYNAMIC, _string_format, 1, 1);
1.14 paf 738:
1.15 paf 739: // ^string.left(n)
1.41 paf 740: add_native_method("left", Method::CT_DYNAMIC, _left, 1, 1);
1.15 paf 741: // ^string.right(n)
1.41 paf 742: add_native_method("right", Method::CT_DYNAMIC, _right, 1, 1);
1.165 misha 743: // ^string.mid(p)
1.15 paf 744: // ^string.mid(p;n)
1.82 parser 745: add_native_method("mid", Method::CT_DYNAMIC, _mid, 1, 2);
1.16 paf 746:
747: // ^string.pos[substr]
1.165 misha 748: // ^string.pos[substr](n)
749: add_native_method("pos", Method::CT_DYNAMIC, _pos, 1, 2);
1.17 paf 750:
1.118 paf 751: // ^string.split[delim]
752: // ^string.split[delim][options]
1.156 misha 753: // ^string.split[delim][options][column name]
754: add_native_method("split", Method::CT_DYNAMIC, _split, 1, 3);
1.118 paf 755: // old names for backward compatibility
756: // ^string.lsplit[delim]
757: add_native_method("lsplit", Method::CT_DYNAMIC, _lsplit, 1, 1);
758: // ^string.rsplit[delim]
759: add_native_method("rsplit", Method::CT_DYNAMIC, _rsplit, 1, 1);
760:
1.32 paf 761: // ^string.match[regexp][options]
762: // ^string.match[regexp][options]{replacement-code}
1.41 paf 763: add_native_method("match", Method::CT_DYNAMIC, _match, 1, 3);
1.49 parser 764:
1.165 misha 765: // ^string.upper[]
1.49 parser 766: add_native_method("upper", Method::CT_DYNAMIC, _upper, 0, 0);
1.165 misha 767: // ^string.lower[]
1.49 parser 768: add_native_method("lower", Method::CT_DYNAMIC, _lower, 0, 0);
1.53 parser 769:
1.70 parser 770: // ^sql[query]
1.141 paf 771: // ^sql[query][options hash]
1.67 parser 772: add_native_method("sql", Method::CT_STATIC, _sql, 1, 2);
1.71 parser 773:
774: // ^string.replace[table]
775: add_native_method("replace", Method::CT_DYNAMIC, _replace, 1, 1);
1.79 parser 776:
777: // ^string.save[file]
1.87 paf 778: add_native_method("save", Method::CT_DYNAMIC, _save, 1, 2);
1.109 paf 779:
1.112 paf 780: // ^string.normalize[]
781: add_native_method("normalize", Method::CT_DYNAMIC, _normalize, 0, 0);
1.133 paf 782:
783: // ^string.trim[[start|both|end][;chars]]
784: add_native_method("trim", Method::CT_DYNAMIC, _trim, 0, 2);
1.139 paf 785:
786: // ^string.append[string]
787: add_native_method("append", Method::CT_DYNAMIC, _append, 1, 1);
1.146 paf 788:
789: // ^string.base64[] << encode
1.147 paf 790: // ^string:base64[encoded string] << decode
1.146 paf 791: add_native_method("base64", Method::CT_ANY, _base64, 0, 1);
1.167 misha 792:
1.168 misha 793: // ^string.js-escape[]
794: // ^string:js-unescape[escaped%uXXXXstring]
795: add_native_method("js-escape", Method::CT_ANY, _escape, 0, 0);
796: add_native_method("js-unescape", Method::CT_STATIC, _unescape, 1, 1);
1.2 paf 797: }
E-mail: