Skip to content

Commit d5bd5b2

Browse files
committed
.
1 parent 4500695 commit d5bd5b2

File tree

4 files changed

+31
-29
lines changed

4 files changed

+31
-29
lines changed

src/Verify/Serialization/Scrubbers/CrossChunkMatcher.cs

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -17,14 +17,14 @@ public static void ReplaceAll<TContext>(
1717
int carryoverSize,
1818
TContext context,
1919
CrossChunkHandler<TContext> onCrossChunk,
20-
WithinChunkHandler<TContext> onWithinChunk,
21-
Func<TContext, List<Match>> getMatches)
20+
WithinChunkHandler<TContext> onWithinChunk)
2221
{
2322
Span<char> carryoverBuffer = stackalloc char[carryoverSize];
2423
var carryoverLength = 0;
2524
var previousChunkAbsoluteEnd = 0;
2625
var absolutePosition = 0;
27-
26+
List<Match> matches = [];
27+
var addMatch = matches.Add;
2828
foreach (var chunk in builder.GetChunks())
2929
{
3030
var chunkSpan = chunk.Span;
@@ -44,7 +44,8 @@ public static void ReplaceAll<TContext>(
4444
remainingInCarryover,
4545
chunkSpan,
4646
startPosition,
47-
context);
47+
context,
48+
addMatch);
4849
}
4950
}
5051

@@ -53,7 +54,7 @@ public static void ReplaceAll<TContext>(
5354
while (chunkIndex < chunk.Length)
5455
{
5556
var absoluteIndex = absolutePosition + chunkIndex;
56-
var skipAhead = onWithinChunk(chunk, chunkSpan, chunkIndex, absoluteIndex, context);
57+
var skipAhead = onWithinChunk(chunk, chunkSpan, chunkIndex, absoluteIndex, context, addMatch);
5758
chunkIndex += skipAhead > 0 ? skipAhead : 1;
5859
}
5960

@@ -66,7 +67,6 @@ public static void ReplaceAll<TContext>(
6667
}
6768

6869
// Apply matches in descending position order
69-
var matches = getMatches(context);
7070
foreach (var match in matches.OrderByDescending(_ => _.Index))
7171
{
7272
builder.Overwrite(match.Value, match.Index, match.Length);
@@ -83,7 +83,8 @@ public delegate void CrossChunkHandler<TContext>(
8383
int remainingInCarryover,
8484
CharSpan currentChunkSpan,
8585
int absoluteStartPosition,
86-
TContext context);
86+
TContext context,
87+
Action<Match> addMatch);
8788

8889
/// <summary>
8990
/// Callback for processing positions within a chunk.
@@ -94,7 +95,8 @@ public delegate int WithinChunkHandler<TContext>(
9495
CharSpan chunkSpan,
9596
int chunkIndex,
9697
int absoluteIndex,
97-
TContext context);
98+
TContext context,
99+
Action<Match> addMatch);
98100
}
99101

100102

src/Verify/Serialization/Scrubbers/DateScrubber.cs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -213,8 +213,7 @@ static void ReplaceInner(StringBuilder builder, string format, Counter counter,
213213
carryoverSize: max - 1,
214214
context,
215215
OnCrossChunk,
216-
OnWithinChunk,
217-
getMatches: c => c.Matches);
216+
OnWithinChunk);
218217
}
219218

220219
static void OnCrossChunk(
@@ -224,7 +223,8 @@ static void OnCrossChunk(
224223
int remainingInCarryover,
225224
CharSpan currentChunkSpan,
226225
int absoluteStartPosition,
227-
MatchContext context)
226+
MatchContext context,
227+
Action<Match> addMatch)
228228
{
229229
Span<char> combinedBuffer = stackalloc char[context.MaxLength];
230230

@@ -247,7 +247,7 @@ static void OnCrossChunk(
247247

248248
if (context.TryConvert(slice, context.Format, context.Counter, context.Culture, out var convert))
249249
{
250-
context.Matches.Add(new(absoluteStartPosition, length, convert));
250+
addMatch(new(absoluteStartPosition, length, convert));
251251
return; // Found match at this position
252252
}
253253
}
@@ -258,7 +258,8 @@ static int OnWithinChunk(
258258
CharSpan chunkSpan,
259259
int chunkIndex,
260260
int absoluteIndex,
261-
MatchContext context)
261+
MatchContext context,
262+
Action<Match> addMatch)
262263
{
263264
// Try lengths from longest to shortest (greedy matching)
264265
for (var length = context.MaxLength; length >= context.MinLength; length--)
@@ -272,7 +273,7 @@ static int OnWithinChunk(
272273

273274
if (context.TryConvert(slice, context.Format, context.Counter, context.Culture, out var convert))
274275
{
275-
context.Matches.Add(new(absoluteIndex, length, convert));
276+
addMatch(new(absoluteIndex, length, convert));
276277
return length; // Skip past match
277278
}
278279
}
@@ -294,6 +295,5 @@ sealed class MatchContext(
294295
public TryConvert TryConvert { get; } = tryConvert;
295296
public int MaxLength { get; } = maxLength;
296297
public int MinLength { get; } = minLength;
297-
public List<Match> Matches { get; } = [];
298298
}
299299
}

