@@ -66,6 +66,51 @@ class ReplacerWithNext: public StreamReplacer
6666
6767};
6868
69+ // / <summary>
70+ // / a class with some common methods for all replacers
71+ // / </summary>
72+ class BaseReplacer : public ReplacerWithNext
73+ {
74+ protected:
75+ // / <summary>
76+ // / Sends target to next replacers, and resets partial match index to zero
77+ // / </summary>
78+ // / <param name="target">the array we need to send</param>
79+ void SendFurther (const span<const char >& target) const
80+ {
81+ for (const char c : target)
82+ {
83+ pNext_->DoReplacements (c, false );
84+ }
85+ }
86+ };
87+
88+ // / <summary>
89+ // / a class with some common methods for all replacers including cache
90+ // / </summary>
91+ class BaseReplacerWithCache : public BaseReplacer
92+ {
93+ protected:
94+ // / <summary>
95+ // / Clean srcMatchedLength bytes of cache from the beginning
96+ // / </summary>
97+ // / <param name="srcMatchedLength">number of bytes we have to clear</param>
98+ void CleanTheCache (size_t srcMatchedLength) const
99+ {
100+ shift_left (cachedData_.data (),
101+ cachedData_.data () + cachedAmount_,
102+ static_cast <std::iterator_traits<decltype (cachedData_.data ())>::difference_type>(srcMatchedLength));
103+ cachedAmount_ -= srcMatchedLength;
104+ }
105+
106+ protected:
107+ mutable size_t cachedAmount_ = 0 ; // we cached this amount of data
108+
109+ // this is used to hold temporary data while the logic is
110+ // looking for the new beginning of the cached value
111+ mutable vector<char > cachedData_;
112+ };
113+
69114
70115// --------------------------------------------------
71116struct ReplacerPairHolder
@@ -83,7 +128,7 @@ struct ReplacerPairHolder
83128
84129// Description????
85130// / The class finds the lexeme src_ and replaces it to trg_, the src_ and trg_ are non empty strings
86- class UsualReplacer final : public ReplacerWithNext
131+ class UsualReplacer final : public BaseReplacerWithCache
87132{
88133public:
89134 UsualReplacer (unique_ptr<AbstractBinaryLexeme>& src, // what to replace
@@ -97,24 +142,6 @@ class UsualReplacer final : public ReplacerWithNext
97142 void DoReplacements (const char toProcess, const bool aEod) const override ;
98143
99144protected:
100- // / <summary>
101- // / Sends target to next replacers, and resets partial match index to zero
102- // / </summary>
103- // / <param name="target">the array we need to send</param>
104- void SendFurther (const span<const char >& target) const
105- {
106- for (const char c : target)
107- {
108- pNext_->DoReplacements (c, false );
109- }
110- }
111- void CleanTheCache (size_t srcMatchedLength) const
112- {
113- shift_left (cachedData_.data (),
114- cachedData_.data () + cachedAmount_,
115- static_cast <std::iterator_traits<decltype (cachedData_.data ())>::difference_type>(srcMatchedLength));
116- cachedAmount_ -= srcMatchedLength;
117- }
118145 // / <summary>
119146 // / We got the 'end' character so there are no match -> we should pass further all the cache
120147 // / </summary>
@@ -127,12 +154,6 @@ class UsualReplacer final : public ReplacerWithNext
127154 }
128155 const span<const char >& src_; // what to replace
129156 const span<const char >& trg_; // with what
130-
131- mutable size_t cachedAmount_ = 0 ; // we cached this amount of data
132-
133- // this is used to hold temporary data while the logic is
134- // looking for the new beginning of the cached value
135- mutable vector<char > cachedData_;
136157};
137158
138159
@@ -150,6 +171,7 @@ void UsualReplacer::DoReplacements(const char toProcess, const bool aEod) const
150171 }
151172
152173 cachedData_[cachedAmount_++] = toProcess;
174+ // our cachedData_ should contain only prefix of src_, otherwise -> clean the cache from the beginning
153175 while (cachedAmount_ > 0 && memcmp (cachedData_.data (), src_.data (), cachedAmount_) != 0 )
154176 {
155177 SendFurther (std::span<char > (cachedData_.data (), 1 ));
@@ -185,7 +207,7 @@ static unique_ptr<StreamReplacer> CreateSimpleReplacer(
185207// / |--SRC N TRG N |
186208// /
187209// / Description????
188- class ChoiceReplacer final : public ReplacerWithNext
210+ class ChoiceReplacer final : public BaseReplacerWithCache
189211{
190212 typedef struct
191213 {
@@ -261,25 +283,10 @@ class ChoiceReplacer final : public ReplacerWithNext
261283 // / <param name="target">the array we need to send</param>
262284 void SendAndResetPartialMatch (const span<const char >& target) const
263285 {
264- for (const char c : target)
265- {
266- pNext_->DoReplacements (c, false );
267- }
286+ SendFurther (target);
268287 indexOfPartialMatch_ = 0 ;
269288 }
270289
271- // / <summary>
272- // / Clean srcMatchedLength bytes of cache from the beginning
273- // / </summary>
274- // / <param name="srcMatchedLength">number of bytes we have to clear</param>
275- void CleanTheCache (size_t srcMatchedLength) const
276- {
277- shift_left (cachedData_.data (),
278- cachedData_.data () + cachedAmount_,
279- static_cast <std::iterator_traits<decltype (cachedData_.data ())>::difference_type>(srcMatchedLength));
280- cachedAmount_ -= srcMatchedLength;
281- }
282-
283290 // / <summary>
284291 // / The end of the data sign has been received and the cached data need to be either send or replaced & send
285292 // / </summary>
@@ -308,12 +315,7 @@ class ChoiceReplacer final : public ReplacerWithNext
308315 // our pairs sorted by priority - only one of them could be replaced for concrete pos
309316 vector<ChoiceReplacerPair> rpairs_;
310317
311- mutable size_t cachedAmount_ = 0 ; // we cached this amount of data
312318 mutable size_t indexOfPartialMatch_ = 0 ; // this index from rpairs_ represents last partial match
313-
314- // this is used to hold temporary data while the logic is
315- // looking for the new beginning of the cached value
316- mutable vector<char > cachedData_;
317319};
318320
319321void ChoiceReplacer::DoReplacements (const char toProcess, const bool aEod) const
@@ -360,11 +362,10 @@ namespace
360362// / <summary>
361363// / replaces for lexemes of the same length
362364// / </summary>
363- class UniformLexemeReplacer final : public ReplacerWithNext
365+ class UniformLexemeReplacer final : public BaseReplacerWithCache
364366{
365367public:
366368 UniformLexemeReplacer (StreamReplacerChoice& choice, const size_t sz)
367- : cachedData_(sz)
368369 {
369370 for (AbstractLexemesPair& alpair : choice)
370371 {
@@ -379,29 +380,12 @@ class UniformLexemeReplacer final : public ReplacerWithNext
379380 cout << coloredconsole::toconsole (warningDuplicatePattern) << endl;
380381 }
381382 }
383+ cachedData_.resize (sz);
382384 }
383385
384386 void DoReplacements (const char toProcess, const bool aEod) const override ;
385387
386388protected:
387- // / <summary>
388- // / Sends target to next replacers, and resets partial match index to zero
389- // / </summary>
390- // / <param name="target">the array we need to send</param>
391- void SendFurther (const span<const char >& target) const
392- {
393- for (const char c : target)
394- {
395- pNext_->DoReplacements (c, false );
396- }
397- }
398- void CleanTheCache (size_t srcMatchedLength) const
399- {
400- shift_left (cachedData_.data (),
401- cachedData_.data () + cachedAmount_,
402- static_cast <std::iterator_traits<decltype (cachedData_.data ())>::difference_type>(srcMatchedLength));
403- cachedAmount_ -= srcMatchedLength;
404- }
405389 // / <summary>
406390 // / We got the 'end' character so there are no match -> we should pass further all the cache
407391 // / </summary>
@@ -414,12 +398,6 @@ class UniformLexemeReplacer final : public ReplacerWithNext
414398 }
415399 // here we hold pairs of sources and targets
416400 unordered_map<string_view, string_view> replaceOptions_;
417-
418- mutable size_t cachedAmount_ = 0 ; // we cache this amount of data in the cachedData_
419-
420- // this is used to hold temporary data while the logic is
421- // looking for the new beginning of the cached value
422- mutable vector<char > cachedData_;
423401};
424402
425403
@@ -439,7 +417,6 @@ void UniformLexemeReplacer::DoReplacements(const char toProcess, const bool aEod
439417
440418 // set buffer of cached at once
441419 cachedData_[cachedAmount_++] = toProcess;
442-
443420 if (cachedAmount_ == cachedData_.size ())
444421 {
445422 if (const auto matchIt = replaceOptions_.find (string_view ( cachedData_.data (), cachedAmount_)); matchIt != replaceOptions_.cend ())
@@ -461,7 +438,7 @@ void UniformLexemeReplacer::DoReplacements(const char toProcess, const bool aEod
461438// / <summary>
462439// / replaces for lexemes of the same length
463440// / </summary>
464- class LexemeOf1Replacer final : public ReplacerWithNext
441+ class LexemeOf1Replacer final : public BaseReplacer
465442{
466443public:
467444 LexemeOf1Replacer (StreamReplacerChoice& choice)
@@ -510,7 +487,7 @@ void LexemeOf1Replacer::DoReplacements(const char toProcess, const bool aEod) co
510487 if (replaces_[index].present_ )
511488 {
512489 auto & trg = replaces_[index].trg_ ;
513- for ( size_t q = 0 ; q < trg. size (); ++q) { pNext_-> DoReplacements (trg[q], false ); }
490+ SendFurther ( trg);
514491 }
515492 else
516493 {
0 commit comments