Remove multispace instances from template filenames (#637)

This commit is contained in:
MBucari 2023-06-25 14:52:15 -06:00
parent ac87d70613
commit 1b09b1fd48
2 changed files with 83 additions and 0 deletions

View File

@ -179,6 +179,7 @@ namespace LibationFileManager
while((slashIndex = part.IndexOf(Path.DirectorySeparatorChar, lastIndex)) > -1)
{
dir.Add(part[lastIndex..slashIndex]);
RemoveSpaces(dir);
directories.Add(dir);
dir = new();
@ -186,11 +187,57 @@ namespace LibationFileManager
}
dir.Add(part[lastIndex..]);
}
RemoveSpaces(dir);
directories.Add(dir);
return directories;
}
/// <summary>
/// Remove spaces from the filename parts to ensure that after concatenation
/// <br>-</br> There is no leading or trailing white space
/// <br>-</br> There are no multispace instances
/// </summary>
private static void RemoveSpaces(List<string> parts)
{
while (parts.Count > 0 && string.IsNullOrWhiteSpace(parts[0]))
parts.RemoveAt(0);
while (parts.Count > 0 && string.IsNullOrWhiteSpace(parts[^1]))
parts.RemoveAt(parts.Count - 1);
if (parts.Count == 0) return;
parts[0] = parts[0].TrimStart();
parts[^1] = parts[^1].TrimEnd();
//Replace all multispace substrings with single space
for (int i = 0; i < parts.Count; i++)
{
string original;
do
{
original = parts[i];
parts[i] = original.Replace(" ", " ");
}while(original.Length != parts[i].Length);
}
//Remove instances of double spaces at part boundaries
for (int i = 1; i < parts.Count; i++)
{
if (parts[i - 1].EndsWith(' ') && parts[i].StartsWith(' '))
{
parts[i] = parts[i].Substring(1);
if (parts[i].Length == 0)
{
parts.RemoveAt(i);
i--;
}
}
}
}
#endregion
#region Registered Template Properties

View File

@ -107,6 +107,42 @@ namespace TemplatesTests
.Should().Be(expected);
}
[TestMethod]
[DataRow("<samplerate>", "", "", "100")]
[DataRow(" <samplerate> ", "", "", "100")]
[DataRow("4<samplerate>4", "", "", "100")]
[DataRow("<bitrate> - <bitrate>", "", "", "1 8 - 1 8")]
[DataRow("<bitrate> 42 <bitrate>", "", "", "1 8 1 8")]
[DataRow(" <bitrate> - <bitrate> ", "", "", "1 8 - 1 8")]
[DataRow("4<bitrate> - <bitrate> 4", "", "", "1 8 - 1 8")]
[DataRow("4<bitrate> - <bitrate> 4", "", "", "1 8 - 1 8")]
[DataRow("<channels><channels><samplerate><channels><channels>", "", "", "100")]
[DataRow(" <channels> <channels> <samplerate> <channels> <channels>", "", "", "100")]
[DataRow(" <channels> - <channels> <samplerate> <channels> - <channels>", "", "", "- 100 -")]
public void Tests_removeSpaces(string template, string dirFullPath, string extension, string expected)
{
if (Environment.OSVersion.Platform is not PlatformID.Win32NT)
{
dirFullPath = dirFullPath.Replace("C:", "").Replace('\\', '/');
expected = expected.Replace("C:", "").Replace('\\', '/');
}
var replacements
= new ReplacementCharacters
{
Replacements = Replacements.Replacements
.Append(new Replacement('4', " ", ""))
.Append(new Replacement('2', " ", ""))
.ToArray() };
Templates.TryGetTemplate<Templates.FileTemplate>(template, out var fileTemplate).Should().BeTrue();
fileTemplate
.GetFilename(GetLibraryBook(), dirFullPath, extension, replacements)
.PathWithoutPrefix
.Should().Be(expected);
}
[TestMethod]
[DataRow("<bitrate>Kbps <samplerate>Hz", "128Kbps 44100Hz")]
[DataRow("<bitrate>Kbps <samplerate[6]>Hz", "128Kbps 044100Hz")]