2 * Copyright (c) 2001-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 split_first(const string
&s
, string
&lhs
, string
&rhs
, char c
)
45 string::size_type offset
= s
.find(c
);
46 if (offset
== string::npos
) {
52 lhs
= s
.substr(0, offset
);
53 rhs
= s
.substr(offset
+ 1);
58 split_last(const string
&s
, string
&lhs
, string
&rhs
, char c
)
60 string::size_type offset
= s
.rfind(c
);
61 if (offset
== string::npos
) {
67 lhs
= s
.substr(0, offset
);
68 rhs
= s
.substr(offset
+ 1);
73 tokenize(vector
<string
>& v
, const string
&s
, char token
, bool ignore
)
75 string::size_type first
= 0;
76 string::size_type last
= s
.find_first_of(token
);
81 last
= s
.find_first_of(token
, ++first
);
83 if (last
== string::npos
) {
90 while (last
!= string::npos
) {
91 v
.push_back(s
.substr(first
, last
- first
));
94 first
= s
.find_first_not_of(token
, last
+ 1);
96 if (first
== string::npos
)
101 last
= s
.find_first_of(token
, first
);
104 v
.push_back(s
.substr(first
));
108 * @todo This function will not handle the smallest negative decimal
109 * value for a signed type
114 __to_number(string value
, T
&retval
)
116 static const T maxnum
= ((T
)-1);
117 static const bool sign
= maxnum
< 0;
118 static const int bits
= sizeof(T
) * 8;
119 static const T hexmax
= maxnum
& (((T
)1 << (bits
- 4 - sign
)) - 1);
120 static const T octmax
= maxnum
& (((T
)1 << (bits
- 3 - sign
)) - 1);
121 static const T signmax
=
122 (sign
) ? maxnum
& (((T
)1 << (bits
- 1)) - 1) : maxnum
;
123 static const T decmax
= signmax
/ 10;
126 cout
<< "maxnum = 0x" << hex
<< (unsigned long long)maxnum
<< "\n"
127 << "sign = 0x" << hex
<< (unsigned long long)sign
<< "\n"
128 << "hexmax = 0x" << hex
<< (unsigned long long)hexmax
<< "\n"
129 << "octmax = 0x" << hex
<< (unsigned long long)octmax
<< "\n"
130 << "signmax = 0x" << hex
<< (unsigned long long)signmax
<< "\n"
131 << "decmax = 0x" << hex
<< (unsigned long long)decmax
<< "\n";
136 bool negative
= false;
139 int last
= value
.size() - 1;
145 if (c
== '-' && sign
)
152 if (last
== 0) return true;
160 if (sign
&& negative
)
164 if (c
== 'X' || c
== 'x') {
172 } else if (!IsDec(c
))
175 if (sign
&& negative
&& c
== '0')
181 if (sign
&& negative
) retval
= -retval
;
190 for (i
= 2; i
<= last
; i
++) {
195 if (retval
> hexmax
) return false;
197 retval
+= Hex2Int(c
);
201 for (i
= 2; i
<= last
; i
++) {
206 if (retval
> octmax
) return false;
213 for (i
= 2; i
< last
; i
++) {
218 if (retval
> decmax
) return false;
219 bool atmax
= retval
== decmax
;
222 if (atmax
&& retval
< decmax
) return false;
223 if (sign
&& (retval
& ((T
)1 << (sizeof(T
) * 8 - 1))))
230 if (retval
> decmax
) return false;
231 bool atmax
= retval
== decmax
;
234 if (atmax
&& retval
< decmax
) return false;
235 if (sign
&& negative
) {
236 if ((retval
& ((T
)1 << (sizeof(T
) * 8 - 1))) &&
237 retval
>= (T
)-signmax
)
242 if (sign
&& (retval
& ((T
)1 << ((sizeof(T
) * 8) - 1))))
248 signed long long mult
= 1;
253 if (i
!= last
) return false;
255 val
= signmax
/ mult
;
259 if (i
!= last
) return false;
261 val
= signmax
/ mult
;
265 if (i
!= last
) return false;
266 mult
= 1024 * 1024 * 1024;
267 val
= signmax
/ mult
;
271 if (i
>= last
) return false;
274 for (i
++; i
<= last
; i
++) {
283 for (i
= 0; i
< mult
; i
++) {
284 if (retval
> signmax
/ 10)
287 if (sign
&& (retval
& ((T
)1 << (sizeof(T
) * 8 - 1))))
290 if (sign
&& negative
) {
291 if ((retval
& ((T
)1 << (sizeof(T
) * 8 - 1))) &&
292 retval
>= (T
)-signmax
)
297 if (sign
&& (retval
& ((T
)1 << ((sizeof(T
) * 8) - 1))))
306 if (sign
&& negative
)
309 if (mult
> (unsigned long long)signmax
)
322 bool to_number<type>(const string &value, type &retval) \
323 { return __to_number(value, retval); }
325 STN(unsigned long long);
326 STN(signed long long);
337 bool to_number
<bool>(const string
&value
, bool &retval
)
339 string lowered
= to_lower(value
);
351 if (lowered
== "false") {
356 if (lowered
== "true"){
361 if (lowered
== "no") {
366 if (lowered
== "yes"){