2 * Copyright (c) 2001-2005 The Regents of The University of Michigan
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions are
7 * met: redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer;
9 * redistributions in binary form must reproduce the above copyright
10 * notice, this list of conditions and the following disclaimer in the
11 * documentation and/or other materials provided with the distribution;
12 * neither the name of the copyright holders nor the names of its
13 * contributors may be used to endorse or promote products derived from
14 * this software without specific prior written permission.
16 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
17 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
18 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
19 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
20 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
21 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
22 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
26 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
36 #include "base/intmath.hh"
37 #include "base/str.hh"
42 split_first(const string
&s
, string
&lhs
, string
&rhs
, char c
)
44 string::size_type offset
= s
.find(c
);
45 if (offset
== string::npos
) {
51 lhs
= s
.substr(0, offset
);
52 rhs
= s
.substr(offset
+ 1);
57 split_last(const string
&s
, string
&lhs
, string
&rhs
, char c
)
59 string::size_type offset
= s
.rfind(c
);
60 if (offset
== string::npos
) {
66 lhs
= s
.substr(0, offset
);
67 rhs
= s
.substr(offset
+ 1);
72 tokenize(vector
<string
>& v
, const string
&s
, char token
, bool ignore
)
74 string::size_type first
= 0;
75 string::size_type last
= s
.find_first_of(token
);
80 if (ignore
&& last
== first
) {
82 last
= s
.find_first_of(token
, ++first
);
84 if (last
== string::npos
) {
85 if (first
!= s
.size())
86 v
.push_back(s
.substr(first
));
91 while (last
!= string::npos
) {
92 v
.push_back(s
.substr(first
, last
- first
));
95 first
= s
.find_first_not_of(token
, last
+ 1);
97 if (first
== string::npos
)
102 last
= s
.find_first_of(token
, first
);
105 v
.push_back(s
.substr(first
));
109 * @todo This function will not handle the smallest negative decimal
110 * value for a signed type
115 __to_number(string value
, T
&retval
)
117 static const T maxnum
= ((T
)-1);
118 static const bool sign
= maxnum
< 0;
119 static const int bits
= sizeof(T
) * 8;
120 static const T hexmax
= maxnum
& (((T
)1 << (bits
- 4 - sign
)) - 1);
121 static const T octmax
= maxnum
& (((T
)1 << (bits
- 3 - sign
)) - 1);
122 static const T signmax
=
123 (sign
) ? maxnum
& (((T
)1 << (bits
- 1)) - 1) : maxnum
;
124 static const T decmax
= signmax
/ 10;
127 cout
<< "maxnum = 0x" << hex
<< (unsigned long long)maxnum
<< "\n"
128 << "sign = 0x" << hex
<< (unsigned long long)sign
<< "\n"
129 << "hexmax = 0x" << hex
<< (unsigned long long)hexmax
<< "\n"
130 << "octmax = 0x" << hex
<< (unsigned long long)octmax
<< "\n"
131 << "signmax = 0x" << hex
<< (unsigned long long)signmax
<< "\n"
132 << "decmax = 0x" << hex
<< (unsigned long long)decmax
<< "\n";
137 bool negative
= false;
140 int last
= value
.size() - 1;
146 if (c
== '-' && sign
)
153 if (last
== 0) return true;
161 if (sign
&& negative
)
165 if (c
== 'X' || c
== 'x') {
173 } else if (!isDec(c
))
176 if (sign
&& negative
&& c
== '0')
182 if (sign
&& negative
) retval
= -retval
;
191 for (i
= 2; i
<= last
; i
++) {
196 if (retval
> hexmax
) return false;
198 retval
+= hex2Int(c
);
202 for (i
= 2; i
<= last
; i
++) {
207 if (retval
> octmax
) return false;
214 for (i
= 2; i
< last
; i
++) {
219 if (retval
> decmax
) return false;
220 bool atmax
= retval
== decmax
;
223 if (atmax
&& retval
< decmax
) return false;
224 if (sign
&& (retval
& ((T
)1 << (sizeof(T
) * 8 - 1))))
231 if (retval
> decmax
) return false;
232 bool atmax
= retval
== decmax
;
235 if (atmax
&& retval
< decmax
) return false;
236 if (sign
&& negative
) {
237 if ((retval
& ((T
)1 << (sizeof(T
) * 8 - 1))) &&
238 retval
>= (T
)-signmax
)
243 if (sign
&& (retval
& ((T
)1 << ((sizeof(T
) * 8) - 1))))
249 signed long long mult
= 1;
254 if (i
!= last
) return false;
256 val
= signmax
/ mult
;
260 if (i
!= last
) return false;
262 val
= signmax
/ mult
;
266 if (i
!= last
) return false;
267 mult
= 1024 * 1024 * 1024;
268 val
= signmax
/ mult
;
272 if (i
>= last
) return false;
275 for (i
++; i
<= last
; i
++) {
284 for (i
= 0; i
< mult
; i
++) {
285 if (retval
> signmax
/ 10)
288 if (sign
&& (retval
& ((T
)1 << (sizeof(T
) * 8 - 1))))
291 if (sign
&& negative
) {
292 if ((retval
& ((T
)1 << (sizeof(T
) * 8 - 1))) &&
293 retval
>= (T
)-signmax
)
298 if (sign
&& (retval
& ((T
)1 << ((sizeof(T
) * 8) - 1))))
307 if (sign
&& negative
)
310 if (mult
> (unsigned long long)signmax
)
323 bool to_number<type>(const string &value, type &retval) \
324 { return __to_number(value, retval); }
326 STN(unsigned long long);
327 STN(signed long long);
338 bool to_number
<bool>(const string
&value
, bool &retval
)
340 string lowered
= to_lower(value
);
352 if (lowered
== "false") {
357 if (lowered
== "true"){
362 if (lowered
== "no") {
367 if (lowered
== "yes"){