@@ -66,6 +66,51 @@ class ReplacerWithNext: public StreamReplacer
66
66
67
67
};
68
68
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
+
69
114
70
115
// --------------------------------------------------
71
116
struct ReplacerPairHolder
@@ -83,7 +128,7 @@ struct ReplacerPairHolder
83
128
84
129
// Description????
85
130
// / 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
87
132
{
88
133
public:
89
134
UsualReplacer (unique_ptr<AbstractBinaryLexeme>& src, // what to replace
@@ -97,24 +142,6 @@ class UsualReplacer final : public ReplacerWithNext
97
142
void DoReplacements (const char toProcess, const bool aEod) const override ;
98
143
99
144
protected:
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
- }
118
145
// / <summary>
119
146
// / We got the 'end' character so there are no match -> we should pass further all the cache
120
147
// / </summary>
@@ -127,12 +154,6 @@ class UsualReplacer final : public ReplacerWithNext
127
154
}
128
155
const span<const char >& src_; // what to replace
129
156
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_;
136
157
};
137
158
138
159
@@ -150,6 +171,7 @@ void UsualReplacer::DoReplacements(const char toProcess, const bool aEod) const
150
171
}
151
172
152
173
cachedData_[cachedAmount_++] = toProcess;
174
+ // our cachedData_ should contain only prefix of src_, otherwise -> clean the cache from the beginning
153
175
while (cachedAmount_ > 0 && memcmp (cachedData_.data (), src_.data (), cachedAmount_) != 0 )
154
176
{
155
177
SendFurther (std::span<char > (cachedData_.data (), 1 ));
@@ -185,7 +207,7 @@ static unique_ptr<StreamReplacer> CreateSimpleReplacer(
185
207
// / |--SRC N TRG N |
186
208
// /
187
209
// / Description????
188
- class ChoiceReplacer final : public ReplacerWithNext
210
+ class ChoiceReplacer final : public BaseReplacerWithCache
189
211
{
190
212
typedef struct
191
213
{
@@ -261,25 +283,10 @@ class ChoiceReplacer final : public ReplacerWithNext
261
283
// / <param name="target">the array we need to send</param>
262
284
void SendAndResetPartialMatch (const span<const char >& target) const
263
285
{
264
- for (const char c : target)
265
- {
266
- pNext_->DoReplacements (c, false );
267
- }
286
+ SendFurther (target);
268
287
indexOfPartialMatch_ = 0 ;
269
288
}
270
289
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
-
283
290
// / <summary>
284
291
// / The end of the data sign has been received and the cached data need to be either send or replaced & send
285
292
// / </summary>
@@ -308,12 +315,7 @@ class ChoiceReplacer final : public ReplacerWithNext
308
315
// our pairs sorted by priority - only one of them could be replaced for concrete pos
309
316
vector<ChoiceReplacerPair> rpairs_;
310
317
311
- mutable size_t cachedAmount_ = 0 ; // we cached this amount of data
312
318
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_;
317
319
};
318
320
319
321
void ChoiceReplacer::DoReplacements (const char toProcess, const bool aEod) const
@@ -360,11 +362,10 @@ namespace
360
362
// / <summary>
361
363
// / replaces for lexemes of the same length
362
364
// / </summary>
363
- class UniformLexemeReplacer final : public ReplacerWithNext
365
+ class UniformLexemeReplacer final : public BaseReplacerWithCache
364
366
{
365
367
public:
366
368
UniformLexemeReplacer (StreamReplacerChoice& choice, const size_t sz)
367
- : cachedData_(sz)
368
369
{
369
370
for (AbstractLexemesPair& alpair : choice)
370
371
{
@@ -379,29 +380,12 @@ class UniformLexemeReplacer final : public ReplacerWithNext
379
380
cout << coloredconsole::toconsole (warningDuplicatePattern) << endl;
380
381
}
381
382
}
383
+ cachedData_.resize (sz);
382
384
}
383
385
384
386
void DoReplacements (const char toProcess, const bool aEod) const override ;
385
387
386
388
protected:
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
- }
405
389
// / <summary>
406
390
// / We got the 'end' character so there are no match -> we should pass further all the cache
407
391
// / </summary>
@@ -414,12 +398,6 @@ class UniformLexemeReplacer final : public ReplacerWithNext
414
398
}
415
399
// here we hold pairs of sources and targets
416
400
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_;
423
401
};
424
402
425
403
@@ -439,7 +417,6 @@ void UniformLexemeReplacer::DoReplacements(const char toProcess, const bool aEod
439
417
440
418
// set buffer of cached at once
441
419
cachedData_[cachedAmount_++] = toProcess;
442
-
443
420
if (cachedAmount_ == cachedData_.size ())
444
421
{
445
422
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
461
438
// / <summary>
462
439
// / replaces for lexemes of the same length
463
440
// / </summary>
464
- class LexemeOf1Replacer final : public ReplacerWithNext
441
+ class LexemeOf1Replacer final : public BaseReplacer
465
442
{
466
443
public:
467
444
LexemeOf1Replacer (StreamReplacerChoice& choice)
@@ -510,7 +487,7 @@ void LexemeOf1Replacer::DoReplacements(const char toProcess, const bool aEod) co
510
487
if (replaces_[index].present_ )
511
488
{
512
489
auto & trg = replaces_[index].trg_ ;
513
- for ( size_t q = 0 ; q < trg. size (); ++q) { pNext_-> DoReplacements (trg[q], false ); }
490
+ SendFurther ( trg);
514
491
}
515
492
else
516
493
{
0 commit comments