From e65512809a4144c17538aac4cc59fac6d325a7e4 Mon Sep 17 00:00:00 2001 From: Daniel Dunbar Date: Wed, 16 Sep 2009 22:38:48 +0000 Subject: Add StringRef::{rfind, rsplit} git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@82087 91177308-0d34-0410-b5e6-96231b3b80d8 --- include/llvm/ADT/StringRef.h | 50 ++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 48 insertions(+), 2 deletions(-) (limited to 'include/llvm/ADT/StringRef.h') 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 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)); + } + /// @} }; -- cgit v1.1