diff --git a/llvm/include/llvm/ADT/StringRef.h b/llvm/include/llvm/ADT/StringRef.h --- a/llvm/include/llvm/ADT/StringRef.h +++ b/llvm/include/llvm/ADT/StringRef.h @@ -189,12 +189,15 @@ compareMemory(Data, RHS.Data, RHS.Length) == 0); } - /// equals_lower - Check for string equality, ignoring case. + /// Check for string equality, ignoring case. LLVM_NODISCARD - bool equals_lower(StringRef RHS) const { - return Length == RHS.Length && compare_lower(RHS) == 0; + bool equals_insensitive(StringRef RHS) const { + return Length == RHS.Length && compare_insensitive(RHS) == 0; } + LLVM_NODISCARD + bool equals_lower(StringRef RHS) const { return equals_insensitive(RHS); } + /// compare - Compare two strings; the result is -1, 0, or 1 if this string /// is lexicographically less than, equal to, or greater than the \p RHS. LLVM_NODISCARD @@ -209,9 +212,12 @@ return Length < RHS.Length ? -1 : 1; } - /// compare_lower - Compare two strings, ignoring case. + /// Compare two strings, ignoring case. LLVM_NODISCARD - int compare_lower(StringRef RHS) const; + int compare_insensitive(StringRef RHS) const; + + LLVM_NODISCARD + int compare_lower(StringRef RHS) const { return compare_insensitive(RHS); } /// compare_numeric - Compare two strings, treating sequences of digits as /// numbers. @@ -290,7 +296,12 @@ /// Check if this string starts with the given \p Prefix, ignoring case. LLVM_NODISCARD - bool startswith_lower(StringRef Prefix) const; + bool startswith_insensitive(StringRef Prefix) const; + + LLVM_NODISCARD + bool startswith_lower(StringRef Prefix) const { + return startswith_insensitive(Prefix); + } /// Check if this string ends with the given \p Suffix. LLVM_NODISCARD @@ -301,7 +312,12 @@ /// Check if this string ends with the given \p Suffix, ignoring case. LLVM_NODISCARD - bool endswith_lower(StringRef Suffix) const; + bool endswith_insensitive(StringRef Suffix) const; + + LLVM_NODISCARD + bool endswith_lower(StringRef Prefix) const { + return endswith_insensitive(Prefix); + } /// @} /// @name String Searching @@ -327,7 +343,12 @@ /// \returns The index of the first occurrence of \p C, or npos if not /// found. LLVM_NODISCARD - size_t find_lower(char C, size_t From = 0) const; + size_t find_insensitive(char C, size_t From = 0) const; + + LLVM_NODISCARD + size_t find_lower(char C, size_t From = 0) const { + return find_insensitive(C, From); + } /// Search for the first character satisfying the predicate \p F /// @@ -365,7 +386,12 @@ /// \returns The index of the first occurrence of \p Str, or npos if not /// found. LLVM_NODISCARD - size_t find_lower(StringRef Str, size_t From = 0) const; + size_t find_insensitive(StringRef Str, size_t From = 0) const; + + LLVM_NODISCARD + size_t find_lower(StringRef Str, size_t From = 0) const { + return find_insensitive(Str, From); + } /// Search for the last character \p C in the string. /// @@ -388,7 +414,12 @@ /// \returns The index of the last occurrence of \p C, or npos if not /// found. LLVM_NODISCARD - size_t rfind_lower(char C, size_t From = npos) const; + size_t rfind_insensitive(char C, size_t From = npos) const; + + LLVM_NODISCARD + size_t rfind_lower(char C, size_t From = npos) const { + return rfind_insensitive(C, From); + } /// Search for the last string \p Str in the string. /// @@ -402,7 +433,10 @@ /// \returns The index of the last occurrence of \p Str, or npos if not /// found. LLVM_NODISCARD - size_t rfind_lower(StringRef Str) const; + size_t rfind_insensitive(StringRef Str) const; + + LLVM_NODISCARD + size_t rfind_lower(StringRef Str) const { return rfind_insensitive(Str); } /// Find the first character in the string that is \p C, or npos if not /// found. Same as find. @@ -468,15 +502,25 @@ /// Return true if the given string is a substring of *this, and false /// otherwise. + LLVM_NODISCARD + bool contains_insensitive(StringRef Other) const { + return find_insensitive(Other) != npos; + } + LLVM_NODISCARD bool contains_lower(StringRef Other) const { - return find_lower(Other) != npos; + return contains_insensitive(Other); } /// Return true if the given character is contained in *this, and false /// otherwise. LLVM_NODISCARD - bool contains_lower(char C) const { return find_lower(C) != npos; } + bool contains_insensitive(char C) const { + return find_insensitive(C) != npos; + } + + LLVM_NODISCARD + bool contains_lower(char C) const { return contains_insensitive(C); } /// @} /// @name Helpful Algorithms @@ -687,14 +731,18 @@ /// Returns true if this StringRef has the given prefix, ignoring case, /// and removes that prefix. - bool consume_front_lower(StringRef Prefix) { - if (!startswith_lower(Prefix)) + bool consume_front_insensitive(StringRef Prefix) { + if (!startswith_insensitive(Prefix)) return false; *this = drop_front(Prefix.size()); return true; } + bool consume_front_lower(StringRef Prefix) { + return consume_front_insensitive(Prefix); + } + /// Returns true if this StringRef has the given suffix and removes that /// suffix. bool consume_back(StringRef Suffix) { @@ -707,14 +755,18 @@ /// Returns true if this StringRef has the given suffix, ignoring case, /// and removes that suffix. - bool consume_back_lower(StringRef Suffix) { - if (!endswith_lower(Suffix)) + bool consume_back_insensitive(StringRef Suffix) { + if (!endswith_insensitive(Suffix)) return false; *this = drop_back(Suffix.size()); return true; } + bool consume_back_lower(StringRef Suffix) { + return consume_back_insensitive(Suffix); + } + /// Return a reference to the substring from [Start, End). /// /// \param Start The index of the starting character in the substring; if diff --git a/llvm/lib/Support/StringRef.cpp b/llvm/lib/Support/StringRef.cpp --- a/llvm/lib/Support/StringRef.cpp +++ b/llvm/lib/Support/StringRef.cpp @@ -34,8 +34,7 @@ return 0; } -/// compare_lower - Compare strings, ignoring case. -int StringRef::compare_lower(StringRef RHS) const { +int StringRef::compare_insensitive(StringRef RHS) const { if (int Res = ascii_strncasecmp(Data, RHS.Data, std::min(Length, RHS.Length))) return Res; if (Length == RHS.Length) @@ -43,19 +42,17 @@ return Length < RHS.Length ? -1 : 1; } -/// Check if this string starts with the given \p Prefix, ignoring case. -bool StringRef::startswith_lower(StringRef Prefix) const { +bool StringRef::startswith_insensitive(StringRef Prefix) const { return Length >= Prefix.Length && ascii_strncasecmp(Data, Prefix.Data, Prefix.Length) == 0; } -/// Check if this string ends with the given \p Suffix, ignoring case. -bool StringRef::endswith_lower(StringRef Suffix) const { +bool StringRef::endswith_insensitive(StringRef Suffix) const { return Length >= Suffix.Length && ascii_strncasecmp(end() - Suffix.Length, Suffix.Data, Suffix.Length) == 0; } -size_t StringRef::find_lower(char C, size_t From) const { +size_t StringRef::find_insensitive(char C, size_t From) const { char L = toLower(C); return find_if([L](char D) { return toLower(D) == L; }, From); } @@ -173,10 +170,10 @@ return npos; } -size_t StringRef::find_lower(StringRef Str, size_t From) const { +size_t StringRef::find_insensitive(StringRef Str, size_t From) const { StringRef This = substr(From); while (This.size() >= Str.size()) { - if (This.startswith_lower(Str)) + if (This.startswith_insensitive(Str)) return From; This = This.drop_front(); ++From; @@ -184,7 +181,7 @@ return npos; } -size_t StringRef::rfind_lower(char C, size_t From) const { +size_t StringRef::rfind_insensitive(char C, size_t From) const { From = std::min(From, Length); size_t i = From; while (i != 0) { @@ -211,13 +208,13 @@ return npos; } -size_t StringRef::rfind_lower(StringRef Str) const { +size_t StringRef::rfind_insensitive(StringRef Str) const { size_t N = Str.size(); if (N > Length) return npos; for (size_t i = Length - N + 1, e = 0; i != e;) { --i; - if (substr(i, N).equals_lower(Str)) + if (substr(i, N).equals_insensitive(Str)) return i; } return npos; diff --git a/llvm/unittests/ADT/StringRefTest.cpp b/llvm/unittests/ADT/StringRefTest.cpp --- a/llvm/unittests/ADT/StringRefTest.cpp +++ b/llvm/unittests/ADT/StringRefTest.cpp @@ -98,15 +98,15 @@ EXPECT_EQ( 1, StringRef("aab").compare("aa")); EXPECT_EQ( 1, StringRef("\xFF").compare("\1")); - EXPECT_EQ(-1, StringRef("AaB").compare_lower("aAd")); - EXPECT_EQ( 0, StringRef("AaB").compare_lower("aab")); - EXPECT_EQ( 1, StringRef("AaB").compare_lower("AAA")); - EXPECT_EQ(-1, StringRef("AaB").compare_lower("aaBb")); - EXPECT_EQ(-1, StringRef("AaB").compare_lower("bb")); - EXPECT_EQ( 1, StringRef("aaBb").compare_lower("AaB")); - EXPECT_EQ( 1, StringRef("bb").compare_lower("AaB")); - EXPECT_EQ( 1, StringRef("AaB").compare_lower("aA")); - EXPECT_EQ( 1, StringRef("\xFF").compare_lower("\1")); + EXPECT_EQ(-1, StringRef("AaB").compare_insensitive("aAd")); + EXPECT_EQ( 0, StringRef("AaB").compare_insensitive("aab")); + EXPECT_EQ( 1, StringRef("AaB").compare_insensitive("AAA")); + EXPECT_EQ(-1, StringRef("AaB").compare_insensitive("aaBb")); + EXPECT_EQ(-1, StringRef("AaB").compare_insensitive("bb")); + EXPECT_EQ( 1, StringRef("aaBb").compare_insensitive("AaB")); + EXPECT_EQ( 1, StringRef("bb").compare_insensitive("AaB")); + EXPECT_EQ( 1, StringRef("AaB").compare_insensitive("aA")); + EXPECT_EQ( 1, StringRef("\xFF").compare_insensitive("\1")); EXPECT_EQ(-1, StringRef("aab").compare_numeric("aad")); EXPECT_EQ( 0, StringRef("aab").compare_numeric("aab")); @@ -366,14 +366,14 @@ EXPECT_FALSE(Str.startswith("hi")); } -TEST(StringRefTest, StartsWithLower) { +TEST(StringRefTest, StartsWithInsensitive) { StringRef Str("heLLo"); - EXPECT_TRUE(Str.startswith_lower("")); - EXPECT_TRUE(Str.startswith_lower("he")); - EXPECT_TRUE(Str.startswith_lower("hell")); - EXPECT_TRUE(Str.startswith_lower("HELlo")); - EXPECT_FALSE(Str.startswith_lower("helloworld")); - EXPECT_FALSE(Str.startswith_lower("hi")); + EXPECT_TRUE(Str.startswith_insensitive("")); + EXPECT_TRUE(Str.startswith_insensitive("he")); + EXPECT_TRUE(Str.startswith_insensitive("hell")); + EXPECT_TRUE(Str.startswith_insensitive("HELlo")); + EXPECT_FALSE(Str.startswith_insensitive("helloworld")); + EXPECT_FALSE(Str.startswith_insensitive("hi")); } TEST(StringRefTest, ConsumeFront) { @@ -392,22 +392,22 @@ EXPECT_TRUE(Str.consume_front("")); } -TEST(StringRefTest, ConsumeFrontLower) { +TEST(StringRefTest, ConsumeFrontInsensitive) { StringRef Str("heLLo"); - EXPECT_TRUE(Str.consume_front_lower("")); + EXPECT_TRUE(Str.consume_front_insensitive("")); EXPECT_EQ("heLLo", Str); EXPECT_FALSE(Str.consume_front("HEl")); EXPECT_EQ("heLLo", Str); - EXPECT_TRUE(Str.consume_front_lower("HEl")); + EXPECT_TRUE(Str.consume_front_insensitive("HEl")); EXPECT_EQ("Lo", Str); - EXPECT_FALSE(Str.consume_front_lower("loworld")); + EXPECT_FALSE(Str.consume_front_insensitive("loworld")); EXPECT_EQ("Lo", Str); - EXPECT_FALSE(Str.consume_front_lower("ol")); + EXPECT_FALSE(Str.consume_front_insensitive("ol")); EXPECT_EQ("Lo", Str); - EXPECT_TRUE(Str.consume_front_lower("lo")); + EXPECT_TRUE(Str.consume_front_insensitive("lo")); EXPECT_EQ("", Str); - EXPECT_FALSE(Str.consume_front_lower("o")); - EXPECT_TRUE(Str.consume_front_lower("")); + EXPECT_FALSE(Str.consume_front_insensitive("o")); + EXPECT_TRUE(Str.consume_front_insensitive("")); } TEST(StringRefTest, EndsWith) { @@ -419,14 +419,14 @@ EXPECT_FALSE(Str.endswith("so")); } -TEST(StringRefTest, EndsWithLower) { +TEST(StringRefTest, EndsWithInsensitive) { StringRef Str("heLLo"); - EXPECT_TRUE(Str.endswith_lower("")); - EXPECT_TRUE(Str.endswith_lower("lo")); - EXPECT_TRUE(Str.endswith_lower("LO")); - EXPECT_TRUE(Str.endswith_lower("ELlo")); - EXPECT_FALSE(Str.endswith_lower("helloworld")); - EXPECT_FALSE(Str.endswith_lower("hi")); + EXPECT_TRUE(Str.endswith_insensitive("")); + EXPECT_TRUE(Str.endswith_insensitive("lo")); + EXPECT_TRUE(Str.endswith_insensitive("LO")); + EXPECT_TRUE(Str.endswith_insensitive("ELlo")); + EXPECT_FALSE(Str.endswith_insensitive("helloworld")); + EXPECT_FALSE(Str.endswith_insensitive("hi")); } TEST(StringRefTest, ConsumeBack) { @@ -445,22 +445,22 @@ EXPECT_TRUE(Str.consume_back("")); } -TEST(StringRefTest, ConsumeBackLower) { +TEST(StringRefTest, ConsumeBackInsensitive) { StringRef Str("heLLo"); - EXPECT_TRUE(Str.consume_back_lower("")); + EXPECT_TRUE(Str.consume_back_insensitive("")); EXPECT_EQ("heLLo", Str); EXPECT_FALSE(Str.consume_back("lO")); EXPECT_EQ("heLLo", Str); - EXPECT_TRUE(Str.consume_back_lower("lO")); + EXPECT_TRUE(Str.consume_back_insensitive("lO")); EXPECT_EQ("heL", Str); - EXPECT_FALSE(Str.consume_back_lower("helhel")); + EXPECT_FALSE(Str.consume_back_insensitive("helhel")); EXPECT_EQ("heL", Str); - EXPECT_FALSE(Str.consume_back_lower("hle")); + EXPECT_FALSE(Str.consume_back_insensitive("hle")); EXPECT_EQ("heL", Str); - EXPECT_TRUE(Str.consume_back_lower("hEl")); + EXPECT_TRUE(Str.consume_back_insensitive("hEl")); EXPECT_EQ("", Str); - EXPECT_FALSE(Str.consume_back_lower("h")); - EXPECT_TRUE(Str.consume_back_lower("")); + EXPECT_FALSE(Str.consume_back_insensitive("h")); + EXPECT_TRUE(Str.consume_back_insensitive("")); } TEST(StringRefTest, Find) { @@ -472,7 +472,7 @@ char C; std::size_t From; std::size_t Pos; - std::size_t LowerPos; + std::size_t InsensitivePos; } CharExpectations[] = { {Str, 'h', 0U, 0U, 0U}, {Str, 'e', 0U, 1U, 1U}, @@ -488,7 +488,7 @@ llvm::StringRef S; std::size_t From; std::size_t Pos; - std::size_t LowerPos; + std::size_t InsensitivePos; } StrExpectations[] = { {Str, "helloword", 0, StringRef::npos, StringRef::npos}, {Str, "hello", 0, 0U, 0U}, @@ -507,14 +507,14 @@ for (auto &E : CharExpectations) { EXPECT_EQ(E.Pos, E.Str.find(E.C, E.From)); - EXPECT_EQ(E.LowerPos, E.Str.find_lower(E.C, E.From)); - EXPECT_EQ(E.LowerPos, E.Str.find_lower(toupper(E.C), E.From)); + EXPECT_EQ(E.InsensitivePos, E.Str.find_insensitive(E.C, E.From)); + EXPECT_EQ(E.InsensitivePos, E.Str.find_insensitive(toupper(E.C), E.From)); } for (auto &E : StrExpectations) { EXPECT_EQ(E.Pos, E.Str.find(E.S, E.From)); - EXPECT_EQ(E.LowerPos, E.Str.find_lower(E.S, E.From)); - EXPECT_EQ(E.LowerPos, E.Str.find_lower(E.S.upper(), E.From)); + EXPECT_EQ(E.InsensitivePos, E.Str.find_insensitive(E.S, E.From)); + EXPECT_EQ(E.InsensitivePos, E.Str.find_insensitive(E.S.upper(), E.From)); } EXPECT_EQ(3U, Str.rfind('l')); @@ -524,10 +524,10 @@ EXPECT_EQ(1U, Str.rfind("ello")); EXPECT_EQ(StringRef::npos, Str.rfind("zz")); - EXPECT_EQ(8U, Str.rfind_lower('l')); - EXPECT_EQ(8U, Str.rfind_lower('L')); - EXPECT_EQ(StringRef::npos, Str.rfind_lower('z')); - EXPECT_EQ(StringRef::npos, Str.rfind_lower("HELLOWORLD")); + EXPECT_EQ(8U, Str.rfind_insensitive('l')); + EXPECT_EQ(8U, Str.rfind_insensitive('L')); + EXPECT_EQ(StringRef::npos, Str.rfind_insensitive('z')); + EXPECT_EQ(StringRef::npos, Str.rfind_insensitive("HELLOWORLD")); EXPECT_EQ(5U, Str.rfind("HELLO")); EXPECT_EQ(6U, Str.rfind("ELLO")); EXPECT_EQ(StringRef::npos, Str.rfind("ZZ"));