src/Verify/Serialization/Scrubbers/DirectoryReplacements_StringBuilder.cs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -46,8 +46,7 @@ public static void Replace(StringBuilder builder, List<Pair> paths)
4646
carryoverSize: maxLength - 1,
4747
context,
4848
OnCrossChunk,
49-
OnWithinChunk,
50-
getMatches: c => c.Matches);
49+
OnWithinChunk);
5150
}
5251

5352
static void OnCrossChunk(
@@ -57,7 +56,8 @@ static void OnCrossChunk(
5756
int remainingInCarryover,
5857
CharSpan currentChunkSpan,
5958
int absoluteStartPosition,
60-
MatchContext context)
59+
MatchContext context,
60+
Action<Match> addMatch)
6161
{
6262
Span<char> combinedBuffer = stackalloc char[context.MaxLength * 2];
6363

@@ -93,7 +93,7 @@ static void OnCrossChunk(
9393
continue;
9494
}
9595

96-
context.Matches.Add(new(absoluteStartPosition, matchLength, pair.Replace));
96+
addMatch(new(absoluteStartPosition, matchLength, pair.Replace));
9797
context.AddMatchedRange(absoluteStartPosition, absoluteStartPosition + matchLength);
9898
// Found a match at this position, skip other pairs
9999
break;
@@ -105,7 +105,8 @@ static int OnWithinChunk(
105105
CharSpan chunkSpan,
106106
int chunkIndex,
107107
int absoluteIndex,
108-
MatchContext context)
108+
MatchContext context,
109+
Action<Match> addMatch)
109110
{
110111
// Skip if already matched
111112
if (context.IsPositionMatched(absoluteIndex))
@@ -133,7 +134,7 @@ static int OnWithinChunk(
133134
continue;
134135
}
135136

136-
context.Matches.Add(new(absoluteIndex, matchLength, pair.Replace));
137+
addMatch(new(absoluteIndex, matchLength, pair.Replace));
137138
context.AddMatchedRange(absoluteIndex, absoluteIndex + matchLength);
138139
// Skip past this match
139140
return matchLength;
@@ -269,7 +270,6 @@ static bool IsPathMatchAt(CharSpan chunk, int chunkPos, string find)
269270
sealed class MatchContext(List<Pair> pairs)
270271
{
271272
public List<Pair> Pairs { get; } = pairs;
272-
public List<Match> Matches { get; } = [];
273273
public int MaxLength { get; } = pairs.Count > 0 ? pairs[0].Find.Length : 0;
274274

275275
List<(int Start, int End)> matchedRanges = [];

src/Verify/Serialization/Scrubbers/GuidScrubber.cs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,7 @@ public static void ReplaceGuids(StringBuilder builder, Counter counter)
2020
carryoverSize: 35,
2121
context,
2222
OnCrossChunk,
23-
OnWithinChunk,
24-
getMatches: c => c.Matches);
23+
OnWithinChunk);
2524
}
2625

2726
static void OnCrossChunk(
@@ -31,7 +30,8 @@ static void OnCrossChunk(
3130
int remainingInCarryover,
3231
CharSpan currentChunkSpan,
3332
int absoluteStartPosition,
34-
MatchContext context)
33+
MatchContext context,
34+
Action<Match> addMatch)
3535
{
3636
var neededFromCurrent = 36 - remainingInCarryover;
3737

@@ -66,15 +66,16 @@ static void OnCrossChunk(
6666
}
6767

6868
var convert = context.Counter.Convert(guid);
69-
context.Matches.Add(new(absoluteStartPosition, 36, convert));
69+
addMatch(new(absoluteStartPosition, 36, convert));
7070
}
7171

7272
static int OnWithinChunk(
7373
ReadOnlyMemory<char> chunk,
7474
CharSpan chunkSpan,
7575
int chunkIndex,
7676
int absoluteIndex,
77-
MatchContext context)
77+
MatchContext context,
78+
Action<Match> addMatch)
7879
{
7980
var end = chunkIndex + 36;
8081
if (end > chunk.Length)
@@ -101,7 +102,7 @@ static int OnWithinChunk(
101102
}
102103

103104
var convert = context.Counter.Convert(guid);
104-
context.Matches.Add(new(absoluteIndex, 36, convert));
105+
addMatch(new(absoluteIndex, 36, convert));
105106
return 36; // Skip past the matched GUID
106107
}
107108

@@ -122,6 +123,5 @@ static bool IsInvalidStartingChar(char ch) =>
122123
sealed class MatchContext(Counter counter)
123124
{
124125
public Counter Counter { get; } = counter;
125-
public List<Match> Matches { get; } = [];
126126
}
127127
}

0 commit comments

Comments
 (0)