diff options
author | Maxim Pimenov <m@maps.me> | 2020-10-30 13:06:22 +0300 |
---|---|---|
committer | Tatiana Yan <tatiana.kondakova@gmail.com> | 2021-02-16 16:27:13 +0300 |
commit | a6441123bb35a7c11b4717188ab5ae2ae81102e9 (patch) | |
tree | 6946566de69d54bb04b1ad193b7b83d401bb5b15 | |
parent | d62567cb0b98deeeaf665e9395556b24f7a4512a (diff) |
[base] Rewrote to_{u}int32 by using internal::ToInteger.
-rw-r--r-- | base/string_utils.cpp | 28 | ||||
-rw-r--r-- | base/string_utils.hpp | 19 |
2 files changed, 16 insertions, 31 deletions
diff --git a/base/string_utils.cpp b/base/string_utils.cpp index 2bae33cbfe..e5276666f0 100644 --- a/base/string_utils.cpp +++ b/base/string_utils.cpp @@ -81,34 +81,6 @@ UniChar LastUniChar(std::string const & s) return *iter; } -bool to_uint32(char const * start, uint32_t & i, int base) -{ - uint64_t num = 0; - if (!to_uint64(start, num, base)) - return false; - - if (num > static_cast<uint64_t>(std::numeric_limits<uint32_t>::max())) - return false; - - i = static_cast<uint32_t>(num); - return true; -} - -bool to_int32(char const * start, int32_t & i) -{ - int64_t num = 0; - if (!to_int64(start, num)) - return false; - - if (num > static_cast<int64_t>(std::numeric_limits<int32_t>::max())) - return false; - if (num < static_cast<int64_t>(std::numeric_limits<int32_t>::min())) - return false; - - i = static_cast<int32_t>(num); - return true; -} - bool to_size_t(char const * start, size_t & i, int base) { uint64_t num = 0; diff --git a/base/string_utils.hpp b/base/string_utils.hpp index 04611b7538..19a2296cba 100644 --- a/base/string_utils.hpp +++ b/base/string_utils.hpp @@ -458,9 +458,22 @@ WARN_UNUSED_RESULT inline bool to_int64(char const * s, int64_t & i) return internal::ToInteger(s, i); } -// Unlike the 64-bit version, to_uint32 will not convert negative values. -WARN_UNUSED_RESULT bool to_uint32(char const * s, uint32_t & i, int base = 10); -WARN_UNUSED_RESULT bool to_int32(char const * s, int32_t & i); +// Unlike the 64-bit version, to_uint32 is not guaranteed to convert negative values. +// Current implementation conflates fixed-width types (uint32, uint64) with types that have no +// guarantees on their exact sizes (unsigned long, unsigned long long) so results of internal +// conversions may differ between platforms. +// Converting strings representing negative numbers to unsigned integers looks like a bad +// idea anyway and it's not worth changing the implementation solely for this reason. +WARN_UNUSED_RESULT inline bool to_uint32(char const * s, uint32_t & i, int base = 10) +{ + return internal::ToInteger(s, i, base); +} + +WARN_UNUSED_RESULT inline bool to_int32(char const * s, int32_t & i) +{ + return internal::ToInteger(s, i); +} + WARN_UNUSED_RESULT bool to_size_t(char const * s, size_t & i, int base = 10); WARN_UNUSED_RESULT bool to_float(char const * s, float & f); WARN_UNUSED_RESULT bool to_double(char const * s, double & d); |