From a1f19371b0a430360e082251b9ae1f7882462dd2 Mon Sep 17 00:00:00 2001 From: levlam Date: Fri, 18 Nov 2022 00:25:36 +0300 Subject: [PATCH] Improve to_integer_safe error message. --- tdutils/td/utils/misc.cpp | 11 +++++++++++ tdutils/td/utils/misc.h | 6 +++++- 2 files changed, 16 insertions(+), 1 deletion(-) diff --git a/tdutils/td/utils/misc.cpp b/tdutils/td/utils/misc.cpp index 53a045d87..df8638203 100644 --- a/tdutils/td/utils/misc.cpp +++ b/tdutils/td/utils/misc.cpp @@ -7,6 +7,7 @@ #include "td/utils/misc.h" #include "td/utils/port/thread_local.h" +#include "td/utils/utf8.h" #include #include @@ -78,6 +79,16 @@ string oneline(Slice str) { return result; } +namespace detail { +Status get_to_integer_safe_error(Slice str) { + auto status = Status::Error(PSLICE() << "Can't parse \"" << str << "\" as an integer"); + if (!check_utf8(status.message())) { + status = Status::Error("Strings must be encoded in UTF-8"); + } + return status; +} +} // namespace detail + double to_double(Slice str) { static TD_THREAD_LOCAL std::stringstream *ss; if (init_thread_local(ss)) { diff --git a/tdutils/td/utils/misc.h b/tdutils/td/utils/misc.h index 604e48044..5d9292f43 100644 --- a/tdutils/td/utils/misc.h +++ b/tdutils/td/utils/misc.h @@ -190,11 +190,15 @@ std::enable_if_t::value, T> to_integer(Slice str) { return integer_value; } +namespace detail { +Status get_to_integer_safe_error(Slice str); +} // namespace detail + template Result to_integer_safe(Slice str) { auto res = to_integer(str); if ((PSLICE() << res) != str) { - return Status::Error(PSLICE() << "Can't parse \"" << str << "\" as an integer"); + return detail::get_to_integer_safe_error(str); } return res; }