Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
52 changes: 26 additions & 26 deletions src/Argon/NamingStrategy/CamelCaseNamingStrategy.cs
Original file line number Diff line number Diff line change
Expand Up @@ -46,38 +46,38 @@ internal static string ToCamelCase(string s)
return s;
}

var chars = s.ToCharArray();

for (var i = 0; i < chars.Length; i++)
return string.Create(s.Length, s, static (span, source) =>
{
var ch = chars[i];
if (i == 1 && !char.IsUpper(ch))
source.AsSpan().CopyTo(span);
for (var i = 0; i < span.Length; i++)
{
break;
}

var hasNext = i + 1 < chars.Length;
if (i > 0 && hasNext && !char.IsUpper(chars[i + 1]))
{
// if the next character is a space, which is not considered uppercase
// (otherwise we wouldn't be here...)
// we want to ensure that the following:
// 'FOO bar' is rewritten as 'foo bar', and not as 'foO bar'
// The code was written in such a way that the first word in uppercase
// ends when if finds an uppercase letter followed by a lowercase letter.
// now a ' ' (space, (char)32) is considered not upper
// but in that case we still want our current character to become lowercase
if (char.IsSeparator(chars[i + 1]))
var ch = span[i];
if (i == 1 && !char.IsUpper(ch))
{
chars[i] = char.ToLower(ch, InvariantCulture);
break;
}

break;
}
var hasNext = i + 1 < span.Length;
if (i > 0 && hasNext && !char.IsUpper(span[i + 1]))
{
// if the next character is a space, which is not considered uppercase
// (otherwise we wouldn't be here...)
// we want to ensure that the following:
// 'FOO bar' is rewritten as 'foo bar', and not as 'foO bar'
// The code was written in such a way that the first word in uppercase
// ends when if finds an uppercase letter followed by a lowercase letter.
// now a ' ' (space, (char)32) is considered not upper
// but in that case we still want our current character to become lowercase
if (char.IsSeparator(span[i + 1]))
{
span[i] = char.ToLower(ch, InvariantCulture);
}

chars[i] = char.ToLower(ch, InvariantCulture);
}
break;
}

return new(chars);
span[i] = char.ToLower(ch, InvariantCulture);
}
});
}
}
37 changes: 37 additions & 0 deletions src/ArgonTests/Benchmarks/CamelCaseBenchmarks.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
// Copyright (c) 2007 James Newton-King. All rights reserved.
// Use of this source code is governed by The MIT License,
// as found in the license.md file.

using BenchmarkDotNet.Attributes;

[MemoryDiagnoser]
public class CamelCaseBenchmarks
{
static readonly string[] names =
[
"Id",
"Name",
"FirstName",
"LastName",
"EmailAddress",
"PhoneNumber",
"DateOfBirth",
"IsActive",
"CreatedAt",
"UpdatedAt",
"URL",
"HTTPStatusCode",
"XMLDocument",
"SomeVeryLongPropertyNameThatExercisesTheLoop",
"ABC"
];

[Benchmark]
public void Run()
{
for (var i = 0; i < names.Length; i++)
{
CamelCaseNamingStrategy.ToCamelCase(names[i]);
}
}
}
2 changes: 1 addition & 1 deletion src/Benchmark.Tests/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ public static void Main(string[] args)
var attribute = (AssemblyFileVersionAttribute)typeof(JsonConvert).Assembly.GetCustomAttribute(typeof(AssemblyFileVersionAttribute))!;
Console.WriteLine($"Json.NET Version: {attribute.Version}");

var switcher = new BenchmarkSwitcher([typeof(WriteEscapedJavaScriptString), typeof(SerializeJTokenList), typeof(ReadQuotedNumbers), typeof(WriteBase64Benchmark)]);
var switcher = new BenchmarkSwitcher([typeof(WriteEscapedJavaScriptString), typeof(SerializeJTokenList), typeof(CamelCaseBenchmarks), typeof(ReadQuotedNumbers), typeof(WriteBase64Benchmark)]);
if (args.Length == 0)
{
switcher.Run(["*"]);
Expand Down
Loading