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