Skip to content

Commit

Permalink
Range::contains(x)
Browse files Browse the repository at this point in the history
Test Plan: Unit tests

Reviewed By: marcelo.juchem@fb.com

FB internal diff: D1236630
  • Loading branch information
ddrcoder authored and Dave Watson committed Mar 31, 2014
1 parent 922f80e commit 7154531
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 0 deletions.
13 changes: 13 additions & 0 deletions folly/Range.h
Original file line number Diff line number Diff line change
Expand Up @@ -501,6 +501,19 @@ class Range : private boost::totally_ordered<Range<Iter> > {
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_);
Expand Down
4 changes: 4 additions & 0 deletions folly/test/RangeTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -116,13 +116,15 @@ 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);
EXPECT_EQ(s.find('z', 55), StringPiece::npos);
// 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);
Expand All @@ -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
Expand Down

0 comments on commit 7154531

Please sign in to comment.