From da541af34da0e370cb1e1566fb6a93d36747bfc4 Mon Sep 17 00:00:00 2001 From: Bi Xue Date: Tue, 30 May 2017 21:09:55 -0700 Subject: [PATCH] add equals interface for Range class. 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 | 6 ++++++ folly/test/RangeTest.cpp | 7 +++++++ 2 files changed, 13 insertions(+) diff --git a/folly/Range.h b/folly/Range.h index 0076b079..ca3731e1 100644 --- a/folly/Range.h +++ b/folly/Range.h @@ -692,6 +692,12 @@ public: trunc.begin(), trunc.end(), other.begin(), std::forward(eq)); } + template + bool equals(const const_range_type& other, Comp&& eq) const { + return size() == other.size() && + std::equal(begin(), 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 8f333403..8a2b2762 100644 --- a/folly/test/RangeTest.cpp +++ b/folly/test/RangeTest.cpp @@ -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("")); -- 2.34.1