diff options
author | Daniel Dunbar <daniel@zuster.org> | 2009-07-21 09:18:49 +0000 |
---|---|---|
committer | Daniel Dunbar <daniel@zuster.org> | 2009-07-21 09:18:49 +0000 |
commit | f5fdf73238dfd923f33bcbbd397cff6752d9c41e (patch) | |
tree | e73b3c03ca8c4dc6587bdb4a8d5f3f8186df5ca1 | |
parent | 5d885023feac777ac0bcf1b63ac8282ab76f3ec7 (diff) | |
download | llvm-f5fdf73238dfd923f33bcbbd397cff6752d9c41e.tar.gz llvm-f5fdf73238dfd923f33bcbbd397cff6752d9c41e.tar.bz2 llvm-f5fdf73238dfd923f33bcbbd397cff6752d9c41e.tar.xz |
Add StringRef::{substr, startswith}.
git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@76559 91177308-0d34-0410-b5e6-96231b3b80d8
-rw-r--r-- | include/llvm/ADT/StringRef.h | 28 | ||||
-rw-r--r-- | unittests/ADT/StringRefTest.cpp | 12 |
2 files changed, 39 insertions, 1 deletions
diff --git a/include/llvm/ADT/StringRef.h b/include/llvm/ADT/StringRef.h index 988701b97d..11903bef79 100644 --- a/include/llvm/ADT/StringRef.h +++ b/include/llvm/ADT/StringRef.h @@ -14,6 +14,7 @@ #include <string> namespace llvm { + /// StringRef - Represent a constant reference to a string, i.e. a character /// array and a length, which need not be null terminated. /// @@ -24,13 +25,14 @@ namespace llvm { class StringRef { public: typedef const char *iterator; + static const size_t npos = std::string::npos; private: /// The start of the string, in an external buffer. const char *Data; /// The length of the string. - unsigned Length; + size_t Length; public: /// @name Constructors @@ -121,7 +123,31 @@ namespace llvm { } /// @} + /// @name Utility Functions + /// @{ + + /// substr - Return a reference to a substring of this object. + /// + /// \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. + /// + /// \param N - The number of characters to included in the substring. If N + /// exceeds the number of characters remaining in the string, the string + /// suffix (starting with \arg Start) will be returned. + StringRef substr(size_t Start, size_t N = npos) const { + Start = std::min(Start, Length); + return StringRef(Data + Start, std::min(N, Length - Start)); + } + + /// startswith - Check if this string starts with the given \arg Prefix. + bool startswith(const StringRef &Prefix) const { + return substr(0, Prefix.Length) == Prefix; + } + + /// @} }; + } #endif diff --git a/unittests/ADT/StringRefTest.cpp b/unittests/ADT/StringRefTest.cpp index 8a2c7435d8..bb46dc010a 100644 --- a/unittests/ADT/StringRefTest.cpp +++ b/unittests/ADT/StringRefTest.cpp @@ -56,4 +56,16 @@ TEST(StringRefTest, Operators) { EXPECT_EQ('a', StringRef("aab")[1]); } +TEST(StringRefTest, Utilities) { + StringRef Str("hello"); + EXPECT_TRUE(Str.substr(3) == "lo"); + EXPECT_TRUE(Str.substr(100) == ""); + EXPECT_TRUE(Str.substr(0, 100) == "hello"); + EXPECT_TRUE(Str.substr(4, 10) == "o"); + + EXPECT_TRUE(Str.startswith("he")); + EXPECT_FALSE(Str.startswith("helloworld")); + EXPECT_FALSE(Str.startswith("hi")); +} + } // end anonymous namespace |