Range::contains(x)
authorTom Jackson <tjackson@fb.com>
Mon, 24 Mar 2014 18:54:02 +0000 (11:54 -0700)
committerDave Watson <davejwatson@fb.com>
Mon, 31 Mar 2014 17:44:46 +0000 (10:44 -0700)
Test Plan: Unit tests

Reviewed By: marcelo.juchem@fb.com

FB internal diff: D1236630

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

index daca42c97202207e8269296c64fa097a1f41e26e..fc238b3ee0fcd98400bfca93f4967cf818f655b4 100644 (file)
@@ -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_);
index e9a56b2c07cbc49871ed34644655f649d0274d40..681aa03d410dfbc7077626e0b9909c8f292835cc 100644 (file)
@@ -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