Skip to content

Commit a30ac45

Browse files
committed
Change Prettify to not prefix identifiers starting with numbers
This lets us handle prefixing and prettification separately, which notably is important if we add prefixes after prettification. We want to prefix the final name, not the intermediate name in this case.
1 parent d51a68e commit a30ac45

5 files changed

Lines changed: 39 additions & 28 deletions

File tree

sources/SilkTouch/SilkTouch/Mods/PrettifyNames.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -508,6 +508,7 @@ NamePrettifier namePrettifier
508508
result = nameAffixer.RemoveAffixes(result, container, name, null);
509509
result = namePrettifier.Prettify(result, allowAllCaps);
510510
result = nameAffixer.ApplyAffixes(result, container, name, null);
511+
result = NameUtils.PrefixIfStartsWithNumber(result);
511512

512513
return result;
513514
}
@@ -1505,7 +1506,7 @@ public void Trim(NameTrimmerContext context)
15051506
foreach (var (original, (primary, secondary)) in context.Names)
15061507
{
15071508
var secondaries = secondary;
1508-
var newPrimary = affixer.ApplyAffixes(primary, null, original, secondaries);
1509+
var newPrimary = affixer.ApplyAffixes(primary, null, original, secondaries); // TODO: Prefix names starting with numbers
15091510
context.Names[original] = new CandidateNames(newPrimary, secondaries);
15101511
}
15111512

sources/SilkTouch/SilkTouch/Naming/NamePrettifier.cs

Lines changed: 2 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -31,11 +31,6 @@ public class NamePrettifier(int longAcronymThreshold)
3131
/// <exception cref="InvalidOperationException">Thrown when the input or output is an empty identifier.</exception>
3232
public string Prettify(string identifier, bool allowAllCaps = false)
3333
{
34-
if (identifier.Length == 0)
35-
{
36-
throw new InvalidOperationException("Cannot prettify an empty identifier");
37-
}
38-
3934
var words = NameSplitter.BreakIntoWords(identifier);
4035

4136
// Add "X" to separate out numbers
@@ -50,15 +45,6 @@ public string Prettify(string identifier, bool allowAllCaps = false)
5045
}
5146
}
5247

53-
// Add "X" if first word is a number
54-
if (words.Count > 0)
55-
{
56-
if (NameUtils.GetCharType(words[0][0]) is CharType.Number)
57-
{
58-
words.Insert(0, "X");
59-
}
60-
}
61-
6248
// Pretend there is an underscore between each word
6349
// This is used as a heuristic for determining whether we can treat short, all uppercase words as acronyms
6450
//
@@ -179,17 +165,11 @@ public string Prettify(string identifier, bool allowAllCaps = false)
179165
}
180166
}
181167

182-
var result = string.Join("", words);
183-
if (result.Length == 0)
184-
{
185-
throw new InvalidOperationException(
186-
$"Prettification for '{identifier}' led to an empty identifier"
187-
);
188-
}
168+
var result = string.Join(null, words);
189169

190170
// Disallow all capitals
191171
var resultSpan = result.AsSpan();
192-
if (!allowAllCaps && IsAllCaps(result))
172+
if (result.Length > 0 && !allowAllCaps && IsAllCaps(result))
193173
{
194174
Span<char> caps = stackalloc char[resultSpan.Length - 1];
195175
resultSpan[1..].ToLower(caps, CultureInfo.InvariantCulture);

sources/SilkTouch/SilkTouch/Naming/NameUtils.cs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,19 @@ public static partial class NameUtils
3838
"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789_"
3939
);
4040

41+
/// <summary>
42+
/// Prefixes the given identifier with the specified prefix if it starts with a number.
43+
/// </summary>
44+
public static string PrefixIfStartsWithNumber(string identifier, string prefix = "X")
45+
{
46+
if (identifier.Length > 0 && char.IsDigit(identifier[0]))
47+
{
48+
return $"X{identifier}";
49+
}
50+
51+
return identifier;
52+
}
53+
4154
/// <summary>
4255
/// Gets the char type for the specified character according
4356
/// to the categorization defined by <see cref="CharType"/>.

tests/SilkTouch/SilkTouch/Naming/NamePrettifierTests.cs

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -71,16 +71,19 @@ public string AcronymsWithNumbers2(string input, int longAcronymThreshold = 0) =
7171
public string ConsecutiveAcronyms(string input, int longAcronymThreshold = 0) =>
7272
new NamePrettifier(longAcronymThreshold).Prettify(input);
7373

74+
/// <summary>
75+
/// C# identifiers cannot start with numbers, but <see cref="NamePrettifier.Prettify"/> no longer handles this
76+
/// in favor of calling <see cref="NameUtils.PrefixIfStartsWithNumber"/> separately.
77+
/// </summary>
7478
[Theory]
75-
// C# identifiers cannot start with numbers
76-
[TestCase("123", ExpectedResult = "X123")]
77-
[TestCase("123Hello", ExpectedResult = "X123Hello")]
79+
[TestCase("123", ExpectedResult = "123")]
80+
[TestCase("123Hello", ExpectedResult = "123Hello")]
7881
public string StartsWithNumber(string input, int longAcronymThreshold = 0) =>
7982
new NamePrettifier(longAcronymThreshold).Prettify(input);
8083

8184
[Theory]
8285
// Add x between numbers to maintain separation
83-
[TestCase("123_123_123", ExpectedResult = "X123x123x123")]
86+
[TestCase("123_123_123", ExpectedResult = "123x123x123")]
8487
[TestCase("Hello123_123_123", ExpectedResult = "Hello123x123x123")]
8588
[TestCase("Hello123X123X123", ExpectedResult = "Hello123x123x123")]
8689
public string ConsecutiveNumbers(string input, int longAcronymThreshold = 0) =>
@@ -172,4 +175,18 @@ public void Lowercase_AfterNumber_IsPartOf_NewWord()
172175
Assert.That(nameTransformer.Prettify("MONO16f"), Is.EqualTo("Mono16F"));
173176
}
174177
}
178+
179+
[Test]
180+
public void EmptyInput_IsAllowed()
181+
{
182+
var nameTransformer = new NamePrettifier(4);
183+
nameTransformer.Prettify("");
184+
}
185+
186+
[Test]
187+
public void EmptyOutput_IsAllowed()
188+
{
189+
var nameTransformer = new NamePrettifier(4);
190+
nameTransformer.Prettify("_");
191+
}
175192
}

tests/SilkTouch/SilkTouch/Naming/NameTrimmerTests.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ public void RegressionFragmentShaderColorModMaskATI()
7777
);
7878
var expected = new Dictionary<string, string>
7979
{
80-
{ "GL_2X_BIT_ATI", "X2XBitAti" },
80+
{ "GL_2X_BIT_ATI", "2XBitAti" },
8181
{ "GL_COMP_BIT_ATI", "CompBitAti" },
8282
{ "GL_NEGATE_BIT_ATI", "NegateBitAti" },
8383
{ "GL_BIAS_BIT_ATI", "BiasBitAti" },

0 commit comments

Comments
 (0)