From: Eli Friedman Date: Mon, 21 Dec 2009 06:49:24 +0000 (+0000) Subject: Change StringRef::startswith and StringRef::endswith to versions which are a X-Git-Url: http://plrg.eecs.uci.edu/git/?p=oota-llvm.git;a=commitdiff_plain;h=d5b1f8a8426e82990dafc6e3336fefc6635c8fa4 Change StringRef::startswith and StringRef::endswith to versions which are a bit more verbose, but optimize to much shorter code. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@91817 91177308-0d34-0410-b5e6-96231b3b80d8 --- diff --git a/include/llvm/ADT/StringRef.h b/include/llvm/ADT/StringRef.h index f299f5fd651..a7442666d67 100644 --- a/include/llvm/ADT/StringRef.h +++ b/include/llvm/ADT/StringRef.h @@ -159,12 +159,14 @@ namespace llvm { /// 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; } /// @} diff --git a/unittests/ADT/StringRefTest.cpp b/unittests/ADT/StringRefTest.cpp index dfa208abefd..19665df88d5 100644 --- a/unittests/ADT/StringRefTest.cpp +++ b/unittests/ADT/StringRefTest.cpp @@ -198,6 +198,14 @@ TEST(StringRefTest, StartsWith) { 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'));