1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "base/strings/string_number_conversions.h"
14 #include "base/logging.h"
15 #include "base/scoped_clear_errno.h"
16 #include "base/strings/utf_string_conversions.h"
17 #include "base/third_party/dmg_fp/dmg_fp.h"
23 template <typename STR
, typename INT
, typename UINT
, bool NEG
>
25 // This is to avoid a compiler warning about unary minus on unsigned type.
26 // For example, say you had the following code:
27 // template <typename INT>
28 // INT abs(INT value) { return value < 0 ? -value : value; }
29 // Even though if INT is unsigned, it's impossible for value < 0, so the
30 // unary minus will never be taken, the compiler will still generate a
31 // warning. We do a little specialization dance...
32 template <typename INT2
, typename UINT2
, bool NEG2
>
33 struct ToUnsignedT
{};
35 template <typename INT2
, typename UINT2
>
36 struct ToUnsignedT
<INT2
, UINT2
, false> {
37 static UINT2
ToUnsigned(INT2 value
) {
38 return static_cast<UINT2
>(value
);
42 template <typename INT2
, typename UINT2
>
43 struct ToUnsignedT
<INT2
, UINT2
, true> {
44 static UINT2
ToUnsigned(INT2 value
) {
45 return static_cast<UINT2
>(value
< 0 ? -value
: value
);
49 // This set of templates is very similar to the above templates, but
50 // for testing whether an integer is negative.
51 template <typename INT2
, bool NEG2
>
53 template <typename INT2
>
54 struct TestNegT
<INT2
, false> {
55 static bool TestNeg(INT2 value
) {
56 // value is unsigned, and can never be negative.
60 template <typename INT2
>
61 struct TestNegT
<INT2
, true> {
62 static bool TestNeg(INT2 value
) {
67 static STR
IntToString(INT value
) {
68 // log10(2) ~= 0.3 bytes needed per bit or per byte log10(2**8) ~= 2.4.
69 // So round up to allocate 3 output characters per byte, plus 1 for '-'.
70 const int kOutputBufSize
= 3 * sizeof(INT
) + 1;
72 // Allocate the whole string right away, we will right back to front, and
73 // then return the substr of what we ended up using.
74 STR
outbuf(kOutputBufSize
, 0);
76 bool is_neg
= TestNegT
<INT
, NEG
>::TestNeg(value
);
77 // Even though is_neg will never be true when INT is parameterized as
78 // unsigned, even the presence of the unary operation causes a warning.
79 UINT res
= ToUnsignedT
<INT
, UINT
, NEG
>::ToUnsigned(value
);
81 typename
STR::iterator
it(outbuf
.end());
84 DCHECK(it
!= outbuf
.begin());
85 *it
= static_cast<typename
STR::value_type
>((res
% 10) + '0');
90 DCHECK(it
!= outbuf
.begin());
91 *it
= static_cast<typename
STR::value_type
>('-');
93 return STR(it
, outbuf
.end());
97 // Utility to convert a character to a digit in a given base
98 template<typename CHAR
, int BASE
, bool BASE_LTE_10
> class BaseCharToDigit
{
101 // Faster specialization for bases <= 10
102 template<typename CHAR
, int BASE
> class BaseCharToDigit
<CHAR
, BASE
, true> {
104 static bool Convert(CHAR c
, uint8
* digit
) {
105 if (c
>= '0' && c
< '0' + BASE
) {
106 *digit
= static_cast<uint8
>(c
- '0');
113 // Specialization for bases where 10 < base <= 36
114 template<typename CHAR
, int BASE
> class BaseCharToDigit
<CHAR
, BASE
, false> {
116 static bool Convert(CHAR c
, uint8
* digit
) {
117 if (c
>= '0' && c
<= '9') {
119 } else if (c
>= 'a' && c
< 'a' + BASE
- 10) {
120 *digit
= c
- 'a' + 10;
121 } else if (c
>= 'A' && c
< 'A' + BASE
- 10) {
122 *digit
= c
- 'A' + 10;
130 template<int BASE
, typename CHAR
> bool CharToDigit(CHAR c
, uint8
* digit
) {
131 return BaseCharToDigit
<CHAR
, BASE
, BASE
<= 10>::Convert(c
, digit
);
134 // There is an IsWhitespace for wchars defined in string_util.h, but it is
135 // locale independent, whereas the functions we are replacing were
136 // locale-dependent. TBD what is desired, but for the moment let's not introduce
137 // a change in behaviour.
138 template<typename CHAR
> class WhitespaceHelper
{
141 template<> class WhitespaceHelper
<char> {
143 static bool Invoke(char c
) {
144 return 0 != isspace(static_cast<unsigned char>(c
));
148 template<> class WhitespaceHelper
<char16
> {
150 static bool Invoke(char16 c
) {
151 return 0 != iswspace(c
);
155 template<typename CHAR
> bool LocalIsWhitespace(CHAR c
) {
156 return WhitespaceHelper
<CHAR
>::Invoke(c
);
159 // IteratorRangeToNumberTraits should provide:
160 // - a typedef for iterator_type, the iterator type used as input.
161 // - a typedef for value_type, the target numeric type.
162 // - static functions min, max (returning the minimum and maximum permitted
164 // - constant kBase, the base in which to interpret the input
165 template<typename IteratorRangeToNumberTraits
>
166 class IteratorRangeToNumber
{
168 typedef IteratorRangeToNumberTraits traits
;
169 typedef typename
traits::iterator_type const_iterator
;
170 typedef typename
traits::value_type value_type
;
172 // Generalized iterator-range-to-number conversion.
174 static bool Invoke(const_iterator begin
,
176 value_type
* output
) {
179 while (begin
!= end
&& LocalIsWhitespace(*begin
)) {
184 if (begin
!= end
&& *begin
== '-') {
185 if (!std::numeric_limits
<value_type
>::is_signed
) {
187 } else if (!Negative::Invoke(begin
+ 1, end
, output
)) {
191 if (begin
!= end
&& *begin
== '+') {
194 if (!Positive::Invoke(begin
, end
, output
)) {
204 // - a static function, CheckBounds, that determines whether the next digit
205 // causes an overflow/underflow
206 // - a static function, Increment, that appends the next digit appropriately
207 // according to the sign of the number being parsed.
208 template<typename Sign
>
211 static bool Invoke(const_iterator begin
, const_iterator end
,
212 typename
traits::value_type
* output
) {
219 // Note: no performance difference was found when using template
220 // specialization to remove this check in bases other than 16
221 if (traits::kBase
== 16 && end
- begin
> 2 && *begin
== '0' &&
222 (*(begin
+ 1) == 'x' || *(begin
+ 1) == 'X')) {
226 for (const_iterator current
= begin
; current
!= end
; ++current
) {
229 if (!CharToDigit
<traits::kBase
>(*current
, &new_digit
)) {
233 if (current
!= begin
) {
234 if (!Sign::CheckBounds(output
, new_digit
)) {
237 *output
*= traits::kBase
;
240 Sign::Increment(new_digit
, output
);
246 class Positive
: public Base
<Positive
> {
248 static bool CheckBounds(value_type
* output
, uint8 new_digit
) {
249 if (*output
> static_cast<value_type
>(traits::max() / traits::kBase
) ||
250 (*output
== static_cast<value_type
>(traits::max() / traits::kBase
) &&
251 new_digit
> traits::max() % traits::kBase
)) {
252 *output
= traits::max();
257 static void Increment(uint8 increment
, value_type
* output
) {
258 *output
+= increment
;
262 class Negative
: public Base
<Negative
> {
264 static bool CheckBounds(value_type
* output
, uint8 new_digit
) {
265 if (*output
< traits::min() / traits::kBase
||
266 (*output
== traits::min() / traits::kBase
&&
267 new_digit
> 0 - traits::min() % traits::kBase
)) {
268 *output
= traits::min();
273 static void Increment(uint8 increment
, value_type
* output
) {
274 *output
-= increment
;
279 template<typename ITERATOR
, typename VALUE
, int BASE
>
280 class BaseIteratorRangeToNumberTraits
{
282 typedef ITERATOR iterator_type
;
283 typedef VALUE value_type
;
284 static value_type
min() {
285 return std::numeric_limits
<value_type
>::min();
287 static value_type
max() {
288 return std::numeric_limits
<value_type
>::max();
290 static const int kBase
= BASE
;
293 template<typename ITERATOR
>
294 class BaseHexIteratorRangeToIntTraits
295 : public BaseIteratorRangeToNumberTraits
<ITERATOR
, int, 16> {
298 template<typename ITERATOR
>
299 class BaseHexIteratorRangeToUIntTraits
300 : public BaseIteratorRangeToNumberTraits
<ITERATOR
, uint32
, 16> {
303 template<typename ITERATOR
>
304 class BaseHexIteratorRangeToInt64Traits
305 : public BaseIteratorRangeToNumberTraits
<ITERATOR
, int64
, 16> {
308 template<typename ITERATOR
>
309 class BaseHexIteratorRangeToUInt64Traits
310 : public BaseIteratorRangeToNumberTraits
<ITERATOR
, uint64
, 16> {
313 typedef BaseHexIteratorRangeToIntTraits
<StringPiece::const_iterator
>
314 HexIteratorRangeToIntTraits
;
316 typedef BaseHexIteratorRangeToUIntTraits
<StringPiece::const_iterator
>
317 HexIteratorRangeToUIntTraits
;
319 typedef BaseHexIteratorRangeToInt64Traits
<StringPiece::const_iterator
>
320 HexIteratorRangeToInt64Traits
;
322 typedef BaseHexIteratorRangeToUInt64Traits
<StringPiece::const_iterator
>
323 HexIteratorRangeToUInt64Traits
;
325 template<typename STR
>
326 bool HexStringToBytesT(const STR
& input
, std::vector
<uint8
>* output
) {
327 DCHECK_EQ(output
->size(), 0u);
328 size_t count
= input
.size();
329 if (count
== 0 || (count
% 2) != 0)
331 for (uintptr_t i
= 0; i
< count
/ 2; ++i
) {
332 uint8 msb
= 0; // most significant 4 bits
333 uint8 lsb
= 0; // least significant 4 bits
334 if (!CharToDigit
<16>(input
[i
* 2], &msb
) ||
335 !CharToDigit
<16>(input
[i
* 2 + 1], &lsb
))
337 output
->push_back((msb
<< 4) | lsb
);
342 template <typename VALUE
, int BASE
>
343 class StringPieceToNumberTraits
344 : public BaseIteratorRangeToNumberTraits
<StringPiece::const_iterator
,
349 template <typename VALUE
>
350 bool StringToIntImpl(const StringPiece
& input
, VALUE
* output
) {
351 return IteratorRangeToNumber
<StringPieceToNumberTraits
<VALUE
, 10> >::Invoke(
352 input
.begin(), input
.end(), output
);
355 template <typename VALUE
, int BASE
>
356 class StringPiece16ToNumberTraits
357 : public BaseIteratorRangeToNumberTraits
<StringPiece16::const_iterator
,
362 template <typename VALUE
>
363 bool String16ToIntImpl(const StringPiece16
& input
, VALUE
* output
) {
364 return IteratorRangeToNumber
<StringPiece16ToNumberTraits
<VALUE
, 10> >::Invoke(
365 input
.begin(), input
.end(), output
);
370 std::string
IntToString(int value
) {
371 return IntToStringT
<std::string
, int, unsigned int, true>::
375 string16
IntToString16(int value
) {
376 return IntToStringT
<string16
, int, unsigned int, true>::
380 std::string
UintToString(unsigned int value
) {
381 return IntToStringT
<std::string
, unsigned int, unsigned int, false>::
385 string16
UintToString16(unsigned int value
) {
386 return IntToStringT
<string16
, unsigned int, unsigned int, false>::
390 std::string
Int64ToString(int64 value
) {
391 return IntToStringT
<std::string
, int64
, uint64
, true>::IntToString(value
);
394 string16
Int64ToString16(int64 value
) {
395 return IntToStringT
<string16
, int64
, uint64
, true>::IntToString(value
);
398 std::string
Uint64ToString(uint64 value
) {
399 return IntToStringT
<std::string
, uint64
, uint64
, false>::IntToString(value
);
402 string16
Uint64ToString16(uint64 value
) {
403 return IntToStringT
<string16
, uint64
, uint64
, false>::IntToString(value
);
406 std::string
SizeTToString(size_t value
) {
407 return IntToStringT
<std::string
, size_t, size_t, false>::IntToString(value
);
410 string16
SizeTToString16(size_t value
) {
411 return IntToStringT
<string16
, size_t, size_t, false>::IntToString(value
);
414 std::string
DoubleToString(double value
) {
415 // According to g_fmt.cc, it is sufficient to declare a buffer of size 32.
417 dmg_fp::g_fmt(buffer
, value
);
418 return std::string(buffer
);
421 bool StringToInt(const StringPiece
& input
, int* output
) {
422 return StringToIntImpl(input
, output
);
425 bool StringToInt(const StringPiece16
& input
, int* output
) {
426 return String16ToIntImpl(input
, output
);
429 bool StringToUint(const StringPiece
& input
, unsigned* output
) {
430 return StringToIntImpl(input
, output
);
433 bool StringToUint(const StringPiece16
& input
, unsigned* output
) {
434 return String16ToIntImpl(input
, output
);
437 bool StringToInt64(const StringPiece
& input
, int64
* output
) {
438 return StringToIntImpl(input
, output
);
441 bool StringToInt64(const StringPiece16
& input
, int64
* output
) {
442 return String16ToIntImpl(input
, output
);
445 bool StringToUint64(const StringPiece
& input
, uint64
* output
) {
446 return StringToIntImpl(input
, output
);
449 bool StringToUint64(const StringPiece16
& input
, uint64
* output
) {
450 return String16ToIntImpl(input
, output
);
453 bool StringToSizeT(const StringPiece
& input
, size_t* output
) {
454 return StringToIntImpl(input
, output
);
457 bool StringToSizeT(const StringPiece16
& input
, size_t* output
) {
458 return String16ToIntImpl(input
, output
);
461 bool StringToDouble(const std::string
& input
, double* output
) {
462 // Thread-safe? It is on at least Mac, Linux, and Windows.
463 ScopedClearErrno clear_errno
;
466 *output
= dmg_fp::strtod(input
.c_str(), &endptr
);
468 // Cases to return false:
469 // - If errno is ERANGE, there was an overflow or underflow.
470 // - If the input string is empty, there was nothing to parse.
471 // - If endptr does not point to the end of the string, there are either
472 // characters remaining in the string after a parsed number, or the string
473 // does not begin with a parseable number. endptr is compared to the
474 // expected end given the string's stated length to correctly catch cases
475 // where the string contains embedded NUL characters.
476 // - If the first character is a space, there was leading whitespace
479 input
.c_str() + input
.length() == endptr
&&
483 // Note: if you need to add String16ToDouble, first ask yourself if it's
484 // really necessary. If it is, probably the best implementation here is to
485 // convert to 8-bit and then use the 8-bit version.
487 // Note: if you need to add an iterator range version of StringToDouble, first
488 // ask yourself if it's really necessary. If it is, probably the best
489 // implementation here is to instantiate a string and use the string version.
491 std::string
HexEncode(const void* bytes
, size_t size
) {
492 static const char kHexChars
[] = "0123456789ABCDEF";
494 // Each input byte creates two output hex characters.
495 std::string
ret(size
* 2, '\0');
497 for (size_t i
= 0; i
< size
; ++i
) {
498 char b
= reinterpret_cast<const char*>(bytes
)[i
];
499 ret
[(i
* 2)] = kHexChars
[(b
>> 4) & 0xf];
500 ret
[(i
* 2) + 1] = kHexChars
[b
& 0xf];
505 bool HexStringToInt(const StringPiece
& input
, int* output
) {
506 return IteratorRangeToNumber
<HexIteratorRangeToIntTraits
>::Invoke(
507 input
.begin(), input
.end(), output
);
510 bool HexStringToUInt(const StringPiece
& input
, uint32
* output
) {
511 return IteratorRangeToNumber
<HexIteratorRangeToUIntTraits
>::Invoke(
512 input
.begin(), input
.end(), output
);
515 bool HexStringToInt64(const StringPiece
& input
, int64
* output
) {
516 return IteratorRangeToNumber
<HexIteratorRangeToInt64Traits
>::Invoke(
517 input
.begin(), input
.end(), output
);
520 bool HexStringToUInt64(const StringPiece
& input
, uint64
* output
) {
521 return IteratorRangeToNumber
<HexIteratorRangeToUInt64Traits
>::Invoke(
522 input
.begin(), input
.end(), output
);
525 bool HexStringToBytes(const std::string
& input
, std::vector
<uint8
>* output
) {
526 return HexStringToBytesT(input
, output
);