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