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