Skip to content

Commit 9cbf604

Browse files
committed
.
1 parent 4fcfe7e commit 9cbf604

File tree

4 files changed

+27
-50
lines changed

4 files changed

+27
-50
lines changed

src/Verify/Serialization/Scrubbers/CrossChunkMatcher.cs

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -12,19 +12,17 @@ static class CrossChunkMatcher
1212
/// <param name="onCrossChunk">Called for each potential cross-chunk match position</param>
1313
/// <param name="onWithinChunk">Called for each position within a chunk</param>
1414
/// <param name="getMatches">Retrieves the list of matches from the context</param>
15-
/// <param name="getIndex">Gets the start index from a match</param>
1615
/// <param name="getLength">Gets the original length from a match</param>
1716
/// <param name="getValue">Gets the replacement value from a match</param>
18-
public static void ReplaceAll<TContext, TMatch>(
17+
public static void ReplaceAll<TContext>(
1918
StringBuilder builder,
2019
int carryoverSize,
2120
TContext context,
2221
CrossChunkHandler<TContext> onCrossChunk,
2322
WithinChunkHandler<TContext> onWithinChunk,
24-
Func<TContext, List<TMatch>> getMatches,
25-
Func<TMatch, int> getIndex,
26-
Func<TMatch, int> getLength,
27-
Func<TMatch, string> getValue)
23+
Func<TContext, List<Match>> getMatches,
24+
Func<Match, int> getLength,
25+
Func<Match, string> getValue)
2826
{
2927
Span<char> carryoverBuffer = stackalloc char[carryoverSize];
3028
var carryoverLength = 0;
@@ -73,9 +71,9 @@ public static void ReplaceAll<TContext, TMatch>(
7371

7472
// Apply matches in descending position order
7573
var matches = getMatches(context);
76-
foreach (var match in matches.OrderByDescending(getIndex))
74+
foreach (var match in matches.OrderByDescending(_ => _.Index))
7775
{
78-
builder.Overwrite(getValue(match), getIndex(match), getLength(match));
76+
builder.Overwrite(match.Value, match.Index, match.Length);
7977
}
8078
}
8179

@@ -102,3 +100,11 @@ public delegate int WithinChunkHandler<TContext>(
102100
int absoluteIndex,
103101
TContext context);
104102
}
103+
104+
105+
readonly struct Match(int index, int length, string value)
106+
{
107+
public readonly int Index = index;
108+
public readonly int Length = length;
109+
public readonly string Value = value;
110+
}

src/Verify/Serialization/Scrubbers/DateScrubber.cs

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -208,15 +208,14 @@ static void ReplaceInner(StringBuilder builder, string format, Counter counter,
208208

209209
var context = new MatchContext(format, counter, culture, tryConvert, max, min);
210210

211-
CrossChunkMatcher.ReplaceAll<MatchContext, Match>(
211+
CrossChunkMatcher.ReplaceAll<MatchContext>(
212212
builder,
213213
carryoverSize: max - 1,
214214
context,
215215
OnCrossChunk,
216216
OnWithinChunk,
217217
getMatches: c => c.Matches,
218-
getIndex: m => m.Index,
219-
getLength: m => m.OriginalLength,
218+
getLength: m => m.Length,
220219
getValue: m => m.Value);
221220
}
222221

@@ -299,11 +298,4 @@ sealed class MatchContext(
299298
public int MinLength { get; } = minLength;
300299
public List<Match> Matches { get; } = [];
301300
}
302-
303-
readonly struct Match(int index, int originalLength, string value)
304-
{
305-
public readonly int Index = index;
306-
public readonly int OriginalLength = originalLength;
307-
public readonly string Value = value;
308-
}
309301
}

src/Verify/Serialization/Scrubbers/DirectoryReplacements_StringBuilder.cs

Lines changed: 8 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -41,14 +41,13 @@ public static void Replace(StringBuilder builder, List<Pair> paths)
4141
var maxLength = paths[0].Find.Length;
4242
var context = new MatchContext(paths);
4343

