[adb] Use STL for ParseUInt() implementation
Test: builds
Change-Id: I8c4d4f24e3496601362fcbbdaa071a29ab9f360a
diff --git a/adb/adb_utils.h b/adb/adb_utils.h
index faad03d..66cba12 100644
--- a/adb/adb_utils.h
+++ b/adb/adb_utils.h
@@ -16,6 +16,7 @@
#pragma once
+#include <charconv>
#include <condition_variable>
#include <mutex>
#include <string>
@@ -112,33 +113,17 @@
// 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;
}