[PATCH] D133658: [Support] Use find() for faster StringRef::count.
Tatsuyuki Ishi via Phabricator via llvm-commits
llvm-commits at lists.llvm.org
Wed Oct 26 02:50:40 PDT 2022
ishitatsuyuki updated this revision to Diff 470762.
ishitatsuyuki added a comment.
Restore check for zero N
Repository:
rG LLVM Github Monorepo
CHANGES SINCE LAST ACTION
https://reviews.llvm.org/D133658/new/
https://reviews.llvm.org/D133658
Files:
llvm/lib/Support/StringRef.cpp
Index: llvm/lib/Support/StringRef.cpp
===================================================================
--- llvm/lib/Support/StringRef.cpp
+++ llvm/lib/Support/StringRef.cpp
@@ -370,16 +370,12 @@
/// 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)
- return 0;
- for (size_t i = 0, e = Length - N + 1; i < e;) {
- if (substr(i, N).equals(Str)) {
- ++Count;
- i += N;
- }
- else
- ++i;
+ if (!N) return Length + 1;
+ while ((Pos = find(Str, Pos)) != npos) {
+ ++Count;
+ Pos += N;
}
return Count;
}
-------------- next part --------------
A non-text attachment was scrubbed...
Name: D133658.470762.patch
Type: text/x-patch
Size: 646 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20221026/a5dc5c3c/attachment.bin>
More information about the llvm-commits
mailing list