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