Annotation of parser3/src/classes/string.C, revision 1.250
1.24 paf 1: /** @file
2: Parser: @b string parser class.
3:
1.247 moko 4: Copyright (c) 2001-2020 Art. Lebedev Studio (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.43 paf 8: #include "classes.h"
1.126 paf 9: #include "pa_vmethod_frame.h"
1.241 moko 10: #include "pa_base64.h"
1.126 paf 11:
1.1 paf 12: #include "pa_request.h"
13: #include "pa_vdouble.h"
14: #include "pa_vint.h"
1.17 paf 15: #include "pa_vtable.h"
1.25 paf 16: #include "pa_vbool.h"
1.27 paf 17: #include "pa_string.h"
1.53 parser 18: #include "pa_sql_connection.h"
1.71 parser 19: #include "pa_dictionary.h"
1.119 paf 20: #include "pa_vmethod_frame.h"
1.174 misha 21: #include "pa_vregex.h"
1.189 misha 22: #include "pa_charsets.h"
1.1 paf 23:
1.250 ! moko 24: volatile const char * IDENT_STRING_C="$Id: string.C,v 1.249 2020/12/17 20:01:03 moko Exp $";
1.203 moko 25:
1.41 paf 26: // class
27:
1.126 paf 28: class MString: public Methoded {
1.41 paf 29: public:
1.126 paf 30: MString();
1.41 paf 31: };
1.1 paf 32:
1.126 paf 33: // global variable
34:
1.224 moko 35: DECLARE_CLASS_VAR(string, new MString);
1.126 paf 36:
1.196 moko 37: // void class, inherited from string and thus should be inited afterwards
38:
39: class MVoid: public Methoded {
40: public:
41: MVoid();
42: };
43:
44: // void global variable should be after string global variable
45:
1.224 moko 46: DECLARE_CLASS_VAR(void, new MVoid);
1.196 moko 47:
1.126 paf 48: // defines for statics
49:
50: #define MATCH_VAR_NAME "match"
1.162 misha 51: #define TRIM_START_OPTION "left"
52: #define TRIM_END_OPTION "right"
1.133 paf 53: #define TRIM_BOTH_OPTION "both"
1.126 paf 54:
1.189 misha 55: #define MODE_APPEND "append"
56:
1.220 moko 57: #define UNESCAPE_MODE_JS "js"
58: #define UNESCAPE_MODE_URI "uri"
59:
1.126 paf 60: // statics
61:
62: static const String match_var_name(MATCH_VAR_NAME);
63:
1.1 paf 64: // methods
65:
1.126 paf 66: static void _length(Request& r, MethodParams&) {
1.163 misha 67: double result=GET_SELF(r, VString).string().length(r.charsets.source());
1.233 moko 68: r.write(*new VDouble(result));
1.1 paf 69: }
70:
1.126 paf 71: static void _int(Request& r, MethodParams& params) {
72: const String& self_string=GET_SELF(r, VString).string();
1.72 parser 73: int converted;
1.206 moko 74:
75: if(self_string.is_empty()) {
1.144 paf 76: if(params.count()>0)
77: converted=params.as_int(0, "default must be int", r); // (default)
1.84 parser 78: else
1.206 moko 79: throw Exception(PARSER_RUNTIME, 0, "unable to convert empty string without default specified");
80: } else {
81: try {
82: converted=self_string.as_int();
83: } catch(...) { // convert problem
84: if(params.count()>0)
85: converted=params.as_int(0, "default must be int", r); // (default)
86: else
87: rethrow; // we have a problem when no default
88: }
1.72 parser 89: }
1.206 moko 90:
1.233 moko 91: r.write(*new VInt(converted));
1.1 paf 92: }
93:
1.126 paf 94: static void _double(Request& r, MethodParams& params) {
95: const String& self_string=GET_SELF(r, VString).string();
1.206 moko 96:
97: if(self_string.is_empty()) {
1.144 paf 98: if(params.count()>0)
1.233 moko 99: r.write(*new VDouble(params.as_double(0, "default must be double", r))); // (default)
1.84 parser 100: else
1.206 moko 101: throw Exception(PARSER_RUNTIME, 0, "unable to convert empty string without default specified");
102: } else {
103: try {
1.233 moko 104: r.write(*new VDouble(self_string.as_double()));
1.206 moko 105: } catch(...) { // convert problem
106: if(params.count()>0)
1.233 moko 107: r.write(*new VDouble(params.as_double(0, "default must be double", r))); // (default)
1.206 moko 108: else
109: rethrow; // we have a problem when no default
110: }
1.72 parser 111: }
1.1 paf 112: }
113:
1.151 misha 114: static void _bool(Request& r, MethodParams& params) {
115: const String& self_string=GET_SELF(r, VString).string();
116: bool converted;
1.206 moko 117: const char *str=self_string.cstr();
118:
119: if(self_string.is_empty()) {
120: if(params.count()>0)
121: converted=params.as_bool(0, "default must be bool", r); // (default)
122: else
123: throw Exception(PARSER_RUNTIME, 0, "unable to convert empty string without default specified");
1.207 moko 124: } else if( (str[0]=='T' || str[0]=='t') && (str[1]=='R' || str[1]=='r') && (str[2]=='U' || str[2]=='u') &&
1.206 moko 125: (str[3]=='E' || str[3]=='e') && str[4]==0 ) { // "true"
126: converted=true;
1.207 moko 127: } else if( (str[0]=='F' || str[0]=='f') && (str[1]=='A' || str[1]=='a') && (str[2]=='L' || str[2]=='l') &&
1.206 moko 128: (str[3]=='S' || str[3]=='s') && (str[4]=='E' || str[4]=='e') && str[5]==0 ) { // "false"
129: converted=false;
130: } else {
1.158 misha 131: try {
132: converted=self_string.as_bool();
1.206 moko 133: } catch(...) { // convert problem
134: if(params.count()>0)
135: converted=params.as_bool(0, "default must be bool", r); // (default)
136: else
137: rethrow; // we have a problem when no default
1.158 misha 138: }
1.151 misha 139: }
140:
1.233 moko 141: r.write(VBool::get(converted));
1.151 misha 142: }
143:
1.126 paf 144: /*not static*/void _string_format(Request& r, MethodParams& params) {
1.9 paf 145:
1.126 paf 146: Value& fmt_maybe_code=params[0];
1.95 paf 147: // for some time due to stupid {} in original design
1.99 paf 148: const String& fmt=r.process_to_string(fmt_maybe_code);
1.9 paf 149:
1.159 misha 150: const char* buf=format(r.get_self().as_double(), fmt.trim().cstrm());
1.63 parser 151:
1.233 moko 152: r.write(String(buf));
1.9 paf 153: }
1.11 paf 154:
1.126 paf 155: static void _left(Request& r, MethodParams& params) {
1.244 moko 156: int sn=params.as_int(0, "n must be int", r);
1.126 paf 157: const String& string=GET_SELF(r, VString).string();
1.244 moko 158: r.write(sn<0 ? string : string.mid(r.charsets.source(), 0, sn));
1.15 paf 159: }
160:
1.126 paf 161: static void _right(Request& r, MethodParams& params) {
1.244 moko 162: int sn=params.as_int(0, "n must be int", r);
1.216 moko 163: if(sn>0){
1.244 moko 164: size_t n=sn;
1.216 moko 165: const String& string=GET_SELF(r, VString).string();
166: size_t length=string.length(r.charsets.source());
1.233 moko 167: r.write(n<length ? string.mid(r.charsets.source(), length-n, length, length) : string);
1.216 moko 168: }
1.15 paf 169: }
170:
1.126 paf 171: static void _mid(Request& r, MethodParams& params) {
172: const String& string=GET_SELF(r, VString).string();
1.83 parser 173:
1.245 moko 174: int begin=params.as_int(0, "p must be int", r);
175: if(begin<0)
176: throw Exception(PARSER_RUNTIME, 0, "p(%d) must be >=0", begin);
1.138 paf 177:
178: size_t end;
1.164 misha 179: size_t length=0;
1.245 moko 180:
1.138 paf 181: if(params.count()>1) {
1.245 moko 182: int n=params.as_int(1, "n must be int", r);
183: if(n<0)
184: throw Exception(PARSER_RUNTIME, 0, "n(%d) must be >=0", n);
185: end=begin+n;
1.164 misha 186: } else {
187: length=string.length(r.charsets.source());
188: end=length;
189: }
1.163 misha 190:
1.233 moko 191: r.write(string.mid(r.charsets.source(), begin, end, length));
1.15 paf 192: }
193:
1.126 paf 194: static void _pos(Request& r, MethodParams& params) {
195: Value& substr=params.as_no_junction(0, "substr must not be code");
1.16 paf 196:
1.126 paf 197: const String& string=GET_SELF(r, VString).string();
1.245 moko 198: int offset=0;
1.165 misha 199: if(params.count()>1){
200: offset=params.as_int(1, "n must be int", r);
201: if(offset<0)
1.227 moko 202: throw Exception(PARSER_RUNTIME, 0, "n(%d) must be >=0", offset);
1.165 misha 203: }
204:
1.245 moko 205: r.write(*new VInt((int)string.pos(r.charsets.source(), substr.as_string(), offset)));
1.16 paf 206: }
207:
1.250 ! moko 208: struct Split_action_info {
! 209: const String& src;
! 210: ArrayString &result;
! 211: };
! 212:
! 213: static void split_action(Table& , ArrayString* row, int prestart, int prefinish, int poststart, int postfinish, void *info) {
! 214: Split_action_info& ai=*static_cast<Split_action_info *>(info);
! 215: if(row) { // begin&middle
! 216: // piece from last match['prestart'] to beginning of this match['prefinish']
! 217: ai.result += &ai.src.mid(prestart, prefinish);
! 218: } else // end
! 219: if(poststart != postfinish)
! 220: ai.result += &ai.src.mid(poststart, postfinish);
! 221: }
! 222:
1.249 moko 223: static void split_list(Value& delim_value, const String& string, ArrayString& result) {
1.250 ! moko 224: if(Value* value=delim_value.as(VREGEX_TYPE)){
! 225: VRegex *vregex=static_cast<VRegex*>(value);
! 226: vregex->study();
! 227:
! 228: int matches_count=0;
! 229: Split_action_info ai = { string, result };
! 230:
! 231: string.match(vregex, split_action, &ai, matches_count);
! 232: } else
! 233: string.split(result, 0, delim_value.as_string());
1.19 paf 234: }
235:
1.118 paf 236: #define SPLIT_LEFT 0x0001
237: #define SPLIT_RIGHT 0x0010
238: #define SPLIT_HORIZONTAL 0x0100
239: #define SPLIT_VERTICAL 0x1000
240:
1.126 paf 241: static int split_options(const String* options) {
1.118 paf 242: struct Split_option {
1.126 paf 243: const char* keyL;
244: const char* keyU;
1.118 paf 245: int setBit;
246: int checkBit;
247: } split_option[]={
248: {"l", "L", SPLIT_LEFT, SPLIT_RIGHT}, // 0xVHRL
249: {"r", "R", SPLIT_RIGHT, SPLIT_LEFT},
250: {"h", "H", SPLIT_HORIZONTAL, SPLIT_VERTICAL},
251: {"v", "V", SPLIT_VERTICAL, SPLIT_HORIZONTAL},
1.130 paf 252: {0, 0, 0, 0}
1.118 paf 253: };
254:
255: int result=0;
1.126 paf 256: if(options) {
1.118 paf 257: for(Split_option *o=split_option; o->keyL; o++)
1.126 paf 258: if(options->pos(o->keyL)!=STRING_NOT_FOUND
259: || (o->keyU && options->pos(o->keyU)!=STRING_NOT_FOUND)) {
1.118 paf 260: if(result & o->checkBit)
1.238 moko 261: throw Exception(PARSER_RUNTIME, options, "conflicting split options");
1.118 paf 262: result |= o->setBit;
263: }
264: }
265:
266: return result;
267: }
268:
1.156 misha 269: static Table& split_vertical(ArrayString& pieces, bool right, const String* column_name) {
1.126 paf 270: Table::columns_type columns(new ArrayString);
1.156 misha 271: *columns+=column_name;
1.19 paf 272:
1.126 paf 273: Table& table=*new Table(columns, pieces.count());
1.118 paf 274: if(right) { // right
1.126 paf 275: for(int i=pieces.count(); --i>=0; ) {
276: Table::element_type row(new ArrayString);
277: *row+=pieces[i];
278: table+=row;
1.118 paf 279: }
280: } else { // left
1.126 paf 281: Array_iterator<const String*> i(pieces);
1.118 paf 282: while(i.has_next()) {
1.126 paf 283: Table::element_type row(new ArrayString);
284: *row+=i.next();
285: table+=row;
1.118 paf 286: }
1.61 parser 287: }
1.118 paf 288:
1.126 paf 289: return table;
1.19 paf 290: }
291:
1.128 paf 292: static Table& split_horizontal(ArrayString& pieces, bool right) {
1.126 paf 293: Table& table=*new Table(Table::columns_type(0) /* nameless */);
294: Table::element_type row(new ArrayString(pieces.count()));
1.118 paf 295: if(right) { // right
1.131 paf 296: for(int i=pieces.count(); --i>=0; )
1.126 paf 297: *row+=pieces[i];
1.118 paf 298: } else { // left
1.126 paf 299: for(Array_iterator<const String*> i(pieces); i.has_next(); )
300: *row+=i.next();
1.118 paf 301: }
1.126 paf 302: table+=row;
1.118 paf 303:
1.126 paf 304: return table;
1.118 paf 305: }
306:
1.238 moko 307: static void split_with_options(Request& r, MethodParams& params, int bits) {
1.126 paf 308: const String& string=GET_SELF(r, VString).string();
1.176 misha 309: size_t params_count=params.count();
1.19 paf 310:
1.126 paf 311: ArrayString pieces;
1.249 moko 312: split_list(params.as_no_junction(0, "delimiter must not be code"), string, pieces);
1.19 paf 313:
1.118 paf 314: if(!bits) {
1.126 paf 315: const String* options=0;
1.176 misha 316: if(params_count>1)
1.187 misha 317: options=¶ms.as_string(1, OPTIONS_MUST_NOT_BE_CODE);
1.118 paf 318: bits=split_options(options);
319: }
1.21 paf 320:
1.118 paf 321: bool right=(bits & SPLIT_RIGHT) != 0;
322: bool horizontal=(bits & SPLIT_HORIZONTAL) !=0;
1.156 misha 323:
324: const String* column_name=0;
1.176 misha 325: if(params_count>2){
1.156 misha 326: column_name=¶ms.as_string(2, COLUMN_NAME_MUST_BE_STRING);
1.178 misha 327: if (horizontal && !column_name->is_empty())
1.238 moko 328: throw Exception(PARSER_RUNTIME, column_name, "column name can't be specified with horisontal split");
1.156 misha 329: }
1.178 misha 330: if(!column_name || column_name->is_empty())
1.156 misha 331: column_name=new String("piece");
332:
333: Table& table=horizontal?split_horizontal(pieces, right):split_vertical(pieces, right, column_name);
1.17 paf 334:
1.233 moko 335: r.write(*new VTable(&table));
1.118 paf 336: }
1.126 paf 337: static void _split(Request& r, MethodParams& params) {
338: split_with_options(r, params, 0 /* maybe-determine from param #2 */);
1.118 paf 339: }
1.126 paf 340: static void _lsplit(Request& r, MethodParams& params) {
341: split_with_options(r, params, SPLIT_LEFT);
1.118 paf 342: }
1.126 paf 343: static void _rsplit(Request& r, MethodParams& params) {
344: split_with_options(r, params, SPLIT_RIGHT);
1.17 paf 345: }
346:
1.126 paf 347: static void search_action(Table& table, Table::element_type row, int, int, int, int, void *) {
1.28 paf 348: if(row)
349: table+=row;
1.27 paf 350: }
351:
1.74 parser 352: #ifndef DOXYGEN
1.27 paf 353: struct Replace_action_info {
1.181 misha 354: Request* request;
355: const String* src;
356: String* dest;
1.126 paf 357: VTable* vtable;
358: Value* replacement_code;
1.27 paf 359: };
1.74 parser 360: #endif
1.105 paf 361: /// @todo they can do $global[$result] there, getting pointer to later-invalid local var, kill this
1.238 moko 362: static void replace_action(Table& table, ArrayString* row, int prestart, int prefinish, int poststart, int postfinish, void *info) {
1.27 paf 363: Replace_action_info& ai=*static_cast<Replace_action_info *>(info);
1.31 paf 364: if(row) { // begin&middle
1.104 paf 365: // piece from last match['prestart'] to beginning of this match['prefinish']
366: if(prestart!=prefinish)
367: *ai.dest << ai.src->mid(prestart, prefinish);//ai.dest->APPEND_CONST("-");
1.51 parser 368: // store found parts in one-record VTable
1.126 paf 369: if(table.count()) // middle
1.32 paf 370: table.put(0, row);
371: else // begin
1.30 paf 372: table+=row;
1.181 misha 373:
1.30 paf 374: { // execute 'replacement_code' in 'table' context
1.181 misha 375: if(ai.replacement_code){
376: ai.vtable->set_table(table);
377: *ai.dest << ai.request->process_to_string(*ai.replacement_code);
378: }
1.29 paf 379: }
380: } else // end
1.104 paf 381: *ai.dest << ai.src->mid(poststart, postfinish);
1.27 paf 382: }
383:
1.126 paf 384: static void _match(Request& r, MethodParams& params) {
1.174 misha 385: size_t params_count=params.count();
386:
1.126 paf 387: Value& regexp=params.as_no_junction(0, "regexp must not be code");
1.187 misha 388: Value* options=(params_count>1)?¶ms.as_no_junction(1, OPTIONS_MUST_NOT_BE_CODE):0;
1.174 misha 389:
390: VRegex* vregex;
391: VRegexCleaner vrcleaner;
1.126 paf 392:
1.188 misha 393: if(Value* value=regexp.as(VREGEX_TYPE)){
1.174 misha 394: if(options && options->is_defined())
1.237 moko 395: throw Exception(PARSER_RUNTIME, 0, "you can not specify regex-object and options together");
1.174 misha 396: vregex=static_cast<VRegex*>(value);
397: } else {
1.237 moko 398: vregex=new VRegex(r.charsets.source(), ®exp.as_string(), (options) ? (&options->as_string()) : 0);
1.175 misha 399: vregex->study();
1.174 misha 400: vrcleaner.vregex=vregex;
401: }
1.126 paf 402:
403: const String& src=GET_SELF(r, VString).string();
1.152 misha 404: int matches_count=0;
1.174 misha 405:
406: if(params_count<3) { // search
1.237 moko 407: Table* table=src.match(vregex, search_action, 0, matches_count);
1.174 misha 408:
1.152 misha 409: if(table){
1.233 moko 410: r.write(*new VTable(table));
1.152 misha 411: } else {
1.233 moko 412: r.write(*new VInt(matches_count));
1.152 misha 413: }
414:
1.27 paf 415: } else { // replace
1.181 misha 416:
417: Value* replacement_code=0;
418: bool is_junction=false;
419:
420: Value* replacement=¶ms[2];
421: if(replacement->get_junction()){
422: replacement_code=replacement;
423: is_junction=true;
424: } else if(replacement->is_string()){
425: if(replacement->is_defined())
426: replacement_code=replacement;
427: } else if(!replacement->is_void())
1.238 moko 428: throw Exception(PARSER_RUNTIME, 0, "replacement option should be junction or string");
1.106 paf 429:
1.238 moko 430: Value* default_code=(params_count==4) ? ¶ms.as_junction(3, "default value must be code") : 0;
1.192 misha 431:
1.126 paf 432: String result;
433: VTable* vtable=new VTable;
1.130 paf 434: Replace_action_info info={
435: &r,
436: &src,
437: &result,
438: vtable,
1.181 misha 439: replacement_code
1.130 paf 440: };
1.181 misha 441:
1.191 misha 442: if(is_junction){
1.211 moko 443: Temp_value_element temp(r, *replacement_code->get_junction()->method_frame, match_var_name, vtable);
1.191 misha 444: src.match(vregex, replace_action, &info, matches_count);
445: } else {
446: src.match(vregex, replace_action, &info, matches_count);
447: }
1.181 misha 448:
1.192 misha 449: if(!matches_count && default_code)
450: r.process_write(*default_code);
451: else
1.233 moko 452: r.write(result);
1.27 paf 453: }
1.24 paf 454: }
455:
1.238 moko 456: static void change_case(Request& r, MethodParams&, String::Change_case_kind kind) {
1.126 paf 457: const String& src=GET_SELF(r, VString).string();
1.49 parser 458:
1.233 moko 459: r.write(src.change_case(r.charsets.source(), kind));
1.49 parser 460: }
1.126 paf 461: static void _upper(Request& r, MethodParams& params) {
462: change_case(r, params, String::CC_UPPER);
1.49 parser 463: }
1.126 paf 464: static void _lower(Request& r, MethodParams& params) {
465: change_case(r, params, String::CC_LOWER);
1.49 parser 466: }
467:
1.65 parser 468: #ifndef DOXYGEN
1.126 paf 469: class String_sql_event_handlers: public SQL_Driver_query_event_handlers {
470: bool got_column;
1.65 parser 471: public:
1.126 paf 472: bool got_cell;
1.208 moko 473: const String* result;
1.126 paf 474: public:
1.240 moko 475: String_sql_event_handlers():
1.126 paf 476: got_column(false),
477: got_cell(false),
1.208 moko 478: result(&String::Empty) {}
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.240 moko 482: error=SQL_Error("result must contain exactly one column");
1.124 paf 483: return true;
484: }
1.65 parser 485: got_column=true;
1.124 paf 486: return false;
1.65 parser 487: }
1.124 paf 488: bool before_rows(SQL_Error& /*error*/ ) { /* ignore */ return false; }
489: bool add_row(SQL_Error& /*error*/) { /* ignore */ return false; }
1.210 moko 490: bool add_row_cell(SQL_Error& error, const char* str, size_t) {
1.124 paf 491: if(got_cell) {
1.240 moko 492: error=SQL_Error("result must not contain more then one row");
1.124 paf 493: return true;
494: }
495: try {
496: got_cell=true;
1.208 moko 497: result=new String(str, String::L_TAINTED /* no length as 0x00 can be inside */ );
1.124 paf 498: return false;
499: } catch(...) {
1.235 moko 500: error=SQL_Error("exception occurred in String_sql_event_handlers::add_row_cell");
1.124 paf 501: return true;
502: }
1.65 parser 503: }
504: };
505: #endif
1.239 moko 506:
1.141 paf 507: extern int marshal_binds(HashStringValue& hash, SQL_Driver::Placeholder*& placeholders);
508: extern void unmarshal_bind_updates(HashStringValue& hash, int placeholder_count, SQL_Driver::Placeholder* placeholders);
509:
1.204 moko 510: const String* sql_result_string(Request& r, MethodParams& params, Value*& default_code) {
1.126 paf 511: Value& statement=params.as_junction(0, "statement must be code");
1.53 parser 512:
1.141 paf 513: HashStringValue* bind=0;
1.160 misha 514: ulong limit=SQL_NO_LIMIT;
1.70 parser 515: ulong offset=0;
1.81 parser 516: default_code=0;
1.199 misha 517: if(params.count()>1)
1.205 misha 518: if(HashStringValue* options=params.as_hash(1, "sql options")) {
1.199 misha 519: int valid_options=0;
520: if(Value* vbind=options->get(sql_bind_name)) {
521: valid_options++;
522: bind=vbind->get_hash();
523: }
524: if(Value* vlimit=options->get(sql_limit_name)) {
525: valid_options++;
1.230 moko 526: limit=(ulong)r.process(*vlimit).as_double();
1.199 misha 527: }
528: if(Value* voffset=options->get(sql_offset_name)) {
529: valid_options++;
1.230 moko 530: offset=(ulong)r.process(*voffset).as_double();
1.199 misha 531: }
532: if((default_code=options->get(sql_default_name))) {
533: valid_options++;
534: }
535: if(valid_options!=options->count())
536: throw Exception(PARSER_RUNTIME, 0, CALLED_WITH_INVALID_OPTION);
1.200 misha 537: }
1.70 parser 538:
1.141 paf 539: SQL_Driver::Placeholder* placeholders=0;
540: uint placeholders_count=0;
541: if(bind)
542: placeholders_count=marshal_binds(*bind, placeholders);
543:
1.99 paf 544: const String& statement_string=r.process_to_string(statement);
1.232 moko 545: const char* statement_cstr=statement_string.untaint_cstr(String::L_SQL, r.connection());
1.183 misha 546:
1.240 moko 547: String_sql_event_handlers handlers;
1.160 misha 548:
1.240 moko 549: r.connection()->query(statement_cstr, placeholders_count, placeholders, offset, limit, handlers, 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.208 moko 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: Value* default_code;
1.204 moko 563: const String* string=sql_result_string(r, params, default_code);
1.62 parser 564: if(!string) {
1.81 parser 565: if(default_code) {
1.99 paf 566: string=&r.process_to_string(*default_code);
1.68 parser 567: } else
1.238 moko 568: throw Exception(PARSER_RUNTIME, 0, "produced no result, but no default option specified");
1.62 parser 569: }
1.100 paf 570:
1.233 moko 571: r.write(*string);
1.53 parser 572: }
573:
1.126 paf 574: static void _replace(Request& r, MethodParams& params) {
575: const String& src=GET_SELF(r, VString).string();
1.71 parser 576:
1.201 misha 577: if(params.count()==1) {
578: // ^string.replace[table]
1.248 moko 579: if(Table* table=params.as_table(0, "param")){
580: Dictionary dict(*table);
581: r.write(src.replace(dict));
582: } else
583: r.write(src);
1.201 misha 584: } else {
585: // ^string.replace[from-string;to-string]
1.238 moko 586: Dictionary dict(params.as_string(0, "from must be string"), params.as_string(1, "to must be string"));
1.233 moko 587: r.write(src.replace(dict));
1.201 misha 588: }
1.71 parser 589:
590: }
1.79 parser 591:
1.126 paf 592: static void _save(Request& r, MethodParams& params) {
1.189 misha 593: bool do_append=false;
594: Charset* asked_charset=0;
595:
596: size_t file_name_index=0;
1.214 moko 597: if(params.count()>1) {
1.189 misha 598: if(HashStringValue* options=params.as_no_junction(1, "second parameter should be string or hash").get_hash()){
1.205 misha 599: // ^file.save[filespec;$.charset[] $.append(true)]
1.190 misha 600: int valid_options=0;
1.189 misha 601: if(Value* vcharset_name=options->get(PA_CHARSET_NAME)){
1.229 moko 602: asked_charset=&pa_charsets.get(vcharset_name->as_string());
1.189 misha 603: valid_options++;
604: }
605: if(Value* vappend=options->get(MODE_APPEND)){
606: do_append=vappend->as_bool();
607: valid_options++;
608: }
609: if(valid_options != options->count())
1.194 misha 610: throw Exception(PARSER_RUNTIME, 0, CALLED_WITH_INVALID_OPTION);
1.189 misha 611: } else {
1.205 misha 612: // ^file.save[append;filespec]
1.189 misha 613: const String& mode=params.as_string(0, "mode must be string");
614: if(mode==MODE_APPEND){
615: do_append=true;
616: file_name_index++;
617: } else
618: throw Exception(PARSER_RUNTIME,
619: &mode,
620: "unknown mode, must be 'append'");
621: }
1.214 moko 622: }
1.79 parser 623:
1.189 misha 624: const String& file_name=params.as_string(file_name_index, FILE_NAME_MUST_BE_STRING);
1.126 paf 625: const String& src=GET_SELF(r, VString).string();
1.79 parser 626:
1.209 moko 627: String::Body sbody=src.cstr_to_string_body_untaint(String::L_AS_IS, r.connection(false), &r.charsets);
1.87 paf 628:
1.79 parser 629: // write
1.246 moko 630: file_write(r.charsets, r.full_disk_path(file_name), sbody.cstr(), sbody.length(), true, do_append, asked_charset);
1.79 parser 631: }
632:
1.126 paf 633: static void _normalize(Request& r, MethodParams&) {
634: const String& src=GET_SELF(r, VString).string();
635:
1.233 moko 636: r.write(src);
1.109 paf 637: }
638:
1.133 paf 639: static void _trim(Request& r, MethodParams& params) {
640: const String& src=GET_SELF(r, VString).string();
641:
642: String::Trim_kind kind=String::TRIM_BOTH;
1.176 misha 643: size_t params_count=params.count();
1.133 paf 644: const char* chars=0;
1.176 misha 645: if(params_count>0) {
646: const String& skind=params.as_string(0, "'where' must be string");
1.214 moko 647: if(!skind.is_empty()) {
1.162 misha 648: if(skind==TRIM_BOTH_OPTION)
649: kind=String::TRIM_BOTH;
650: else if(skind==TRIM_START_OPTION || skind=="start")
1.137 paf 651: kind=String::TRIM_START;
1.162 misha 652: else if(skind==TRIM_END_OPTION || skind=="end")
1.137 paf 653: kind=String::TRIM_END;
1.218 moko 654: else if(params_count==1)
655: chars=skind.cstr();
1.137 paf 656: else
1.238 moko 657: throw Exception(PARSER_RUNTIME, &skind, "'kind' must be one of " TRIM_START_OPTION ", " TRIM_BOTH_OPTION ", " TRIM_END_OPTION);
1.214 moko 658: }
1.133 paf 659:
1.176 misha 660: if(params_count>1) {
1.136 paf 661: const String& schars=params.as_string(1, "'chars' must be string");
1.178 misha 662: if(!schars.is_empty())
1.137 paf 663: chars=schars.cstr();
1.136 paf 664: }
1.133 paf 665: }
666:
1.233 moko 667: r.write(src.trim(kind, chars, &r.charsets.source()));
1.133 paf 668: }
669:
1.241 moko 670: Base64Options base64_encode_options(Request& r, HashStringValue* options) {
671: Base64Options result;
672: if(options) {
673: int valid_options=0;
674: for(HashStringValue::Iterator i(*options); i; i.next()) {
675: String::Body key=i.key();
676: Value* value=i.value();
677: if(key == "pad") {
678: result.pad=r.process(*value).as_bool();
679: valid_options++;
680: } else if(key == "wrap") {
681: result.wrap=r.process(*value).as_bool();
682: valid_options++;
683: } else if(key == "url-safe") {
684: if(r.process(*value).as_bool())
685: result.set_url_safe_abc();
686: valid_options++;
687: }
688: }
689:
690: if(valid_options != options->count())
691: throw Exception(PARSER_RUNTIME, 0, CALLED_WITH_INVALID_OPTION);
692: }
693: return result;
694: }
695:
696: Base64Options base64_decode_options(Request& r, HashStringValue* options) {
697: Base64Options result;
698: if(options) {
699: int valid_options=0;
700: for(HashStringValue::Iterator i(*options); i; i.next() ) {
701: String::Body key=i.key();
702: Value* value=i.value();
703: if(key == "pad") {
704: result.pad=r.process(*value).as_bool();
705: valid_options++;
706: } else if(key == "strict") {
707: result.strict=r.process(*value).as_bool();
708: valid_options++;
709: } else if(key == "url-safe") {
710: if(r.process(*value).as_bool())
711: result.set_url_safe_abc();
712: valid_options++;
713: }
714: }
715:
716: if(valid_options != options->count())
717: throw Exception(PARSER_RUNTIME, 0, CALLED_WITH_INVALID_OPTION);
718: }
719: return result;
720: }
721:
1.146 paf 722: static void _base64(Request& r, MethodParams& params) {
1.219 moko 723: if(&r.get_self() == string_class) {
1.241 moko 724: // decode: ^string:base64[encoded[;options]]
1.219 moko 725: const char* cstr=params.count() ? params.as_string(0, PARAMETER_MUST_BE_STRING).cstr() : "";
1.241 moko 726: Base64Options options = base64_decode_options(r, params.count() > 1 ? params.as_hash(1) : NULL);
727:
1.169 misha 728: char* decoded=0;
1.243 moko 729: size_t length=pa_base64_decode(cstr, strlen(cstr), decoded, options);
1.202 misha 730:
1.169 misha 731: if(decoded && length){
1.242 moko 732: if(memchr(decoded, 0, length))
733: throw Exception(PARSER_RUNTIME, 0, "Invalid \\x00 character found while decoding to string. Decode to file instead.");
1.169 misha 734:
735: fix_line_breaks(decoded, length);
1.242 moko 736:
1.179 misha 737: if(length)
1.233 moko 738: r.write(*new String(decoded, String::L_TAINTED));
1.169 misha 739: }
1.147 paf 740: } else {
1.241 moko 741: // encode: ^str.base64[options]
1.148 paf 742: VString& self=GET_SELF(r, VString);
743: const char* cstr=self.string().cstr();
1.241 moko 744: Base64Options options = base64_encode_options(r, params.count() ? params.as_hash(0) : NULL);
745: const char* encoded=pa_base64_encode(cstr, strlen(cstr), options);
746: r.write(*new String(encoded, String::L_TAINTED /*once ?param=base64(something) was needed*/ ));
1.146 paf 747: }
748: }
749:
1.215 moko 750: static void _idna(Request& r, MethodParams& params) {
1.219 moko 751: if(&r.get_self() == string_class) {
1.215 moko 752: // decode: ^string:idna[encoded]
1.219 moko 753: const char* cstr=params.count() ? params.as_string(0, PARAMETER_MUST_BE_STRING).cstr() : "";
1.233 moko 754: r.write(*new String(pa_idna_decode(cstr, r.charsets.source()), String::L_TAINTED));
1.215 moko 755: } else {
756: // encode: ^str.idna[]
757: VString& self=GET_SELF(r, VString);
758: const char* cstr=self.string().cstr();
1.233 moko 759: r.write(*new String(pa_idna_encode(cstr, r.charsets.source()), String::L_TAINTED));
1.215 moko 760: }
761: }
762:
1.220 moko 763: static void _js_escape(Request& r, MethodParams&){
1.167 misha 764: const String& src=GET_SELF(r, VString).string();
1.233 moko 765: r.write(src.escape(r.charsets.source()));
1.167 misha 766: }
767:
1.220 moko 768: static void _js_unescape(Request& r, MethodParams& params){
1.167 misha 769: const String& src=params.as_string(0, PARAMETER_MUST_BE_STRING);
1.193 misha 770: if(const char* result=unescape_chars(src.cstr(), src.length(), &r.charsets.source(), true))
1.233 moko 771: r.write(*new String(result, String::L_TAINTED));
1.167 misha 772: }
773:
1.220 moko 774: static void _unescape(Request& r, MethodParams& params){
775: const String& mode=params.as_string(0, MODE_MUST_NOT_BE_CODE);
776: const String& src=params.as_string(1, PARAMETER_MUST_BE_STRING);
777:
778: Charset* from_charset=&r.charsets.client();
779:
780: if(params.count() > 2)
781: if(HashStringValue* options=params.as_hash(2)) {
782: int valid_options=0;
783: if(Value* vcharset_name=options->get(PA_CHARSET_NAME)){
1.229 moko 784: from_charset=&pa_charsets.get(vcharset_name->as_string());
1.220 moko 785: valid_options++;
786: }
787: if(valid_options!=options->count())
788: throw Exception(PARSER_RUNTIME, 0, CALLED_WITH_INVALID_OPTION);
789: }
790:
791: bool mode_js;
792: if(mode==UNESCAPE_MODE_JS){
793: mode_js=true;
794: } else if(mode==UNESCAPE_MODE_URI){
795: mode_js=false;
796: } else {
1.228 moko 797: throw Exception(PARSER_RUNTIME, &mode, "is invalid mode, must be either '" UNESCAPE_MODE_JS "' or '" UNESCAPE_MODE_URI "'");
1.220 moko 798: }
799:
800: const char* unescaped=unescape_chars(src.cstr(), src.length(), from_charset, mode_js);
801: if(*unescaped){
1.221 moko 802: const String* result=new String(Charset::transcode(unescaped, *from_charset, r.charsets.source()), String::L_TAINTED);
1.233 moko 803: r.write(*result);
1.220 moko 804: }
805: }
806:
1.225 moko 807: static void _contains(Request& r, MethodParams& params) {
808: // empty or whitespace string is hash compatible
809: GET_SELF(r, VString).get_element(params.as_string(0, "key must be string"));
810: // ignoring result as it allways null
1.233 moko 811: r.write(VBool::get(false));
1.225 moko 812: }
813:
1.41 paf 814: // constructor
815:
1.126 paf 816: MString::MString(): Methoded("string") {
1.1 paf 817: // ^string.length[]
1.41 paf 818: add_native_method("length", Method::CT_DYNAMIC, _length, 0, 0);
1.6 paf 819:
1.1 paf 820: // ^string.int[]
1.72 parser 821: // ^string.int(default)
822: add_native_method("int", Method::CT_DYNAMIC, _int, 0, 1);
1.1 paf 823: // ^string.double[]
1.72 parser 824: // ^string.double(default)
825: add_native_method("double", Method::CT_DYNAMIC, _double, 0, 1);
1.151 misha 826: // ^void.bool[]
827: // ^void.bool(default)
828: add_native_method("bool", Method::CT_DYNAMIC, _bool, 0, 1);
1.9 paf 829:
1.165 misha 830: // ^string.format[format]
1.41 paf 831: add_native_method("format", Method::CT_DYNAMIC, _string_format, 1, 1);
1.14 paf 832:
1.15 paf 833: // ^string.left(n)
1.41 paf 834: add_native_method("left", Method::CT_DYNAMIC, _left, 1, 1);
1.15 paf 835: // ^string.right(n)
1.41 paf 836: add_native_method("right", Method::CT_DYNAMIC, _right, 1, 1);
1.165 misha 837: // ^string.mid(p)
1.15 paf 838: // ^string.mid(p;n)
1.82 parser 839: add_native_method("mid", Method::CT_DYNAMIC, _mid, 1, 2);
1.16 paf 840:
841: // ^string.pos[substr]
1.165 misha 842: // ^string.pos[substr](n)
843: add_native_method("pos", Method::CT_DYNAMIC, _pos, 1, 2);
1.17 paf 844:
1.118 paf 845: // ^string.split[delim]
846: // ^string.split[delim][options]
1.156 misha 847: // ^string.split[delim][options][column name]
848: add_native_method("split", Method::CT_DYNAMIC, _split, 1, 3);
1.236 moko 849: // old names for backward compatibility
850: // ^string.lsplit[delim]
851: add_native_method("lsplit", Method::CT_DYNAMIC, _lsplit, 1, 1);
852: // ^string.rsplit[delim]
853: add_native_method("rsplit", Method::CT_DYNAMIC, _rsplit, 1, 1);
854:
1.32 paf 855: // ^string.match[regexp][options]
856: // ^string.match[regexp][options]{replacement-code}
1.192 misha 857: // ^string.match[regexp][options]{replacement-code}{code-if-nothing-is-found}
858: add_native_method("match", Method::CT_DYNAMIC, _match, 1, 4);
1.49 parser 859:
1.165 misha 860: // ^string.upper[]
1.49 parser 861: add_native_method("upper", Method::CT_DYNAMIC, _upper, 0, 0);
1.165 misha 862: // ^string.lower[]
1.49 parser 863: add_native_method("lower", Method::CT_DYNAMIC, _lower, 0, 0);
1.53 parser 864:
1.189 misha 865: // ^string:sql{query}
866: // ^string:sql{query}[options hash]
1.67 parser 867: add_native_method("sql", Method::CT_STATIC, _sql, 1, 2);
1.71 parser 868:
869: // ^string.replace[table]
1.201 misha 870: add_native_method("replace", Method::CT_DYNAMIC, _replace, 1, 2);
1.79 parser 871:
1.189 misha 872: // ^string.save[append][file]
873: // ^string.save[file]
874: // ^string.save[file][$.append(true) $.charset[...]]
1.87 paf 875: add_native_method("save", Method::CT_DYNAMIC, _save, 1, 2);
1.109 paf 876:
1.112 paf 877: // ^string.normalize[]
878: add_native_method("normalize", Method::CT_DYNAMIC, _normalize, 0, 0);
1.133 paf 879:
880: // ^string.trim[[start|both|end][;chars]]
881: add_native_method("trim", Method::CT_DYNAMIC, _trim, 0, 2);
1.139 paf 882:
1.146 paf 883: // ^string.base64[] << encode
1.215 moko 884: // ^string:base64[encoded string] << decode
1.202 misha 885: add_native_method("base64", Method::CT_ANY, _base64, 0, 2);
1.167 misha 886:
1.215 moko 887: // ^string.idna[] << encode
888: // ^string:idna[encoded string] << decode
889: add_native_method("idna", Method::CT_ANY, _idna, 0, 1);
890:
1.168 misha 891: // ^string.js-escape[]
1.220 moko 892: add_native_method("js-escape", Method::CT_DYNAMIC, _js_escape, 0, 0);
1.189 misha 893:
1.168 misha 894: // ^string:js-unescape[escaped%uXXXXstring]
1.220 moko 895: add_native_method("js-unescape", Method::CT_STATIC, _js_unescape, 1, 1);
896:
897: // ^string:unescape[js|uri;escaped;$.charset[...]]
898: add_native_method("unescape", Method::CT_STATIC, _unescape, 2, 3);
1.225 moko 899:
900: // ^string.contains[key] for hash compatibility
901: add_native_method("contains", Method::CT_DYNAMIC, _contains, 1, 1);
1.220 moko 902: }
E-mail: