Commit 41cee12b authored by Alex Malyshev's avatar Alex Malyshev Committed by Facebook Github Bot

Add overloads of Range::{start,end}sWith with custom comparator

Summary: Intended to be used for case-insensitive prefix/suffix matching.

Reviewed By: yfeldblum

Differential Revision: D4552336

fbshipit-source-id: 0030b883426dd67bea27c3f878279359d51e1d33
parent 4aa405ec
...@@ -659,6 +659,16 @@ public: ...@@ -659,6 +659,16 @@ public:
return !empty() && front() == c; return !empty() && front() == c;
} }
template <class Comp>
bool startsWith(const const_range_type& other, Comp&& eq) const {
if (size() < other.size()) {
return false;
}
auto const trunc = subpiece(0, other.size());
return std::equal(
trunc.begin(), trunc.end(), other.begin(), std::forward<Comp>(eq));
}
/** /**
* Does this Range end with another range? * Does this Range end with another range?
*/ */
...@@ -670,6 +680,16 @@ public: ...@@ -670,6 +680,16 @@ public:
return !empty() && back() == c; return !empty() && back() == c;
} }
template <class Comp>
bool endsWith(const const_range_type& other, Comp&& eq) const {
if (size() < other.size()) {
return false;
}
auto const trunc = subpiece(size() - other.size());
return std::equal(
trunc.begin(), trunc.end(), other.begin(), std::forward<Comp>(eq));
}
/** /**
* Remove the items in [b, e), as long as this subrange is at the beginning * Remove the items in [b, e), as long as this subrange is at the beginning
* or end of the Range. * or end of the Range.
......
...@@ -315,6 +315,15 @@ TEST(StringPiece, Prefix) { ...@@ -315,6 +315,15 @@ TEST(StringPiece, Prefix) {
EXPECT_FALSE(a.startsWith('x')); EXPECT_FALSE(a.startsWith('x'));
EXPECT_FALSE(a.startsWith("x")); EXPECT_FALSE(a.startsWith("x"));
EXPECT_TRUE(a.startsWith("", folly::AsciiCaseInsensitive()));
EXPECT_TRUE(a.startsWith("hello", folly::AsciiCaseInsensitive()));
EXPECT_TRUE(a.startsWith("hellO", folly::AsciiCaseInsensitive()));
EXPECT_TRUE(a.startsWith("HELL", folly::AsciiCaseInsensitive()));
EXPECT_TRUE(a.startsWith("H", folly::AsciiCaseInsensitive()));
EXPECT_FALSE(a.startsWith("HELLOX", folly::AsciiCaseInsensitive()));
EXPECT_FALSE(a.startsWith("x", folly::AsciiCaseInsensitive()));
EXPECT_FALSE(a.startsWith("X", folly::AsciiCaseInsensitive()));
{ {
auto b = a; auto b = a;
EXPECT_TRUE(b.removePrefix("")); EXPECT_TRUE(b.removePrefix(""));
...@@ -362,6 +371,16 @@ TEST(StringPiece, Suffix) { ...@@ -362,6 +371,16 @@ TEST(StringPiece, Suffix) {
EXPECT_FALSE(a.endsWith("x")); EXPECT_FALSE(a.endsWith("x"));
EXPECT_FALSE(a.endsWith('x')); EXPECT_FALSE(a.endsWith('x'));
EXPECT_TRUE(a.endsWith("", folly::AsciiCaseInsensitive()));
EXPECT_TRUE(a.endsWith("o", folly::AsciiCaseInsensitive()));
EXPECT_TRUE(a.endsWith("O", folly::AsciiCaseInsensitive()));
EXPECT_TRUE(a.endsWith("hello", folly::AsciiCaseInsensitive()));
EXPECT_TRUE(a.endsWith("hellO", folly::AsciiCaseInsensitive()));
EXPECT_FALSE(a.endsWith("xhello", folly::AsciiCaseInsensitive()));
EXPECT_FALSE(a.endsWith("Xhello", folly::AsciiCaseInsensitive()));
EXPECT_FALSE(a.endsWith("x", folly::AsciiCaseInsensitive()));
EXPECT_FALSE(a.endsWith("X", folly::AsciiCaseInsensitive()));
{ {
auto b = a; auto b = a;
EXPECT_TRUE(b.removeSuffix("")); EXPECT_TRUE(b.removeSuffix(""));
......
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