diff options
author | Daniel Dunbar <daniel@zuster.org> | 2009-09-16 22:38:48 +0000 |
---|---|---|
committer | Daniel Dunbar <daniel@zuster.org> | 2009-09-16 22:38:48 +0000 |
commit | d86d635d2831ee15f0e5bafb597d29f0318aaeb4 (patch) | |
tree | be1cc374a08ca577ef4144eb7065872af199c499 /include | |
parent | 2eddf5d5241b54e02b035b40f16ffa1e5effbe19 (diff) | |
download | external_llvm-d86d635d2831ee15f0e5bafb597d29f0318aaeb4.zip external_llvm-d86d635d2831ee15f0e5bafb597d29f0318aaeb4.tar.gz external_llvm-d86d635d2831ee15f0e5bafb597d29f0318aaeb4.tar.bz2 |
Add StringRef::{rfind, rsplit}
git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@82087 91177308-0d34-0410-b5e6-96231b3b80d8
Diffstat (limited to 'include')
-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 acfc335..74d3eaa 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)); + } + /// @} }; |