Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 71d621dd authored by Yurii Zubrytskyi's avatar Yurii Zubrytskyi Committed by android-build-merger
Browse files

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

am: 61bffa5f

Change-Id: Iafbacab5e2ddf180683d501cd702ad39017104f9
parents 48a396b3 61bffa5f
Loading
Loading
Loading
Loading
+8 −23
Original line number Diff line number Diff line
@@ -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) {
    if (res.ptr != str.end() && !remaining) {
        return false;
    }

        value += digit;
    }
    *result = value;
    if (remaining) {
        *remaining = str.substr(it - str.begin());
    } else {
      return it == str.end();
        *remaining = std::string_view(res.ptr, str.end() - res.ptr);
    }

    *result = value;
    return true;
}