[llvm-commits] [llvm] r76559 - in /llvm/trunk: include/llvm/ADT/StringRef.h unittests/ADT/StringRefTest.cpp
Daniel Dunbar
daniel at zuster.org
Tue Jul 21 02:19:30 PDT 2009
Author: ddunbar
Date: Tue Jul 21 04:18:49 2009
New Revision: 76559
URL: http://llvm.org/viewvc/llvm-project?rev=76559&view=rev
Log:
Add StringRef::{substr, startswith}.
Modified:
llvm/trunk/include/llvm/ADT/StringRef.h
llvm/trunk/unittests/ADT/StringRefTest.cpp
Modified: llvm/trunk/include/llvm/ADT/StringRef.h
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/include/llvm/ADT/StringRef.h?rev=76559&r1=76558&r2=76559&view=diff
==============================================================================
--- llvm/trunk/include/llvm/ADT/StringRef.h (original)
+++ llvm/trunk/include/llvm/ADT/StringRef.h Tue Jul 21 04:18:49 2009
@@ -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 @@
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 @@
}
/// @}
+ /// @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
Modified: llvm/trunk/unittests/ADT/StringRefTest.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/unittests/ADT/StringRefTest.cpp?rev=76559&r1=76558&r2=76559&view=diff
==============================================================================
--- llvm/trunk/unittests/ADT/StringRefTest.cpp (original)
+++ llvm/trunk/unittests/ADT/StringRefTest.cpp Tue Jul 21 04:18:49 2009
@@ -56,4 +56,16 @@
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
More information about the llvm-commits
mailing list