Annotation of parser3/src/classes/string.C, revision 1.23
1.1 paf 1: /*
1.4 paf 2: Parser
3: Copyright (c) 2001 ArtLebedev Group (http://www.artlebedev.com)
1.5 paf 4: Author: Alexander Petrosyan <paf@design.ru> (http://design.ru/paf)
1.4 paf 5:
1.23 ! paf 6: $Id: string.C,v 1.22 2001/03/30 05:51:12 paf Exp $
1.1 paf 7: */
8:
9: #include "pa_request.h"
10: #include "_string.h"
11: #include "pa_vdouble.h"
12: #include "pa_vint.h"
1.17 paf 13: #include "pa_vtable.h"
1.1 paf 14:
15: // global var
16:
1.10 paf 17: VStateless_class *string_class;
1.1 paf 18:
19: // methods
20:
1.7 paf 21: static void _length(Request& r, const String&, Array *) {
1.1 paf 22: Pool& pool=r.pool();
1.15 paf 23: Value& value=*new(pool) VDouble(pool, r.self->get_string()->size());
1.11 paf 24: r.write_no_lang(value);
1.1 paf 25: }
26:
1.7 paf 27: static void _int(Request& r, const String&, Array *) {
1.1 paf 28: Pool& pool=r.pool();
1.13 paf 29: Value& value=*new(pool) VInt(pool, (int)r.self->as_double());
1.11 paf 30: r.write_no_lang(value);
1.1 paf 31: }
32:
1.7 paf 33: static void _double(Request& r, const String&, Array *) {
1.1 paf 34: Pool& pool=r.pool();
1.13 paf 35: Value& value=*new(pool) VDouble(pool, r.self->as_double());
1.11 paf 36: r.write_no_lang(value);
1.1 paf 37: }
38:
1.18 paf 39: /*not static*/void _string_format(Request& r, const String& method_name, Array *params) {
1.9 paf 40: Pool& pool=r.pool();
41:
42: Value& fmt=*static_cast<Value *>(params->get(0));
1.17 paf 43: // forcing [this param type]
1.16 paf 44: r.fail_if_junction_(true, fmt, method_name, "fmt must not be junction");
1.9 paf 45:
1.13 paf 46: char *buf=format(pool, r.self->as_double(), fmt.as_string().cstr());
1.9 paf 47:
1.12 paf 48: r.write_no_lang(String(pool, buf));
1.9 paf 49: }
1.11 paf 50:
1.18 paf 51: static void _left(Request& r, const String&, Array *params) {
1.15 paf 52: Pool& pool=r.pool();
53:
54: size_t n=(size_t)r.process(*static_cast<Value *>(params->get(0))).as_double();
55:
56: const String& string=*static_cast<VString *>(r.self)->get_string();
57: r.write_assign_lang(*new(pool) VString(string.piece(0, n)));
58: }
59:
1.18 paf 60: static void _right(Request& r, const String&, Array *params) {
1.15 paf 61: Pool& pool=r.pool();
62:
63: size_t n=(size_t)r.process(*static_cast<Value *>(params->get(0))).as_double();
64:
65: const String& string=*static_cast<VString *>(r.self)->get_string();
66: r.write_assign_lang(*new(pool) VString(string.piece(string.size()-n, string.size())));
67: }
68:
1.18 paf 69: static void _mid(Request& r, const String&, Array *params) {
1.15 paf 70: Pool& pool=r.pool();
71:
72: size_t p=(size_t)r.process(*static_cast<Value *>(params->get(0))).as_double();
73: size_t n=(size_t)r.process(*static_cast<Value *>(params->get(1))).as_double();
74:
75: const String& string=*static_cast<VString *>(r.self)->get_string();
76: r.write_assign_lang(*new(pool) VString(string.piece(p, p+n)));
77: }
78:
1.18 paf 79: static void _pos(Request& r, const String& method_name, Array *params) {
1.16 paf 80: Pool& pool=r.pool();
81:
82: Value& substr=*static_cast<Value *>(params->get(0));
1.17 paf 83: // forcing [this param type]
1.16 paf 84: r.fail_if_junction_(true, substr, method_name, "substr must not be junction");
85:
86: const String& string=*static_cast<VString *>(r.self)->get_string();
87: r.write_assign_lang(*new(pool) VInt(pool, string.pos(substr.as_string())));
88: }
89:
1.19 paf 90: static void split_list(Request& r, const String& method_name, Array *params,
1.23 ! paf 91: const String& string,
! 92: Array& result) {
1.17 paf 93: Pool& pool=r.pool();
94:
95: Value& delim_value=*static_cast<Value *>(params->get(0));
96: // forcing [this param type]
97: r.fail_if_junction_(true, delim_value, method_name, "delimiter must not be junction");
1.23 ! paf 98:
! 99: string.split(result, 0, delim_value.as_string(), String::UL_CLEAN, -1);
1.19 paf 100: }
101:
102: static void _lsplit(Request& r, const String& method_name, Array *params) {
103: Pool& pool=r.pool();
104: const String& string=*static_cast<VString *>(r.self)->get_string();
105:
1.21 paf 106: Array& row=*new(pool) Array(pool);
107: split_list(r, method_name, params, string, row);
1.19 paf 108:
1.21 paf 109: Table& table=*new(pool) Table(pool, &string,
110: 0/*nameless*/, 1/*row preallocate(and only)*/);
111: table+=&row;
1.19 paf 112:
113: r.write_no_lang(*new(pool) VTable(pool, &table));
114: }
115:
116: static void _rsplit(Request& r, const String& method_name, Array *params) {
117: Pool& pool=r.pool();
118: const String& string=*static_cast<VString *>(r.self)->get_string();
119:
120: Array list(pool);
121: split_list(r, method_name, params, string, list);
122:
1.21 paf 123: Array& row=*new(pool) Array(pool);
124: for(int i=list.size(); --i>=0; )
125: row+=list.get(i);
126:
127: Table& table=*new(pool) Table(pool, &string,
128: 0/*nameless*/, 1/*row preallocate(and only)*/);
129: table+=&row;
1.17 paf 130:
1.19 paf 131: r.write_no_lang(*new(pool) VTable(pool, &table));
1.17 paf 132: }
133:
1.11 paf 134: // initialize
1.9 paf 135:
1.10 paf 136: void initialize_string_class(Pool& pool, VStateless_class& vclass) {
1.1 paf 137: // ^string.length[]
1.22 paf 138: vclass.add_native_method("length", Method::CT_DYNAMIC, _length, 0, 0);
1.6 paf 139:
1.1 paf 140: // ^string.int[]
1.22 paf 141: vclass.add_native_method("int", Method::CT_DYNAMIC, _int, 0, 0);
1.6 paf 142:
1.1 paf 143: // ^string.double[]
1.22 paf 144: vclass.add_native_method("double", Method::CT_DYNAMIC, _double, 0, 0);
1.9 paf 145:
146: // ^string.format[]
1.22 paf 147: vclass.add_native_method("format", Method::CT_DYNAMIC, _string_format, 1, 1);
1.14 paf 148:
1.15 paf 149: // ^string.left(n)
1.22 paf 150: vclass.add_native_method("left", Method::CT_DYNAMIC, _left, 1, 1);
1.15 paf 151: // ^string.right(n)
1.22 paf 152: vclass.add_native_method("right", Method::CT_DYNAMIC, _right, 1, 1);
1.15 paf 153: // ^string.mid(p;n)
1.22 paf 154: vclass.add_native_method("mid", Method::CT_DYNAMIC, _mid, 2, 2);
1.16 paf 155:
156: // ^string.pos[substr]
1.22 paf 157: vclass.add_native_method("pos", Method::CT_DYNAMIC, _pos, 1, 1);
1.17 paf 158:
159: // ^string.lsplit[delim]
1.22 paf 160: vclass.add_native_method("lsplit", Method::CT_DYNAMIC, _lsplit, 1, 1);
1.19 paf 161: // ^string.rsplit[delim]
1.22 paf 162: vclass.add_native_method("rsplit", Method::CT_DYNAMIC, _rsplit, 1, 1);
1.2 paf 163: }
1.1 paf 164:
E-mail: