From 1b09b1fd481221b141a75fa12da5c5de051a7b2d Mon Sep 17 00:00:00 2001 From: MBucari Date: Sun, 25 Jun 2023 14:52:15 -0600 Subject: [PATCH] Remove multispace instances from template filenames (#637) --- Source/LibationFileManager/Templates.cs | 47 +++++++++++++++++++ .../TemplatesTests.cs | 36 ++++++++++++++ 2 files changed, 83 insertions(+) diff --git a/Source/LibationFileManager/Templates.cs b/Source/LibationFileManager/Templates.cs index 4c56d824..97146032 100644 --- a/Source/LibationFileManager/Templates.cs +++ b/Source/LibationFileManager/Templates.cs @@ -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; } + /// + /// Remove spaces from the filename parts to ensure that after concatenation + ///
-
There is no leading or trailing white space + ///
-
There are no multispace instances + ///
+ private static void RemoveSpaces(List 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 diff --git a/Source/_Tests/LibationFileManager.Tests/TemplatesTests.cs b/Source/_Tests/LibationFileManager.Tests/TemplatesTests.cs index efd96faf..aeb66acb 100644 --- a/Source/_Tests/LibationFileManager.Tests/TemplatesTests.cs +++ b/Source/_Tests/LibationFileManager.Tests/TemplatesTests.cs @@ -107,6 +107,42 @@ namespace TemplatesTests .Should().Be(expected); } + [TestMethod] + [DataRow("", "", "", "100")] + [DataRow(" ", "", "", "100")] + [DataRow("44", "", "", "100")] + [DataRow(" - ", "", "", "1 8 - 1 8")] + [DataRow(" 42 ", "", "", "1 8 1 8")] + [DataRow(" - ", "", "", "1 8 - 1 8")] + [DataRow("4 - 4", "", "", "1 8 - 1 8")] + [DataRow("4 - 4", "", "", "1 8 - 1 8")] + [DataRow("", "", "", "100")] + [DataRow(" ", "", "", "100")] + [DataRow(" - - ", "", "", "- 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(template, out var fileTemplate).Should().BeTrue(); + + fileTemplate + .GetFilename(GetLibraryBook(), dirFullPath, extension, replacements) + .PathWithoutPrefix + .Should().Be(expected); + } + [TestMethod] [DataRow("Kbps Hz", "128Kbps 44100Hz")] [DataRow("Kbps Hz", "128Kbps 044100Hz")]