diff options
Diffstat (limited to 'include/llvm/ADT/StringRef.h')
-rw-r--r-- | include/llvm/ADT/StringRef.h | 50 |
1 files changed, 48 insertions, 2 deletions
diff --git a/include/llvm/ADT/StringRef.h b/include/llvm/ADT/StringRef.h index acfc335b80..74d3eaaf34 100644 --- a/include/llvm/ADT/StringRef.h +++ b/include/llvm/ADT/StringRef.h @@ -147,7 +147,7 @@ namespace llvm { /// @name String Searching /// @{ - /// find - Search for the character \arg C in the string. + /// find - Search for the first character \arg C in the string. /// /// \return - The index of the first occurence of \arg C, or npos if not /// found. @@ -158,7 +158,7 @@ namespace llvm { return npos; } - /// find - Search for the string \arg Str in the string. + /// find - Search for the first string \arg Str in the string. /// /// \return - The index of the first occurence of \arg Str, or npos if not /// found. @@ -172,6 +172,35 @@ namespace llvm { return npos; } + /// rfind - Search for the last character \arg C in the string. + /// + /// \return - The index of the last occurence of \arg C, or npos if not + /// found. + size_t rfind(char C) const { + for (size_t i = Length, e = 0; i != e;) { + --i; + if (Data[i] == C) + return i; + } + return npos; + } + + /// rfind - Search for the last string \arg Str in the string. + /// + /// \return - The index of the last occurence of \arg Str, or npos if not + /// found. + size_t rfind(const StringRef &Str) const { + size_t N = Str.size(); + if (N > Length) + return npos; + for (size_t i = Length - N + 1, e = 0; i != e;) { + --i; + if (substr(i, N).equals(Str)) + return i; + } + return npos; + } + /// count - Return the number of occurrences of \arg C in the string. size_t count(char C) const { size_t Count = 0; @@ -245,6 +274,23 @@ namespace llvm { return std::make_pair(slice(0, Idx), slice(Idx+1, npos)); } + /// rsplit - Split into two substrings around the last occurence of a + /// separator character. + /// + /// If \arg Separator is in the string, then the result is a pair (LHS, RHS) + /// such that (*this == LHS + Separator + RHS) is true and RHS is + /// minimal. If \arg Separator is not in the string, then the result is a + /// pair (LHS, RHS) where (*this == LHS) and (RHS == ""). + /// + /// \param Separator - The character to split on. + /// \return - The split substrings. + std::pair<StringRef, StringRef> rsplit(char Separator) const { + size_t Idx = rfind(Separator); + if (Idx == npos) + return std::make_pair(*this, StringRef()); + return std::make_pair(slice(0, Idx), slice(Idx+1, npos)); + } + /// @} }; |