Created
April 19, 2021 14:36
-
-
Save Liversage/4a9eb294d7f68d18b7bd81a9667ba6da to your computer and use it in GitHub Desktop.
Camel case string extension
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
internal static class StringExtensions | |
{ | |
[return: NotNullIfNotNull("string")] | |
public static string? ToCamelCase(this string? @string) | |
{ | |
if (@string is null) | |
return null; | |
// "abcDef" => "abcDef" | |
// "AbcDef" => "abcDef" | |
// "ABCDef" => "abcDef" | |
// "ABCDEF" => "abcdef" | |
if (@string.Length == 0 || char.IsLower(@string[0])) | |
return @string; | |
var indexOfFirstLowerAfterUpper = -1; | |
for (var i = 1; i < @string.Length; i += 1) | |
if (char.IsUpper(@string[i - 1]) && char.IsLower(@string[i])) | |
{ | |
indexOfFirstLowerAfterUpper = i; | |
break; | |
} | |
return indexOfFirstLowerAfterUpper switch | |
{ | |
1 => string.Create(@string.Length, (@string, indexOfFirstLowerAfterUpper), StartToLowerSpanAction), | |
>= 2 => string.Create(@string.Length, (@string, indexOfFirstLowerAfterUpper - 1), StartToLowerSpanAction), | |
_ => string.Create(@string.Length, @string, AllToLowerSpanAction) | |
}; | |
static void StartToLowerSpanAction(Span<char> span, (string Source, int Index) tuple) | |
{ | |
var (source, index) = tuple; | |
source.AsSpan(0, index).ToLowerInvariant(span); | |
source.AsSpan(index).CopyTo(span[index..]); | |
} | |
static void AllToLowerSpanAction(Span<char> span, string source) => source.AsSpan().ToLowerInvariant(span); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment