From: Bi Xue Date: Wed, 31 May 2017 04:09:55 +0000 (-0700) Subject: add equals interface for Range class. X-Git-Tag: v2017.06.05.00~17 X-Git-Url: http://plrg.eecs.uci.edu/git/?p=folly.git;a=commitdiff_plain;h=da541af34da0e370cb1e1566fb6a93d36747bfc4 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 --- 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(""));