1 | // $Source$ |
---|
2 | // $Revision: 723 $ |
---|
3 | // $Date: 2003-05-05 12:04:11 +0000 (Mon, 05 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 | unsigned ShortInt::compute_max_len () |
---|
15 | { |
---|
16 | intptr_t i = INTPTR_MAX / 10000; |
---|
17 | unsigned len = 4; |
---|
18 | for ( ; i; i /= 10) len++; |
---|
19 | return len; |
---|
20 | } |
---|
21 | |
---|
22 | const unsigned ShortInt::max_len = compute_max_len(); |
---|
23 | |
---|
24 | Expr ShortInt::create_expr (intptr_t _n) |
---|
25 | { |
---|
26 | Expr e = Term::create_expr(1); |
---|
27 | new(e.get_first()) ShortInt(_n); |
---|
28 | return e; |
---|
29 | } |
---|
30 | |
---|
31 | |
---|
32 | Expr ShortInt::create_expr (Expr& _expr, int _flag) |
---|
33 | { |
---|
34 | // _flag == 1, if the number in _expr has "+" |
---|
35 | //_flag == -1, if the number in _expr has "-" |
---|
36 | //othewise _flag == 0 |
---|
37 | |
---|
38 | Term* p = _expr.get_first(); |
---|
39 | Term* q = _expr.get_last(); |
---|
40 | |
---|
41 | intptr_t n = 0; |
---|
42 | |
---|
43 | if (p->get_type() == type_short_int) { |
---|
44 | n = ((ShortInt*)p)->to_int(); |
---|
45 | if (n < 0) { |
---|
46 | n = -n; |
---|
47 | _flag = -1; |
---|
48 | } |
---|
49 | else |
---|
50 | _flag = 1; |
---|
51 | } |
---|
52 | |
---|
53 | for (p += _flag * _flag; p < q; p++) { |
---|
54 | if (p->get_type() == type_short_int) { |
---|
55 | intptr_t n_temp = ((ShortInt*)p)->to_int() / 10; |
---|
56 | intptr_t k = 10; |
---|
57 | |
---|
58 | while (n_temp) { |
---|
59 | n_temp /= 10; |
---|
60 | k *= 10; |
---|
61 | } |
---|
62 | |
---|
63 | n = n * k + ((ShortInt*)p)->to_int(); |
---|
64 | } |
---|
65 | else |
---|
66 | if (p->get_type() == type_char) { |
---|
67 | n = n * 10 + (((Char*)p)->to_wchar_t() - 48); |
---|
68 | } |
---|
69 | } |
---|
70 | |
---|
71 | if (-1 == _flag) n = -n; |
---|
72 | |
---|
73 | Expr e = Term::create_expr(1); |
---|
74 | |
---|
75 | new(e.get_first()) ShortInt(n); |
---|
76 | return e; |
---|
77 | } |
---|
78 | |
---|
79 | uintptr_t ShortInt::ctor (uintptr_t _data) |
---|
80 | { |
---|
81 | return _data; |
---|
82 | } |
---|
83 | |
---|
84 | void ShortInt::dtor (uintptr_t) |
---|
85 | { } |
---|
86 | |
---|
87 | int ShortInt::compare (uintptr_t _data1, uintptr_t _data2) |
---|
88 | { |
---|
89 | if (_data1 == _data2) return 0; |
---|
90 | else if (_data1 < _data2) return -1; |
---|
91 | else return 1; |
---|
92 | } |
---|
93 | |
---|
94 | uint32_t ShortInt::hash (uintptr_t _data) |
---|
95 | { |
---|
96 | return _data; |
---|
97 | } |
---|
98 | |
---|
99 | pxx::WString ShortInt::to_string (uintptr_t _data) { |
---|
100 | intptr_t n = _data; |
---|
101 | int flag = 0; |
---|
102 | |
---|
103 | if (n < 0) { |
---|
104 | n = -n; |
---|
105 | flag = -1; |
---|
106 | } |
---|
107 | |
---|
108 | wchar_t str[max_len + 1]; |
---|
109 | int i = max_len; |
---|
110 | |
---|
111 | if (!n) { |
---|
112 | str[i] = 48; |
---|
113 | } |
---|
114 | else { |
---|
115 | while (n) { |
---|
116 | str[i] = n % 10 + 48; |
---|
117 | n /= 10; |
---|
118 | i--; |
---|
119 | }; |
---|
120 | |
---|
121 | if (flag) { |
---|
122 | str[i] = '-'; |
---|
123 | } |
---|
124 | else i++; |
---|
125 | } |
---|
126 | return pxx::WString(&str[i], max_len + 1 - i); |
---|
127 | |
---|
128 | } |
---|
129 | |
---|
130 | short_type_funcs_t ShortInt::funcs = { |
---|
131 | ShortInt::ctor, |
---|
132 | ShortInt::dtor, |
---|
133 | null, // ShortInt::eq, |
---|
134 | ShortInt::compare, |
---|
135 | ShortInt::hash, |
---|
136 | ShortInt::to_string |
---|
137 | }; |
---|
138 | |
---|
139 | } |
---|
140 | |
---|