[llvm-commits] [llvm] r91817 - in /llvm/trunk: include/llvm/ADT/StringRef.h unittests/ADT/StringRefTest.cpp
Eli Friedman
eli.friedman at gmail.com
Sun Dec 20 22:49:24 PST 2009
Author: efriedma
Date: Mon Dec 21 00:49:24 2009
New Revision: 91817
URL: http://llvm.org/viewvc/llvm-project?rev=91817&view=rev
Log:
Change StringRef::startswith and StringRef::endswith to versions which are a
bit more verbose, but optimize to much shorter code.
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=91817&r1=91816&r2=91817&view=diff
==============================================================================
--- llvm/trunk/include/llvm/ADT/StringRef.h (original)
+++ llvm/trunk/include/llvm/ADT/StringRef.h Mon Dec 21 00:49:24 2009
@@ -159,12 +159,14 @@
/// startswith - Check if this string starts with the given \arg Prefix.
bool startswith(StringRef Prefix) const {
- return substr(0, Prefix.Length).equals(Prefix);
+ return Length >= Prefix.Length &&
+ memcmp(Data, Prefix.Data, Prefix.Length) == 0;
}
/// endswith - Check if this string ends with the given \arg Suffix.
bool endswith(StringRef Suffix) const {
- return slice(size() - Suffix.Length, size()).equals(Suffix);
+ return Length >= Suffix.Length &&
+ memcmp(end() - Suffix.Length, Suffix.Data, Suffix.Length) == 0;
}
/// @}
Modified: llvm/trunk/unittests/ADT/StringRefTest.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/unittests/ADT/StringRefTest.cpp?rev=91817&r1=91816&r2=91817&view=diff
==============================================================================
--- llvm/trunk/unittests/ADT/StringRefTest.cpp (original)
+++ llvm/trunk/unittests/ADT/StringRefTest.cpp Mon Dec 21 00:49:24 2009
@@ -198,6 +198,14 @@
EXPECT_FALSE(Str.startswith("hi"));
}
+TEST(StringRefTest, EndsWith) {
+ StringRef Str("hello");
+ EXPECT_TRUE(Str.endswith("lo"));
+ EXPECT_FALSE(Str.endswith("helloworld"));
+ EXPECT_FALSE(Str.endswith("worldhello"));
+ EXPECT_FALSE(Str.endswith("so"));
+}
+
TEST(StringRefTest, Find) {
StringRef Str("hello");
EXPECT_EQ(2U, Str.find('l'));
More information about the llvm-commits
mailing list