summaryrefslogtreecommitdiff
path: root/modules
diff options
context:
space:
mode:
authorIgnacio Roldán Etcheverry <neikeq@users.noreply.github.com>2022-02-08 22:16:54 +0100
committerGitHub <noreply@github.com>2022-02-08 22:16:54 +0100
commitf05f2dd80fca926a5dc1a58249321b638209f425 (patch)
treed89be88cee4eb1220aaa4496d98319b422bb6160 /modules
parent76ce5c16f351632d40fea9e6ea2bb015cb827aca (diff)
parent6c3b6664b55677e4d86c1728b5e483d861a6fdfe (diff)
Merge pull request #57076 from IgorKordiukiewicz/fix-mono-string-capitalize
String.Capitalize() in C# now matches the behaviour of String::capitalize() in C++
Diffstat (limited to 'modules')
-rw-r--r--modules/mono/glue/GodotSharp/GodotSharp/Core/StringExtensions.cs47
1 files changed, 46 insertions, 1 deletions
diff --git a/modules/mono/glue/GodotSharp/GodotSharp/Core/StringExtensions.cs b/modules/mono/glue/GodotSharp/GodotSharp/Core/StringExtensions.cs
index eba0ea9a79..a1f058ffe5 100644
--- a/modules/mono/glue/GodotSharp/GodotSharp/Core/StringExtensions.cs
+++ b/modules/mono/glue/GodotSharp/GodotSharp/Core/StringExtensions.cs
@@ -266,7 +266,7 @@ namespace Godot
/// <returns>The capitalized string.</returns>
public static string Capitalize(this string instance)
{
- string aux = instance.Replace("_", " ").ToLower();
+ string aux = instance.CamelcaseToUnderscore(true).Replace("_", " ").Trim();
string cap = string.Empty;
for (int i = 0; i < aux.GetSliceCount(" "); i++)
@@ -284,6 +284,51 @@ namespace Godot
return cap;
}
+ private static string CamelcaseToUnderscore(this string instance, bool lowerCase)
+ {
+ string newString = string.Empty;
+ int startIndex = 0;
+
+ for (int i = 1; i < instance.Length; i++)
+ {
+ bool isUpper = char.IsUpper(instance[i]);
+ bool isNumber = char.IsDigit(instance[i]);
+
+ bool areNext2Lower = false;
+ bool isNextLower = false;
+ bool isNextNumber = false;
+ bool wasPrecedentUpper = char.IsUpper(instance[i - 1]);
+ bool wasPrecedentNumber = char.IsDigit(instance[i - 1]);
+
+ if (i + 2 < instance.Length)
+ {
+ areNext2Lower = char.IsLower(instance[i + 1]) && char.IsLower(instance[i + 2]);
+ }
+
+ if (i + 1 < instance.Length)
+ {
+ isNextLower = char.IsLower(instance[i + 1]);
+ isNextNumber = char.IsDigit(instance[i + 1]);
+ }
+
+ bool condA = isUpper && !wasPrecedentUpper && !wasPrecedentNumber;
+ bool condB = wasPrecedentUpper && isUpper && areNext2Lower;
+ bool condC = isNumber && !wasPrecedentNumber;
+ bool canBreakNumberLetter = isNumber && !wasPrecedentNumber && isNextLower;
+ bool canBreakLetterNumber = !isNumber && wasPrecedentNumber && (isNextLower || isNextNumber);
+
+ bool shouldSplit = condA || condB || condC || canBreakNumberLetter || canBreakLetterNumber;
+ if (shouldSplit)
+ {
+ newString += instance.Substring(startIndex, i - startIndex) + "_";
+ startIndex = i;
+ }
+ }
+
+ newString += instance.Substring(startIndex, instance.Length - startIndex);
+ return lowerCase ? newString.ToLower() : newString;
+ }
+
/// <summary>
/// Performs a case-sensitive comparison to another string, return -1 if less, 0 if equal and +1 if greater.
/// </summary>