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