Skip to content

Commit

Permalink
Added ByAsciiWhitespace to str_split library.
Browse files Browse the repository at this point in the history
PiperOrigin-RevId: 592653487
Change-Id: Iddd2f484807cb02dd2ee8bba26c22d196be02c88
  • Loading branch information
Abseil Team authored and copybara-github committed Dec 20, 2023
1 parent 7a1898a commit 72d7a15
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 0 deletions.
5 changes: 5 additions & 0 deletions absl/strings/str_split.cc
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,11 @@ absl::string_view ByString::Find(absl::string_view text, size_t pos) const {
return GenericFind(text, delimiter_, pos, LiteralPolicy());
}

absl::string_view ByAsciiWhitespace::Find(absl::string_view text,
size_t pos) const {
return GenericFind(text, " \t\v\f\r\n", pos, AnyOfPolicy());
}

//
// ByChar
//
Expand Down
18 changes: 18 additions & 0 deletions absl/strings/str_split.h
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,24 @@ class ByString {
const std::string delimiter_;
};

// ByAsciiWhitespace
//
// A sub-string delimiter that splits by ASCII whitespace
// (space, tab, vertical tab, formfeed, linefeed, or carriage return).
// Note: you probably want to use absl::SkipEmpty() as well!
//
// This class is equivalent to ByAnyChar with ASCII whitespace chars.
//
// Example:
//
// std::vector<std::string> v = absl::StrSplit(
// "a b\tc\n d \n", absl::ByAsciiWhitespace(), absl::SkipEmpty());
// // v[0] == "a", v[1] == "b", v[2] == "c", v[3] == "d"
class ByAsciiWhitespace {
public:
absl::string_view Find(absl::string_view text, size_t pos) const;
};

// ByChar
//
// A single character delimiter. `ByChar` is functionally equivalent to a
Expand Down
40 changes: 40 additions & 0 deletions absl/strings/str_split_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@
namespace {

using ::testing::ElementsAre;
using ::testing::IsEmpty;
using ::testing::Pair;
using ::testing::UnorderedElementsAre;

Expand Down Expand Up @@ -923,6 +924,45 @@ TEST(Delimiter, ByAnyChar) {
EXPECT_TRUE(IsFoundAt("abc", empty, 1));
}

//
// Tests for ByAsciiWhitespace
//
TEST(Split, ByAsciiWhitespace) {
using absl::ByAsciiWhitespace;
using absl::SkipEmpty;
std::vector<absl::string_view> results;

results = absl::StrSplit("aaaa\n", ByAsciiWhitespace());
EXPECT_THAT(results, ElementsAre("aaaa", ""));

results = absl::StrSplit("aaaa\n", ByAsciiWhitespace(), SkipEmpty());
EXPECT_THAT(results, ElementsAre("aaaa"));

results = absl::StrSplit(" ", ByAsciiWhitespace());
EXPECT_THAT(results, ElementsAre("", ""));

results = absl::StrSplit(" ", ByAsciiWhitespace(), SkipEmpty());
EXPECT_THAT(results, IsEmpty());

results = absl::StrSplit("a", ByAsciiWhitespace());
EXPECT_THAT(results, ElementsAre("a"));

results = absl::StrSplit("", ByAsciiWhitespace());
EXPECT_THAT(results, ElementsAre(""));

results = absl::StrSplit("", ByAsciiWhitespace(), SkipEmpty());
EXPECT_THAT(results, IsEmpty());

results = absl::StrSplit("a b\tc\n d\n", ByAsciiWhitespace());
EXPECT_THAT(results, ElementsAre("a", "b", "c", "", "", "d", ""));

results = absl::StrSplit("a b\tc\n d \n", ByAsciiWhitespace(), SkipEmpty());
EXPECT_THAT(results, ElementsAre("a", "b", "c", "d"));

results = absl::StrSplit("a\t\n\v\f\r b", ByAsciiWhitespace(), SkipEmpty());
EXPECT_THAT(results, ElementsAre("a", "b"));
}

//
// Tests for ByLength
//
Expand Down

0 comments on commit 72d7a15

Please sign in to comment.