From: Alex Malyshev Date: Tue, 14 Feb 2017 19:09:42 +0000 (-0800) Subject: Add overloads of Range::{start,end}sWith with custom comparator X-Git-Tag: v2017.03.06.00~38 X-Git-Url: http://plrg.eecs.uci.edu/git/?a=commitdiff_plain;h=41cee12b073235250d4df38b6179f8c2977a6b39;p=folly.git 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 --- diff --git a/folly/Range.h b/folly/Range.h index bfbb4eb0..eb53f937 100644 --- a/folly/Range.h +++ b/folly/Range.h @@ -659,6 +659,16 @@ public: return !empty() && front() == c; } + template + 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(eq)); + } + /** * Does this Range end with another range? */ @@ -670,6 +680,16 @@ public: return !empty() && back() == c; } + template + 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(eq)); + } + /** * Remove the items in [b, e), as long as this subrange is at the beginning * or end of the Range. diff --git a/folly/test/RangeTest.cpp b/folly/test/RangeTest.cpp index d4f65865..791df2a0 100644 --- a/folly/test/RangeTest.cpp +++ b/folly/test/RangeTest.cpp @@ -315,6 +315,15 @@ TEST(StringPiece, Prefix) { 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; EXPECT_TRUE(b.removePrefix("")); @@ -362,6 +371,16 @@ TEST(StringPiece, Suffix) { 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; EXPECT_TRUE(b.removeSuffix(""));