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