OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "base/strings/string_number_conversions.h" | 5 #include "base/strings/string_number_conversions.h" |
6 | 6 |
7 #include <ctype.h> | 7 #include <ctype.h> |
8 #include <errno.h> | 8 #include <errno.h> |
9 #include <stdlib.h> | 9 #include <stdlib.h> |
10 #include <wctype.h> | 10 #include <wctype.h> |
(...skipping 24 matching lines...) Expand all Loading... |
35 template <typename INT2, typename UINT2> | 35 template <typename INT2, typename UINT2> |
36 struct ToUnsignedT<INT2, UINT2, false> { | 36 struct ToUnsignedT<INT2, UINT2, false> { |
37 static UINT2 ToUnsigned(INT2 value) { | 37 static UINT2 ToUnsigned(INT2 value) { |
38 return static_cast<UINT2>(value); | 38 return static_cast<UINT2>(value); |
39 } | 39 } |
40 }; | 40 }; |
41 | 41 |
42 template <typename INT2, typename UINT2> | 42 template <typename INT2, typename UINT2> |
43 struct ToUnsignedT<INT2, UINT2, true> { | 43 struct ToUnsignedT<INT2, UINT2, true> { |
44 static UINT2 ToUnsigned(INT2 value) { | 44 static UINT2 ToUnsigned(INT2 value) { |
45 return static_cast<UINT2>(value < 0 ? -value : value); | 45 if (value >= 0) { |
| 46 return value; |
| 47 } else { |
| 48 // Avoid integer overflow when negating INT_MIN. |
| 49 return static_cast<UINT2>(-(value + 1)) + 1; |
| 50 } |
46 } | 51 } |
47 }; | 52 }; |
48 | 53 |
49 // This set of templates is very similar to the above templates, but | 54 // This set of templates is very similar to the above templates, but |
50 // for testing whether an integer is negative. | 55 // for testing whether an integer is negative. |
51 template <typename INT2, bool NEG2> | 56 template <typename INT2, bool NEG2> |
52 struct TestNegT {}; | 57 struct TestNegT {}; |
53 template <typename INT2> | 58 template <typename INT2> |
54 struct TestNegT<INT2, false> { | 59 struct TestNegT<INT2, false> { |
55 static bool TestNeg(INT2 value) { | 60 static bool TestNeg(INT2 value) { |
(...skipping 68 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
124 return false; | 129 return false; |
125 } | 130 } |
126 return true; | 131 return true; |
127 } | 132 } |
128 }; | 133 }; |
129 | 134 |
130 template<int BASE, typename CHAR> bool CharToDigit(CHAR c, uint8* digit) { | 135 template<int BASE, typename CHAR> bool CharToDigit(CHAR c, uint8* digit) { |
131 return BaseCharToDigit<CHAR, BASE, BASE <= 10>::Convert(c, digit); | 136 return BaseCharToDigit<CHAR, BASE, BASE <= 10>::Convert(c, digit); |
132 } | 137 } |
133 | 138 |
134 // There is an IsWhitespace for wchars defined in string_util.h, but it is | 139 // There is an IsUnicodeWhitespace for wchars defined in string_util.h, but it |
135 // locale independent, whereas the functions we are replacing were | 140 // is locale independent, whereas the functions we are replacing were |
136 // locale-dependent. TBD what is desired, but for the moment let's not introduce | 141 // locale-dependent. TBD what is desired, but for the moment let's not |
137 // a change in behaviour. | 142 // introduce a change in behaviour. |
138 template<typename CHAR> class WhitespaceHelper { | 143 template<typename CHAR> class WhitespaceHelper { |
139 }; | 144 }; |
140 | 145 |
141 template<> class WhitespaceHelper<char> { | 146 template<> class WhitespaceHelper<char> { |
142 public: | 147 public: |
143 static bool Invoke(char c) { | 148 static bool Invoke(char c) { |
144 return 0 != isspace(static_cast<unsigned char>(c)); | 149 return 0 != isspace(static_cast<unsigned char>(c)); |
145 } | 150 } |
146 }; | 151 }; |
147 | 152 |
(...skipping 372 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
520 bool HexStringToUInt64(const StringPiece& input, uint64* output) { | 525 bool HexStringToUInt64(const StringPiece& input, uint64* output) { |
521 return IteratorRangeToNumber<HexIteratorRangeToUInt64Traits>::Invoke( | 526 return IteratorRangeToNumber<HexIteratorRangeToUInt64Traits>::Invoke( |
522 input.begin(), input.end(), output); | 527 input.begin(), input.end(), output); |
523 } | 528 } |
524 | 529 |
525 bool HexStringToBytes(const std::string& input, std::vector<uint8>* output) { | 530 bool HexStringToBytes(const std::string& input, std::vector<uint8>* output) { |
526 return HexStringToBytesT(input, output); | 531 return HexStringToBytesT(input, output); |
527 } | 532 } |
528 | 533 |
529 } // namespace base | 534 } // namespace base |
OLD | NEW |