2 * Copyright (c) 2003 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.
37 #include "base/intmath.hh"
38 #include "base/str.hh"
43 tokenize(vector
<string
>& v
, const string
&s
, char token
, bool ignore
)
45 string::size_type first
= 0;
46 string::size_type last
= s
.find_first_of(token
);
51 last
= s
.find_first_of(token
, ++first
);
53 if (last
== string::npos
) {
60 while (last
!= string::npos
) {
61 v
.push_back(s
.substr(first
, last
- first
));
64 first
= s
.find_first_not_of(token
, last
+ 1);
66 if (first
== string::npos
)
71 last
= s
.find_first_of(token
, first
);
74 v
.push_back(s
.substr(first
));
79 __to_number(string value
, T
&retval
)
81 static const T maxnum
= ((T
)-1);
82 static const bool sign
= maxnum
< 0;
83 static const T hexmax
= maxnum
& (((T
)1 << (sizeof(T
) * 8 - 4)) - 1);
84 static const T octmax
= maxnum
& (((T
)1 << (sizeof(T
) * 8 - 3)) - 1);
85 static const T signmax
=
86 (sign
) ? maxnum
& (((T
)1 << (sizeof(T
) * 8 - 1)) - 1) : maxnum
;
87 static const T decmax
= signmax
/ 10 - 1;
90 cout
<< "maxnum = 0x" << hex
<< maxnum
<< "\n"
91 << "sign = 0x" << hex
<< sign
<< "\n"
92 << "hexmax = 0x" << hex
<< hexmax
<< "\n"
93 << "octmax = 0x" << hex
<< octmax
<< "\n"
94 << "signmax = 0x" << hex
<< signmax
<< "\n"
95 << "decmax = 0x" << hex
<< decmax
<< "\n";
100 bool negative
= false;
103 int last
= value
.size() - 1;
109 if (c
== '-' && sign
)
116 if (last
== 0) return true;
124 if (sign
&& negative
)
128 if (c
== 'X' || c
== 'x') {
136 } else if (!IsDec(c
))
139 if (sign
&& negative
&& c
== '0')
145 if (sign
&& negative
) retval
= -retval
;
154 for (i
= 2; i
<= last
; i
++) {
159 if (retval
> hexmax
) return false;
161 retval
+= Hex2Int(c
);
165 for (i
= 2; i
<= last
; i
++) {
170 if (retval
> octmax
) return false;
177 for (i
= 2; i
< last
; i
++) {
182 if (retval
> decmax
) return false;
185 if (sign
&& (retval
& ((T
)1 << (sizeof(T
) * 8 - 1))))
192 if (retval
> decmax
) return false;
195 if (sign
&& negative
) {
196 if ((retval
& ((T
)1 << (sizeof(T
) * 8 - 1))) &&
197 retval
>= (T
)-signmax
)
202 if (sign
&& (retval
& ((T
)1 << ((sizeof(T
) * 8) - 1))))
208 signed long long mult
= 1;
213 if (i
!= last
) return false;
215 val
= signmax
/ mult
;
219 if (i
!= last
) return false;
221 val
= signmax
/ mult
;
225 if (i
!= last
) return false;
226 mult
= 1024 * 1024 * 1024;
227 val
= signmax
/ mult
;
231 if (i
>= last
) return false;
234 for (i
++; i
<= last
; i
++) {
243 for (i
= 0; i
< mult
; i
++) {
244 if (retval
> signmax
/ 10)
247 if (sign
&& (retval
& ((T
)1 << (sizeof(T
) * 8 - 1))))
250 if (sign
&& negative
) {
251 if ((retval
& ((T
)1 << (sizeof(T
) * 8 - 1))) &&
252 retval
>= (T
)-signmax
)
257 if (sign
&& (retval
& ((T
)1 << ((sizeof(T
) * 8) - 1))))
266 if (sign
&& negative
)
269 if (mult
> (unsigned long long)signmax
)
282 bool to_number<type>(const string &value, type &retval) \
283 { return __to_number(value, retval); }
285 STN(unsigned long long);
286 STN(signed long long);
297 bool to_number
<bool>(const string
&value
, bool &retval
)
299 string lowered
= to_lower(value
);
311 if (lowered
== "false") {
316 if (lowered
== "true"){
321 if (lowered
== "no") {
326 if (lowered
== "yes"){