From d61918fc6898a89df8b0a03e068f234ded010cdf Mon Sep 17 00:00:00 2001 From: Daniel Dunbar Date: Sun, 26 Jul 2009 03:18:15 +0000 Subject: [PATCH] Add StringRef::{slice, split}, two convenient string operations which are simple and efficient on a StringRef. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@77117 91177308-0d34-0410-b5e6-96231b3b80d8 --- include/llvm/ADT/StringRef.h | 41 ++++++++++++++++++++++++++++++--- unittests/ADT/StringRefTest.cpp | 15 ++++++++++++ 2 files changed, 53 insertions(+), 3 deletions(-) diff --git a/include/llvm/ADT/StringRef.h b/include/llvm/ADT/StringRef.h index 1d7a9b6d56f..5f3e5fba372 100644 --- a/include/llvm/ADT/StringRef.h +++ b/include/llvm/ADT/StringRef.h @@ -10,6 +10,7 @@ #ifndef LLVM_ADT_STRINGREF_H #define LLVM_ADT_STRINGREF_H +#include #include #include #include @@ -120,11 +121,11 @@ namespace llvm { /// @name Utility Functions /// @{ - /// substr - Return a reference to a substring of this object. + /// substr - Return a reference to the substring from [Start, Start + N). /// /// \param Start - The index of the starting character in the substring; if - /// the index is greater than the length of the string then the empty - /// substring will be returned. + /// the index is npos or greater than the length of the string then the + /// empty substring will be returned. /// /// \param N - The number of characters to included in the substring. If N /// exceeds the number of characters remaining in the string, the string @@ -134,6 +135,40 @@ namespace llvm { return StringRef(Data + Start, std::min(N, Length - Start)); } + /// slice - Return a reference to the substring from [Start, End). + /// + /// \param Start - The index of the starting character in the substring; if + /// the index is npos or greater than the length of the string then the + /// empty substring will be returned. + /// + /// \param End - The index following the last character to include in the + /// substring. If this is npos, or less than \arg Start, or exceeds the + /// number of characters remaining in the string, the string suffix + /// (starting with \arg Start) will be returned. + StringRef slice(size_t Start, size_t End) const { + Start = std::min(Start, Length); + End = std::min(std::max(Start, End), Length); + return StringRef(Data + Start, End - Start); + } + + /// split - Split into two substrings around the first 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 + /// maximal. 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 split(char Separator) const { + iterator it = std::find(begin(), end(), Separator); + if (it == end()) + 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); diff --git a/unittests/ADT/StringRefTest.cpp b/unittests/ADT/StringRefTest.cpp index ba2216debd6..e7498cca1b0 100644 --- a/unittests/ADT/StringRefTest.cpp +++ b/unittests/ADT/StringRefTest.cpp @@ -64,6 +64,21 @@ TEST(StringRefTest, Utilities) { EXPECT_TRUE(Str.substr(0, 100) == "hello"); EXPECT_TRUE(Str.substr(4, 10) == "o"); + EXPECT_TRUE(Str.slice(2, 3) == "l"); + EXPECT_TRUE(Str.slice(1, 4) == "ell"); + EXPECT_TRUE(Str.slice(2, 100) == "llo"); + EXPECT_TRUE(Str.slice(2, 1) == ""); + EXPECT_TRUE(Str.slice(10, 20) == ""); + + EXPECT_TRUE(Str.split('X') == std::make_pair(StringRef("hello"), + StringRef(""))); + EXPECT_TRUE(Str.split('e') == std::make_pair(StringRef("h"), + StringRef("llo"))); + EXPECT_TRUE(Str.split('h') == std::make_pair(StringRef(""), + StringRef("ello"))); + EXPECT_TRUE(Str.split('o') == std::make_pair(StringRef("hell"), + StringRef(""))); + EXPECT_TRUE(Str.startswith("he")); EXPECT_FALSE(Str.startswith("helloworld")); EXPECT_FALSE(Str.startswith("hi")); -- 2.34.1