Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use IndexOfAnyValues in FileProviders PathUtils #85190

Merged
merged 2 commits into from
Apr 22, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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