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 464 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 |