Annotation of parser3/src/classes/string.C, revision 1.125.2.15.2.21
1.24 paf 1: /** @file
2: Parser: @b string parser class.
3:
1.125.2.1 paf 4: Copyright (c) 2001-2003 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.125.2.15.2. 1(paf 8:3): static const char* IDENT_STRING_C="$Date: 2003/04/03 10:48:24 $";
1.1 paf 9:
1.43 paf 10: #include "classes.h"
1.125.2.4 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.125.2.9 paf 25: class MString: public Methoded {
1.41 paf 26: public:
1.125.2.7 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.125.2.7 paf 32: // global variable
33:
1.125.2.12 paf 34: DECLARE_CLASS_VAR(string, new MString, 0);
1.125.2.7 paf 35:
1.125.2.8 paf 36: // defines for statics
37:
38: #define MATCH_VAR_NAME "match"
39:
40: // statics
41:
1.125.2.15.2. 1(paf 42:3): static const String match_var_name(MATCH_VAR_NAME);
1.125.2.8 paf 43:
1.1 paf 44: // methods
45:
1.125.2.15.2. 0(paf 46:3): static void _length(Request& r, MethodParams&) {
3(paf 47:3): double result=GET_SELF(r, VString).string().length();
48:3): r.write_no_lang(*new VDouble(result));
1.1 paf 49: }
50:
1.125.2.15.2. 0(paf 51:3): static void _int(Request& r, MethodParams& params) {
3(paf 52:3): const String& self_string=GET_SELF(r, VString).string();
1.72 parser 53: int converted;
1.125.2.15.2. 0(paf 54:3): Value* default_code=params.count()>0?¶ms.as_junction(0, "default must be int")
(paf 55:): :0; // (default)
1.84 parser 56: try {
1.125.2.15.2. 3(paf 57:3): if(self_string.is_empty())
1.121 paf 58: throw Exception("parser.runtime",
1.125.2.15.2. 3(paf 59:3): 0,
1.121 paf 60: "parameter is empty string, error converting");
1.125.2.15.2. 3(paf 61:3): converted=self_string.as_int();
1.84 parser 62: } catch(...) { // convert problem
1.125.2.15.2. 3(paf 63:3): if(default_code)
64:3): converted=r.process_to_value(*default_code).as_int();
1.84 parser 65: else
1.125.2.15.2. 3(paf 66:3): rethrow; // we have a problem when no default
1.72 parser 67: }
1.125.2.15.2. 3(paf 68:3): r.write_no_lang(*new VInt(converted));
1.1 paf 69: }
70:
1.125.2.15.2. 0(paf 71:3): static void _double(Request& r, MethodParams& params) {
(paf 72:): const String& self_string=GET_SELF(r, VString).string();
1.72 parser 73: double converted;
1.125.2.15.2. 0(paf 74:3): Value* default_code=params.count()>0?¶ms.as_junction(0, "default must be double")
(paf 75:): :0; // (default)
1.84 parser 76: try {
1.125.2.15.2. 3(paf 77:3): if(self_string.is_empty())
1.121 paf 78: throw Exception("parser.runtime",
1.125.2.15.2. 3(paf 79:3): 0,
1.121 paf 80: "parameter is empty string, error converting");
1.125.2.15.2. 3(paf 81:3): converted=self_string.as_double();
1.84 parser 82: } catch(...) { // convert problem
1.125.2.15.2. 3(paf 83:3): if(default_code)
84:3): converted=r.process_to_value(*default_code).as_double();
1.84 parser 85: else
1.125.2.15.2. 3(paf 86:3): rethrow; // we have a problem when no default
1.72 parser 87: }
88:
1.125.2.15.2. 3(paf 89:3): r.write_no_lang(*new VDouble(converted));
1.1 paf 90: }
91:
1.125.2.15.2. 0(paf 92:3): /*not static*/void _string_format(Request& r, MethodParams& params) {
1.9 paf 93:
1.125.2.15.2. 1(paf 94:3): Value& fmt_maybe_code=params[0];
1.95 paf 95: // for some time due to stupid {} in original design
1.125.2.15.2. (paf 96:): const String& fmt=r.process_to_string(fmt_maybe_code);
1.9 paf 97:
1.125.2.15.2. 3(paf 98:3): const char* buf=format(r.get_self().as_double(), fmt.cstrm());
1.63 parser 99:
1.125.2.15.2. 3(paf 100:3): r.write_no_lang(String(buf));
1.9 paf 101: }
1.11 paf 102:
1.125.2.15.2. 0(paf 103:3): static void _left(Request& r, MethodParams& params) {
104:3): size_t n=(size_t)params.as_int(0, "n must be int", r);
1.15 paf 105:
1.125.2.15.2. (paf 106:): const String& string=GET_SELF(r, VString).string();
3(paf 107:3): r.write_assign_lang(string.mid(0, n));
1.15 paf 108: }
109:
1.125.2.15.2. 0(paf 110:3): static void _right(Request& r, MethodParams& params) {
111:3): size_t n=(size_t)params.as_int(0, "n must be int", r);
1.15 paf 112:
1.125.2.15.2. (paf 113:): const String& string=GET_SELF(r, VString).string();
3(paf 114:3): r.write_assign_lang(string.mid(string.length()-n, string.length()));
1.15 paf 115: }
116:
1.125.2.15.2. 0(paf 117:3): static void _mid(Request& r, MethodParams& params) {
(paf 118:): const String& string=GET_SELF(r, VString).string();
1.83 parser 119:
1.125.2.15.2. 0(paf 120:3): size_t p=(size_t)max(0, params.as_int(0, "p must be int", r));
121:3): size_t n=params.count()>1?
122:3): (size_t)max(0, params.as_int(1, "n must be int", r)):string.length();
1.15 paf 123:
1.125.2.15.2. 3(paf 124:3): r.write_assign_lang(string.mid(p, p+n));
1.15 paf 125: }
126:
1.125.2.15.2. 0(paf 127:3): static void _pos(Request& r, MethodParams& params) {
128:3): Value& substr=params.as_no_junction(0, "substr must not be code");
1.16 paf 129:
1.125.2.15.2. (paf 130:): const String& string=GET_SELF(r, VString).string();
3(paf 131:3): r.write_assign_lang(*new VInt(string.pos(substr.as_string())));
1.16 paf 132: }
133:
1.125.2.15.2. (paf 134:): static void split_list(Request& r,
0(paf 135:3): MethodParams& params, int paramIndex,
3(paf 136:3): const String& string,
137:3): ArrayString& result) {
0(paf 138:3): Value& delim_value=params.as_no_junction(paramIndex, "delimiter must not be code");
1.23 paf 139:
1.125.2.15.2. 3(paf 140:3): size_t pos_after=0;
141:3): string.split(result, pos_after, delim_value.as_string());
1.19 paf 142: }
143:
1.118 paf 144: #define SPLIT_LEFT 0x0001
145: #define SPLIT_RIGHT 0x0010
146: #define SPLIT_HORIZONTAL 0x0100
147: #define SPLIT_VERTICAL 0x1000
148:
1.125.2.15.2. 3(paf 149:3): static int split_options(const String* options) {
1.118 paf 150: struct Split_option {
1.125.2.1 paf 151: const char* keyL;
152: const char* keyU;
1.118 paf 153: int setBit;
154: int checkBit;
155: } split_option[]={
156: {"l", "L", SPLIT_LEFT, SPLIT_RIGHT}, // 0xVHRL
157: {"r", "R", SPLIT_RIGHT, SPLIT_LEFT},
158: {"h", "H", SPLIT_HORIZONTAL, SPLIT_VERTICAL},
159: {"v", "V", SPLIT_VERTICAL, SPLIT_HORIZONTAL},
160: {0}
161: };
162:
163: int result=0;
1.125.2.15.2. 3(paf 164:3): if(options) {
1.118 paf 165: for(Split_option *o=split_option; o->keyL; o++)
166: if(options->pos(o->keyL)>=0
167: || (o->keyU && options->pos(o->keyU)>=0)) {
168: if(result & o->checkBit)
169: throw Exception("parser.runtime",
170: options,
171: "conflicting split options");
172: result |= o->setBit;
173: }
174: }
175:
176: return result;
177: }
178:
1.125.2.15.2. 3(paf 179:3): static Table& split_vertical(Request& r, ArrayString& pieces, bool right) {
1.61 parser 180:
1.125.2.7 paf 181: Table::columns_type columns(new ArrayString);
1.125.2.15.2. 3(paf 182:3): *columns+=new String("piece");
1.19 paf 183:
1.125.2.15.2. 3(paf 184:3): Table& table=*new Table(columns, pieces.count());
1.118 paf 185: if(right) { // right
1.125.2.7 paf 186: for(int i=pieces.count(); --i>=0; ) {
187: Table::element_type row(new ArrayString);
188: *row+=pieces[i];
1.125.2.15.2. 3(paf 189:3): table+=row;
1.118 paf 190: }
191: } else { // left
1.125.2.15.2. 3(paf 192:3): Array_iterator<const String*> i(pieces);
1.118 paf 193: while(i.has_next()) {
1.125.2.7 paf 194: Table::element_type row(new ArrayString);
195: *row+=i.next();
1.125.2.15.2. 3(paf 196:3): table+=row;
1.118 paf 197: }
1.61 parser 198: }
1.118 paf 199:
1.125.2.7 paf 200: return table;
1.19 paf 201: }
202:
1.125.2.15.2. 3(paf 203:3): static Table& split_horizontal(Request& r, ArrayString& pieces, bool right) {
204:3): Table& table=*new Table(Table::columns_type(0) /* nameless */);
1.125.2.7 paf 205: Table::element_type row(new ArrayString(pieces.count()));
1.118 paf 206: if(right) { // right
1.125.2.15.2. 7(paf 207:3): for(size_t i=pieces.count(); --i>=0; )
1.125.2.7 paf 208: *row+=pieces[i];
1.118 paf 209: } else { // left
1.125.2.15.2. 7(paf 210:3): for(Array_iterator<const String*> i(pieces); i.has_next(); )
1.125.2.7 paf 211: *row+=i.next();
1.118 paf 212: }
1.125.2.15.2. 3(paf 213:3): table+=row;
1.118 paf 214:
1.125.2.7 paf 215: return table;
1.118 paf 216: }
217:
1.125.2.15.2. 0(paf 218:3): static void split_with_options(Request& r, MethodParams& params,
1.118 paf 219: int bits) {
1.125.2.15.2. (paf 220:): const String& string=GET_SELF(r, VString).string();
1.19 paf 221:
1.125.2.7 paf 222: ArrayString pieces;
1.125.2.15.2. 3(paf 223:3): split_list(r, params, 0, string, pieces);
1.19 paf 224:
1.118 paf 225: if(!bits) {
1.125.2.15.2. 3(paf 226:3): const String* options=0;
0(paf 227:3): if(params.count()>1)
228:3): options=¶ms.as_string(1, "options must not be code");
3(paf 229:3):
1.118 paf 230: bits=split_options(options);
231: }
1.21 paf 232:
1.118 paf 233: bool right=(bits & SPLIT_RIGHT) != 0;
234: bool horizontal=(bits & SPLIT_HORIZONTAL) !=0;
1.125.2.15.2. 3(paf 235:3): Table& table=horizontal?split_horizontal(r, pieces, right)
236:3): :split_vertical(r, pieces, right);
1.17 paf 237:
1.125.2.15.2. 3(paf 238:3): r.write_no_lang(*new VTable(&table));
1.118 paf 239: }
1.125.2.15.2. 0(paf 240:3): static void _split(Request& r, MethodParams& params) {
3(paf 241:3): split_with_options(r, params, 0 /* maybe-determine from param #2 */);
1.118 paf 242: }
1.125.2.15.2. 0(paf 243:3): static void _lsplit(Request& r, MethodParams& params) {
3(paf 244:3): split_with_options(r, params, SPLIT_LEFT);
1.118 paf 245: }
1.125.2.15.2. 0(paf 246:3): static void _rsplit(Request& r, MethodParams& params) {
3(paf 247:3): split_with_options(r, params, SPLIT_RIGHT);
1.17 paf 248: }
249:
1.125.2.15.2. 4(paf 250:3): static void search_action(Table& table, Table::element_type row, int, int, int, int, void *) {
1.28 paf 251: if(row)
1.125.2.15.2. 4(paf 252:3): table+=row;
1.27 paf 253: }
254:
1.74 parser 255: #ifndef DOXYGEN
1.27 paf 256: struct Replace_action_info {
1.125.2.15.2. 1(paf 257:3): Request* request;
258:3): const String* src; String* dest;
(paf 259:): VTable* vtable;
260:): Value* replacement_code;
1.27 paf 261: };
1.74 parser 262: #endif
1.105 paf 263: /// @todo they can do $global[$result] there, getting pointer to later-invalid local var, kill this
1.125.2.15.2. 4(paf 264:3): static void replace_action(Table& table, ArrayString* row,
0(paf 265:3): int prestart, int prefinish,
266:3): int poststart, int postfinish,
267:3): void *info) {
1.27 paf 268: Replace_action_info& ai=*static_cast<Replace_action_info *>(info);
1.31 paf 269: if(row) { // begin&middle
1.104 paf 270: // piece from last match['prestart'] to beginning of this match['prefinish']
271: if(prestart!=prefinish)
272: *ai.dest << ai.src->mid(prestart, prefinish);//ai.dest->APPEND_CONST("-");
1.51 parser 273: // store found parts in one-record VTable
1.125.2.15.2. 4(paf 274:3): if(table.count()) // middle
275:3): table.put(0, row);
1.32 paf 276: else // begin
1.125.2.15.2. 4(paf 277:3): table+=row;
1.30 paf 278: { // execute 'replacement_code' in 'table' context
1.105 paf 279: ai.vtable->set_table(table);
1.30 paf 280:
1.125.2.15.2. 3(paf 281:3): *ai.dest << ai.request->process_to_string(*ai.replacement_code);
1.29 paf 282: }
283: } else // end
1.125.2.15.2. 3(paf 284:3): *ai.dest << ai.src->mid(poststart, postfinish);
1.27 paf 285: }
286:
1.50 parser 287: /// @todo use pcre:study somehow
1.125.2.15.2. 0(paf 288:3): static void _match(Request& r, MethodParams& params) {
289:3): Value& regexp=params.as_no_junction(0, "regexp must not be code");
1.38 paf 290:
1.125.2.15.2. 3(paf 291:3): const String* options=
0(paf 292:3): params.count()>1?
293:3): ¶ms.as_no_junction(1, "options must not be code").as_string():0;
1.24 paf 294:
1.125.2.15.2. (paf 295:): Temp_lang temp_lang(r, String::L_PASS_APPENDED);
296:): const String& src=GET_SELF(r, VString).string();
1.125.2.14 paf 297: bool just_matched;
1.125.2.15.2. 0(paf 298:3): if(params.count()<3) { // search
(paf 299:): Table* table=src.match(r.charsets.source(),
3(paf 300:3): regexp.as_string(), options,
1.64 parser 301: search_action, 0,
1.125.2.14 paf 302: just_matched);
1.125.2.15.2. (paf 303:): Value* result;
1.125.2.14 paf 304: if(table)
1.125.2.15.2. 3(paf 305:3): result=new VTable(table); // table of pre/match/post+substrings
1.64 parser 306: else
1.125.2.15.2. 3(paf 307:3): result=new VBool(just_matched);
308:3): r.write_assign_lang(*result);
1.27 paf 309: } else { // replace
1.125.2.15.2. 0(paf 310:3): Value& replacement_code=params.as_junction(2, "replacement param must be code");
1.24 paf 311:
1.125.2.7 paf 312: String result;
1.125.2.15.2. 3(paf 313:3): VTable* vtable=new VTable;
1.125.2.7 paf 314: Replace_action_info info;
315: info.request=&r;
1.125.2.15.2. 3(paf 316:3): info.src=&src;
1.125.2.7 paf 317: info.dest=&result;
318: info.vtable=vtable;
1.125.2.15.2. 3(paf 319:3): info.replacement_code=&replacement_code;
1.105 paf 320: Temp_value_element temp_match_var(
1.125.2.15.2. 3(paf 321:3): *replacement_code.get_junction()->method_frame,
1.125.2.7 paf 322: match_var_name, vtable);
1.125.2.15.2. 3(paf 323:3): src.match(r.charsets.source(),
324:3): r.process_to_string(regexp), options,
1.125.2.14 paf 325: replace_action, &info,
326: just_matched);
1.102 paf 327: r.write_assign_lang(result);
1.27 paf 328: }
1.24 paf 329: }
330:
1.125.2.15.2. 0(paf 331:3): static void change_case(Request& r, MethodParams& params,
1.49 parser 332: String::Change_case_kind kind) {
1.125.2.15.2. (paf 333:): const String& src=GET_SELF(r, VString).string();
1.49 parser 334:
1.125.2.15.2. 0(paf 335:3): r.write_assign_lang(src.change_case(r.charsets.source(), kind));
1.49 parser 336: }
1.125.2.15.2. 0(paf 337:3): static void _upper(Request& r, MethodParams& params) {
3(paf 338:3): change_case(r, params, String::CC_UPPER);
1.49 parser 339: }
1.125.2.15.2. 0(paf 340:3): static void _lower(Request& r, MethodParams& params) {
3(paf 341:3): change_case(r, params, String::CC_LOWER);
1.49 parser 342: }
343:
1.65 parser 344: #ifndef DOXYGEN
1.125.2.7 paf 345: class String_sql_event_handlers: public SQL_Driver_query_event_handlers {
1.125.2.15.2. (paf 346:): const String& statement_string; const char* statement_cstr;
1.125.2.7 paf 347: bool got_column;
1.65 parser 348: public:
1.125.2.7 paf 349: bool got_cell;
1.125.2.15.2. 3(paf 350:3): String& result;
1.125.2.7 paf 351: public:
1.125.2.15 paf 352: String_sql_event_handlers(
1.125.2.15.2. (paf 353:): const String& astatement_string, const char* astatement_cstr):
1.125.2.15 paf 354: statement_string(astatement_string), statement_cstr(astatement_cstr),
355: got_column(false),
356: got_cell(false),
1.125.2.15.2. 3(paf 357:3): result(*new String) {}
1.65 parser 358:
1.125.2.15.2. 9(paf 359:3): bool add_column(SQL_Error& error, const char* str, size_t /*length*/) {
1.124 paf 360: if(got_column) {
361: error=SQL_Error("parser.runtime",
1.125.2.7 paf 362: //statement_string,
1.65 parser 363: "result must contain exactly one column");
1.124 paf 364: return true;
365: }
1.65 parser 366: got_column=true;
1.124 paf 367: return false;
1.65 parser 368: }
1.124 paf 369: bool before_rows(SQL_Error& /*error*/ ) { /* ignore */ return false; }
370: bool add_row(SQL_Error& /*error*/) { /* ignore */ return false; }
1.125.2.15.2. 9(paf 371:3): bool add_row_cell(SQL_Error& error, const char* str, size_t length) {
1.124 paf 372: if(got_cell) {
373: error=SQL_Error("parser.runtime",
1.125.2.7 paf 374: //statement_string,
1.65 parser 375: "result must not contain more then one row");
1.124 paf 376: return true;
377: }
1.65 parser 378:
1.124 paf 379: try {
380: got_cell=true;
1.125.2.15.2. 9(paf 381:3): result.append_help_length(str, length, String::L_TAINTED);
1.124 paf 382: return false;
383: } catch(...) {
384: error=SQL_Error("exception occured in String_sql_event_handlers::add_row_cell");
385: return true;
386: }
1.65 parser 387: }
388: };
389: #endif
1.125.2.15.2. 6(paf 390:3): extern String sql_limit_name;
391:3): extern String sql_offset_name;
392:3): extern String sql_default_name;
393:3): extern String sql_distinct_name;
0(paf 394:3): const String* sql_result_string(Request& r, MethodParams& params,
3(paf 395:3): HashStringValue*& options, Value*& default_code) {
0(paf 396:3): Value& statement=params.as_junction(0, "statement must be code");
1.53 parser 397:
1.70 parser 398: ulong limit=0;
399: ulong offset=0;
1.125.2.15.2. (paf 400:): default_code=0;
0(paf 401:3): if(params.count()>1) {
402:3): Value& voptions=params.as_no_junction(1, "options must be hash, not code");
3(paf 403:3): if(!voptions.is_string())
404:3): if(options=voptions.get_hash()) {
(paf 405:): if(Value* vlimit=options->get(sql_limit_name))
3(paf 406:3): limit=(ulong)r.process_to_value(*vlimit).as_double();
(paf 407:): if(Value* voffset=options->get(sql_offset_name))
3(paf 408:3): offset=(ulong)r.process_to_value(*voffset).as_double();
1.125.2.7 paf 409: if(default_code=options->get(sql_default_name)) {
1.125.2.15.2. (paf 410:): if(Junction* default_junction=default_code->get_junction())
1.119 paf 411: ;//default_junction->change_context(statement.get_junction());
1.110 paf 412: else
1.98 paf 413: throw Exception("parser.runtime",
1.125.2.15.2. 3(paf 414:3): 0,
1.81 parser 415: "default option must be code");
416: }
1.73 parser 417: } else
1.98 paf 418: throw Exception("parser.runtime",
1.125.2.15.2. 3(paf 419:3): 0,
1.73 parser 420: "options must be hash");
1.70 parser 421: } else
422: options=0;
423:
1.125.2.15.2. (paf 424:): Temp_lang temp_lang(r, String::L_SQL);
425:): const String& statement_string=r.process_to_string(statement);
1.125.2.1 paf 426: const char* statement_cstr=
1.125.2.15.2. 3(paf 427:3): statement_string.cstr(String::L_UNSPECIFIED, r.connection());
1.125.2.7 paf 428: String_sql_event_handlers handlers(statement_string, statement_cstr);
1.125.2.15.2. (paf 429:): r.connection()->query(
1.117 paf 430: statement_cstr, offset, limit,
431: handlers,
432: statement_string);
1.53 parser 433:
1.65 parser 434: if(!handlers.got_cell)
1.125.2.15.2. (paf 435:): return 0; // no lines, caller should return second param[default value]
1.62 parser 436:
1.125.2.15.2. 3(paf 437:3): return &handlers.result;
1.53 parser 438: }
439:
1.125.2.15.2. 0(paf 440:3): static void _sql(Request& r, MethodParams& params) {
1.53 parser 441:
1.125.2.7 paf 442: HashStringValue* options;
1.125.2.15.2. (paf 443:): Value* default_code;
3(paf 444:3): const String* string=sql_result_string(r, params, options, default_code);
1.62 parser 445: if(!string) {
1.81 parser 446: if(default_code) {
1.125.2.15.2. 3(paf 447:3): string=&r.process_to_string(*default_code);
1.68 parser 448: } else
1.98 paf 449: throw Exception("parser.runtime",
1.125.2.15.2. 3(paf 450:3): 0,
1.81 parser 451: "produced no result, but no default option specified");
1.62 parser 452: }
1.100 paf 453:
1.102 paf 454: r.write_assign_lang(*string);
1.53 parser 455: }
456:
1.125.2.15.2. 0(paf 457:3): static void _replace(Request& r, MethodParams& params) {
(paf 458:): const String& src=GET_SELF(r, VString).string();
1.71 parser 459:
1.125.2.15.2. 0(paf 460:3): Table* table=params.as_no_junction(0, "parameter must not be code").get_table();
1.71 parser 461: if(!table)
1.98 paf 462: throw Exception("parser.runtime",
1.125.2.15.2. 3(paf 463:3): 0,
1.71 parser 464: "parameter must be table");
465:
1.125.2.15.2. 3(paf 466:3): Dictionary dict(*table);
467:3): r.write_assign_lang(src.replace(dict));
1.71 parser 468: }
1.79 parser 469:
1.125.2.15.2. 0(paf 470:3): static void _save(Request& r, MethodParams& params) {
471:3): const String& file_name=params.as_string(params.count()-1,
1.87 paf 472: "file name must be string");
1.79 parser 473:
1.125.2.15.2. (paf 474:): const String& src=GET_SELF(r, VString).string();
1.79 parser 475:
1.87 paf 476: bool do_append=false;
1.125.2.15.2. 0(paf 477:3): if(params.count()>1) {
478:3): const String& mode=params.as_string(0, "mode must be string");
3(paf 479:3): if(mode=="append")
1.87 paf 480: do_append=true;
481: else
1.98 paf 482: throw Exception("parser.runtime",
1.125.2.15.2. 3(paf 483:3): &mode,
1.87 paf 484: "unknown mode, must be 'append'");
485: }
486:
1.79 parser 487: // write
1.125.2.15.2. 3(paf 488:3): const char* buf=src.cstr(String::L_UNSPECIFIED, r.connection(false/*no error if none*/));
1.94 paf 489: file_write(r.absolute(file_name),
1.89 paf 490: buf, strlen(buf), true, do_append);
1.79 parser 491: }
492:
1.125.2.15.2. 0(paf 493:3): static void _normalize(Request& r, MethodParams&) {
(paf 494:): const String& src=GET_SELF(r, VString).string();
1.125.2.7 paf 495:
1.125.2.15.2. 3(paf 496:3): r.write_assign_lang(src);
1.109 paf 497: }
498:
1.41 paf 499: // constructor
500:
1.125.2.7 paf 501: MString::MString(): Methoded("string") {
1.1 paf 502: // ^string.length[]
1.41 paf 503: add_native_method("length", Method::CT_DYNAMIC, _length, 0, 0);
1.6 paf 504:
1.1 paf 505: // ^string.int[]
1.72 parser 506: // ^string.int(default)
507: add_native_method("int", Method::CT_DYNAMIC, _int, 0, 1);
1.1 paf 508: // ^string.double[]
1.72 parser 509: // ^string.double(default)
510: add_native_method("double", Method::CT_DYNAMIC, _double, 0, 1);
1.9 paf 511:
1.24 paf 512: // ^string.format{format}
1.41 paf 513: add_native_method("format", Method::CT_DYNAMIC, _string_format, 1, 1);
1.14 paf 514:
1.15 paf 515: // ^string.left(n)
1.41 paf 516: add_native_method("left", Method::CT_DYNAMIC, _left, 1, 1);
1.15 paf 517: // ^string.right(n)
1.41 paf 518: add_native_method("right", Method::CT_DYNAMIC, _right, 1, 1);
1.15 paf 519: // ^string.mid(p;n)
1.82 parser 520: add_native_method("mid", Method::CT_DYNAMIC, _mid, 1, 2);
1.16 paf 521:
522: // ^string.pos[substr]
1.41 paf 523: add_native_method("pos", Method::CT_DYNAMIC, _pos, 1, 1);
1.17 paf 524:
1.118 paf 525: // ^string.split[delim]
526: // ^string.split[delim][options]
527: add_native_method("split", Method::CT_DYNAMIC, _split, 1, 2);
528: // old names for backward compatibility
529: // ^string.lsplit[delim]
530: add_native_method("lsplit", Method::CT_DYNAMIC, _lsplit, 1, 1);
531: // ^string.rsplit[delim]
532: add_native_method("rsplit", Method::CT_DYNAMIC, _rsplit, 1, 1);
533:
1.32 paf 534: // ^string.match[regexp][options]
535: // ^string.match[regexp][options]{replacement-code}
1.41 paf 536: add_native_method("match", Method::CT_DYNAMIC, _match, 1, 3);
1.49 parser 537:
538: // ^string.toupper[]
539: add_native_method("upper", Method::CT_DYNAMIC, _upper, 0, 0);
540: // ^string.tolower[]
541: add_native_method("lower", Method::CT_DYNAMIC, _lower, 0, 0);
1.53 parser 542:
1.70 parser 543: // ^sql[query]
544: // ^sql[query][$.limit(1) $.offset(2) $.default[n/a]]
1.67 parser 545: add_native_method("sql", Method::CT_STATIC, _sql, 1, 2);
1.71 parser 546:
547: // ^string.replace[table]
548: add_native_method("replace", Method::CT_DYNAMIC, _replace, 1, 1);
1.79 parser 549:
550: // ^string.save[file]
1.87 paf 551: add_native_method("save", Method::CT_DYNAMIC, _save, 1, 2);
1.109 paf 552:
1.112 paf 553: // ^string.normalize[]
554: add_native_method("normalize", Method::CT_DYNAMIC, _normalize, 0, 0);
1.2 paf 555: }
E-mail: