Skip to content

Commit

Permalink
non regexp string normalization
Browse files Browse the repository at this point in the history
  • Loading branch information
andreaTP committed Mar 21, 2023
1 parent 4e8a6c7 commit 246e55a
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 21 deletions.
57 changes: 43 additions & 14 deletions src/Kiota.Builder/Extensions/StringExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -144,18 +144,13 @@ public static string ReplaceDotsWithSlashInNamespaces(this string? namespaced)
///<summary>
/// Cleanup regex that removes all special characters from ASCII 0-127
///</summary>
private static readonly Regex propertyCleanupRegex = new(@"[""\s!#$%&'()*+,./:;<=>?@\[\]\\^`{}|~-](?<followingLetter>\w)?", RegexOptions.Compiled, Constants.DefaultRegexTimeout);
private static readonly Regex propertyCleanupRegex = new(@"[""\s!#$%&'()*,./:;<=>?@\[\]\\^`{}|~-](?<followingLetter>\w)?", RegexOptions.Compiled, Constants.DefaultRegexTimeout);
private const string CleanupGroupName = "followingLetter";
public static string CleanupSymbolName(this string? original)
{
if (string.IsNullOrEmpty(original)) return string.Empty;

string result = (String)original.Clone();
foreach (var norm in NormalizedTrailingSymbols)
{
if (result.StartsWith(norm.Key, StringComparison.OrdinalIgnoreCase))
result = NormalizeTrailingSymbol(result, norm.Value.Item1, norm.Value.Item2);
}
string result = NormalizeSymbolsBeforeCleanup(original);

result = propertyCleanupRegex.Replace(result,
static x => x.Groups.Keys.Contains(CleanupGroupName) ?
Expand All @@ -168,6 +163,7 @@ public static string CleanupSymbolName(this string? original)
.Select(static x => SpelledOutNumbers[x])
.Aggregate(static (z, y) => z + y));

result = NormalizeSymbolsAfterCleanup(result);
return result;
}
private static readonly Regex NumbersSpellingRegex = new(@"^(?<number>\d+)", RegexOptions.Compiled, Constants.DefaultRegexTimeout);
Expand All @@ -183,16 +179,49 @@ public static string CleanupSymbolName(this string? original)
{'8', "Eight"},
{'9', "Nine"},
};
public static string NormalizeTrailingSymbol(string original, string symbol, string replacement)
/// <summary>
/// Normalizing logic for custom symbols handling before cleanup
/// </summary>
/// <param name="original">The original string</param>
/// <returns></returns>
private static string NormalizeSymbolsBeforeCleanup(string original)
{
var result = (String)original.Clone();
if (result.StartsWith("-", StringComparison.OrdinalIgnoreCase))
{
result = string.Concat("minus_", result.AsSpan(1));
}

if (result.Contains('+', StringComparison.OrdinalIgnoreCase))
{
result = result.Replace("+", "_plus_", StringComparison.OrdinalIgnoreCase);
}

Regex startRegex = new(@"^(?<symbol>" + symbol + ")", RegexOptions.Compiled, Constants.DefaultRegexTimeout);
return startRegex.Replace((String)original.Clone(), replacement + "_");
return result;
}
/// <summary>
/// Normalizing logic for custom symbols handling after cleanup
/// </summary>
/// <param name="original">The original string</param>
/// <returns></returns>
private static string NormalizeSymbolsAfterCleanup(string original)
{
var result = (String)original.Clone();
if (result.EndsWith("minus_"))
{
result = result[..^1];
}
if (result.StartsWith("_plus"))
{
result = result[1..];
}
if (result.EndsWith("plus_"))
{
result = result[..^1];
}

return result;
}
private static readonly Dictionary<string, (string, string)> NormalizedTrailingSymbols = new() {
{"+", ("\\+", "plus")},
{"-", ("\\-", "minus")},
};
/// <summary>
/// Cleanup the XML string
/// </summary>
Expand Down
13 changes: 6 additions & 7 deletions tests/Kiota.Builder.Tests/Extensions/StringExtensionsTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -85,20 +85,19 @@ public void NormalizeNameSpaceName()
Assert.Equal("Toto", "toto".NormalizeNameSpaceName("-"));
Assert.Equal("Microsoft_Graph_Message_Content", "microsoft.Graph.Message.Content".NormalizeNameSpaceName("_"));
}
[Fact]
public void NormalizeTrailingSymbols()
{
Assert.Equal("plus_1", StringExtensions.NormalizeTrailingSymbol("+1", "\\+", "plus"));
Assert.Equal("minus_1", StringExtensions.NormalizeTrailingSymbol("-1", "\\-", "minus"));
}
[InlineData("\" !#$%&'()*+,./:;<=>?@[]\\^`{}|~-", "")]
[InlineData("\" !#$%&'()*+,./:;<=>?@[]\\^`{}|~-", "plus")]
[InlineData("unchanged", "unchanged")]
[InlineData("@odata.changed", "OdataChanged")]
[InlineData("specialLast@", "specialLast")]
[InlineData("kebab-cased", "kebabCased")]
[InlineData("123Spelled", "OneTwoThreeSpelled")]
[InlineData("+1", "plus_1")]
[InlineData("+1+", "plus_1_plus")]
[InlineData("+1+1", "plus_1_plus_1")]
[InlineData("-1", "minus_1")]
[InlineData("-1-", "minus_1")]
[InlineData("-1-1", "minus_11")]
[InlineData("-", "minus")]
[Theory]
public void CleansUpSymbolNames(string input, string expected)
{
Expand Down

0 comments on commit 246e55a

Please sign in to comment.