-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Adjust ToCamelCase Extension #8274
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
{ | ||
var current = chars[i]; | ||
|
||
if (char.IsSeparator(current)) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
if (char.IsSeparator(current)) | |
if (!char.IsLetter(current)) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for the PR 🙂
I suggested a few adjustments.
One suggestion is a minor performance optimization using a stack allocated Span
instead of allocating a new array on the heap.
The other suggestion is a functional one. I think we want to handle e.g. digits the same way as separators so that for instance SHA1
is converted to sha1
instead of shA1
. To save some checks, I reversed the logic to check for char.IsLetter()
(or !char.IsLetter()
) instead of explicitly excluding separators, digits, etc.
Edit:
In this usecase, we don't expect to ever encounter "whitespace" characters as they are invalid to use in .NET field/property-names, but if we want to be even more accurate, we would have to add special handling for separators like e.g. _
. For example Camel_Case
should end up as camelCase
as camel-case does not allow such separators.
if (0 < i && i + 1 < chars.Length) | ||
{ | ||
var next = chars[i + 1]; | ||
if (!char.IsUpper(next) && !char.IsSeparator(next)) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
if (!char.IsUpper(next) && !char.IsSeparator(next)) | |
if (char.IsLetter(next) && !char.IsUpper(next)) |
var camelCase = char.ToLowerInvariant(s[0]).ToString(); | ||
if (s.Length > 1) | ||
camelCase += s.Substring(1); | ||
var chars = s.ToCharArray(); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
var chars = s.ToCharArray(); | |
#if NET6_0_OR_GREATER | |
Span<char> chars = stackalloc char[s.Length]; | |
s.CopyTo(chars); | |
#else | |
var chars = s.ToCharArray(); | |
#endif |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This requires an additional:
#if NET6_0_OR_GREATER
using System;
#endif
Actually, let's just copy the internal static string ToCamelCase3(this string name)
{
if (string.IsNullOrEmpty(name) || !char.IsUpper(name[0]))
{
return name;
}
#if NET
return string.Create(name.Length, name, (chars, name) =>
{
name.CopyTo(chars);
FixCasing(chars);
});
#else
char[] chars = name.ToCharArray();
FixCasing(chars);
return new string(chars);
#endif
}
private static void FixCasing(Span<char> chars)
{
for (var i = 0; i < chars.Length; i++)
{
if (i == 1 && !char.IsUpper(chars[i]))
{
break;
}
var hasNext = (i + 1 < chars.Length);
// Stop when next char is already lowercase.
if (i > 0 && hasNext && !char.IsUpper(chars[i + 1]))
{
// If the next char is a space, lowercase current char before exiting.
if (chars[i + 1] == ' ')
{
chars[i] = char.ToLowerInvariant(chars[i]);
}
break;
}
chars[i] = char.ToLowerInvariant(chars[i]);
}
} |
Wasn't sure about the license. But if we are allowed to use it, it would be the best. Then the properties for sure gonna match. Sad they didn't make theirs classes public. Edit: Do you integrate it, or should I go for a try? |
@cwuethrich I can open a new PR, if you don't mind 🙂 Thanks for creating this one! |
Adjust
ToCamelCase
Extension ofstring
to match the behavior ofSystem.Text.Json
andNewtonsoft.Json
.Bug: #8273