diff --git a/llvm/include/llvm/ADT/StringRef.h b/llvm/include/llvm/ADT/StringRef.h index 8ebd5fc346e60..2d2f0bedfe1f8 100644 --- a/llvm/include/llvm/ADT/StringRef.h +++ b/llvm/include/llvm/ADT/StringRef.h @@ -175,7 +175,14 @@ namespace llvm { /// if this string is lexicographically less than, equal to, or greater than /// the \p RHS. [[nodiscard]] int compare(StringRef RHS) const { - return std::string_view(*this).compare(RHS); + // Check the prefix for a mismatch. + if (int Res = compareMemory(Data, RHS.Data, std::min(Length, RHS.Length))) + return Res < 0 ? -1 : 1; + + // Otherwise the prefixes match, so we only need to check the lengths. + if (Length == RHS.Length) + return 0; + return Length < RHS.Length ? -1 : 1; } /// Compare two strings, ignoring case.