diff --git a/Source/MemorySearch/HybridPatternFinder.h b/Source/MemorySearch/HybridPatternFinder.h index 1b9c0fb03a2..580902aeca3 100644 --- a/Source/MemorySearch/HybridPatternFinder.h +++ b/Source/MemorySearch/HybridPatternFinder.h @@ -10,20 +10,33 @@ class HybridPatternFinder { public: - HybridPatternFinder(std::span bytes, BytePattern pattern) + HybridPatternFinder(std::span bytes, BytePattern pattern) noexcept : bytes{ bytes }, pattern{ pattern } { } - [[nodiscard]] const std::byte* operator()() const noexcept + [[nodiscard]] const std::byte* findNextOccurrence() noexcept { - PatternFinderSIMD simdFinder{ bytes, pattern }; - if (const auto foundSIMD = simdFinder()) + PatternFinderSIMD simdFinder{bytes, pattern}; + if (const auto foundSIMD = simdFinder()) { + updateRemainingBytes(foundSIMD); return foundSIMD; - return PatternFinderScalar{ simdFinder.getNotCheckedBytes(), pattern }(); + } + + if (const auto foundScalar{PatternFinderScalar{simdFinder.getNotCheckedBytes(), pattern}()}) { + updateRemainingBytes(foundScalar); + return foundScalar; + } + + return nullptr; } private: + void updateRemainingBytes(const std::byte* foundPosition) noexcept + { + bytes = {foundPosition + 1, bytes.data() + bytes.size()}; + } + std::span bytes; BytePattern pattern; }; diff --git a/Source/MemorySearch/PatternFinder.h b/Source/MemorySearch/PatternFinder.h index 6fc7c50c887..e577d7a1e86 100644 --- a/Source/MemorySearch/PatternFinder.h +++ b/Source/MemorySearch/PatternFinder.h @@ -24,7 +24,7 @@ struct PatternFinder { [[nodiscard]] [[NOINLINE]] SafeAddress operator()(BytePattern pattern) const noexcept { - const auto found = HybridPatternFinder{ bytes, pattern }(); + const auto found = HybridPatternFinder{ bytes, pattern }.findNextOccurence(); if (!found) notFoundHandler(pattern, bytes); return SafeAddress{ found }; @@ -32,7 +32,7 @@ struct PatternFinder { [[nodiscard]] [[NOINLINE]] SafeAddress operator()(BytePattern pattern, OffsetHint offsetHint) const noexcept { - const auto foundWithHint = HybridPatternFinder{ getSliceForHint(offsetHint), pattern }(); + const auto foundWithHint = HybridPatternFinder{ getSliceForHint(offsetHint), pattern }.findNextOccurence(); if (foundWithHint) return SafeAddress{ foundWithHint }; return operator()(pattern);