1 | // $Source$ |
---|
2 | // $Revision: 710 $ |
---|
3 | // $Date: 2003-05-02 20:05:08 +0000 (Fri, 02 May 2003) $ |
---|
4 | // $Author: orlov $ |
---|
5 | |
---|
6 | #include "rf_short_int.ih" |
---|
7 | #include "rf_types.ih" |
---|
8 | #include "rf_expr.ih" |
---|
9 | #include "rf_char.ih" |
---|
10 | |
---|
11 | namespace rftype |
---|
12 | { |
---|
13 | |
---|
14 | Expr ShortInt::create_expr (intptr_t _n) |
---|
15 | { |
---|
16 | Expr e = Term::create_expr(1); |
---|
17 | new(e.get_first()) ShortInt(_n); |
---|
18 | return e; |
---|
19 | } |
---|
20 | |
---|
21 | |
---|
22 | Expr ShortInt::create_expr (Expr& _expr, int _flag) |
---|
23 | { |
---|
24 | // _flag == 1, if the number in _expr has "+" |
---|
25 | //_flag == -1, if the number in _expr has "-" |
---|
26 | //othewise _flag == 0 |
---|
27 | |
---|
28 | Term* p = _expr.get_first(); |
---|
29 | Term* q = _expr.get_last(); |
---|
30 | |
---|
31 | intptr_t n = 0; |
---|
32 | |
---|
33 | if (p->get_type() == type_short_int) { |
---|
34 | n = ((ShortInt*)p)->to_int(); |
---|
35 | if (n < 0) { |
---|
36 | n = -n; |
---|
37 | _flag = -1; |
---|
38 | } |
---|
39 | else |
---|
40 | _flag = 1; |
---|
41 | } |
---|
42 | |
---|
43 | for (p += _flag * _flag; p < q; p++) { |
---|
44 | if (p->get_type() == type_short_int) { |
---|
45 | intptr_t n_temp = ((ShortInt*)p)->to_int() / 10; |
---|
46 | intptr_t k = 10; |
---|
47 | |
---|
48 | while (n_temp) { |
---|
49 | n_temp /= 10; |
---|
50 | k *= 10; |
---|
51 | } |
---|
52 | |
---|
53 | n = n * k + ((ShortInt*)p)->to_int(); |
---|
54 | } |
---|
55 | else |
---|
56 | if (p->get_type() == type_char) { |
---|
57 | n = n * 10 + (((Char*)p)->to_wchar_t() - 48); |
---|
58 | } |
---|
59 | } |
---|
60 | |
---|
61 | if (-1 == _flag) n = -n; |
---|
62 | |
---|
63 | Expr e = Term::create_expr(1); |
---|
64 | |
---|
65 | new(e.get_first()) ShortInt(n); |
---|
66 | return e; |
---|
67 | } |
---|
68 | |
---|
69 | uintptr_t ShortInt::ctor (uintptr_t _data) |
---|
70 | { |
---|
71 | return _data; |
---|
72 | } |
---|
73 | |
---|
74 | void ShortInt::dtor (uintptr_t) |
---|
75 | { } |
---|
76 | |
---|
77 | int ShortInt::compare (uintptr_t _data1, uintptr_t _data2) |
---|
78 | { |
---|
79 | if (_data1 == _data2) return 0; |
---|
80 | else if (_data1 < _data2) return -1; |
---|
81 | else return 1; |
---|
82 | } |
---|
83 | |
---|
84 | uint32_t ShortInt::hash (uintptr_t _data) |
---|
85 | { |
---|
86 | return _data; |
---|
87 | } |
---|
88 | |
---|
89 | pxx::WString ShortInt::to_string (uintptr_t _data) { |
---|
90 | intptr_t n = _data; |
---|
91 | int flag = 0; |
---|
92 | |
---|
93 | if (n < 0) { |
---|
94 | flag = 1; |
---|
95 | n = -n; |
---|
96 | } |
---|
97 | |
---|
98 | wchar_t str[11]; |
---|
99 | int i = 11; |
---|
100 | |
---|
101 | if (!n) { |
---|
102 | str[i] = 48; |
---|
103 | } |
---|
104 | else { |
---|
105 | while (n) { |
---|
106 | str[i] = n % 10 + 48; |
---|
107 | n /= 10; |
---|
108 | i--; |
---|
109 | }; |
---|
110 | |
---|
111 | if (flag) { |
---|
112 | str[i] = '-'; |
---|
113 | } |
---|
114 | else i++; |
---|
115 | } |
---|
116 | return pxx::WString(&str[i], 12 - i); |
---|
117 | |
---|
118 | } |
---|
119 | |
---|
120 | size_t ShortInt::get_char_len(uintptr_t _data) { |
---|
121 | intptr_t n = _data; |
---|
122 | uintptr_t k = 0; |
---|
123 | if (!n) k = 1; |
---|
124 | else { |
---|
125 | if (n < 0) k++; |
---|
126 | while (n) { |
---|
127 | n /= 10; |
---|
128 | k++; |
---|
129 | }; |
---|
130 | }; |
---|
131 | return k; |
---|
132 | }; |
---|
133 | |
---|
134 | /*Term* ShortInt::to_chars(uintptr_t _data, Term* _p) { |
---|
135 | char str[11]; |
---|
136 | sprintf(str, "%d", _data); |
---|
137 | for (char *s = str; *s; s++) |
---|
138 | *_p++ = Char(*s); |
---|
139 | return _p; |
---|
140 | |
---|
141 | // new(_p++) ShortInt(_data); |
---|
142 | // return _p; |
---|
143 | }; |
---|
144 | */ |
---|
145 | |
---|
146 | short_type_funcs_t ShortInt::funcs = { |
---|
147 | ShortInt::ctor, |
---|
148 | ShortInt::dtor, |
---|
149 | null, // ShortInt::eq, |
---|
150 | ShortInt::compare, |
---|
151 | ShortInt::hash, |
---|
152 | ShortInt::to_string, |
---|
153 | ShortInt::get_char_len, |
---|
154 | null // ShortInt::to_chars |
---|
155 | }; |
---|
156 | |
---|
157 | } |
---|
158 | |
---|