Improve display and function of character replacement

This commit is contained in:
Michael Bucari-Tovo 2022-06-21 23:39:24 -06:00
parent 0cb18f9e1a
commit b698697256
5 changed files with 272 additions and 249 deletions

View file

@ -84,39 +84,12 @@ namespace FileManager
var pathNoPrefix = path.PathWithoutPrefix;
pathNoPrefix = replaceInvalidChars(pathNoPrefix, replacements);
pathNoPrefix = replacements.ReplaceInvalidChars(pathNoPrefix);
pathNoPrefix = removeDoubleSlashes(pathNoPrefix);
return pathNoPrefix;
}
public static char[] invalidChars { get; } = Path.GetInvalidPathChars().Union(new[] {
'*', '?', ':',
// these are weird. If you run Path.GetInvalidPathChars() in Visual Studio's "C# Interactive", then these characters are included.
// In live code, Path.GetInvalidPathChars() does not include them
'"', '<', '>'
}).ToArray();
private static string replaceInvalidChars(string path, ReplacementCharacters replacements)
{
// replace all colons except within the first 2 chars
var builder = new System.Text.StringBuilder();
for (var i = 0; i < path.Length; i++)
{
var c = path[i];
if (!invalidChars.Contains(c) || (i <= 2 && Path.IsPathRooted(path)))
builder.Append(c);
else
{
char preceding = i > 0 ? path[i - 1] : default;
char succeeding = i < path.Length - 1 ? path[i + 1] : default;
builder.Append(replacements.GetReplacement(c, preceding, succeeding));
}
}
return builder.ToString();
}
private static string removeDoubleSlashes(string path)
{
if (path.Length < 2)