45d3107b087964c4eba73c203dbdeb3a524b245d
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
= numeric_limits
<T
>::is_signed
;
121 static const int bits
= numeric_limits
<T
>::digits
;
122 static const T hexmax
= maxnum
& (((T
)1 << (bits
- 4)) - 1);
123 static const T octmax
= maxnum
& (((T
)1 << (bits
- 3)) - 1);
124 static const T signmax
= numeric_limits
<T
>::max();
125 static const T decmax
= signmax
/ 10;
128 cout
<< "maxnum = 0x" << hex
<< (unsigned long long)maxnum
<< "\n"
129 << "sign = 0x" << hex
<< (unsigned long long)sign
<< "\n"
130 << "hexmax = 0x" << hex
<< (unsigned long long)hexmax
<< "\n"
131 << "octmax = 0x" << hex
<< (unsigned long long)octmax
<< "\n"
132 << "signmax = 0x" << hex
<< (unsigned long long)signmax
<< "\n"
133 << "decmax = 0x" << hex
<< (unsigned long long)decmax
<< "\n";
138 bool negative
= false;
141 int last
= value
.size() - 1;
147 if (c
== '-' && sign
)
154 if (last
== 0) return true;
162 if (sign
&& negative
)
166 if (c
== 'X' || c
== 'x') {
174 } else if (!isDec(c
))
177 if (sign
&& negative
&& c
== '0')
183 if (sign
&& negative
) retval
= -retval
;
192 for (i
= 2; i
<= last
; i
++) {
197 if (retval
> hexmax
) return false;
199 retval
+= hex2Int(c
);
203 for (i
= 2; i
<= last
; i
++) {
208 if (retval
> octmax
) return false;
215 for (i
= 2; i
< last
; i
++) {
220 if (retval
> decmax
) return false;
221 bool atmax
= retval
== decmax
;
224 if (atmax
&& retval
< decmax
) return false;
225 if (sign
&& (retval
& ((T
)1 << (sizeof(T
) * 8 - 1))))
232 if (retval
> decmax
) return false;
233 bool atmax
= retval
== decmax
;
236 if (atmax
&& retval
< decmax
) return false;
237 if (sign
&& negative
) {
238 if ((retval
& ((T
)1 << (sizeof(T
) * 8 - 1))) &&
239 retval
>= (T
)-signmax
)
244 if (sign
&& (retval
& ((T
)1 << ((sizeof(T
) * 8) - 1))))
250 signed long long mult
= 1;
255 if (i
!= last
) return false;
257 val
= signmax
/ mult
;
261 if (i
!= last
) return false;
263 val
= signmax
/ mult
;
267 if (i
!= last
) return false;
268 mult
= 1024 * 1024 * 1024;
269 val
= signmax
/ mult
;
273 if (i
>= last
) return false;
276 for (i
++; i
<= last
; i
++) {
285 for (i
= 0; i
< mult
; i
++) {
286 if (retval
> signmax
/ 10)
289 if (sign
&& (retval
& ((T
)1 << (sizeof(T
) * 8 - 1))))
292 if (sign
&& negative
) {
293 if ((retval
& ((T
)1 << (sizeof(T
) * 8 - 1))) &&
294 retval
>= (T
)-signmax
)
299 if (sign
&& (retval
& ((T
)1 << ((sizeof(T
) * 8) - 1))))
308 if (sign
&& negative
)
311 if (mult
> (unsigned long long)signmax
)
324 bool to_number<type>(const string &value, type &retval) \
325 { return __to_number(value, retval); }
327 STN(unsigned long long)
328 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"){