diff --git a/base/string_utils.cpp b/base/string_utils.cpp index 2263af3255..f37e88bcd9 100644 --- a/base/string_utils.cpp +++ b/base/string_utils.cpp @@ -1,6 +1,8 @@ -#include "base/assert.hpp" #include "base/string_utils.hpp" +#include "base/assert.hpp" +#include "base/checked_cast.hpp" + #include "std/target_os.hpp" #include @@ -103,6 +105,16 @@ bool to_int64(char const * s, int64_t & i) return *stop == 0 && s != stop; } +bool to_size_t(char const * s, size_t & i, int base) +{ + uint64_t num = 0; + if (!to_uint64(s, num, base)) + return false; + + i = static_cast(num); + return i == num; +} + bool to_float(char const * s, float & f) { char * stop; diff --git a/base/string_utils.hpp b/base/string_utils.hpp index 1f86537a4b..43cc707fee 100644 --- a/base/string_utils.hpp +++ b/base/string_utils.hpp @@ -376,6 +376,7 @@ WARN_UNUSED_RESULT bool to_int(char const * s, int & i, int base = 10); WARN_UNUSED_RESULT bool to_uint(char const * s, unsigned int & i, int base = 10); WARN_UNUSED_RESULT bool to_uint64(char const * s, uint64_t & i, int base = 10); WARN_UNUSED_RESULT bool to_int64(char const * s, int64_t & 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); @@ -402,6 +403,10 @@ WARN_UNUSED_RESULT inline bool to_int64(std::string const & s, int64_t & i) { return to_int64(s.c_str(), i); } +WARN_UNUSED_RESULT inline bool to_size_t(std::string const & s, size_t & i) +{ + return to_size_t(s.c_str(), i); +} WARN_UNUSED_RESULT inline bool to_float(std::string const & s, float & f) { return to_float(s.c_str(), f);