fix: Better NiceName implementation, handles more cases of syntax, whitespace and punctuation

This commit is contained in:
Chris Cameron
2020-05-28 16:21:03 -04:00
parent 4339f02698
commit f53d05936c
2 changed files with 33 additions and 13 deletions

View File

@@ -24,11 +24,18 @@ namespace ICD.Common.Utils.Tests
Assert.AreEqual("\x08\x22\x00\x00\x00\x02", output); Assert.AreEqual("\x08\x22\x00\x00\x00\x02", output);
} }
[Test, UsedImplicitly] [TestCase("Test", "Test")]
public void NiceNameTest() [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"); string output = StringUtils.NiceName(input);
Assert.AreEqual("Today I Live In The USA With Simon", output); Assert.AreEqual(expected, output);
} }
[Test, UsedImplicitly] [Test, UsedImplicitly]

View File

@@ -346,7 +346,7 @@ namespace ICD.Common.Utils
} }
/// <summary> /// <summary>
/// Returns the object.ToString() with spaces before capital letters. /// Formats the object.ToString() to a human readable representation.
/// </summary> /// </summary>
/// <param name="obj"></param> /// <param name="obj"></param>
/// <returns></returns> /// <returns></returns>
@@ -359,9 +359,7 @@ namespace ICD.Common.Utils
} }
/// <summary> /// <summary>
/// Inserts spaces before capital letters. /// Formats the string to a human readable representation.
///
/// http://stackoverflow.com/questions/4488969/split-a-string-by-capital-letters
/// </summary> /// </summary>
/// <param name="name"></param> /// <param name="name"></param>
/// <returns></returns> /// <returns></returns>
@@ -370,12 +368,27 @@ namespace ICD.Common.Utils
if (name == null) if (name == null)
throw new ArgumentNullException("name"); throw new ArgumentNullException("name");
Regex regex = new Regex(@" // Remove s_, m_, _ member prefixes and delimiters
(?<=[A-Z])(?=[A-Z][a-z]) | name = Regex.Replace(name, "s_|m_|_", " ");
(?<=[^A-Z])(?=[A-Z]) |
(?<=[A-Za-z])(?=[^A-Za-z])", RegexOptions.IgnorePatternWhitespace);
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);
} }
/// <summary> /// <summary>