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