Skip to content

Commit d2be103

Browse files
committed
.
1 parent d37a4e7 commit d2be103

File tree

2 files changed

+18
-36
lines changed

2 files changed

+18
-36
lines changed

src/Verify.Tests/LinesScrubberTests.cs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -337,7 +337,7 @@ public void FilterLines_LineSpansMultipleChunks()
337337
builder.Append(longLine);
338338
builder.Append("\nkeep2");
339339

340-
builder.FilterLines(line => line == longLine);
340+
builder.FilterLines(line => line.SequenceEqual(longLine));
341341

342342
Assert.Equal("keep1\nkeep2", builder.ToString());
343343
}
@@ -352,7 +352,7 @@ public void FilterLines_NewlineAtChunkBoundary()
352352
builder.Append('\n');
353353
builder.Append("line2");
354354

355-
builder.FilterLines(line => line == chunkFiller);
355+
builder.FilterLines(line => line.SequenceEqual(chunkFiller));
356356

357357
Assert.Equal("line2", builder.ToString());
358358
}
@@ -367,7 +367,7 @@ public void FilterLines_CrLfSplitAcrossChunks()
367367
builder.Append("\r\n");
368368
builder.Append("nextline");
369369

370-
builder.FilterLines(line => line == lineContent);
370+
builder.FilterLines(line => line.SequenceEqual(lineContent));
371371

372372
Assert.Equal("nextline", builder.ToString());
373373
}
@@ -391,7 +391,7 @@ public void FilterLines_MultipleLinesSpanningChunks()
391391
}
392392

393393
// Remove even-indexed lines
394-
builder.FilterLines(line => lines.IndexOf(line) % 2 == 0);
394+
builder.FilterLines(line => lines.IndexOf(line.ToString()) % 2 == 0);
395395

396396
var expected = string.Join('\n', lines.Where((_, i) => i % 2 == 1));
397397
Assert.Equal(expected, builder.ToString());
@@ -406,7 +406,7 @@ public void FilterLines_VeryLongLineKept()
406406
builder.Append(longLine);
407407
builder.Append("\nremove");
408408

409-
builder.FilterLines(line => line == "remove");
409+
builder.FilterLines(line => line is "remove");
410410

411411
Assert.Equal(longLine, builder.ToString());
412412
}
@@ -457,7 +457,7 @@ public void FilterLines_ChunkBoundaryInMiddleOfLine()
457457
builder.Append(fullLine);
458458
builder.Append("\nafter");
459459

460-
builder.FilterLines(line => line == "before");
460+
builder.FilterLines(line => line is "before");
461461

462462
Assert.Equal(fullLine + "\nafter", builder.ToString());
463463
}
@@ -471,7 +471,7 @@ public void FilterLines_EmptyLinesAroundChunkBoundaries()
471471
builder.Append("\n\n\n");
472472
builder.Append("keep");
473473

474-
builder.FilterLines(string.IsNullOrEmpty);
474+
builder.FilterLines(_ => _.IsWhiteSpace());
475475

476476
Assert.Equal(longLine + "\nkeep", builder.ToString());
477477
}

src/Verify/Extensions.cs

Lines changed: 11 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -152,44 +152,26 @@ public static string NameWithParent(this Type type)
152152
return null;
153153
}
154154

155-
// public static void FilterLines(this StringBuilder input, Func<string, bool> removeLine) =>
156-
// FilterLines(input, (CharSpan line) => removeLine(line.ToString()));
157-
158155
public static void FilterLines(this StringBuilder input, RemoveLine removeLine)
159156
{
160-
if (input.Length == 0)
161-
{
162-
return;
163-
}
157+
var theString = input.ToString();
158+
using var reader = new StringReader(theString);
159+
input.Clear();
164160

165-
var length = input.Length;
166-
var pool = ArrayPool<char>.Shared;
167-
var array = pool.Rent(length);
168-
169-
try
161+
while (reader.ReadLine() is { } line)
170162
{
171-
input.CopyTo(0, array, 0, length);
172-
var span = array.AsSpan(0, length);
173-
var hasTrailingNewline = span[^1] == '\n';
174-
175-
input.Clear();
176-
177-
foreach (var line in span.EnumerateLines())
163+
if (removeLine(line))
178164
{
179-
if (!removeLine(line))
180-
{
181-
input.AppendLineN(line);
182-
}
165+
continue;
183166
}
184167

185-
if (input.Length > 0 && !hasTrailingNewline)
186-
{
187-
input.Length--;
188-
}
168+
input.AppendLineN(line);
189169
}
190-
finally
170+
171+
var endsWithNewLine = theString.EndsWith('\n');
172+
if (input.Length > 0 && !endsWithNewLine)
191173
{
192-
pool.Return(array);
174+
input.Length -= 1;
193175
}
194176
}
195177

0 commit comments

Comments
 (0)