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.
28 * Authors: Nathan Binkert
38 #include "base/intmath.hh"
39 #include "base/str.hh"
44 split_first(const string
&s
, string
&lhs
, string
&rhs
, char c
)
46 string::size_type offset
= s
.find(c
);
47 if (offset
== string::npos
) {
53 lhs
= s
.substr(0, offset
);
54 rhs
= s
.substr(offset
+ 1);
59 split_last(const string
&s
, string
&lhs
, string
&rhs
, char c
)
61 string::size_type offset
= s
.rfind(c
);
62 if (offset
== string::npos
) {
68 lhs
= s
.substr(0, offset
);
69 rhs
= s
.substr(offset
+ 1);
74 tokenize(vector
<string
>& v
, const string
&s
, char token
, bool ignore
)
76 string::size_type first
= 0;
77 string::size_type last
= s
.find_first_of(token
);
82 if (ignore
&& last
== first
) {
84 last
= s
.find_first_of(token
, ++first
);
86 if (last
== string::npos
) {
87 if (first
!= s
.size())
88 v
.push_back(s
.substr(first
));
93 while (last
!= string::npos
) {
94 v
.push_back(s
.substr(first
, last
- first
));
97 first
= s
.find_first_not_of(token
, last
+ 1);
99 if (first
== string::npos
)
104 last
= s
.find_first_of(token
, first
);
107 v
.push_back(s
.substr(first
));
111 * @todo This function will not handle the smallest negative decimal
112 * value for a signed type
117 __to_number(string value
, T
&retval
)
119 static const T maxnum
= ((T
)-1);
120 static const bool sign
= maxnum
< 0;
121 static const int bits
= sizeof(T
) * 8;
122 static const T hexmax
= maxnum
& (((T
)1 << (bits
- 4 - sign
)) - 1);
123 static const T octmax
= maxnum
& (((T
)1 << (bits
- 3 - sign
)) - 1);
124 static const T signmax
=
125 (sign
) ? maxnum
& (((T
)1 << (bits
- 1)) - 1) : maxnum
;
126 static const T decmax
= signmax
/ 10;
129 cout
<< "maxnum = 0x" << hex
<< (unsigned long long)maxnum
<< "\n"
130 << "sign = 0x" << hex
<< (unsigned long long)sign
<< "\n"
131 << "hexmax = 0x" << hex
<< (unsigned long long)hexmax
<< "\n"
132 << "octmax = 0x" << hex
<< (unsigned long long)octmax
<< "\n"
133 << "signmax = 0x" << hex
<< (unsigned long long)signmax
<< "\n"
134 << "decmax = 0x" << hex
<< (unsigned long long)decmax
<< "\n";
139 bool negative
= false;
142 int last
= value
.size() - 1;
148 if (c
== '-' && sign
)
155 if (last
== 0) return true;
163 if (sign
&& negative
)
167 if (c
== 'X' || c
== 'x') {
175 } else if (!isDec(c
))
178 if (sign
&& negative
&& c
== '0')
184 if (sign
&& negative
) retval
= -retval
;
193 for (i
= 2; i
<= last
; i
++) {
198 if (retval
> hexmax
) return false;
200 retval
+= hex2Int(c
);
204 for (i
= 2; i
<= last
; i
++) {
209 if (retval
> octmax
) return false;
216 for (i
= 2; i
< last
; i
++) {
221 if (retval
> decmax
) return false;
222 bool atmax
= retval
== decmax
;
225 if (atmax
&& retval
< decmax
) return false;
226 if (sign
&& (retval
& ((T
)1 << (sizeof(T
) * 8 - 1))))
233 if (retval
> decmax
) return false;
234 bool atmax
= retval
== decmax
;
237 if (atmax
&& retval
< decmax
) return false;
238 if (sign
&& negative
) {
239 if ((retval
& ((T
)1 << (sizeof(T
) * 8 - 1))) &&
240 retval
>= (T
)-signmax
)
245 if (sign
&& (retval
& ((T
)1 << ((sizeof(T
) * 8) - 1))))
251 signed long long mult
= 1;
256 if (i
!= last
) return false;
258 val
= signmax
/ mult
;
262 if (i
!= last
) return false;
264 val
= signmax
/ mult
;
268 if (i
!= last
) return false;
269 mult
= 1024 * 1024 * 1024;
270 val
= signmax
/ mult
;
274 if (i
>= last
) return false;
277 for (i
++; i
<= last
; i
++) {
286 for (i
= 0; i
< mult
; i
++) {
287 if (retval
> signmax
/ 10)
290 if (sign
&& (retval
& ((T
)1 << (sizeof(T
) * 8 - 1))))
293 if (sign
&& negative
) {
294 if ((retval
& ((T
)1 << (sizeof(T
) * 8 - 1))) &&
295 retval
>= (T
)-signmax
)
300 if (sign
&& (retval
& ((T
)1 << ((sizeof(T
) * 8) - 1))))
309 if (sign
&& negative
)
312 if (mult
> (unsigned long long)signmax
)
325 bool to_number<type>(const string &value, type &retval) \
326 { return __to_number(value, retval); }
328 STN(unsigned long long);
329 STN(signed long long);
340 bool to_number
<bool>(const string
&value
, bool &retval
)
342 string lowered
= to_lower(value
);
354 if (lowered
== "false") {
359 if (lowered
== "true"){
364 if (lowered
== "no") {
369 if (lowered
== "yes"){