Skip to content

Commit b1c5d9a

Browse files
committed
.
1 parent 2e84ea2 commit b1c5d9a

File tree

5 files changed

+79
-77
lines changed

5 files changed

+79
-77
lines changed

src/Verify.Tests/Serialization/CrossChunkMatcherTests.cs

Lines changed: 59 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,10 @@ public Task SimpleWithinChunkMatch()
1414
if (content.Length >= context.Length &&
1515
content[..context.Length].SequenceEqual(context))
1616
{
17-
return MatchResult.Match(context.Length, "Universe");
17+
return new(context.Length, "Universe");
1818
}
19-
return MatchResult.NoMatch();
19+
20+
return null;
2021
});
2122

2223
return Verify(builder.ToString());
@@ -36,9 +37,10 @@ public Task MultipleWithinChunkMatches()
3637
if (content.Length >= context.Length &&
3738
content[..context.Length].SequenceEqual(context))
3839
{
39-
return MatchResult.Match(context.Length, "bar");
40+
return new(context.Length, "bar");
4041
}
41-
return MatchResult.NoMatch();
42+
43+
return null;
4244
});
4345

4446
return Verify(builder.ToString());
@@ -62,9 +64,10 @@ public Task CrossChunkMatch()
6264
if (content.Length >= context.Length &&
6365
content[..context.Length].SequenceEqual(context))
6466
{
65-
return MatchResult.Match(context.Length, "FOUND");
67+
return new(context.Length, "FOUND");
6668
}
67-
return MatchResult.NoMatch();
69+
70+
return null;
6871
});
6972

7073
var result = builder.ToString();
@@ -95,9 +98,10 @@ public Task NoMatches()
9598
if (content.Length >= context.Length &&
9699
content[..context.Length].SequenceEqual(context))
97100
{
98-
return MatchResult.Match(context.Length, "Replaced");
101+
return new(context.Length, "Replaced");
99102
}
100-
return MatchResult.NoMatch();
103+
104+
return null;
101105
});
102106

103107
return Verify(builder.ToString());
@@ -117,9 +121,10 @@ public Task EmptyStringBuilder()
117121
if (content.Length >= context.Length &&
118122
content[..context.Length].SequenceEqual(context))
119123
{
120-
return MatchResult.Match(context.Length, "replaced");
124+
return new(context.Length, "replaced");
121125
}
122-
return MatchResult.NoMatch();
126+
127+
return null;
123128
});
124129

125130
return Verify(builder.ToString());
@@ -139,9 +144,10 @@ public Task MatchAtStart()
139144
if (content.Length >= context.Length &&
140145
content[..context.Length].SequenceEqual(context))
141146
{
142-
return MatchResult.Match(context.Length, "Result");
147+
return new(context.Length, "Result");
143148
}
144-
return MatchResult.NoMatch();
149+
150+
return null;
145151
});
146152

147153
return Verify(builder.ToString());
@@ -161,9 +167,10 @@ public Task MatchAtEnd()
161167
if (content.Length >= context.Length &&
162168
content[..context.Length].SequenceEqual(context))
163169
{
164-
return MatchResult.Match(context.Length, "Result");
170+
return new(context.Length, "Result");
165171
}
166-
return MatchResult.NoMatch();
172+
173+
return null;
167174
});
168175

169176
return Verify(builder.ToString());
@@ -183,9 +190,10 @@ public Task OverlappingMatches()
183190
if (content.Length >= context.Length &&
184191
content[..context.Length].SequenceEqual(context))
185192
{
186-
return MatchResult.Match(context.Length, "bb");
193+
return new(context.Length, "bb");
187194
}
188-
return MatchResult.NoMatch();
195+
196+
return null;
189197
});
190198

191199
return Verify(builder.ToString());
@@ -205,14 +213,16 @@ public Task DifferentReplacementLengths()
205213
if (content.Length >= context.Short.Length &&
206214
content[..context.Short.Length].SequenceEqual(context.Short))
207215
{
208-
return MatchResult.Match(context.Short.Length, "replaced");
216+
return new(context.Short.Length, "replaced");
209217
}
218+
210219
if (content.Length >= context.Long.Length &&
211220
content[..context.Long.Length].SequenceEqual(context.Long))
212221
{
213-
return MatchResult.Match(context.Long.Length, "r");
222+
return new(context.Long.Length, "r");
214223
}
215-
return MatchResult.NoMatch();
224+
225+
return null;
216226
});
217227

