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