Skip to content

Commit

Permalink
Use IndexOfAnyValues in FileProviders PathUtils (#85190)
Browse files Browse the repository at this point in the history
* Use IndexOfAnyValues in FileProviders PathUtils

* Turn two fields into methods
  • Loading branch information
MihaZupan authored Apr 22, 2023
1 parent a33d95b commit df74710
Showing 1 changed file with 14 additions and 4 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

using System;
using System.Buffers;
using System.IO;
using System.Linq;
using Microsoft.Extensions.Primitives;
Expand All @@ -9,23 +11,31 @@ namespace Microsoft.Extensions.FileProviders.Physical.Internal
{
internal static class PathUtils
{
private static readonly char[] _invalidFileNameChars = Path.GetInvalidFileNameChars()
private static char[] GetInvalidFileNameChars() => Path.GetInvalidFileNameChars()
.Where(c => c != Path.DirectorySeparatorChar && c != Path.AltDirectorySeparatorChar).ToArray();

private static readonly char[] _invalidFilterChars = _invalidFileNameChars
private static char[] GetInvalidFilterChars() => GetInvalidFileNameChars()
.Where(c => c != '*' && c != '|' && c != '?').ToArray();

#if NET8_0_OR_GREATER
private static readonly IndexOfAnyValues<char> _invalidFileNameChars = IndexOfAnyValues.Create(GetInvalidFileNameChars());
private static readonly IndexOfAnyValues<char> _invalidFilterChars = IndexOfAnyValues.Create(GetInvalidFilterChars());
#else
private static readonly char[] _invalidFileNameChars = GetInvalidFileNameChars();
private static readonly char[] _invalidFilterChars = GetInvalidFilterChars();
#endif

private static readonly char[] _pathSeparators = new[]
{Path.DirectorySeparatorChar, Path.AltDirectorySeparatorChar};

internal static bool HasInvalidPathChars(string path)
{
return path.IndexOfAny(_invalidFileNameChars) != -1;
return path.AsSpan().IndexOfAny(_invalidFileNameChars) >= 0;
}

internal static bool HasInvalidFilterChars(string path)
{
return path.IndexOfAny(_invalidFilterChars) != -1;
return path.AsSpan().IndexOfAny(_invalidFilterChars) >= 0;
}

internal static string EnsureTrailingSlash(string path)
Expand Down

0 comments on commit df74710

Please sign in to comment.