diff --git a/adb/adb_utils.h b/adb/adb_utils.h index faad03d07..66cba121b 100644 --- a/adb/adb_utils.h +++ b/adb/adb_utils.h @@ -16,6 +16,7 @@ #pragma once +#include #include #include #include @@ -112,33 +113,17 @@ inline std::string_view StripTrailingNulls(std::string_view str) { // Base-10 stroll on a string_view. template inline bool ParseUint(T* result, std::string_view str, std::string_view* remaining = nullptr) { - if (str.empty() || !isdigit(str[0])) { + T value; + const auto res = std::from_chars(str.begin(), str.end(), value); + if (res.ec != std::errc{}) { return false; } - - T value = 0; - std::string_view::iterator it; - constexpr T max = std::numeric_limits::max(); - for (it = str.begin(); it != str.end() && isdigit(*it); ++it) { - if (value > max / 10) { - return false; - } - - value *= 10; - - T digit = *it - '0'; - if (value > max - digit) { - return false; - } - - value += digit; + if (res.ptr != str.end() && !remaining) { + return false; + } + if (remaining) { + *remaining = std::string_view(res.ptr, str.end() - res.ptr); } *result = value; - if (remaining) { - *remaining = str.substr(it - str.begin()); - } else { - return it == str.end(); - } - return true; }