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> |
11 | 11 |
12 #include <limits> | 12 #include <limits> |
13 | 13 |
14 #include "base/logging.h" | 14 #include "base/logging.h" |
15 #include "base/numerics/safe_conversions.h" | |
16 #include "base/numerics/safe_math.h" | 15 #include "base/numerics/safe_math.h" |
17 #include "base/scoped_clear_errno.h" | 16 #include "base/scoped_clear_errno.h" |
18 #include "base/strings/utf_string_conversions.h" | |
19 #include "base/third_party/dmg_fp/dmg_fp.h" | 17 #include "base/third_party/dmg_fp/dmg_fp.h" |
20 | 18 |
21 namespace base { | 19 namespace base { |
22 | 20 |
23 namespace { | 21 namespace { |
24 | 22 |
25 template <typename STR, typename INT> | 23 template <typename STR, typename INT> |
26 struct IntToStringT { | 24 struct IntToStringT { |
27 static STR IntToString(INT value) { | 25 static STR IntToString(INT value) { |
28 // log10(2) ~= 0.3 bytes needed per bit or per byte log10(2**8) ~= 2.4. | 26 // log10(2) ~= 0.3 bytes needed per bit or per byte log10(2**8) ~= 2.4. |
(...skipping 102 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
131 typedef IteratorRangeToNumberTraits traits; | 129 typedef IteratorRangeToNumberTraits traits; |
132 typedef typename traits::iterator_type const_iterator; | 130 typedef typename traits::iterator_type const_iterator; |
133 typedef typename traits::value_type value_type; | 131 typedef typename traits::value_type value_type; |
134 | 132 |
135 // Generalized iterator-range-to-number conversion. | 133 // Generalized iterator-range-to-number conversion. |
136 // | 134 // |
137 static bool Invoke(const_iterator begin, | 135 static bool Invoke(const_iterator begin, |
138 const_iterator end, | 136 const_iterator end, |
139 value_type* output) { | 137 value_type* output) { |
140 bool valid = true; | 138 bool valid = true; |
141 | |
scottmg
2016/06/14 15:58:48
nit; probably no need to delete these blank lines.
ananta
2016/06/14 19:13:15
Done.
| |
142 while (begin != end && LocalIsWhitespace(*begin)) { | 139 while (begin != end && LocalIsWhitespace(*begin)) { |
143 valid = false; | 140 valid = false; |
144 ++begin; | 141 ++begin; |
145 } | 142 } |
146 | 143 |
147 if (begin != end && *begin == '-') { | 144 if (begin != end && *begin == '-') { |
148 if (!std::numeric_limits<value_type>::is_signed) { | 145 if (!std::numeric_limits<value_type>::is_signed) { |
149 *output = 0; | 146 *output = 0; |
150 valid = false; | 147 valid = false; |
151 } else if (!Negative::Invoke(begin + 1, end, output)) { | 148 } else if (!Negative::Invoke(begin + 1, end, output)) { |
152 valid = false; | 149 valid = false; |
153 } | 150 } |
154 } else { | 151 } else { |
155 if (begin != end && *begin == '+') { | 152 if (begin != end && *begin == '+') { |
156 ++begin; | 153 ++begin; |
157 } | 154 } |
158 if (!Positive::Invoke(begin, end, output)) { | 155 if (!Positive::Invoke(begin, end, output)) { |
159 valid = false; | 156 valid = false; |
160 } | 157 } |
161 } | 158 } |
162 | |
163 return valid; | 159 return valid; |
164 } | 160 } |
165 | 161 |
166 private: | 162 private: |
167 // Sign provides: | 163 // Sign provides: |
168 // - a static function, CheckBounds, that determines whether the next digit | 164 // - a static function, CheckBounds, that determines whether the next digit |
169 // causes an overflow/underflow | 165 // causes an overflow/underflow |
170 // - a static function, Increment, that appends the next digit appropriately | 166 // - a static function, Increment, that appends the next digit appropriately |
171 // according to the sign of the number being parsed. | 167 // according to the sign of the number being parsed. |
172 template<typename Sign> | 168 template<typename Sign> |
(...skipping 304 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
477 bool HexStringToUInt64(const StringPiece& input, uint64_t* output) { | 473 bool HexStringToUInt64(const StringPiece& input, uint64_t* output) { |
478 return IteratorRangeToNumber<HexIteratorRangeToUInt64Traits>::Invoke( | 474 return IteratorRangeToNumber<HexIteratorRangeToUInt64Traits>::Invoke( |
479 input.begin(), input.end(), output); | 475 input.begin(), input.end(), output); |
480 } | 476 } |
481 | 477 |
482 bool HexStringToBytes(const std::string& input, std::vector<uint8_t>* output) { | 478 bool HexStringToBytes(const std::string& input, std::vector<uint8_t>* output) { |
483 return HexStringToBytesT(input, output); | 479 return HexStringToBytesT(input, output); |
484 } | 480 } |
485 | 481 |
486 } // namespace base | 482 } // namespace base |
OLD | NEW |