Index: include/llvm/ADT/StringRef.h =================================================================== --- include/llvm/ADT/StringRef.h +++ include/llvm/ADT/StringRef.h @@ -17,6 +17,7 @@ #include #include #include +#include #include namespace llvm { @@ -337,6 +338,10 @@ /// Complexity: O(size() + Chars.size()) size_t find_last_not_of(StringRef Chars, size_t From = npos) const; + /// Check if the specified substring is contained in this string, and + /// return a boolean indicating success or failure. + bool contains(llvm::StringRef Str) const { return find(Str) != npos; } + /// @} /// @name Helpful Algorithms /// @{ @@ -492,6 +497,22 @@ return true; } + /// Returns a StringRef representing the first occurrence of a string Result + /// such that \p Left + Result + \p Right is a substring of *this. When + /// \p Left is empty, the result is taken from the beginning of the string + /// and when \p Right is empty, the result runs up until the end of the + /// string. + LLVM_ATTRIBUTE_ALWAYS_INLINE + LLVM_ATTRIBUTE_UNUSED_RESULT + StringRef scan_between(StringRef Left, StringRef Right) const { + StringRef A, B; + std::tie(A, B) = split(Left); + if (Right.empty()) + return B; + std::tie(B, A) = B.split(Right); + return B; + } + /// Return a reference to the substring from [Start, End). /// /// \param Start The index of the starting character in the substring; if Index: unittests/ADT/StringRefTest.cpp =================================================================== --- unittests/ADT/StringRefTest.cpp +++ unittests/ADT/StringRefTest.cpp @@ -373,6 +373,16 @@ EXPECT_TRUE(Str.consume_back("")); } +TEST(StringRefTest, ScanBetween) { + StringRef Str("StringRefTest::ScanBetween()"); + EXPECT_EQ(Str, Str.scan_between("", "")); + EXPECT_EQ("StringRefTest", Str.scan_between("", "::")); + EXPECT_EQ("ScanBetween()", Str.scan_between("::", "")); + EXPECT_EQ("ScanBetween", Str.scan_between("::", "()")); + EXPECT_EQ("::ScanBetween()", Str.scan_between("StringRefTest", "ABCDE")); + EXPECT_EQ("", Str.scan_between("ABCDE", "::")); +} + TEST(StringRefTest, Find) { StringRef Str("hello"); EXPECT_EQ(2U, Str.find('l'));