From 7154531b595a12505fdc50c536f68d6eaf6b84cf Mon Sep 17 00:00:00 2001 From: Tom Jackson Date: Mon, 24 Mar 2014 11:54:02 -0700 Subject: [PATCH] Range::contains(x) Test Plan: Unit tests Reviewed By: marcelo.juchem@fb.com FB internal diff: D1236630 --- folly/Range.h | 13 +++++++++++++ folly/test/RangeTest.cpp | 4 ++++ 2 files changed, 17 insertions(+) diff --git a/folly/Range.h b/folly/Range.h index daca42c9..fc238b3e 100644 --- a/folly/Range.h +++ b/folly/Range.h @@ -501,6 +501,19 @@ public: return find(c, pos); } + /** + * Determine whether the range contains the given subrange or item. + * + * Note: Call find() directly if the index is needed. + */ + bool contains(const Range& other) const { + return find(other) != std::string::npos; + } + + bool contains(const value_type& other) const { + return find(other) != std::string::npos; + } + void swap(Range& rhs) { std::swap(b_, rhs.b_); std::swap(e_, rhs.e_); diff --git a/folly/test/RangeTest.cpp b/folly/test/RangeTest.cpp index e9a56b2c..681aa03d 100644 --- a/folly/test/RangeTest.cpp +++ b/folly/test/RangeTest.cpp @@ -116,6 +116,7 @@ TEST(StringPiece, All) { EXPECT_EQ(s.find('y'), StringPiece::npos); EXPECT_EQ(s.find('y', 1), StringPiece::npos); EXPECT_EQ(s.find('o', 4), StringPiece::npos); // starting position too far + EXPECT_TRUE(s.contains('z')); // starting pos that is obviously past the end -- This works for std::string EXPECT_EQ(s.toString().find('y', 55), StringPiece::npos); EXPECT_EQ(s.find('z', s.size()), StringPiece::npos); @@ -123,6 +124,7 @@ TEST(StringPiece, All) { // null char EXPECT_EQ(s.find('\0'), std::string().find('\0')); EXPECT_EQ(s.find('\0'), StringPiece::npos); + EXPECT_FALSE(s.contains('\0')); // single char rfinds EXPECT_EQ(s.rfind('b'), 6); @@ -139,8 +141,10 @@ TEST(StringPiece, All) { EXPECT_EQ(s.find_first_of("bar"), 3); EXPECT_EQ(s.find_first_of("ba", 3), 3); EXPECT_EQ(s.find_first_of("ba", 4), 4); + EXPECT_TRUE(s.contains("bar")); EXPECT_EQ(s.find_first_of("xyxy"), StringPiece::npos); EXPECT_EQ(s.find_first_of("xyxy", 1), StringPiece::npos); + EXPECT_FALSE(s.contains("xyxy")); // starting position too far EXPECT_EQ(s.find_first_of("foo", 4), StringPiece::npos); // starting pos that is obviously past the end -- This works for std::string -- 2.34.1