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