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

Commit 26c7becf authored by Elliott Hughes's avatar Elliott Hughes Committed by android-build-merger
Browse files

Merge "Add std::string StartsWith*/EndsWith* overloads."

am: dbbba76f

Change-Id: Ic1c31d9197691d81bdefd267e9ba5182e17102a6
parents 498a076c dbbba76f
Loading
Loading
Loading
Loading
+4 −4
Original line number Diff line number Diff line
@@ -118,7 +118,7 @@ static bool tcp_host_is_local(const std::string& hostname) {
bool is_socket_spec(const std::string& spec) {
    for (const auto& it : kLocalSocketTypes) {
        std::string prefix = it.first + ":";
        if (StartsWith(spec, prefix.c_str())) {
        if (StartsWith(spec, prefix)) {
            return true;
        }
    }
@@ -128,7 +128,7 @@ bool is_socket_spec(const std::string& spec) {
bool is_local_socket_spec(const std::string& spec) {
    for (const auto& it : kLocalSocketTypes) {
        std::string prefix = it.first + ":";
        if (StartsWith(spec, prefix.c_str())) {
        if (StartsWith(spec, prefix)) {
            return true;
        }
    }
@@ -170,7 +170,7 @@ int socket_spec_connect(const std::string& spec, std::string* error) {

    for (const auto& it : kLocalSocketTypes) {
        std::string prefix = it.first + ":";
        if (StartsWith(spec, prefix.c_str())) {
        if (StartsWith(spec, prefix)) {
            if (!it.second.available) {
                *error = StringPrintf("socket type %s is unavailable on this platform",
                                      it.first.c_str());
@@ -213,7 +213,7 @@ int socket_spec_listen(const std::string& spec, std::string* error, int* resolve

    for (const auto& it : kLocalSocketTypes) {
        std::string prefix = it.first + ":";
        if (StartsWith(spec, prefix.c_str())) {
        if (StartsWith(spec, prefix)) {
            if (!it.second.available) {
                *error = StringPrintf("attempted to listen on unavailable socket type: '%s'",
                                      spec.c_str());
+6 −0
Original line number Diff line number Diff line
@@ -57,12 +57,18 @@ extern template std::string Join(const std::vector<std::string>&, const std::str
extern template std::string Join(const std::vector<const char*>&, const std::string&);

// Tests whether 's' starts with 'prefix'.
// TODO: string_view
bool StartsWith(const std::string& s, const char* prefix);
bool StartsWithIgnoreCase(const std::string& s, const char* prefix);
bool StartsWith(const std::string& s, const std::string& prefix);
bool StartsWithIgnoreCase(const std::string& s, const std::string& prefix);

// Tests whether 's' ends with 'suffix'.
// TODO: string_view
bool EndsWith(const std::string& s, const char* suffix);
bool EndsWithIgnoreCase(const std::string& s, const char* suffix);
bool EndsWith(const std::string& s, const std::string& prefix);
bool EndsWithIgnoreCase(const std::string& s, const std::string& prefix);

// Tests whether 'lhs' equals 'rhs', ignoring case.
bool EqualsIgnoreCase(const std::string& lhs, const std::string& rhs);
+20 −4
Original line number Diff line number Diff line
@@ -91,12 +91,20 @@ bool StartsWith(const std::string& s, const char* prefix) {
  return strncmp(s.c_str(), prefix, strlen(prefix)) == 0;
}

bool StartsWith(const std::string& s, const std::string& prefix) {
  return strncmp(s.c_str(), prefix.c_str(), prefix.size()) == 0;
}

bool StartsWithIgnoreCase(const std::string& s, const char* prefix) {
  return strncasecmp(s.c_str(), prefix, strlen(prefix)) == 0;
}

static bool EndsWith(const std::string& s, const char* suffix, bool case_sensitive) {
  size_t suffix_length = strlen(suffix);
bool StartsWithIgnoreCase(const std::string& s, const std::string& prefix) {
  return strncasecmp(s.c_str(), prefix.c_str(), prefix.size()) == 0;
}

static bool EndsWith(const std::string& s, const char* suffix, size_t suffix_length,
                     bool case_sensitive) {
  size_t string_length = s.size();
  if (suffix_length > string_length) {
    return false;
@@ -106,11 +114,19 @@ static bool EndsWith(const std::string& s, const char* suffix, bool case_sensiti
}

bool EndsWith(const std::string& s, const char* suffix) {
  return EndsWith(s, suffix, true);
  return EndsWith(s, suffix, strlen(suffix), true);
}

bool EndsWith(const std::string& s, const std::string& suffix) {
  return EndsWith(s, suffix.c_str(), suffix.size(), true);
}

bool EndsWithIgnoreCase(const std::string& s, const char* suffix) {
  return EndsWith(s, suffix, false);
  return EndsWith(s, suffix, strlen(suffix), false);
}

bool EndsWithIgnoreCase(const std::string& s, const std::string& suffix) {
  return EndsWith(s, suffix.c_str(), suffix.size(), false);
}

bool EqualsIgnoreCase(const std::string& lhs, const std::string& rhs) {
+20 −0
Original line number Diff line number Diff line
@@ -253,6 +253,26 @@ TEST(strings, EndsWithIgnoreCase_contains_prefix) {
  ASSERT_FALSE(android::base::EndsWithIgnoreCase("foobar", "FOO"));
}

TEST(strings, StartsWith_std_string) {
  ASSERT_TRUE(android::base::StartsWith("hello", std::string{"hell"}));
  ASSERT_FALSE(android::base::StartsWith("goodbye", std::string{"hell"}));
}

TEST(strings, StartsWithIgnoreCase_std_string) {
  ASSERT_TRUE(android::base::StartsWithIgnoreCase("HeLlO", std::string{"hell"}));
  ASSERT_FALSE(android::base::StartsWithIgnoreCase("GoOdByE", std::string{"hell"}));
}

TEST(strings, EndsWith_std_string) {
  ASSERT_TRUE(android::base::EndsWith("hello", std::string{"lo"}));
  ASSERT_FALSE(android::base::EndsWith("goodbye", std::string{"lo"}));
}

TEST(strings, EndsWithIgnoreCase_std_string) {
  ASSERT_TRUE(android::base::EndsWithIgnoreCase("HeLlO", std::string{"lo"}));
  ASSERT_FALSE(android::base::EndsWithIgnoreCase("GoOdByE", std::string{"lo"}));
}

TEST(strings, EqualsIgnoreCase) {
  ASSERT_TRUE(android::base::EqualsIgnoreCase("foo", "FOO"));
  ASSERT_TRUE(android::base::EqualsIgnoreCase("FOO", "foo"));
+3 −6
Original line number Diff line number Diff line
@@ -315,8 +315,7 @@ bool isStrongRebootReason(const std::string& r) {
  for (auto& s : knownReasons) {
    if (s == "cold") break;
    // Prefix defined as terminated by a nul or comma (,).
    if (android::base::StartsWith(r, s.c_str()) &&
        ((r.length() == s.length()) || (r[s.length()] == ','))) {
    if (android::base::StartsWith(r, s) && ((r.length() == s.length()) || (r[s.length()] == ','))) {
      return true;
    }
  }
@@ -328,8 +327,7 @@ bool isKernelRebootReason(const std::string& r) {
  for (auto& s : knownReasons) {
    if (s == "recovery") break;
    // Prefix defined as terminated by a nul or comma (,).
    if (android::base::StartsWith(r, s.c_str()) &&
        ((r.length() == s.length()) || (r[s.length()] == ','))) {
    if (android::base::StartsWith(r, s) && ((r.length() == s.length()) || (r[s.length()] == ','))) {
      return true;
    }
  }
@@ -340,8 +338,7 @@ bool isKernelRebootReason(const std::string& r) {
bool isKnownRebootReason(const std::string& r) {
  for (auto& s : knownReasons) {
    // Prefix defined as terminated by a nul or comma (,).
    if (android::base::StartsWith(r, s.c_str()) &&
        ((r.length() == s.length()) || (r[s.length()] == ','))) {
    if (android::base::StartsWith(r, s) && ((r.length() == s.length()) || (r[s.length()] == ','))) {
      return true;
    }
  }
Loading