aboutsummaryrefslogtreecommitdiff
path: root/contrib/llvm-project/llvm/lib/Support/StringRef.cpp
diff options
context:
space:
mode:
authorDimitry Andric <dim@FreeBSD.org>2023-04-14 21:41:27 +0000
committerDimitry Andric <dim@FreeBSD.org>2023-06-22 18:20:56 +0000
commitbdd1243df58e60e85101c09001d9812a789b6bc4 (patch)
treea1ce621c7301dd47ba2ddc3b8eaa63b441389481 /contrib/llvm-project/llvm/lib/Support/StringRef.cpp
parent781624ca2d054430052c828ba8d2c2eaf2d733e7 (diff)
parente3b557809604d036af6e00c60f012c2025b59a5e (diff)
Diffstat (limited to 'contrib/llvm-project/llvm/lib/Support/StringRef.cpp')
-rw-r--r--contrib/llvm-project/llvm/lib/Support/StringRef.cpp56
1 files changed, 28 insertions, 28 deletions
diff --git a/contrib/llvm-project/llvm/lib/Support/StringRef.cpp b/contrib/llvm-project/llvm/lib/Support/StringRef.cpp
index 096b2d2d8c07..fb93940592c7 100644
--- a/contrib/llvm-project/llvm/lib/Support/StringRef.cpp
+++ b/contrib/llvm-project/llvm/lib/Support/StringRef.cpp
@@ -42,12 +42,12 @@ int StringRef::compare_insensitive(StringRef RHS) const {
return Length < RHS.Length ? -1 : 1;
}
-bool StringRef::startswith_insensitive(StringRef Prefix) const {
+bool StringRef::starts_with_insensitive(StringRef Prefix) const {
return Length >= Prefix.Length &&
ascii_strncasecmp(Data, Prefix.Data, Prefix.Length) == 0;
}
-bool StringRef::endswith_insensitive(StringRef Suffix) const {
+bool StringRef::ends_with_insensitive(StringRef Suffix) const {
return Length >= Suffix.Length &&
ascii_strncasecmp(end() - Suffix.Length, Suffix.Data, Suffix.Length) == 0;
}
@@ -92,16 +92,15 @@ int StringRef::compare_numeric(StringRef RHS) const {
unsigned StringRef::edit_distance(llvm::StringRef Other,
bool AllowReplacements,
unsigned MaxEditDistance) const {
- return llvm::ComputeEditDistance(
- makeArrayRef(data(), size()),
- makeArrayRef(Other.data(), Other.size()),
- AllowReplacements, MaxEditDistance);
+ return llvm::ComputeEditDistance(ArrayRef(data(), size()),
+ ArrayRef(Other.data(), Other.size()),
+ AllowReplacements, MaxEditDistance);
}
unsigned llvm::StringRef::edit_distance_insensitive(
StringRef Other, bool AllowReplacements, unsigned MaxEditDistance) const {
return llvm::ComputeMappedEditDistance(
- makeArrayRef(data(), size()), makeArrayRef(Other.data(), Other.size()),
+ ArrayRef(data(), size()), ArrayRef(Other.data(), Other.size()),
llvm::toLower, AllowReplacements, MaxEditDistance);
}
@@ -148,6 +147,18 @@ size_t StringRef::find(StringRef Str, size_t From) const {
const char *Stop = Start + (Size - N + 1);
+ if (N == 2) {
+ // Provide a fast path for newline finding (CRLF case) in InclusionRewriter.
+ // Not the most optimized strategy, but getting memcmp inlined should be
+ // good enough.
+ do {
+ if (std::memcmp(Start, Needle, 2) == 0)
+ return Start - Data;
+ ++Start;
+ } while (Start < Stop);
+ return npos;
+ }
+
// For short haystacks or unsupported needles fall back to the naive algorithm
if (Size < 16 || N > 255) {
do {
@@ -204,15 +215,7 @@ size_t StringRef::rfind_insensitive(char C, size_t From) const {
/// \return - The index of the last occurrence of \arg Str, or npos if not
/// found.
size_t StringRef::rfind(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(Str))
- return i;
- }
- return npos;
+ return std::string_view(*this).rfind(Str);
}
size_t StringRef::rfind_insensitive(StringRef Str) const {
@@ -246,10 +249,7 @@ StringRef::size_type StringRef::find_first_of(StringRef Chars,
/// find_first_not_of - Find the first character in the string that is not
/// \arg C or npos if not found.
StringRef::size_type StringRef::find_first_not_of(char C, size_t From) const {
- for (size_type i = std::min(From, Length), e = Length; i != e; ++i)
- if (Data[i] != C)
- return i;
- return npos;
+ return std::string_view(*this).find_first_not_of(C, From);
}
/// find_first_not_of - Find the first character in the string that is not
@@ -370,16 +370,16 @@ void StringRef::split(SmallVectorImpl<StringRef> &A, char Separator,
/// the string.
size_t StringRef::count(StringRef Str) const {
size_t Count = 0;
+ size_t Pos = 0;
size_t N = Str.size();
- if (!N || N > Length)
+ // TODO: For an empty `Str` we return 0 for legacy reasons. Consider changing
+ // this to `Length + 1` which is more in-line with the function
+ // description.
+ if (!N)
return 0;
- for (size_t i = 0, e = Length - N + 1; i < e;) {
- if (substr(i, N).equals(Str)) {
- ++Count;
- i += N;
- }
- else
- ++i;
+ while ((Pos = find(Str, Pos)) != npos) {
+ ++Count;
+ Pos += N;
}
return Count;
}