Skip to content

Commit f38ab0a

Browse files
committed
Update Extensions.cs
1 parent 03e8a6d commit f38ab0a

File tree

1 file changed

+19
-11
lines changed

1 file changed

+19
-11
lines changed

src/Verify/Extensions.cs

Lines changed: 19 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
// ReSharper disable UnusedVariable
22

3+
public delegate bool RemoveLine(CharSpan line);
4+
35
static class Extensions
46
{
57
static HashSet<Type> numericTypes =
@@ -152,26 +154,32 @@ public static string NameWithParent(this Type type)
152154
return null;
153155
}
154156

155-
public static void FilterLines(this StringBuilder input, Func<string, bool> removeLine)
157+
// public static void FilterLines(this StringBuilder input, Func<string, bool> removeLine) =>
158+
// FilterLines(input, (CharSpan line) => removeLine(line.ToString()));
159+
160+
public static void FilterLines(this StringBuilder input, RemoveLine removeLine)
156161
{
157-
var theString = input.ToString();
158-
using var reader = new StringReader(theString);
162+
if (input.Length == 0)
163+
{
164+
return;
165+
}
166+
167+
var span = input.AsSpan();
168+
var hasTrailingNewline = span[^1] == '\n';
169+
159170
input.Clear();
160171

161-
while (reader.ReadLine() is { } line)
172+
foreach (var line in span.EnumerateLines())
162173
{
163-
if (removeLine(line))
174+
if (!removeLine(line))
164175
{
165-
continue;
176+
input.AppendLineN(line);
166177
}
167-
168-
input.AppendLineN(line);
169178
}
170179

171-
if (input.Length > 0 &&
172-
!theString.EndsWith('\n'))
180+
if (input.Length > 0 && !hasTrailingNewline)
173181
{
174-
input.Length -= 1;
182+
input.Length--;
175183
}
176184
}
177185

0 commit comments

Comments
 (0)