Skip to content

Commit df74710

Browse files
authored
Use IndexOfAnyValues in FileProviders PathUtils (#85190)
* Use IndexOfAnyValues in FileProviders PathUtils * Turn two fields into methods
1 parent a33d95b commit df74710

File tree

1 file changed

+14
-4
lines changed
  • src/libraries/Microsoft.Extensions.FileProviders.Physical/src/Internal

1 file changed

+14
-4
lines changed

src/libraries/Microsoft.Extensions.FileProviders.Physical/src/Internal/PathUtils.cs

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
// Licensed to the .NET Foundation under one or more agreements.
22
// The .NET Foundation licenses this file to you under the MIT license.
33

4+
using System;
5+
using System.Buffers;
46
using System.IO;
57
using System.Linq;
68
using Microsoft.Extensions.Primitives;
@@ -9,23 +11,31 @@ namespace Microsoft.Extensions.FileProviders.Physical.Internal
911
{
1012
internal static class PathUtils
1113
{
12-
private static readonly char[] _invalidFileNameChars = Path.GetInvalidFileNameChars()
14+
private static char[] GetInvalidFileNameChars() => Path.GetInvalidFileNameChars()
1315
.Where(c => c != Path.DirectorySeparatorChar && c != Path.AltDirectorySeparatorChar).ToArray();
1416

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

20+
#if NET8_0_OR_GREATER
21+
private static readonly IndexOfAnyValues<char> _invalidFileNameChars = IndexOfAnyValues.Create(GetInvalidFileNameChars());
22+
private static readonly IndexOfAnyValues<char> _invalidFilterChars = IndexOfAnyValues.Create(GetInvalidFilterChars());
23+
#else
24+
private static readonly char[] _invalidFileNameChars = GetInvalidFileNameChars();
25+
private static readonly char[] _invalidFilterChars = GetInvalidFilterChars();
26+
#endif
27+
1828
private static readonly char[] _pathSeparators = new[]
1929
{Path.DirectorySeparatorChar, Path.AltDirectorySeparatorChar};
2030

2131
internal static bool HasInvalidPathChars(string path)
2232
{
23-
return path.IndexOfAny(_invalidFileNameChars) != -1;
33+
return path.AsSpan().IndexOfAny(_invalidFileNameChars) >= 0;
2434
}
2535

2636
internal static bool HasInvalidFilterChars(string path)
2737
{
28-
return path.IndexOfAny(_invalidFilterChars) != -1;
38+
return path.AsSpan().IndexOfAny(_invalidFilterChars) >= 0;
2939
}
3040

3141
internal static string EnsureTrailingSlash(string path)

0 commit comments

Comments
 (0)