diff options
Diffstat (limited to 'runtime/utils.h')
-rw-r--r-- | runtime/utils.h | 29 |
1 files changed, 29 insertions, 0 deletions
diff --git a/runtime/utils.h b/runtime/utils.h index eb79968e21..448c591f2b 100644 --- a/runtime/utils.h +++ b/runtime/utils.h @@ -19,6 +19,7 @@ #include <pthread.h> +#include <limits> #include <string> #include <vector> @@ -50,6 +51,34 @@ enum TimeUnit { kTimeUnitSecond, }; +template <typename T> +bool ParseUint(const char *in, T* out) { + char* end; + unsigned long long int result = strtoull(in, &end, 0); // NOLINT(runtime/int) + if (in == end || *end != '\0') { + return false; + } + if (std::numeric_limits<T>::max() < result) { + return false; + } + *out = static_cast<T>(result); + return true; +} + +template <typename T> +bool ParseInt(const char* in, T* out) { + char* end; + long long int result = strtoll(in, &end, 0); // NOLINT(runtime/int) + if (in == end || *end != '\0') { + return false; + } + if (result < std::numeric_limits<T>::min() || std::numeric_limits<T>::max() < result) { + return false; + } + *out = static_cast<T>(result); + return true; +} + template<typename T> static constexpr bool IsPowerOfTwo(T x) { return (x & (x - 1)) == 0; |