Skip to content

Commit

Permalink
Second fix for SnakeCase
Browse files Browse the repository at this point in the history
This update accommodates `XDimension`
  • Loading branch information
tbm0115 committed Apr 11, 2023
1 parent 09e5a0c commit ba7632d
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 22 deletions.
34 changes: 13 additions & 21 deletions MtconnectTranspiler.Sinks.CSharp/Models/ScribanHelperMethods.cs
Original file line number Diff line number Diff line change
Expand Up @@ -176,36 +176,28 @@ public static string ToSummary(string markdown)
/// <returns><c>"The Quick Brown Fox"</c> => <c>"the_quick_brown_fox"</c></returns>
public static string ToSnakeCase(string input)
{
if (string.IsNullOrEmpty(input))
{
return input;
}
if (string.IsNullOrEmpty(input)) return input;

var result = new StringBuilder();
var lastCharWasUpper = false;
var lastCharWasLetterOrDigit = false;
var sb = new StringBuilder();
sb.Append(char.ToLower(input[0]));

for (int i = 0; i < input.Length; i++)
for (var i = 1; i < input.Length; i++)
{
char c = input[i];
if (char.IsUpper(c))
if (char.IsUpper(input[i]))
{
if (i > 0 && lastCharWasLetterOrDigit && !lastCharWasUpper)
if (i > 1 && !char.IsUpper(input[i - 1]))
{
result.Append('_');
sb.Append("_");
}
else if (i < input.Length - 1 && !char.IsUpper(input[i + 1]))
{
sb.Append("_");
}
result.Append(char.ToLower(c));
lastCharWasUpper = true;
}
else
{
result.Append(c);
lastCharWasUpper = false;
}
lastCharWasLetterOrDigit = char.IsLetterOrDigit(c);
sb.Append(char.ToLower(input[i]));
}

return result.ToString();
return sb.ToString();
}

/// <inheritdoc cref="ToSnakeCase(string)" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
<TargetFramework>netstandard2.0</TargetFramework>
<GeneratePackageOnBuild>True</GeneratePackageOnBuild>
<Title>MTConnect Transpiler Sink for C#</Title>
<Version>1.0.8</Version>
<Version>1.0.9</Version>
<Authors>mtconnect, tbm0115</Authors>
<Company>MTConnect Institute; TAMS;</Company>
<Description>An implementation of `ITranspilerSink` from the `MtconnectTranspiler` library. This libary makes it possible to transpile the MTConnect Standard SysML model into C# code.</Description>
Expand Down

0 comments on commit ba7632d

Please sign in to comment.