Commit 7154531b authored by Tom Jackson's avatar Tom Jackson Committed by Dave Watson

Range::contains(x)

Test Plan: Unit tests

Reviewed By: marcelo.juchem@fb.com

FB internal diff: D1236630
parent 922f80e0
......@@ -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_);
......
......@@ -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
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment