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
));
78 * @todo This function will not handle the smallest negative decimal
79 * value for a signed type
84 __to_number(string value
, T
&retval
)
86 static const T maxnum
= ((T
)-1);
87 static const bool sign
= maxnum
< 0;
88 static const int bits
= sizeof(T
) * 8;
89 static const T hexmax
= maxnum
& (((T
)1 << (bits
- 4 - sign
)) - 1);
90 static const T octmax
= maxnum
& (((T
)1 << (bits
- 3 - sign
)) - 1);
91 static const T signmax
=
92 (sign
) ? maxnum
& (((T
)1 << (bits
- 1)) - 1) : maxnum
;
93 static const T decmax
= signmax
/ 10;
96 cout
<< "maxnum = 0x" << hex
<< (unsigned long long)maxnum
<< "\n"
97 << "sign = 0x" << hex
<< (unsigned long long)sign
<< "\n"
98 << "hexmax = 0x" << hex
<< (unsigned long long)hexmax
<< "\n"
99 << "octmax = 0x" << hex
<< (unsigned long long)octmax
<< "\n"
100 << "signmax = 0x" << hex
<< (unsigned long long)signmax
<< "\n"
101 << "decmax = 0x" << hex
<< (unsigned long long)decmax
<< "\n";
106 bool negative
= false;
109 int last
= value
.size() - 1;
115 if (c
== '-' && sign
)
122 if (last
== 0) return true;
130 if (sign
&& negative
)
134 if (c
== 'X' || c
== 'x') {
142 } else if (!IsDec(c
))
145 if (sign
&& negative
&& c
== '0')
151 if (sign
&& negative
) retval
= -retval
;
160 for (i
= 2; i
<= last
; i
++) {
165 if (retval
> hexmax
) return false;
167 retval
+= Hex2Int(c
);
171 for (i
= 2; i
<= last
; i
++) {
176 if (retval
> octmax
) return false;
183 for (i
= 2; i
< last
; i
++) {
188 if (retval
> decmax
) return false;
189 bool atmax
= retval
== decmax
;
192 if (atmax
&& retval
< decmax
) return false;
193 if (sign
&& (retval
& ((T
)1 << (sizeof(T
) * 8 - 1))))
200 if (retval
> decmax
) return false;
201 bool atmax
= retval
== decmax
;
204 if (atmax
&& retval
< decmax
) return false;
205 if (sign
&& negative
) {
206 if ((retval
& ((T
)1 << (sizeof(T
) * 8 - 1))) &&
207 retval
>= (T
)-signmax
)
212 if (sign
&& (retval
& ((T
)1 << ((sizeof(T
) * 8) - 1))))
218 signed long long mult
= 1;
223 if (i
!= last
) return false;
225 val
= signmax
/ mult
;
229 if (i
!= last
) return false;
231 val
= signmax
/ mult
;
235 if (i
!= last
) return false;
236 mult
= 1024 * 1024 * 1024;
237 val
= signmax
/ mult
;
241 if (i
>= last
) return false;
244 for (i
++; i
<= last
; i
++) {
253 for (i
= 0; i
< mult
; i
++) {
254 if (retval
> signmax
/ 10)
257 if (sign
&& (retval
& ((T
)1 << (sizeof(T
) * 8 - 1))))
260 if (sign
&& negative
) {
261 if ((retval
& ((T
)1 << (sizeof(T
) * 8 - 1))) &&
262 retval
>= (T
)-signmax
)
267 if (sign
&& (retval
& ((T
)1 << ((sizeof(T
) * 8) - 1))))
276 if (sign
&& negative
)
279 if (mult
> (unsigned long long)signmax
)
292 bool to_number<type>(const string &value, type &retval) \
293 { return __to_number(value, retval); }
295 STN(unsigned long long);
296 STN(signed long long);
307 bool to_number
<bool>(const string
&value
, bool &retval
)
309 string lowered
= to_lower(value
);
321 if (lowered
== "false") {
326 if (lowered
== "true"){
331 if (lowered
== "no") {
336 if (lowered
== "yes"){