Annotation of parser3/src/classes/string.C, revision 1.15
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.15 ! paf 6: $Id: string.C,v 1.14 2001/03/29 09:31:42 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"
13:
14: // global var
15:
1.10 paf 16: VStateless_class *string_class;
1.1 paf 17:
18: // methods
19:
1.7 paf 20: static void _length(Request& r, const String&, Array *) {
1.1 paf 21: Pool& pool=r.pool();
1.15 ! paf 22: Value& value=*new(pool) VDouble(pool, r.self->get_string()->size());
1.11 paf 23: r.write_no_lang(value);
1.1 paf 24: }
25:
1.7 paf 26: static void _int(Request& r, const String&, Array *) {
1.1 paf 27: Pool& pool=r.pool();
1.13 paf 28: Value& value=*new(pool) VInt(pool, (int)r.self->as_double());
1.11 paf 29: r.write_no_lang(value);
1.1 paf 30: }
31:
1.7 paf 32: static void _double(Request& r, const String&, Array *) {
1.1 paf 33: Pool& pool=r.pool();
1.13 paf 34: Value& value=*new(pool) VDouble(pool, r.self->as_double());
1.11 paf 35: r.write_no_lang(value);
1.1 paf 36: }
37:
1.9 paf 38: void _string_format(Request& r, const String& method_name, Array *params) {
39: Pool& pool=r.pool();
40:
41: Value& fmt=*static_cast<Value *>(params->get(0));
42: // forcing ^format[this param type]
43: r.fail_if_junction_(true, fmt,
44: method_name, "fmt must not be junction");
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.15 ! paf 51: void _left(Request& r, const String& method_name, Array *params) {
! 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:
! 60: void _right(Request& r, const String& method_name, Array *params) {
! 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:
! 69: void _mid(Request& r, const String& method_name, Array *params) {
! 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.11 paf 79: // initialize
1.9 paf 80:
1.10 paf 81: void initialize_string_class(Pool& pool, VStateless_class& vclass) {
1.1 paf 82: // ^string.length[]
1.6 paf 83: vclass.add_native_method("length", _length, 0, 0);
84:
1.1 paf 85: // ^string.int[]
1.6 paf 86: vclass.add_native_method("int", _int, 0, 0);
87:
1.1 paf 88: // ^string.double[]
1.6 paf 89: vclass.add_native_method("double", _double, 0, 0);
1.9 paf 90:
91: // ^string.format[]
92: vclass.add_native_method("format", _string_format, 1, 1);
1.14 paf 93:
1.15 ! paf 94: // ^string.left(n)
! 95: vclass.add_native_method("left", _left, 1, 1);
! 96: // ^string.right(n)
! 97: vclass.add_native_method("right", _right, 1, 1);
! 98: // ^string.mid(p;n)
! 99: vclass.add_native_method("mid", _mid, 2, 2);
1.2 paf 100: }
1.1 paf 101:
E-mail: