Skip to content

Commit

Permalink
shortcut FileNameCleaner.AppendValid for better perf (#1243)
Browse files Browse the repository at this point in the history
  • Loading branch information
SimonCropp authored Jul 16, 2024
1 parent a75ad58 commit 8f0543a
Showing 1 changed file with 18 additions and 5 deletions.
23 changes: 18 additions & 5 deletions src/Verify/FileNameCleaner.cs
Original file line number Diff line number Diff line change
Expand Up @@ -53,11 +53,7 @@ public static string ReplaceInvalidFileNameChars(this string value)
{
var span = value.AsSpan();

#if NET8_0_OR_GREATER
var index = span.IndexOfAny(invalidFileNameSearchValues);
#else
var index = span.IndexOfAny(invalidFileNameChars.AsSpan());
#endif
var index = IndexOfInvalidChar(span);

if (index == -1)
{
Expand All @@ -80,6 +76,14 @@ public static string ReplaceInvalidFileNameChars(this string value)
return new(chars);
}

static int IndexOfInvalidChar(CharSpan span) =>
#if NET8_0_OR_GREATER
span.IndexOfAny(invalidFileNameSearchValues);
#else
span.IndexOfAny(invalidFileNameChars.AsSpan());
#endif


static bool IsInvalid(char ch) =>
#if NET8_0_OR_GREATER
invalidFileNameSearchValues.Contains(ch);
Expand All @@ -89,6 +93,15 @@ static bool IsInvalid(char ch) =>

public static void AppendValid(StringBuilder builder, string value)
{
var span = value.AsSpan();
var index = IndexOfInvalidChar(span);

if (index == -1)
{
builder.Append(value);
return;
}

foreach (var ch in value)
{
if (IsInvalid(ch))
Expand Down

0 comments on commit 8f0543a

Please sign in to comment.