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