From a6441123bb35a7c11b4717188ab5ae2ae81102e9 Mon Sep 17 00:00:00 2001 From: Maxim Pimenov Date: Fri, 30 Oct 2020 13:06:22 +0300 Subject: [PATCH] [base] Rewrote to_{u}int32 by using internal::ToInteger. --- base/string_utils.cpp | 28 ---------------------------- 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 2bae33cbfe1..e5276666f03 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(std::numeric_limits::max())) - return false; - - i = static_cast(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(std::numeric_limits::max())) - return false; - if (num < static_cast(std::numeric_limits::min())) - return false; - - i = static_cast(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 04611b75383..19a2296cbab 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);