From e68b9f992a386ddb2269faa50d28ffcdeff5a83d Mon Sep 17 00:00:00 2001 From: "Evgeniy A. Dushistov" Date: Sun, 2 May 2021 22:01:11 +0300 Subject: [PATCH] [base] use std::to_string instead of home made one for integers Signed-off-by: Evgeniy A. Dushistov --- base/string_utils.hpp | 64 ++----------------------------------------- 1 file changed, 3 insertions(+), 61 deletions(-) diff --git a/base/string_utils.hpp b/base/string_utils.hpp index 58d5ce89e7..635dd01646 100644 --- a/base/string_utils.hpp +++ b/base/string_utils.hpp @@ -566,67 +566,9 @@ WARN_UNUSED_RESULT inline bool to_any(std::string const & s, std::string & resul return true; } -namespace impl -{ -template -int UpperBoundOnChars() -{ - // It's wrong to return just numeric_limits::digits10 + [is - // signed] because digits10 for a type T is computed as: - // - // floor(log10(2 ^ (CHAR_BITS * sizeof(T)))) = - // floor(CHAR_BITS * sizeof(T) * log10(2)) - // - // Therefore, due to rounding, we need to compensate possible - // error. - // - // NOTE: following code works only on two-complement systems! - - return std::numeric_limits::digits10 + std::is_signed::value + 1; -} - -template -char * to_string_digits(char * buf, T i) -{ - do - { - --buf; - *buf = static_cast(i % 10) + '0'; - i = i / 10; - } while (i != 0); - return buf; -} - -template -std::string to_string_signed(T i) -{ - bool const negative = i < 0; - int const sz = UpperBoundOnChars(); - char buf[sz]; - char * end = buf + sz; - char * beg = to_string_digits(end, negative ? -i : i); - if (negative) - { - --beg; - *beg = '-'; - } - return std::string(beg, end - beg); -} - -template -std::string to_string_unsigned(T i) -{ - int const sz = UpperBoundOnChars(); - char buf[sz]; - char * end = buf + sz; - char * beg = to_string_digits(end, i); - return std::string(beg, end - beg); -} -} // namespace impl - -inline std::string to_string(int32_t i) { return impl::to_string_signed(i); } -inline std::string to_string(int64_t i) { return impl::to_string_signed(i); } -inline std::string to_string(uint64_t i) { return impl::to_string_unsigned(i); } +inline std::string to_string(int32_t i) { return std::to_string(i); } +inline std::string to_string(int64_t i) { return std::to_string(i); } +inline std::string to_string(uint64_t i) { return std::to_string(i); } /// Use this function to get string with fixed count of /// "Digits after comma". std::string to_string_dac(double d, int dac);