218228
return Verify(builder.ToString());
@@ -231,9 +241,10 @@ public Task SingleCharacterMatch()
231241
{
232242
if (content.Length >= 1 && content[0] == context)
233243
{
234-
return MatchResult.Match(1, "x");
244+
return new(1, "x");
235245
}
236-
return MatchResult.NoMatch();
246+
247+
return null;
237248
});
238249

239250
return Verify(builder.ToString());
@@ -259,9 +270,10 @@ public Task LargeStringBuilderWithMultipleChunks()
259270
if (content.Length >= context.Length &&
260271
content[..context.Length].SequenceEqual(context))
261272
{
262-
return MatchResult.Match(context.Length, "MATCH");
273+
return new(context.Length, "MATCH");
263274
}
264-
return MatchResult.NoMatch();
275+
276+
return null;
265277
});
266278

267279
var result = builder.ToString();
@@ -273,7 +285,7 @@ public Task LargeStringBuilderWithMultipleChunks()
273285
index += "MATCH".Length;
274286
}
275287

276-
return Verify(new { MatchCount = matchCount });
288+
return Verify(new {MatchCount = matchCount});
277289
}
278290

279291
[Fact]
@@ -297,9 +309,10 @@ public Task PatternSplitAcrossChunkBoundary()
297309
if (content.Length >= context.Length &&
298310
content[..context.Length].SequenceEqual(context))
299311
{
300-
return MatchResult.Match(context.Length, "SUCCESS");
312+
return new(context.Length, "SUCCESS");
301313
}
302-
return MatchResult.NoMatch();
314+
315+
return null;
303316
});
304317

305318
var result = builder.ToString();
@@ -327,9 +340,10 @@ public Task ConsecutiveMatches()
327340
if (content.Length >= context.Length &&
328341
content[..context.Length].SequenceEqual(context))
329342
{
330-
return MatchResult.Match(context.Length, "XY");
343+
return new(context.Length, "XY");
331344
}
332-
return MatchResult.NoMatch();
345+
346+
return null;
333347
});
334348

335349
return Verify(builder.ToString());
@@ -357,9 +371,10 @@ public Task PatternSpanningThreeChunks()
357371
if (content.Length >= context.Length &&
358372
content[..context.Length].SequenceEqual(context))
359373
{
360-
return MatchResult.Match(context.Length, "FOUND!!!");
374+
return new(context.Length, "FOUND!!!");
361375
}
362-
return MatchResult.NoMatch();
376+
377+
return null;
363378
});
364379

365380
var result = builder.ToString();
@@ -393,9 +408,10 @@ public Task PatternSpanningThreeChunks_AlternativeLayout()
393408
if (content.Length >= context.Length &&
394409
content[..context.Length].SequenceEqual(context))
395410
{
396-
return MatchResult.Match(context.Length, "FOUND!!!");
411+
return new(context.Length, "FOUND!!!");
397412
}
398-
return MatchResult.NoMatch();
413+
414+
return null;
399415
});
400416

401417
var result = builder.ToString();
@@ -428,9 +444,10 @@ public Task PatternSpanningFourChunks()
428444
if (content.Length >= context.Length &&
429445
content[..context.Length].SequenceEqual(context))
430446
{
431-
return MatchResult.Match(context.Length, "SUCCESS!");
447+
return new(context.Length, "SUCCESS!");
432448
}
433-
return MatchResult.NoMatch();
449+
450+
return null;
434451
});
435452

436453
var result = builder.ToString();
@@ -462,9 +479,10 @@ public Task MultipleMatchesAcrossChunks()
462479
if (content.Length >= context.Length &&
463480
content[..context.Length].SequenceEqual(context))
464481
{
465-
return MatchResult.Match(context.Length, "MATCH");
482+
return new(context.Length, "MATCH");
466483
}
467-
return MatchResult.NoMatch();
484+
485+
return null;
468486
});
469487

470488
var result = builder.ToString();
@@ -476,7 +494,7 @@ public Task MultipleMatchesAcrossChunks()
476494
index += "MATCH".Length;
477495
}
478496

479-
return Verify(new { MatchCount = matchCount });
497+
return Verify(new {MatchCount = matchCount});
480498
}
481499