44-
CrossChunkMatcher.ReplaceAll<MatchContext, Match>(
44+
CrossChunkMatcher.ReplaceAll<MatchContext>(
4545
builder,
4646
carryoverSize: maxLength - 1,
4747
context,
4848
OnCrossChunk,
4949
OnWithinChunk,
5050
getMatches: c => c.Matches,
51-
getIndex: m => m.Index,
5251
getLength: m => m.Length,
5352
getValue: m => m.Value);
5453
}
@@ -269,26 +268,20 @@ static bool IsPathMatchAt(CharSpan chunk, int chunkPos, string find)
269268
return true;
270269
}
271270

272-
sealed class MatchContext
271+
sealed class MatchContext(List<Pair> pairs)
273272
{
274-
public List<Pair> Pairs { get; }
273+
public List<Pair> Pairs { get; } = pairs;
275274
public List<Match> Matches { get; } = [];
276-
public int MaxLength { get; }
275+
public int MaxLength { get; } = pairs.Count > 0 ? pairs[0].Find.Length : 0;
277276

278-
List<(int Start, int End)> _matchedRanges = [];
279-
280-
public MatchContext(List<Pair> pairs)
281-
{
282-
Pairs = pairs;
283-
MaxLength = pairs.Count > 0 ? pairs[0].Find.Length : 0;
284-
}
277+
List<(int Start, int End)> matchedRanges = [];
285278

286279
public void AddMatchedRange(int start, int end) =>
287-
_matchedRanges.Add((start, end));
280+
matchedRanges.Add((start, end));
288281

289282
public bool IsPositionMatched(int position)
290283
{
291-
foreach (var (start, end) in _matchedRanges)
284+
foreach (var (start, end) in matchedRanges)
292285
{
293286
if (position >= start && position < end)
294287
{
@@ -302,7 +295,7 @@ public bool IsPositionMatched(int position)
302295
public bool OverlapsExistingMatch(int start, int length)
303296
{
304297
var end = start + length;
305-
foreach (var range in _matchedRanges)
298+
foreach (var range in matchedRanges)
306299
{
307300
if (start < range.End && end > range.Start)
308301
{
@@ -313,11 +306,4 @@ public bool OverlapsExistingMatch(int start, int length)
313306
return false;
314307
}
315308
}
316-
317-
readonly struct Match(int index, int length, string value)
318-
{
319-
public readonly int Index = index;
320-
public readonly int Length = length;
321-
public readonly string Value = value;
322-
}
323309
}

src/Verify/Serialization/Scrubbers/GuidScrubber.cs

Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -15,14 +15,13 @@ public static void ReplaceGuids(StringBuilder builder, Counter counter)
1515

1616
var context = new MatchContext(counter);
1717

18-
CrossChunkMatcher.ReplaceAll<MatchContext, Match>(
18+
CrossChunkMatcher.ReplaceAll<MatchContext>(
1919
builder,
2020
carryoverSize: 35,
2121
context,
2222
OnCrossChunk,
2323
OnWithinChunk,
2424
getMatches: c => c.Matches,
25-
getIndex: m => m.Index,
2625
getLength: _ => 36,
2726
getValue: m => m.Value);
2827
}
@@ -69,7 +68,7 @@ static void OnCrossChunk(
6968
}
7069

7170
var convert = context.Counter.Convert(guid);
72-
context.Matches.Add(new(absoluteStartPosition, convert));
71+
context.Matches.Add(new(absoluteStartPosition, 36, convert));
7372
}
7473

7574
static int OnWithinChunk(
@@ -104,7 +103,7 @@ static int OnWithinChunk(
104103
}
105104

106105
var convert = context.Counter.Convert(guid);
107-
context.Matches.Add(new(absoluteIndex, convert));
106+
context.Matches.Add(new(absoluteIndex, 36, convert));
108107
return 36; // Skip past the matched GUID
109108
}
110109

@@ -127,10 +126,4 @@ sealed class MatchContext(Counter counter)
127126
public Counter Counter { get; } = counter;
128127
public List<Match> Matches { get; } = [];
129128
}
130-
131-
internal readonly struct Match(int index, string value)
132-
{
133-
public readonly int Index = index;
134-
public readonly string Value = value;
135-
}
136129
}

0 commit comments

Comments
 (0)