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

Commit da46b392 authored by Elliott Hughes's avatar Elliott Hughes
Browse files

Move off std::sto* function which abort on failure.

Bug: http://b/31403370
Test: builds, boots, libbase tests pass
Change-Id: I89cd7ca3d8f1c8a1bad0ddf3043439449d19a293
parent 58f7f612
Loading
Loading
Loading
Loading
+6 −3
Original line number Diff line number Diff line
@@ -21,6 +21,7 @@
#include <string>
#include <vector>

#include <android-base/parseint.h>
#include <android-base/strings.h>

#include "sysdeps.h"
@@ -143,9 +144,11 @@ class BugreportStandardStreamsCallback : public StandardStreamsCallbackInterface
            //
            size_t idx1 = line.rfind(BUGZ_PROGRESS_PREFIX) + strlen(BUGZ_PROGRESS_PREFIX);
            size_t idx2 = line.rfind(BUGZ_PROGRESS_SEPARATOR);
            int progress = std::stoi(line.substr(idx1, (idx2 - idx1)));
            int total = std::stoi(line.substr(idx2 + 1));
            int progress, total;
            if (android::base::ParseInt(line.substr(idx1, (idx2 - idx1)), &progress) &&
                android::base::ParseInt(line.substr(idx2 + 1), &total)) {
                br_->UpdateProgress(line_message_, progress, total);
            }
        } else {
            invalid_lines_.push_back(line);
        }
+19 −2
Original line number Diff line number Diff line
@@ -21,13 +21,15 @@
#include <stdlib.h>

#include <limits>
#include <string>

namespace android {
namespace base {

// Parses the unsigned decimal integer in the string 's' and sets 'out' to
// that value. Optionally allows the caller to define a 'max' beyond which
// otherwise valid values will be rejected. Returns boolean success.
// otherwise valid values will be rejected. Returns boolean success; 'out'
// is untouched if parsing fails.
template <typename T>
bool ParseUint(const char* s, T* out,
               T max = std::numeric_limits<T>::max()) {
@@ -45,10 +47,17 @@ bool ParseUint(const char* s, T* out,
  return true;
}

// TODO: string_view
template <typename T>
bool ParseUint(const std::string& s, T* out,
               T max = std::numeric_limits<T>::max()) {
  return ParseUint(s.c_str(), out, max);
}

// Parses the signed decimal integer in the string 's' and sets 'out' to
// that value. Optionally allows the caller to define a 'min' and 'max
// beyond which otherwise valid values will be rejected. Returns boolean
// success.
// success; 'out' is untouched if parsing fails.
template <typename T>
bool ParseInt(const char* s, T* out,
              T min = std::numeric_limits<T>::min(),
@@ -67,6 +76,14 @@ bool ParseInt(const char* s, T* out,
  return true;
}

// TODO: string_view
template <typename T>
bool ParseInt(const std::string& s, T* out,
              T min = std::numeric_limits<T>::min(),
              T max = std::numeric_limits<T>::max()) {
  return ParseInt(s.c_str(), out, min, max);
}

}  // namespace base
}  // namespace android

+28 −8
Original line number Diff line number Diff line
@@ -19,7 +19,7 @@
#include <gtest/gtest.h>

TEST(parseint, signed_smoke) {
  int i;
  int i = 0;
  ASSERT_FALSE(android::base::ParseInt("x", &i));
  ASSERT_FALSE(android::base::ParseInt("123x", &i));

@@ -28,7 +28,7 @@ TEST(parseint, signed_smoke) {
  ASSERT_TRUE(android::base::ParseInt("-123", &i));
  ASSERT_EQ(-123, i);

  short s;
  short s = 0;
  ASSERT_TRUE(android::base::ParseInt("1234", &s));
  ASSERT_EQ(1234, s);

@@ -39,7 +39,7 @@ TEST(parseint, signed_smoke) {
}

TEST(parseint, unsigned_smoke) {
  unsigned int i;
  unsigned int i = 0u;
  ASSERT_FALSE(android::base::ParseUint("x", &i));
  ASSERT_FALSE(android::base::ParseUint("123x", &i));

@@ -47,7 +47,7 @@ TEST(parseint, unsigned_smoke) {
  ASSERT_EQ(123u, i);
  ASSERT_FALSE(android::base::ParseUint("-123", &i));

  unsigned short s;
  unsigned short s = 0u;
  ASSERT_TRUE(android::base::ParseUint("1234", &s));
  ASSERT_EQ(1234u, s);

@@ -58,21 +58,41 @@ TEST(parseint, unsigned_smoke) {
}

TEST(parseint, no_implicit_octal) {
  int i;
  int i = 0;
  ASSERT_TRUE(android::base::ParseInt("0123", &i));
  ASSERT_EQ(123, i);

  unsigned int u;
  unsigned int u = 0u;
  ASSERT_TRUE(android::base::ParseUint("0123", &u));
  ASSERT_EQ(123u, u);
}

TEST(parseint, explicit_hex) {
  int i;
  int i = 0;
  ASSERT_TRUE(android::base::ParseInt("0x123", &i));
  ASSERT_EQ(0x123, i);

  unsigned int u;
  unsigned int u = 0u;
  ASSERT_TRUE(android::base::ParseUint("0x123", &u));
  ASSERT_EQ(0x123u, u);
}

TEST(parseint, string) {
  int i = 0;
  ASSERT_TRUE(android::base::ParseInt(std::string("123"), &i));
  ASSERT_EQ(123, i);

  unsigned int u = 0u;
  ASSERT_TRUE(android::base::ParseUint(std::string("123"), &u));
  ASSERT_EQ(123u, u);
}

TEST(parseint, untouched_on_failure) {
  int i = 123;
  ASSERT_FALSE(android::base::ParseInt("456x", &i));
  ASSERT_EQ(123, i);

  unsigned int u = 123u;
  ASSERT_FALSE(android::base::ParseInt("456x", &u));
  ASSERT_EQ(123u, u);
}
+2 −2
Original line number Diff line number Diff line
@@ -52,7 +52,7 @@ template <typename T>
T GetIntProperty(const std::string& key, T default_value, T min, T max) {
  T result;
  std::string value = GetProperty(key, "");
  if (!value.empty() && android::base::ParseInt(value.c_str(), &result, min, max)) return result;
  if (!value.empty() && android::base::ParseInt(value, &result, min, max)) return result;
  return default_value;
}

@@ -60,7 +60,7 @@ template <typename T>
T GetUintProperty(const std::string& key, T default_value, T max) {
  T result;
  std::string value = GetProperty(key, "");
  if (!value.empty() && android::base::ParseUint(value.c_str(), &result, max)) return result;
  if (!value.empty() && android::base::ParseUint(value, &result, max)) return result;
  return default_value;
}

+1 −1
Original line number Diff line number Diff line
@@ -59,7 +59,7 @@ bool ParseRecordEventTime(const std::string& path, int32_t* uptime) {
  // Ignore existing bootstat records (which do not contain file content).
  if (!content.empty()) {
    int32_t value;
    if (android::base::ParseInt(content.c_str(), &value)) {
    if (android::base::ParseInt(content, &value)) {
      bootstat::LogHistogram("bootstat_mtime_matches_content", value == *uptime);
    }
  }
Loading