Improve to_integer_safe error message.
This commit is contained in:
parent
5081ef4c2a
commit
a1f19371b0
@ -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)) {
|
||||
|
@ -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;
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user