From f53d05936c853f7846268274b659449c8399b33b Mon Sep 17 00:00:00 2001 From: Chris Cameron Date: Thu, 28 May 2020 16:21:03 -0400 Subject: [PATCH] fix: Better NiceName implementation, handles more cases of syntax, whitespace and punctuation --- ICD.Common.Utils.Tests/StringUtilsTest.cs | 15 ++++++++--- ICD.Common.Utils/StringUtils.cs | 31 ++++++++++++++++------- 2 files changed, 33 insertions(+), 13 deletions(-) diff --git a/ICD.Common.Utils.Tests/StringUtilsTest.cs b/ICD.Common.Utils.Tests/StringUtilsTest.cs index 18318ff..941565a 100644 --- a/ICD.Common.Utils.Tests/StringUtilsTest.cs +++ b/ICD.Common.Utils.Tests/StringUtilsTest.cs @@ -24,11 +24,18 @@ namespace ICD.Common.Utils.Tests Assert.AreEqual("\x08\x22\x00\x00\x00\x02", output); } - [Test, UsedImplicitly] - public void NiceNameTest() + [TestCase("Test", "Test")] + [TestCase("test", "Test")] + [TestCase("TodayILiveInTheUSAWithSimon", "Today I Live In The USA With Simon")] + [TestCase("CONST_VALUE", "CONST VALUE")] + [TestCase("m_PrivateMember", "Private Member")] + [TestCase("variableName", "Variable Name")] + [TestCase("Comma, Delimited", "Comma, Delimited")] + [TestCase("Comma,Delimited", "Comma, Delimited")] + public void NiceNameTest(string input, string expected) { - string output = StringUtils.NiceName("TodayILiveInTheUSAWithSimon"); - Assert.AreEqual("Today I Live In The USA With Simon", output); + string output = StringUtils.NiceName(input); + Assert.AreEqual(expected, output); } [Test, UsedImplicitly] diff --git a/ICD.Common.Utils/StringUtils.cs b/ICD.Common.Utils/StringUtils.cs index b838fa4..43d7533 100644 --- a/ICD.Common.Utils/StringUtils.cs +++ b/ICD.Common.Utils/StringUtils.cs @@ -346,7 +346,7 @@ namespace ICD.Common.Utils } /// - /// Returns the object.ToString() with spaces before capital letters. + /// Formats the object.ToString() to a human readable representation. /// /// /// @@ -359,9 +359,7 @@ namespace ICD.Common.Utils } /// - /// Inserts spaces before capital letters. - /// - /// http://stackoverflow.com/questions/4488969/split-a-string-by-capital-letters + /// Formats the string to a human readable representation. /// /// /// @@ -370,12 +368,27 @@ namespace ICD.Common.Utils if (name == null) throw new ArgumentNullException("name"); - Regex regex = new Regex(@" - (?<=[A-Z])(?=[A-Z][a-z]) | - (?<=[^A-Z])(?=[A-Z]) | - (?<=[A-Za-z])(?=[^A-Za-z])", RegexOptions.IgnorePatternWhitespace); + // Remove s_, m_, _ member prefixes and delimiters + name = Regex.Replace(name, "s_|m_|_", " "); - return regex.Replace(name, " "); + // Split string by capital letters + // http://stackoverflow.com/questions/4488969/split-a-string-by-capital-letters + name = Regex.Replace(name, @"(?<=[A-Z])(?=[A-Z][a-z])|(?<=[^A-Z])(?=[A-Z])|(?<=[A-Za-z])(?=[^A-Za-z])", " "); + + // Fix punctuation from the capital letter split + // "Comma, Delimited" + // became + // "Comma , Delimited" + name = RegexUtils.ReplaceGroup(name, "(?'space' )[^A-Z]", "space", ""); + + // Replace runs of whitespace with a single space + name = Regex.Replace(name, @"\s+", " "); + + // Remove leading/trailing whitespace + name = name.Trim(); + + // Capitalize first letter + return UppercaseFirst(name); } ///