adb: add stroll equivalent for string_view.
Test: $ANDROID_HOST_OUT/nativetest64/adb_test/adb_test
Test: adb shell /data/nativetest64/adbd_test/adbd_test
Change-Id: I1d89913474fcd1aa4a856b5e4583a3c1f076ddd4
diff --git a/adb/adb_utils.h b/adb/adb_utils.h
index 8712e1b..8253487 100644
--- a/adb/adb_utils.h
+++ b/adb/adb_utils.h
@@ -19,6 +19,8 @@
#include <condition_variable>
#include <mutex>
#include <string>
+#include <string_view>
+#include <type_traits>
#include <vector>
#include <android-base/macros.h>
@@ -107,3 +109,34 @@
str.remove_suffix(n);
return str;
}
+
+// Base-10 stroll on a string_view.
+template <typename T>
+inline bool ParseUint(T* result, std::string_view str, std::string_view* remaining) {
+ if (str.empty() || !isdigit(str[0])) {
+ 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;
+ }
+ *result = value;
+ if (remaining) {
+ *remaining = str.substr(it - str.begin());
+ }
+ return true;
+}