From 3f72a1924a299a37a2c61c92f3a5edf39d6b57d9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=92=D0=BB=D0=B0=D0=B4=D0=B8=D0=BC=D0=B8=D1=80=20=D0=A7?= =?UTF-8?q?=D0=B8=D0=B6?= Date: Fri, 9 Aug 2024 21:52:00 +0200 Subject: [PATCH] fix code style --- srcbpatch/streamreplacer.cpp | 72 +++++++++++++++++++++--------------- 1 file changed, 43 insertions(+), 29 deletions(-) diff --git a/srcbpatch/streamreplacer.cpp b/srcbpatch/streamreplacer.cpp index 16a18c7..41d55f5 100644 --- a/srcbpatch/streamreplacer.cpp +++ b/srcbpatch/streamreplacer.cpp @@ -222,9 +222,16 @@ class ChoiceReplacer final : public ReplacerWithNext protected: void ProcessLastCharacter(const char toProcess) const; void ProcessCharacter(const char toProcess) const; + /// + /// Sends target to next replacers. + /// + /// the array we need to replace with void MakeReplace(span target) const; - void CleanTheCache(size_t SrcMatchedLength) const; - void SavePartialReplaceIdx(size_t index) const; + /// + /// Clean srcMatchedLength bytes of cache from the start + /// + /// number of bytes we have to clear + void CleanTheCache(size_t srcMatchedLength) const; /// /// check for partial or full match of data from buffer_ /// with any of lexemes sequentially @@ -263,7 +270,8 @@ class ChoiceReplacer final : public ReplacerWithNext mutable vector buffer_; }; -void ChoiceReplacer::DoReplacements(const char toProcess, const bool aEod) const{ +void ChoiceReplacer::DoReplacements(const char toProcess, const bool aEod) const +{ if (nullptr == pNext_) { throw logic_error("Replacement chain has been broken. Communicate with maintainer"); @@ -274,22 +282,25 @@ void ChoiceReplacer::DoReplacements(const char toProcess, const bool aEod) const ProcessCharacter(toProcess); } -void ChoiceReplacer::ProcessLastCharacter(const char toProcess) const { +void ChoiceReplacer::ProcessLastCharacter(const char toProcess) const +{ while (cachedAmount_ > 0) { - bool WasFullMatch = false; - for (size_t i = indexOfCached_; i < rpairs_.size(); ++i) { - auto [IsPartialMatch, IsFullMatch, SrcMatchedLength, DestStringIdx] = CheckMatch(i); - if (IsFullMatch){ - WasFullMatch = true; - MakeReplace(rpairs_[DestStringIdx].trg_); - CleanTheCache(SrcMatchedLength); + bool foundFullMatch = false; + for (size_t i = indexOfCached_; i < rpairs_.size(); ++i) + { + auto [partialMatch, fullMatch, srcMatchedLength, destStringIdx] = CheckMatch(i); + if (fullMatch) + { + foundFullMatch = true; + MakeReplace(rpairs_[destStringIdx].trg_); + CleanTheCache(srcMatchedLength); break; } } // No full match -> send 1 char from cache - if (!WasFullMatch) + if (!foundFullMatch) { MakeReplace(std::span (&buffer_.front(), 1)); CleanTheCache(1); @@ -298,19 +309,23 @@ void ChoiceReplacer::ProcessLastCharacter(const char toProcess) const { pNext_->DoReplacements(toProcess, true); } -void ChoiceReplacer::ProcessCharacter(const char toProcess) const { +void ChoiceReplacer::ProcessCharacter(const char toProcess) const +{ buffer_[cachedAmount_++] = toProcess; while (cachedAmount_ > 0) { - for (size_t i = indexOfCached_; i < rpairs_.size(); ++i) { - auto [IsPartialMatch, IsFullMatch, SrcMatchedLength, DestStringIdx] = CheckMatch(i); - if (IsFullMatch){ - MakeReplace(rpairs_[DestStringIdx].trg_); - CleanTheCache(SrcMatchedLength); + for (size_t i = indexOfCached_; i < rpairs_.size(); ++i) + { + auto [partialMatch, fullMatch, srcMatchedLength, matchPairIdx] = CheckMatch(i); + if (fullMatch) + { + MakeReplace(rpairs_[matchPairIdx].trg_); + CleanTheCache(srcMatchedLength); return; } - if (IsPartialMatch) { - SavePartialReplaceIdx(DestStringIdx); + if (partialMatch) + { + indexOfCached_ = matchPairIdx; return; } } @@ -320,20 +335,19 @@ void ChoiceReplacer::ProcessCharacter(const char toProcess) const { } } -inline void ChoiceReplacer::MakeReplace(span target) const { - for (char elem : target) { +inline void ChoiceReplacer::MakeReplace(span target) const +{ + for (const char elem : target) + { pNext_->DoReplacements(elem, false); } -} - -inline void ChoiceReplacer::CleanTheCache(size_t SrcMatchedLength) const{ - shift_left(buffer_.data(), buffer_.data() + cachedAmount_, SrcMatchedLength); - cachedAmount_ -= SrcMatchedLength; indexOfCached_ = 0; } -inline void ChoiceReplacer::SavePartialReplaceIdx(size_t index) const{ - indexOfCached_ = index; +inline void ChoiceReplacer::CleanTheCache(size_t srcMatchedLength) const +{ + shift_left(buffer_.data(), buffer_.data() + cachedAmount_, static_cast(srcMatchedLength)); + cachedAmount_ -= srcMatchedLength; } namespace