diff options
author | Johannes Doerfert <johannes@jdoerfert.de> | 2019-12-24 17:25:37 -0600 |
---|---|---|
committer | Johannes Doerfert <johannes@jdoerfert.de> | 2019-12-24 18:30:41 -0600 |
commit | 9f6b13e5cce96066d7262d224c971d93c2724795 (patch) | |
tree | 9c6f399d19ea75ac2270657902bfffa0042df6ae /llvm/lib/Support/StringRef.cpp | |
parent | a36ddf0aa9db5c1086e04f56b5f077b761712eb5 (diff) | |
download | bcm5719-llvm-9f6b13e5cce96066d7262d224c971d93c2724795.tar.gz bcm5719-llvm-9f6b13e5cce96066d7262d224c971d93c2724795.zip |
[Support] Fix behavior of StringRef::count with overlapping occurrences, add tests
Summary:
Fix the behavior of StringRef::count(StringRef) to not count overlapping occurrences, as is stated in the documentation.
Fixes bug https://bugs.llvm.org/show_bug.cgi?id=44072
I added Krzysztof Parzyszek to review this change because a use of this function in HexagonInstrInfo::getInlineAsmLength might depend on the overlapping-behavior. I don't have enough domain knowledge to tell if this change could break anything there.
All other uses of this method in LLVM (besides the unit tests) only use single-character search strings. In those cases, search occurrences can not overlap anyway.
Patch by Benno (@Bensge)
Reviewed By: jdoerfert
Differential Revision: https://reviews.llvm.org/D70585
Diffstat (limited to 'llvm/lib/Support/StringRef.cpp')
-rw-r--r-- | llvm/lib/Support/StringRef.cpp | 9 |
1 files changed, 7 insertions, 2 deletions
diff --git a/llvm/lib/Support/StringRef.cpp b/llvm/lib/Support/StringRef.cpp index 4bafc4ec718..d7fa99dbde2 100644 --- a/llvm/lib/Support/StringRef.cpp +++ b/llvm/lib/Support/StringRef.cpp @@ -374,9 +374,14 @@ size_t StringRef::count(StringRef Str) const { size_t N = Str.size(); if (N > Length) return 0; - for (size_t i = 0, e = Length - N + 1; i != e; ++i) - if (substr(i, N).equals(Str)) + for (size_t i = 0, e = Length - N + 1; i < e;) { + if (substr(i, N).equals(Str)) { ++Count; + i += N; + } + else + ++i; + } return Count; } |