Commit 72d7a159 by Abseil Team Committed by Copybara-Service

Added ByAsciiWhitespace to str_split library.

PiperOrigin-RevId: 592653487
Change-Id: Iddd2f484807cb02dd2ee8bba26c22d196be02c88
parent 7a1898a0
...@@ -96,6 +96,11 @@ absl::string_view ByString::Find(absl::string_view text, size_t pos) const { ...@@ -96,6 +96,11 @@ absl::string_view ByString::Find(absl::string_view text, size_t pos) const {
return GenericFind(text, delimiter_, pos, LiteralPolicy()); 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 // ByChar
// //
......
...@@ -130,6 +130,24 @@ class ByString { ...@@ -130,6 +130,24 @@ class ByString {
const std::string delimiter_; 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 // ByChar
// //
// A single character delimiter. `ByChar` is functionally equivalent to a // A single character delimiter. `ByChar` is functionally equivalent to a
......
...@@ -40,6 +40,7 @@ ...@@ -40,6 +40,7 @@
namespace { namespace {
using ::testing::ElementsAre; using ::testing::ElementsAre;
using ::testing::IsEmpty;
using ::testing::Pair; using ::testing::Pair;
using ::testing::UnorderedElementsAre; using ::testing::UnorderedElementsAre;
...@@ -924,6 +925,45 @@ TEST(Delimiter, ByAnyChar) { ...@@ -924,6 +925,45 @@ TEST(Delimiter, ByAnyChar) {
} }
// //
// 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 // Tests for ByLength
// //
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment