Skip to content

Commit 46b0270

Browse files
authored
Replace regex with string operations (#7107)
1 parent 3cc6ac1 commit 46b0270

File tree

1 file changed

+5
-17
lines changed

1 file changed

+5
-17
lines changed

Files/Filesystem/FilesystemOperations/Helpers/FilesystemHelpers.cs

Lines changed: 5 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@
1616
using System.Diagnostics;
1717
using System.IO;
1818
using System.Linq;
19-
using System.Text.RegularExpressions;
2019
using System.Threading;
2120
using System.Threading.Tasks;
2221
using Windows.ApplicationModel.AppService;
@@ -48,7 +47,9 @@ public class FilesystemHelpers : IFilesystemHelpers
4847

4948
#region Helpers Members
5049

51-
private static readonly List<string> RestrictedFileNames = new List<string>()
50+
private static readonly char[] RestrictedCharacters = new[] { '\\', '/', ':', '*', '?', '"', '<', '>', '|' };
51+
52+
private static readonly string[] RestrictedFileNames = new string[]
5253
{
5354
"CON", "PRN", "AUX",
5455
"NUL", "COM1", "COM2",
@@ -1115,28 +1116,15 @@ public static async Task<IEnumerable<IStorageItemWithPath>> GetDraggedStorageIte
11151116

11161117
public static bool ContainsRestrictedCharacters(string input)
11171118
{
1118-
Regex regex = new Regex("\\\\|\\/|\\:|\\*|\\?|\\\"|\\<|\\>|\\|"); // Restricted symbols for file names
1119-
MatchCollection matches = regex.Matches(input);
1120-
1121-
if (matches.Count > 0)
1122-
{
1123-
return true;
1124-
}
1125-
1126-
return false;
1119+
return input.IndexOfAny(RestrictedCharacters) >= 0;
11271120
}
11281121

11291122
public static bool ContainsRestrictedFileName(string input)
11301123
{
11311124
foreach (string name in RestrictedFileNames)
11321125
{
1133-
Regex regex = new Regex($"^{name}($|\\.)(.+)?");
1134-
MatchCollection matches = regex.Matches(input.ToUpperInvariant());
1135-
1136-
if (matches.Count > 0)
1137-
{
1126+
if (input.StartsWith(name, StringComparison.OrdinalIgnoreCase) && (input.Length == name.Length || input[name.Length] == '.'))
11381127
return true;
1139-
}
11401128
}
11411129

11421130
return false;

0 commit comments

Comments
 (0)