Skip to content

Commit

Permalink
Performance optimization
Browse files Browse the repository at this point in the history
  • Loading branch information
chubrik committed Mar 26, 2023
1 parent 50d15bb commit 21a15df
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 40 deletions.
9 changes: 5 additions & 4 deletions Chubrik.Json/JsonKebabLowerCaseNamingPolicy.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,15 +20,16 @@ public override unsafe string ConvertName(string? name)
var typeMap = Constants.CharTypeMap;
var prevType = CharType.ULine;
char @char;
int charInt;
CharType type;

for (var inputIndex = 0; inputIndex <= lastInputIndex; inputIndex++)
{
@char = name[inputIndex];
charInt = @char = name[inputIndex];

if (@char < 128)
if (charInt < 128)
{
type = typeMap[@char];
type = typeMap[charInt];

switch (type)
{
Expand Down Expand Up @@ -56,7 +57,7 @@ public override unsafe string ConvertName(string? name)
else if (prevType != CharType.ULine)
output[outputIndex++] = '-';

output[outputIndex++] = unchecked((char)(@char + 32));
output[outputIndex++] = unchecked((char)(charInt + 32));
break;

case CharType.ULine:
Expand Down
9 changes: 5 additions & 4 deletions Chubrik.Json/JsonKebabUpperCaseNamingPolicy.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,20 +20,21 @@ public override unsafe string ConvertName(string? name)
var typeMap = Constants.CharTypeMap;
var prevType = CharType.ULine;
char @char;
int charInt;
CharType type;

for (var inputIndex = 0; inputIndex <= lastInputIndex; inputIndex++)
{
@char = name[inputIndex];
charInt = @char = name[inputIndex];

if (@char < 128)
if (charInt < 128)
{
type = typeMap[@char];
type = typeMap[charInt];

switch (type)
{
case CharType.Lower:
output[outputIndex++] = unchecked((char)(@char - 32));
output[outputIndex++] = unchecked((char)(charInt - 32));
break;

case CharType.Upper:
Expand Down
48 changes: 20 additions & 28 deletions Chubrik.Json/JsonSnakeLowerCaseNamingPolicy.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,49 +20,41 @@ public override unsafe string ConvertName(string? name)
var typeMap = Constants.CharTypeMap;
var prevType = CharType.ULine;
char @char;
int charInt;
CharType type;

for (var inputIndex = 0; inputIndex <= lastInputIndex; inputIndex++)
{
@char = name[inputIndex];
charInt = @char = name[inputIndex];

if (@char < 128)
if (charInt < 128)
{
type = typeMap[@char];
type = typeMap[charInt];

switch (type)
if (type == CharType.Upper)
{
case CharType.Lower:
output[outputIndex++] = @char;
break;

case CharType.Upper:

if (prevType == CharType.Upper)
if (prevType == CharType.Upper)
{
if (inputIndex < lastInputIndex)
{
if (inputIndex < lastInputIndex)
{
var nextChar = name[inputIndex + 1];
var nextChar = name[inputIndex + 1];

if (nextChar < 128)
{
if (typeMap[nextChar] == CharType.Lower)
output[outputIndex++] = '_';
}
else if (char.IsLower(nextChar))
if (nextChar < 128)
{
if (typeMap[nextChar] == CharType.Lower)
output[outputIndex++] = '_';
}
else if (char.IsLower(nextChar))
output[outputIndex++] = '_';
}
else if (prevType != CharType.ULine)
output[outputIndex++] = '_';

output[outputIndex++] = unchecked((char)(@char + 32));
break;
}
else if (prevType != CharType.ULine)
output[outputIndex++] = '_';

default:
output[outputIndex++] = @char;
break;
output[outputIndex++] = unchecked((char)(charInt + 32));
}
else
output[outputIndex++] = @char;

prevType = type;
}
Expand Down
9 changes: 5 additions & 4 deletions Chubrik.Json/JsonSnakeUpperCaseNamingPolicy.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,20 +20,21 @@ public override unsafe string ConvertName(string? name)
var typeMap = Constants.CharTypeMap;
var prevType = CharType.ULine;
char @char;
int charInt;
CharType type;

for (var inputIndex = 0; inputIndex <= lastInputIndex; inputIndex++)
{
@char = name[inputIndex];
charInt = @char = name[inputIndex];

if (@char < 128)
if (charInt < 128)
{
type = typeMap[@char];
type = typeMap[charInt];

switch (type)
{
case CharType.Lower:
output[outputIndex++] = unchecked((char)(@char - 32));
output[outputIndex++] = unchecked((char)(charInt - 32));
break;

case CharType.Upper:
Expand Down

0 comments on commit 21a15df

Please sign in to comment.