diff options
| author | Rafael Espindola <rafael.espindola@gmail.com> | 2009-11-13 01:24:40 +0000 | 
|---|---|---|
| committer | Rafael Espindola <rafael.espindola@gmail.com> | 2009-11-13 01:24:40 +0000 | 
| commit | d554e4409278791a30906b5de18c7f539d816c9c (patch) | |
| tree | 0e7b53821ce2eb883ef1a66ee2a469a70c8e876b /llvm | |
| parent | 46a524c3e8ca4dbe2b3910e3640de8f0a51bb2a4 (diff) | |
| download | bcm5719-llvm-d554e4409278791a30906b5de18c7f539d816c9c.tar.gz bcm5719-llvm-d554e4409278791a30906b5de18c7f539d816c9c.zip  | |
Add a new split method to StringRef that puts the substrings in a vector.
llvm-svn: 87058
Diffstat (limited to 'llvm')
| -rw-r--r-- | llvm/include/llvm/ADT/StringRef.h | 27 | ||||
| -rw-r--r-- | llvm/lib/Support/StringExtras.cpp | 19 | ||||
| -rw-r--r-- | llvm/unittests/ADT/StringRefTest.cpp | 75 | 
3 files changed, 121 insertions, 0 deletions
diff --git a/llvm/include/llvm/ADT/StringRef.h b/llvm/include/llvm/ADT/StringRef.h index b07dcc12ced..6ccb37d346c 100644 --- a/llvm/include/llvm/ADT/StringRef.h +++ b/llvm/include/llvm/ADT/StringRef.h @@ -15,6 +15,14 @@  #include <cstring>  #include <string> +namespace std { + template<typename _Tp> + class allocator; + + template<typename _Tp, typename _Alloc> + class vector; +} +  namespace llvm {    /// StringRef - Represent a constant reference to a string, i.e. a character @@ -314,6 +322,25 @@ namespace llvm {        return std::make_pair(slice(0, Idx), slice(Idx + Separator.size(), npos));      } +    /// split - Split into substrings around the occurences of a separator +    /// string. +    /// +    /// Each substring is stored in \arg A. If \arg MaxSplit is >= 0, at most +    /// \arg MaxSplit splits are done and consequently <= \arg MaxSplit +    /// elements are added to A. +    /// If \arg KeepEmpty is false, empty strings are not added to \arg A. They +    /// still count when considering \arg MaxSplit +    /// An useful invariant is that +    /// Separator.join(A) == *this if MaxSplit == -1 and KeepEmpty == true +    /// +    /// \param A - Where to put the substrings. +    /// \param Separator - The string to split on. +    /// \param MaxSplit - The maximum number of times the string is split. +    /// \parm KeepEmpty - True if empty substring should be added. +    void split(std::vector<StringRef, std::allocator<StringRef> > &A, +               StringRef Separator, unsigned MaxSplit = -1, +               bool KeepEmpty = true) const; +      /// rsplit - Split into two substrings around the last occurence of a      /// separator character.      /// diff --git a/llvm/lib/Support/StringExtras.cpp b/llvm/lib/Support/StringExtras.cpp index c72f1216534..05ba34b2e7b 100644 --- a/llvm/lib/Support/StringExtras.cpp +++ b/llvm/lib/Support/StringExtras.cpp @@ -56,3 +56,22 @@ void llvm::SplitString(const std::string &Source,      S2 = getToken(S, Delimiters);    }  } + +void llvm::StringRef::split(std::vector<StringRef> &A, +                            StringRef Separators, unsigned MaxSplit, +                            bool KeepEmpty) const { +  StringRef rest = *this; + +  for (unsigned splits = 0; +       rest.size() != 0 && (MaxSplit < 0 || splits < MaxSplit); +       ++splits) { +    std::pair<llvm::StringRef, llvm::StringRef> p = rest.split(Separators); + +    if (p.first.size() != 0 || KeepEmpty) +      A.push_back(p.first); +    rest = p.second; +  } + +  if (rest.size() != 0 || KeepEmpty) +    A.push_back(rest); +} diff --git a/llvm/unittests/ADT/StringRefTest.cpp b/llvm/unittests/ADT/StringRefTest.cpp index 7828b5fadf2..3c0cc58ad2d 100644 --- a/llvm/unittests/ADT/StringRefTest.cpp +++ b/llvm/unittests/ADT/StringRefTest.cpp @@ -110,6 +110,81 @@ TEST(StringRefTest, Split) {              Str.rsplit('o'));  } +TEST(StringRefTest, Split2) { +  std::vector<StringRef> parts; +  std::vector<StringRef> expected; + +  expected.push_back("ab"); expected.push_back("c"); +  StringRef(",ab,,c,").split(parts, ",", -1, false); +  EXPECT_TRUE(parts == expected); + +  expected.clear(); parts.clear(); +  expected.push_back(""); expected.push_back("ab"); expected.push_back(""); +  expected.push_back("c"); expected.push_back(""); +  StringRef(",ab,,c,").split(parts, ",", -1, true); +  EXPECT_TRUE(parts == expected); + +  expected.clear(); parts.clear(); +  expected.push_back(""); +  StringRef("").split(parts, ",", -1, true); +  EXPECT_TRUE(parts == expected); + +  expected.clear(); parts.clear(); +  StringRef("").split(parts, ",", -1, false); +  EXPECT_TRUE(parts == expected); + +  expected.clear(); parts.clear(); +  StringRef(",").split(parts, ",", -1, false); +  EXPECT_TRUE(parts == expected); + +  expected.clear(); parts.clear(); +  expected.push_back(""); expected.push_back(""); +  StringRef(",").split(parts, ",", -1, true); +  EXPECT_TRUE(parts == expected); + +  // Test MaxSplit +  expected.clear(); parts.clear(); +  expected.push_back("a,,b,c"); +  StringRef("a,,b,c").split(parts, ",", 0, true); +  EXPECT_TRUE(parts == expected); + +  expected.clear(); parts.clear(); +  expected.push_back("a,,b,c"); +  StringRef("a,,b,c").split(parts, ",", 0, false); +  EXPECT_TRUE(parts == expected); + +  expected.clear(); parts.clear(); +  expected.push_back("a"); expected.push_back(",b,c"); +  StringRef("a,,b,c").split(parts, ",", 1, true); +  EXPECT_TRUE(parts == expected); + +  expected.clear(); parts.clear(); +  expected.push_back("a"); expected.push_back(",b,c"); +  StringRef("a,,b,c").split(parts, ",", 1, false); +  EXPECT_TRUE(parts == expected); + +  expected.clear(); parts.clear(); +  expected.push_back("a"); expected.push_back(""); expected.push_back("b,c"); +  StringRef("a,,b,c").split(parts, ",", 2, true); +  EXPECT_TRUE(parts == expected); + +  expected.clear(); parts.clear(); +  expected.push_back("a"); expected.push_back("b,c"); +  StringRef("a,,b,c").split(parts, ",", 2, false); +  EXPECT_TRUE(parts == expected); + +  expected.clear(); parts.clear(); +  expected.push_back("a"); expected.push_back(""); expected.push_back("b"); +  expected.push_back("c"); +  StringRef("a,,b,c").split(parts, ",", 3, true); +  EXPECT_TRUE(parts == expected); + +  expected.clear(); parts.clear(); +  expected.push_back("a"); expected.push_back("b"); expected.push_back("c"); +  StringRef("a,,b,c").split(parts, ",", 3, false); +  EXPECT_TRUE(parts == expected); +} +  TEST(StringRefTest, StartsWith) {    StringRef Str("hello");    EXPECT_TRUE(Str.startswith("he"));  | 