482500
[Fact]
@@ -487,18 +505,19 @@ public Task VariableLengthMatches()
487505
CrossChunkMatcher.ReplaceAll(
488506
builder,
489507
maxLength: 8,
490-
context: new[] { "cat", "dog", "bird", "elephant" },
508+
context: new[] {"cat", "dog", "bird", "elephant"},
491509
matcher: static (content, _, context) =>
492510
{
493511
foreach (var word in context)
494512
{
495513
if (content.Length >= word.Length &&
496514
content[..word.Length].SequenceEqual(word))
497515
{
498-
return MatchResult.Match(word.Length, "animal");
516+
return new(word.Length, "animal");
499517
}
500518
}
501-
return MatchResult.NoMatch();
519+
520+
return null;
502521
});
503522

504523
return Verify(builder.ToString());

src/Verify/Serialization/Scrubbers/CrossChunkMatcher.cs

Lines changed: 10 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -36,17 +36,19 @@ public static void ReplaceAll<TContext>(
3636

3737
// Check for match at this position
3838
var windowSlice = buffer[..bufferLength];
39-
var result = matcher(windowSlice, absolutePosition, context);
39+
var potentialMatch = matcher(windowSlice, absolutePosition, context);
4040

41-
if (!result.IsMatch)
41+
if (potentialMatch == null)
4242
{
4343
continue;
4444
}
4545

46-
matches.Add(new(absolutePosition, result.MatchLength, result.Replacement));
46+
var match = potentialMatch.Value;
47+
48+
matches.Add(new(absolutePosition, match.Length, match.Replacement));
4749

4850
// Skip past the match
49-
var skipAmount = result.MatchLength - 1;
51+
var skipAmount = match.Length - 1;
5052
if (skipAmount <= 0)
5153
{
5254
continue;
@@ -112,7 +114,7 @@ static int FillBuffer(StringBuilder builder, int start, Span<char> buffer)
112114
/// <param name="absolutePosition">Absolute position in the StringBuilder where this content starts</param>
113115
/// <param name="context">User-provided context</param>
114116
/// <returns>Match result indicating if a match was found and replacement details</returns>
115-
public delegate MatchResult MatchHandler<in TContext>(
117+
public delegate MatchResult? MatchHandler<in TContext>(
116118
CharSpan content,
117119
int absolutePosition,
118120
TContext context);
@@ -121,29 +123,10 @@ public delegate MatchResult MatchHandler<in TContext>(
121123
/// <summary>
122124
/// Result of a match check operation.
123125
/// </summary>
124-
readonly struct MatchResult
126+
readonly struct MatchResult(int length, string replacement)
125127
{
126-
public readonly bool IsMatch;
127-
public readonly int MatchLength;
128-
public readonly string Replacement;
129-
130-
MatchResult(bool isMatch, int matchLength, string replacement)
131-
{
132-
IsMatch = isMatch;
133-
MatchLength = matchLength;
134-
Replacement = replacement;
135-
}
136-
137-
/// <summary>
138-
/// Creates a result indicating a match was found.
139-
/// </summary>
140-
public static MatchResult Match(int length, string replacement) =>
141-
new(true, length, replacement);
142-
143-
/// <summary>
144-
/// Creates a result indicating no match was found.
145-
/// </summary>
146-
public static MatchResult NoMatch() => default;
128+
public readonly int Length = length;
129+
public readonly string Replacement = replacement;
147130
}
148131

149132
readonly struct Match(int index, int length, string value)

src/Verify/Serialization/Scrubbers/DateScrubber.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -227,11 +227,11 @@ static void ReplaceInner(StringBuilder builder, string format, Counter counter,
227227

228228
if (context.TryConvert(slice, context.Format, context.Counter, context.Culture, out var converted))
229229
{
230-
return MatchResult.Match(length, converted);
230+
return new MatchResult(length, converted);
231231
}
232232
}
233233

234-
return MatchResult.NoMatch();
234+
return null;
235235
});
236236
}
237237

src/Verify/Serialization/Scrubbers/DirectoryReplacements_StringBuilder.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ public static void Replace(StringBuilder builder, List<Pair> paths)
5050
// Skip if already matched
5151
if (context.IsPositionMatched(absolutePosition))
5252
{
53-
return MatchResult.NoMatch();
53+
return null;
5454
}
5555

5656
foreach (var pair in context.Pairs)
@@ -74,10 +74,10 @@ public static void Replace(StringBuilder builder, List<Pair> paths)
7474
}
7575

7676
context.AddMatchedRange(absolutePosition, absolutePosition + matchLength);
77-
return MatchResult.Match(matchLength, pair.Replace);
77+
return new MatchResult(matchLength, pair.Replace);
7878
}
7979

80-
return MatchResult.NoMatch();
80+
return null;
8181
});
8282
}
8383

0 commit comments

Comments
 (0)