Skip to content

Commit 8f0543a

Browse files
authored
shortcut FileNameCleaner.AppendValid for better perf (#1243)
1 parent a75ad58 commit 8f0543a

File tree

1 file changed

+18
-5
lines changed

1 file changed

+18
-5
lines changed

src/Verify/FileNameCleaner.cs

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -53,11 +53,7 @@ public static string ReplaceInvalidFileNameChars(this string value)
5353
{
5454
var span = value.AsSpan();
5555

56-
#if NET8_0_OR_GREATER
57-
var index = span.IndexOfAny(invalidFileNameSearchValues);
58-
#else
59-
var index = span.IndexOfAny(invalidFileNameChars.AsSpan());
60-
#endif
56+
var index = IndexOfInvalidChar(span);
6157

6258
if (index == -1)
6359
{
@@ -80,6 +76,14 @@ public static string ReplaceInvalidFileNameChars(this string value)
8076
return new(chars);
8177
}
8278

79+
static int IndexOfInvalidChar(CharSpan span) =>
80+
#if NET8_0_OR_GREATER
81+
span.IndexOfAny(invalidFileNameSearchValues);
82+
#else
83+
span.IndexOfAny(invalidFileNameChars.AsSpan());
84+
#endif
85+
86+
8387
static bool IsInvalid(char ch) =>
8488
#if NET8_0_OR_GREATER
8589
invalidFileNameSearchValues.Contains(ch);
@@ -89,6 +93,15 @@ static bool IsInvalid(char ch) =>
8993

9094
public static void AppendValid(StringBuilder builder, string value)
9195
{
96+
var span = value.AsSpan();
97+
var index = IndexOfInvalidChar(span);
98+
99+
if (index == -1)
100+
{
101+
builder.Append(value);
102+
return;
103+
}
104+
92105
foreach (var ch in value)
93106
{
94107
if (IsInvalid(ch))

0 commit comments

Comments
 (0)