Annotation of parser3/src/classes/string.C, revision 1.236
1.24 paf 1: /** @file
2: Parser: @b string parser class.
3:
1.234 moko 4: Copyright (c) 2001-2017 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.236 ! moko 23: volatile const char * IDENT_STRING_C="$Id: string.C,v 1.235 2017/05/17 14:22:11 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.233 moko 67: r.write(*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.233 moko 90: r.write(*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.233 moko 98: r.write(*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.233 moko 103: r.write(*new VDouble(self_string.as_double()));
1.206 moko 104: } catch(...) { // convert problem
105: if(params.count()>0)
1.233 moko 106: r.write(*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.233 moko 140: r.write(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.233 moko 151: r.write(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.233 moko 157: r.write(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());
1.233 moko 166: r.write(n<length ? string.mid(r.charsets.source(), length-n, length, length) : string);
1.216 moko 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.233 moko 194: r.write(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.233 moko 208: r.write(*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.233 moko 321: r.write(*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: const String& src=GET_SELF(r, VString).string();
1.152 misha 398: int matches_count=0;
1.174 misha 399:
400: if(params_count<3) { // search
401: Table* table=src.match(vregex,
1.64 parser 402: search_action, 0,
1.152 misha 403: matches_count);
1.174 misha 404:
1.152 misha 405: if(table){
1.233 moko 406: r.write(*new VTable(table));
1.152 misha 407: } else {
1.233 moko 408: r.write(*new VInt(matches_count));
1.152 misha 409: }
410:
1.27 paf 411: } else { // replace
1.181 misha 412:
413: Value* replacement_code=0;
414: bool is_junction=false;
415:
416: Value* replacement=¶ms[2];
417: if(replacement->get_junction()){
418: replacement_code=replacement;
419: is_junction=true;
420: } else if(replacement->is_string()){
421: if(replacement->is_defined())
422: replacement_code=replacement;
423: } else if(!replacement->is_void())
424: throw Exception(PARSER_RUNTIME,
425: 0,
426: "replacement option should be junction or string");
1.106 paf 427:
1.192 misha 428: Value* default_code=(params_count==4)?¶ms.as_junction(3, "default value must be code"):0;
429:
1.126 paf 430: String result;
431: VTable* vtable=new VTable;
1.130 paf 432: Replace_action_info info={
433: &r,
434: &src,
435: &result,
436: vtable,
1.181 misha 437: replacement_code
1.130 paf 438: };
1.181 misha 439:
1.191 misha 440: if(is_junction){
1.211 moko 441: Temp_value_element temp(r, *replacement_code->get_junction()->method_frame, match_var_name, vtable);
1.191 misha 442: src.match(vregex, replace_action, &info, matches_count);
443: } else {
444: src.match(vregex, replace_action, &info, matches_count);
445: }
1.181 misha 446:
1.192 misha 447: if(!matches_count && default_code)
448: r.process_write(*default_code);
449: else
1.233 moko 450: r.write(result);
1.27 paf 451: }
1.24 paf 452: }
453:
1.128 paf 454: static void change_case(Request& r, MethodParams&,
1.49 parser 455: String::Change_case_kind kind) {
1.126 paf 456: const String& src=GET_SELF(r, VString).string();
1.49 parser 457:
1.233 moko 458: r.write(src.change_case(r.charsets.source(), kind));
1.49 parser 459: }
1.126 paf 460: static void _upper(Request& r, MethodParams& params) {
461: change_case(r, params, String::CC_UPPER);
1.49 parser 462: }
1.126 paf 463: static void _lower(Request& r, MethodParams& params) {
464: change_case(r, params, String::CC_LOWER);
1.49 parser 465: }
466:
1.65 parser 467: #ifndef DOXYGEN
1.126 paf 468: class String_sql_event_handlers: public SQL_Driver_query_event_handlers {
469: const String& statement_string; const char* statement_cstr;
470: bool got_column;
1.65 parser 471: public:
1.126 paf 472: bool got_cell;
1.208 moko 473: const String* result;
1.126 paf 474: public:
475: String_sql_event_handlers(
476: const String& astatement_string, const char* astatement_cstr):
477: statement_string(astatement_string), statement_cstr(astatement_cstr),
478: got_column(false),
479: got_cell(false),
1.208 moko 480: result(&String::Empty) {}
1.65 parser 481:
1.128 paf 482: bool add_column(SQL_Error& error, const char* /*str*/, size_t /*length*/) {
1.124 paf 483: if(got_column) {
1.153 misha 484: error=SQL_Error(PARSER_RUNTIME,
1.126 paf 485: //statement_string,
1.65 parser 486: "result must contain exactly one column");
1.124 paf 487: return true;
488: }
1.65 parser 489: got_column=true;
1.124 paf 490: return false;
1.65 parser 491: }
1.124 paf 492: bool before_rows(SQL_Error& /*error*/ ) { /* ignore */ return false; }
493: bool add_row(SQL_Error& /*error*/) { /* ignore */ return false; }
1.210 moko 494: bool add_row_cell(SQL_Error& error, const char* str, size_t) {
1.124 paf 495: if(got_cell) {
1.153 misha 496: error=SQL_Error(PARSER_RUNTIME,
1.126 paf 497: //statement_string,
1.65 parser 498: "result must not contain more then one row");
1.124 paf 499: return true;
500: }
1.65 parser 501:
1.124 paf 502: try {
503: got_cell=true;
1.208 moko 504: result=new String(str, String::L_TAINTED /* no length as 0x00 can be inside */ );
1.124 paf 505: return false;
506: } catch(...) {
1.235 moko 507: error=SQL_Error("exception occurred in String_sql_event_handlers::add_row_cell");
1.124 paf 508: return true;
509: }
1.65 parser 510: }
511: };
512: #endif
1.141 paf 513: extern String sql_bind_name;
1.126 paf 514: extern String sql_limit_name;
515: extern String sql_offset_name;
516: extern String sql_default_name;
517: extern String sql_distinct_name;
1.141 paf 518: extern int marshal_binds(HashStringValue& hash, SQL_Driver::Placeholder*& placeholders);
519: extern void unmarshal_bind_updates(HashStringValue& hash, int placeholder_count, SQL_Driver::Placeholder* placeholders);
520:
1.204 moko 521: const String* sql_result_string(Request& r, MethodParams& params, Value*& default_code) {
1.126 paf 522: Value& statement=params.as_junction(0, "statement must be code");
1.53 parser 523:
1.141 paf 524: HashStringValue* bind=0;
1.160 misha 525: ulong limit=SQL_NO_LIMIT;
1.70 parser 526: ulong offset=0;
1.81 parser 527: default_code=0;
1.199 misha 528: if(params.count()>1)
1.205 misha 529: if(HashStringValue* options=params.as_hash(1, "sql options")) {
1.199 misha 530: int valid_options=0;
531: if(Value* vbind=options->get(sql_bind_name)) {
532: valid_options++;
533: bind=vbind->get_hash();
534: }
535: if(Value* vlimit=options->get(sql_limit_name)) {
536: valid_options++;
1.230 moko 537: limit=(ulong)r.process(*vlimit).as_double();
1.199 misha 538: }
539: if(Value* voffset=options->get(sql_offset_name)) {
540: valid_options++;
1.230 moko 541: offset=(ulong)r.process(*voffset).as_double();
1.199 misha 542: }
543: if((default_code=options->get(sql_default_name))) {
544: valid_options++;
545: }
546: if(valid_options!=options->count())
547: throw Exception(PARSER_RUNTIME, 0, CALLED_WITH_INVALID_OPTION);
1.200 misha 548: }
1.70 parser 549:
1.141 paf 550: SQL_Driver::Placeholder* placeholders=0;
551: uint placeholders_count=0;
552: if(bind)
553: placeholders_count=marshal_binds(*bind, placeholders);
554:
1.99 paf 555: const String& statement_string=r.process_to_string(statement);
1.232 moko 556: const char* statement_cstr=statement_string.untaint_cstr(String::L_SQL, r.connection());
1.183 misha 557:
1.126 paf 558: String_sql_event_handlers handlers(statement_string, statement_cstr);
1.160 misha 559:
1.126 paf 560: r.connection()->query(
1.140 paf 561: statement_cstr,
1.142 paf 562: placeholders_count, placeholders,
1.140 paf 563: offset, limit,
1.117 paf 564: handlers,
565: statement_string);
1.53 parser 566:
1.141 paf 567: if(bind)
568: unmarshal_bind_updates(*bind, placeholders_count, placeholders);
569:
1.65 parser 570: if(!handlers.got_cell)
571: return 0; // no lines, caller should return second param[default value]
1.62 parser 572:
1.208 moko 573: return handlers.result;
1.53 parser 574: }
575:
1.126 paf 576: static void _sql(Request& r, MethodParams& params) {
1.53 parser 577:
1.126 paf 578: Value* default_code;
1.204 moko 579: const String* string=sql_result_string(r, params, default_code);
1.62 parser 580: if(!string) {
1.81 parser 581: if(default_code) {
1.99 paf 582: string=&r.process_to_string(*default_code);
1.68 parser 583: } else
1.153 misha 584: throw Exception(PARSER_RUNTIME,
1.126 paf 585: 0,
1.81 parser 586: "produced no result, but no default option specified");
1.62 parser 587: }
1.100 paf 588:
1.233 moko 589: r.write(*string);
1.53 parser 590: }
591:
1.126 paf 592: static void _replace(Request& r, MethodParams& params) {
593: const String& src=GET_SELF(r, VString).string();
1.71 parser 594:
1.201 misha 595: if(params.count()==1) {
596: // ^string.replace[table]
1.205 misha 597: Table* table=params.as_table(0, "param");
1.201 misha 598: Dictionary dict(*table);
1.233 moko 599: r.write(src.replace(dict));
1.201 misha 600: } else {
601: // ^string.replace[from-string;to-string]
602: Dictionary dict(
603: params.as_string(0, "from must be string"),
604: params.as_string(1, "to must be string")
605: );
1.233 moko 606: r.write(src.replace(dict));
1.201 misha 607: }
1.71 parser 608:
609: }
1.79 parser 610:
1.126 paf 611: static void _save(Request& r, MethodParams& params) {
1.189 misha 612: bool do_append=false;
613: Charset* asked_charset=0;
614:
615: size_t file_name_index=0;
1.214 moko 616: if(params.count()>1) {
1.189 misha 617: if(HashStringValue* options=params.as_no_junction(1, "second parameter should be string or hash").get_hash()){
1.205 misha 618: // ^file.save[filespec;$.charset[] $.append(true)]
1.190 misha 619: int valid_options=0;
1.189 misha 620: if(Value* vcharset_name=options->get(PA_CHARSET_NAME)){
1.229 moko 621: asked_charset=&pa_charsets.get(vcharset_name->as_string());
1.189 misha 622: valid_options++;
623: }
624: if(Value* vappend=options->get(MODE_APPEND)){
625: do_append=vappend->as_bool();
626: valid_options++;
627: }
628: if(valid_options != options->count())
1.194 misha 629: throw Exception(PARSER_RUNTIME, 0, CALLED_WITH_INVALID_OPTION);
1.189 misha 630: } else {
1.205 misha 631: // ^file.save[append;filespec]
1.189 misha 632: const String& mode=params.as_string(0, "mode must be string");
633: if(mode==MODE_APPEND){
634: do_append=true;
635: file_name_index++;
636: } else
637: throw Exception(PARSER_RUNTIME,
638: &mode,
639: "unknown mode, must be 'append'");
640: }
1.214 moko 641: }
1.79 parser 642:
1.189 misha 643: const String& file_name=params.as_string(file_name_index, FILE_NAME_MUST_BE_STRING);
1.126 paf 644: const String& src=GET_SELF(r, VString).string();
1.79 parser 645:
1.209 moko 646: String::Body sbody=src.cstr_to_string_body_untaint(String::L_AS_IS, r.connection(false), &r.charsets);
1.87 paf 647:
1.79 parser 648: // write
1.189 misha 649: file_write(r.charsets, r.absolute(file_name), sbody.cstr(), sbody.length(), true, do_append, asked_charset);
1.79 parser 650: }
651:
1.126 paf 652: static void _normalize(Request& r, MethodParams&) {
653: const String& src=GET_SELF(r, VString).string();
654:
1.233 moko 655: r.write(src);
1.109 paf 656: }
657:
1.133 paf 658: static void _trim(Request& r, MethodParams& params) {
659: const String& src=GET_SELF(r, VString).string();
660:
661: String::Trim_kind kind=String::TRIM_BOTH;
1.176 misha 662: size_t params_count=params.count();
1.133 paf 663: const char* chars=0;
1.176 misha 664: if(params_count>0) {
665: const String& skind=params.as_string(0, "'where' must be string");
1.214 moko 666: if(!skind.is_empty()) {
1.162 misha 667: if(skind==TRIM_BOTH_OPTION)
668: kind=String::TRIM_BOTH;
669: else if(skind==TRIM_START_OPTION || skind=="start")
1.137 paf 670: kind=String::TRIM_START;
1.162 misha 671: else if(skind==TRIM_END_OPTION || skind=="end")
1.137 paf 672: kind=String::TRIM_END;
1.218 moko 673: else if(params_count==1)
674: chars=skind.cstr();
1.137 paf 675: else
1.153 misha 676: throw Exception(PARSER_RUNTIME,
1.137 paf 677: &skind,
1.213 moko 678: "'kind' must be one of " TRIM_START_OPTION ", " TRIM_BOTH_OPTION ", " TRIM_END_OPTION);
1.214 moko 679: }
1.133 paf 680:
1.176 misha 681: if(params_count>1) {
1.136 paf 682: const String& schars=params.as_string(1, "'chars' must be string");
1.178 misha 683: if(!schars.is_empty())
1.137 paf 684: chars=schars.cstr();
1.136 paf 685: }
1.133 paf 686: }
687:
1.233 moko 688: r.write(src.trim(kind, chars, &r.charsets.source()));
1.133 paf 689: }
690:
1.146 paf 691: static void _base64(Request& r, MethodParams& params) {
1.219 moko 692: if(&r.get_self() == string_class) {
1.202 misha 693: // decode: ^string:base64[encoded[;$.strict(true|false)]]
1.219 moko 694: const char* cstr=params.count() ? params.as_string(0, PARAMETER_MUST_BE_STRING).cstr() : "";
1.169 misha 695: char* decoded=0;
696: size_t length=0;
1.202 misha 697:
698: bool strict=false;
699: if(params.count() > 1)
700: if(HashStringValue* options=params.as_hash(1)) {
701: int valid_options=0;
702: if(Value* vstrict=options->get(BASE64_STRICT_OPTION_NAME)) {
1.230 moko 703: strict=r.process(*vstrict).as_bool();
1.202 misha 704: valid_options++;
705: }
706: if(valid_options!=options->count())
707: throw Exception(PARSER_RUNTIME, 0, CALLED_WITH_INVALID_OPTION);
708: }
709:
710: pa_base64_decode(cstr, strlen(cstr), decoded, length, strict);
1.169 misha 711: if(decoded && length){
712: if(memchr((const char*)decoded, 0, length))
713: throw Exception(PARSER_RUNTIME,
714: 0,
715: "Invalid \\x00 character found while decode to string. Decode it to file instead.");
716:
717: fix_line_breaks(decoded, length);
1.179 misha 718: if(length)
1.233 moko 719: r.write(*new String(decoded, String::L_TAINTED));
1.169 misha 720: }
1.147 paf 721: } else {
1.169 misha 722: // encode: ^str.base64[]
1.148 paf 723: VString& self=GET_SELF(r, VString);
724: const char* cstr=self.string().cstr();
1.147 paf 725: const char* encoded=pa_base64_encode(cstr, strlen(cstr));
1.233 moko 726: r.write(*new String(encoded, String::L_TAINTED/*once ?param=base64(something) was needed*/));
1.146 paf 727: }
728: }
729:
1.215 moko 730: static void _idna(Request& r, MethodParams& params) {
1.219 moko 731: if(&r.get_self() == string_class) {
1.215 moko 732: // decode: ^string:idna[encoded]
1.219 moko 733: const char* cstr=params.count() ? params.as_string(0, PARAMETER_MUST_BE_STRING).cstr() : "";
1.233 moko 734: r.write(*new String(pa_idna_decode(cstr, r.charsets.source()), String::L_TAINTED));
1.215 moko 735: } else {
736: // encode: ^str.idna[]
737: VString& self=GET_SELF(r, VString);
738: const char* cstr=self.string().cstr();
1.233 moko 739: r.write(*new String(pa_idna_encode(cstr, r.charsets.source()), String::L_TAINTED));
1.215 moko 740: }
741: }
742:
1.220 moko 743: static void _js_escape(Request& r, MethodParams&){
1.167 misha 744: const String& src=GET_SELF(r, VString).string();
1.233 moko 745: r.write(src.escape(r.charsets.source()));
1.167 misha 746: }
747:
1.220 moko 748: static void _js_unescape(Request& r, MethodParams& params){
1.167 misha 749: const String& src=params.as_string(0, PARAMETER_MUST_BE_STRING);
1.193 misha 750: if(const char* result=unescape_chars(src.cstr(), src.length(), &r.charsets.source(), true))
1.233 moko 751: r.write(*new String(result, String::L_TAINTED));
1.167 misha 752: }
753:
1.220 moko 754: static void _unescape(Request& r, MethodParams& params){
755: const String& mode=params.as_string(0, MODE_MUST_NOT_BE_CODE);
756: const String& src=params.as_string(1, PARAMETER_MUST_BE_STRING);
757:
758: Charset* from_charset=&r.charsets.client();
759:
760: if(params.count() > 2)
761: if(HashStringValue* options=params.as_hash(2)) {
762: int valid_options=0;
763: if(Value* vcharset_name=options->get(PA_CHARSET_NAME)){
1.229 moko 764: from_charset=&pa_charsets.get(vcharset_name->as_string());
1.220 moko 765: valid_options++;
766: }
767: if(valid_options!=options->count())
768: throw Exception(PARSER_RUNTIME, 0, CALLED_WITH_INVALID_OPTION);
769: }
770:
771: bool mode_js;
772: if(mode==UNESCAPE_MODE_JS){
773: mode_js=true;
774: } else if(mode==UNESCAPE_MODE_URI){
775: mode_js=false;
776: } else {
1.228 moko 777: throw Exception(PARSER_RUNTIME, &mode, "is invalid mode, must be either '" UNESCAPE_MODE_JS "' or '" UNESCAPE_MODE_URI "'");
1.220 moko 778: }
779:
780: const char* unescaped=unescape_chars(src.cstr(), src.length(), from_charset, mode_js);
781: if(*unescaped){
1.221 moko 782: const String* result=new String(Charset::transcode(unescaped, *from_charset, r.charsets.source()), String::L_TAINTED);
1.233 moko 783: r.write(*result);
1.220 moko 784: }
785: }
786:
1.225 moko 787: static void _contains(Request& r, MethodParams& params) {
788: // empty or whitespace string is hash compatible
789: GET_SELF(r, VString).get_element(params.as_string(0, "key must be string"));
790: // ignoring result as it allways null
1.233 moko 791: r.write(VBool::get(false));
1.225 moko 792: }
793:
1.41 paf 794: // constructor
795:
1.126 paf 796: MString::MString(): Methoded("string") {
1.1 paf 797: // ^string.length[]
1.41 paf 798: add_native_method("length", Method::CT_DYNAMIC, _length, 0, 0);
1.6 paf 799:
1.1 paf 800: // ^string.int[]
1.72 parser 801: // ^string.int(default)
802: add_native_method("int", Method::CT_DYNAMIC, _int, 0, 1);
1.1 paf 803: // ^string.double[]
1.72 parser 804: // ^string.double(default)
805: add_native_method("double", Method::CT_DYNAMIC, _double, 0, 1);
1.151 misha 806: // ^void.bool[]
807: // ^void.bool(default)
808: add_native_method("bool", Method::CT_DYNAMIC, _bool, 0, 1);
1.9 paf 809:
1.165 misha 810: // ^string.format[format]
1.41 paf 811: add_native_method("format", Method::CT_DYNAMIC, _string_format, 1, 1);
1.14 paf 812:
1.15 paf 813: // ^string.left(n)
1.41 paf 814: add_native_method("left", Method::CT_DYNAMIC, _left, 1, 1);
1.15 paf 815: // ^string.right(n)
1.41 paf 816: add_native_method("right", Method::CT_DYNAMIC, _right, 1, 1);
1.165 misha 817: // ^string.mid(p)
1.15 paf 818: // ^string.mid(p;n)
1.82 parser 819: add_native_method("mid", Method::CT_DYNAMIC, _mid, 1, 2);
1.16 paf 820:
821: // ^string.pos[substr]
1.165 misha 822: // ^string.pos[substr](n)
823: add_native_method("pos", Method::CT_DYNAMIC, _pos, 1, 2);
1.17 paf 824:
1.118 paf 825: // ^string.split[delim]
826: // ^string.split[delim][options]
1.156 misha 827: // ^string.split[delim][options][column name]
828: add_native_method("split", Method::CT_DYNAMIC, _split, 1, 3);
1.236 ! moko 829: // old names for backward compatibility
! 830: // ^string.lsplit[delim]
! 831: add_native_method("lsplit", Method::CT_DYNAMIC, _lsplit, 1, 1);
! 832: // ^string.rsplit[delim]
! 833: add_native_method("rsplit", Method::CT_DYNAMIC, _rsplit, 1, 1);
! 834:
1.32 paf 835: // ^string.match[regexp][options]
836: // ^string.match[regexp][options]{replacement-code}
1.192 misha 837: // ^string.match[regexp][options]{replacement-code}{code-if-nothing-is-found}
838: add_native_method("match", Method::CT_DYNAMIC, _match, 1, 4);
1.49 parser 839:
1.165 misha 840: // ^string.upper[]
1.49 parser 841: add_native_method("upper", Method::CT_DYNAMIC, _upper, 0, 0);
1.165 misha 842: // ^string.lower[]
1.49 parser 843: add_native_method("lower", Method::CT_DYNAMIC, _lower, 0, 0);
1.53 parser 844:
1.189 misha 845: // ^string:sql{query}
846: // ^string:sql{query}[options hash]
1.67 parser 847: add_native_method("sql", Method::CT_STATIC, _sql, 1, 2);
1.71 parser 848:
849: // ^string.replace[table]
1.201 misha 850: add_native_method("replace", Method::CT_DYNAMIC, _replace, 1, 2);
1.79 parser 851:
1.189 misha 852: // ^string.save[append][file]
853: // ^string.save[file]
854: // ^string.save[file][$.append(true) $.charset[...]]
1.87 paf 855: add_native_method("save", Method::CT_DYNAMIC, _save, 1, 2);
1.109 paf 856:
1.112 paf 857: // ^string.normalize[]
858: add_native_method("normalize", Method::CT_DYNAMIC, _normalize, 0, 0);
1.133 paf 859:
860: // ^string.trim[[start|both|end][;chars]]
861: add_native_method("trim", Method::CT_DYNAMIC, _trim, 0, 2);
1.139 paf 862:
1.146 paf 863: // ^string.base64[] << encode
1.215 moko 864: // ^string:base64[encoded string] << decode
1.202 misha 865: add_native_method("base64", Method::CT_ANY, _base64, 0, 2);
1.167 misha 866:
1.215 moko 867: // ^string.idna[] << encode
868: // ^string:idna[encoded string] << decode
869: add_native_method("idna", Method::CT_ANY, _idna, 0, 1);
870:
1.168 misha 871: // ^string.js-escape[]
1.220 moko 872: add_native_method("js-escape", Method::CT_DYNAMIC, _js_escape, 0, 0);
1.189 misha 873:
1.168 misha 874: // ^string:js-unescape[escaped%uXXXXstring]
1.220 moko 875: add_native_method("js-unescape", Method::CT_STATIC, _js_unescape, 1, 1);
876:
877: // ^string:unescape[js|uri;escaped;$.charset[...]]
878: add_native_method("unescape", Method::CT_STATIC, _unescape, 2, 3);
1.225 moko 879:
880: // ^string.contains[key] for hash compatibility
881: add_native_method("contains", Method::CT_DYNAMIC, _contains, 1, 1);
1.220 moko 882: }
E-mail: