[adb] Use STL for ParseUInt() implementation
Test: builds Change-Id: I8c4d4f24e3496601362fcbbdaa071a29ab9f360a
This commit is contained in:
parent
f7970cee69
commit
d3d650628c
1 changed files with 9 additions and 24 deletions
|
@ -16,6 +16,7 @@
|
|||
|
||||
#pragma once
|
||||
|
||||
#include <charconv>
|
||||
#include <condition_variable>
|
||||
#include <mutex>
|
||||
#include <string>
|
||||
|
@ -112,33 +113,17 @@ inline std::string_view StripTrailingNulls(std::string_view str) {
|
|||
// Base-10 stroll on a string_view.
|
||||
template <typename T>
|
||||
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<T>::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;
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue