Improve to_integer_safe error message.

This commit is contained in:
levlam 2022-11-18 00:25:36 +03:00
parent 5081ef4c2a
commit a1f19371b0
2 changed files with 16 additions and 1 deletions

View File

@ -7,6 +7,7 @@
#include "td/utils/misc.h"
#include "td/utils/port/thread_local.h"
#include "td/utils/utf8.h"
#include <algorithm>
#include <cstdlib>
@ -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<std::stringstream>(ss)) {

View File

@ -190,11 +190,15 @@ std::enable_if_t<std::is_unsigned<T>::value, T> to_integer(Slice str) {
return integer_value;
}
namespace detail {
Status get_to_integer_safe_error(Slice str);
} // namespace detail
template <class T>
Result<T> to_integer_safe(Slice str) {
auto res = to_integer<T>(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;
}