aboutsummaryrefslogtreecommitdiffstats
path: root/include/llvm/ADT
diff options
context:
space:
mode:
authorDaniel Dunbar <daniel@zuster.org>2009-08-11 20:47:15 +0000
committerDaniel Dunbar <daniel@zuster.org>2009-08-11 20:47:15 +0000
commit52597ba5703cc8f3f7599cd3a3a85b796df42b62 (patch)
treee5e52a07f3fc5d958c9f0671e42c48c23fba87c9 /include/llvm/ADT
parentd9236ecabe1bd73e89ce5cdb12da8a1bfdc67a0d (diff)
downloadexternal_llvm-52597ba5703cc8f3f7599cd3a3a85b796df42b62.zip
external_llvm-52597ba5703cc8f3f7599cd3a3a85b796df42b62.tar.gz
external_llvm-52597ba5703cc8f3f7599cd3a3a85b796df42b62.tar.bz2
StringRef: Add find(char) and find(StringRef).
Also, regroup functions. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@78712 91177308-0d34-0410-b5e6-96231b3b80d8
Diffstat (limited to 'include/llvm/ADT')
-rw-r--r--include/llvm/ADT/StringRef.h62
1 files changed, 47 insertions, 15 deletions
diff --git a/include/llvm/ADT/StringRef.h b/include/llvm/ADT/StringRef.h
index e24fe05..b239e3b 100644
--- a/include/llvm/ADT/StringRef.h
+++ b/include/llvm/ADT/StringRef.h
@@ -123,7 +123,50 @@ namespace llvm {
}
/// @}
- /// @name Utility Functions
+ /// @name String Predicates
+ /// @{
+
+ /// startswith - Check if this string starts with the given \arg Prefix.
+ bool startswith(const StringRef &Prefix) const {
+ return substr(0, Prefix.Length).equals(Prefix);
+ }
+
+ /// endswith - Check if this string ends with the given \arg Suffix.
+ bool endswith(const StringRef &Suffix) const {
+ return slice(size() - Suffix.Length, size()).equals(Suffix);
+ }
+
+ /// @}
+ /// @name String Searching
+ /// @{
+
+ /// find - Search for the character \arg C in the string.
+ ///
+ /// \return - The index of the first occurence of \arg C, or npos if not
+ /// found.
+ size_t find(char C) const {
+ for (size_t i = 0, e = Length; i != e; ++i)
+ if (Data[i] == C)
+ return i;
+ return npos;
+ }
+
+ /// find - Search for the string \arg Str in the string.
+ ///
+ /// \return - The index of the first occurence of \arg Str, or npos if not
+ /// found.
+ size_t find(const StringRef &Str) const {
+ size_t N = Str.size();
+ if (N > Length)
+ return npos;
+ for (size_t i = 0, e = Length - N + 1; i != e; ++i)
+ if (substr(i, N).equals(Str))
+ return i;
+ return npos;
+ }
+
+ /// @}
+ /// @name Substring Operations
/// @{
/// substr - Return a reference to the substring from [Start, Start + N).
@@ -167,21 +210,10 @@ namespace llvm {
/// \param Separator - The character to split on.
/// \return - The split substrings.
std::pair<StringRef, StringRef> split(char Separator) const {
- iterator it = std::find(begin(), end(), Separator);
- if (it == end())
+ size_t Idx = find(Separator);
+ if (Idx == npos)
return std::make_pair(*this, StringRef());
- return std::make_pair(StringRef(begin(), it - begin()),
- StringRef(it + 1, end() - (it + 1)));
- }
-
- /// startswith - Check if this string starts with the given \arg Prefix.
- bool startswith(const StringRef &Prefix) const {
- return substr(0, Prefix.Length).equals(Prefix);
- }
-
- /// endswith - Check if this string ends with the given \arg Suffix.
- bool endswith(const StringRef &Suffix) const {
- return slice(size() - Suffix.Length, size()).equals(Suffix);
+ return std::make_pair(slice(0, Idx), slice(Idx+1, npos));
}
/// @}