Merge "[adb] Use STL for ParseUInt() implementation"

This commit is contained in:
Treehugger Robot 2019-08-14 06:32:44 +00:00 committed by Gerrit Code Review
commit 61bffa5fc0
1 changed files with 9 additions and 24 deletions

View File

@ -16,6 +16,7 @@
#pragma once #pragma once
#include <charconv>
#include <condition_variable> #include <condition_variable>
#include <mutex> #include <mutex>
#include <string> #include <string>
@ -112,33 +113,17 @@ inline std::string_view StripTrailingNulls(std::string_view str) {
// Base-10 stroll on a string_view. // Base-10 stroll on a string_view.
template <typename T> template <typename T>
inline bool ParseUint(T* result, std::string_view str, std::string_view* remaining = nullptr) { 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; return false;
} }
if (res.ptr != str.end() && !remaining) {
T value = 0; return false;
std::string_view::iterator it; }
constexpr T max = std::numeric_limits<T>::max(); if (remaining) {
for (it = str.begin(); it != str.end() && isdigit(*it); ++it) { *remaining = std::string_view(res.ptr, str.end() - res.ptr);
if (value > max / 10) {
return false;
}
value *= 10;
T digit = *it - '0';
if (value > max - digit) {
return false;
}
value += digit;
} }
*result = value; *result = value;
if (remaining) {
*remaining = str.substr(it - str.begin());
} else {
return it == str.end();
}
return true; return true;
} }