Add overloads of Range::{start,end}sWith with custom comparator
authorAlex Malyshev <alexanderm@fb.com>
Tue, 14 Feb 2017 19:09:42 +0000 (11:09 -0800)
committerFacebook Github Bot <facebook-github-bot@users.noreply.github.com>
Tue, 14 Feb 2017 19:21:43 +0000 (11:21 -0800)
Summary: Intended to be used for case-insensitive prefix/suffix matching.

Reviewed By: yfeldblum

Differential Revision: D4552336

fbshipit-source-id: 0030b883426dd67bea27c3f878279359d51e1d33

folly/Range.h
folly/test/RangeTest.cpp

index bfbb4eb0f609c0547da193f3f0875a36f36219cd..eb53f9372db76460aad9089d0b2f3592e75eb4cf 100644 (file)
@@ -659,6 +659,16 @@ public:
     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?
    */
@@ -670,6 +680,16 @@ public:
     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
    * or end of the Range.
index d4f658653fb340c2dc0b91e4a94e10ab693917bb..791df2a051887cdadd8dcfe436126f90651e5654 100644 (file)
@@ -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(""));