add equals interface for Range class.
authorBi Xue <bixue@fb.com>
Wed, 31 May 2017 04:09:55 +0000 (21:09 -0700)
committerFacebook Github Bot <facebook-github-bot@users.noreply.github.com>
Wed, 31 May 2017 04:21:27 +0000 (21:21 -0700)
Summary:
Add `equals` interface to StringPiece (Range class). To support following case insensitive compare case:
```
folly::StringPiece a("hello");
if (a.equals("HELLO", folly::AsciiCaseInsensitive())) {
  // Do something.
}
```

Reviewed By: ot

Differential Revision: D5150495

fbshipit-source-id: 26816820f93959678f550768396f55293b5588cb

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

index 0076b07923bbb902bdc4b01266db8ca03f449da1..ca3731e11c5915a542e9c0e223f751714db46328 100644 (file)
@@ -692,6 +692,12 @@ public:
         trunc.begin(), trunc.end(), other.begin(), std::forward<Comp>(eq));
   }
 
+  template <class Comp>
+  bool equals(const const_range_type& other, Comp&& eq) const {
+    return size() == other.size() &&
+        std::equal(begin(), 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 8f333403a174fe2832d12446cb5bc25bae62b5f6..8a2b2762def14124b57ea0c7b927d48400d3f11d 100644 (file)
@@ -418,6 +418,13 @@ TEST(StringPiece, Suffix) {
   }
 }
 
+TEST(StringPiece, Equals) {
+  StringPiece a("hello");
+
+  EXPECT_TRUE(a.equals("HELLO", AsciiCaseInsensitive()));
+  EXPECT_FALSE(a.equals("HELLOX", AsciiCaseInsensitive()));
+}
+
 TEST(StringPiece, PrefixEmpty) {
   StringPiece a;
   EXPECT_TRUE(a.startsWith(""));