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

Commit 97179d4a authored by Dan Albert's avatar Dan Albert Committed by Android Git Automerger
Browse files

am a5e9639c: Merge "Update string Split API."

* commit 'a5e9639c':
  Update string Split API.
parents 585f1fd9 a5e9639c
Loading
Loading
Loading
Loading
+9 −4
Original line number Diff line number Diff line
@@ -23,10 +23,15 @@
namespace android {
namespace base {

// Splits a string using the given separator character into a vector of strings.
// Empty strings will be omitted.
void Split(const std::string& s, char separator,
           std::vector<std::string>* result);
// Splits a string into a vector of strings.
//
// The string is split at each occurence of a character in delimiters.
//
// Empty splits will be omitted. I.e. Split("a,,b", ",") -> {"a", "b"}
//
// The empty string is not a valid delimiter list.
std::vector<std::string> Split(const std::string& s,
                               const std::string& delimiters);

// Trims whitespace off both ends of the given string.
std::string Trim(const std::string& s);
+1 −2
Original line number Diff line number Diff line
@@ -108,8 +108,7 @@ void InitLogging(char* argv[]) {
    return;
  }

  std::vector<std::string> specs;
  Split(tags, ' ', &specs);
  std::vector<std::string> specs = Split(tags, " ");
  for (size_t i = 0; i < specs.size(); ++i) {
    // "tag-pattern:[vdiwefs]"
    std::string spec(specs[i]);
+26 −14
Original line number Diff line number Diff line
@@ -16,27 +16,39 @@

#include "base/strings.h"

#include <stdlib.h>

#include <string>
#include <vector>

namespace android {
namespace base {

void Split(const std::string& s, char separator,
           std::vector<std::string>* result) {
  const char* p = s.data();
  const char* end = p + s.size();
  while (p != end) {
    if (*p == separator) {
      ++p;
    } else {
      const char* start = p;
      while (++p != end && *p != separator) {
        // Skip to the next occurrence of the separator.
      }
      result->push_back(std::string(start, p - start));
#define CHECK_NE(a, b) \
  if ((a) == (b)) abort();

std::vector<std::string> Split(const std::string& s,
                               const std::string& delimiters) {
  CHECK_NE(delimiters.size(), 0U);

  std::vector<std::string> split;
  if (s.size() == 0) {
    // Split("", d) returns {} rather than {""}.
    return split;
  }

  size_t base = 0;
  size_t found;
  do {
    found = s.find_first_of(delimiters, base);
    if (found != base) {
      split.push_back(s.substr(base, found - base));
    }

    base = found + 1;
  } while (found != s.npos);

  return split;
}

std::string Trim(const std::string& s) {
+27 −8
Original line number Diff line number Diff line
@@ -22,21 +22,18 @@
#include <vector>

TEST(strings, split_empty) {
  std::vector<std::string> parts;
  android::base::Split("", '\0', &parts);
  std::vector<std::string> parts = android::base::Split("", ",");
  ASSERT_EQ(0U, parts.size());
}

TEST(strings, split_single) {
  std::vector<std::string> parts;
  android::base::Split("foo", ',', &parts);
  std::vector<std::string> parts = android::base::Split("foo", ",");
  ASSERT_EQ(1U, parts.size());
  ASSERT_EQ("foo", parts[0]);
}

TEST(strings, split_simple) {
  std::vector<std::string> parts;
  android::base::Split("foo,bar,baz", ',', &parts);
  std::vector<std::string> parts = android::base::Split("foo,bar,baz", ",");
  ASSERT_EQ(3U, parts.size());
  ASSERT_EQ("foo", parts[0]);
  ASSERT_EQ("bar", parts[1]);
@@ -44,8 +41,30 @@ TEST(strings, split_simple) {
}

TEST(strings, split_with_empty_part) {
  std::vector<std::string> parts;
  android::base::Split("foo,,bar", ',', &parts);
  std::vector<std::string> parts = android::base::Split("foo,,bar", ",");
  ASSERT_EQ(2U, parts.size());
  ASSERT_EQ("foo", parts[0]);
  ASSERT_EQ("bar", parts[1]);
}

TEST(strings, split_null_char) {
  std::vector<std::string> parts =
      android::base::Split(std::string("foo\0bar", 7), std::string("\0", 1));
  ASSERT_EQ(2U, parts.size());
  ASSERT_EQ("foo", parts[0]);
  ASSERT_EQ("bar", parts[1]);
}

TEST(strings, split_any) {
  std::vector<std::string> parts = android::base::Split("foo:bar,baz", ",:");
  ASSERT_EQ(3U, parts.size());
  ASSERT_EQ("foo", parts[0]);
  ASSERT_EQ("bar", parts[1]);
  ASSERT_EQ("baz", parts[2]);
}

TEST(strings, split_any_with_empty_part) {
  std::vector<std::string> parts = android::base::Split("foo:,bar", ",:");
  ASSERT_EQ(2U, parts.size());
  ASSERT_EQ("foo", parts[0]);
  ASSERT_EQ("bar", parts[1]);