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