Skip to content

Commit f330fd4

Browse files
committed
Expose String::camelcase_to_underscore
1 parent 73ec378 commit f330fd4

File tree

4 files changed

+28
-0
lines changed

4 files changed

+28
-0
lines changed

core/variant/variant_call.cpp

+1
Original file line numberDiff line numberDiff line change
@@ -1387,6 +1387,7 @@ static void _register_variant_builtin_methods() {
13871387
bind_method(String, repeat, sarray("count"), varray());
13881388
bind_method(String, insert, sarray("position", "what"), varray());
13891389
bind_method(String, capitalize, sarray(), varray());
1390+
bind_method(String, camelcase_to_underscore, sarray("lowercase"), varray(true));
13901391
bind_method(String, split, sarray("delimiter", "allow_empty", "maxsplit"), varray(true, 0));
13911392
bind_method(String, rsplit, sarray("delimiter", "allow_empty", "maxsplit"), varray(true, 0));
13921393
bind_method(String, split_floats, sarray("delimiter", "allow_empty"), varray(true));

doc/classes/String.xml

+7
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,13 @@
7979
[b]Note:[/b] Unlike the GDScript parser, this method doesn't support the [code]\uXXXX[/code] escape sequence.
8080
</description>
8181
</method>
82+
<method name="camelcase_to_underscore" qualifiers="const">
83+
<return type="String" />
84+
<argument index="0" name="lowercase" type="bool" default="true" />
85+
<description>
86+
Adds underscores before uppercase characters (except for acronyms and the first character of the string). If [code]lowercase[/code] is [code]true[/code], the string will be converted to lowercase. For [code]ToHtml5 ToHTMLElement toHtml to_html TO_HTML A_B-C.D[/code], it will return [code]to_html_5 _to_html_element to_html to_html _to__html _a__b-_c._d[/code].
87+
</description>
88+
</method>
8289
<method name="capitalize" qualifiers="const">
8390
<return type="String" />
8491
<description>

modules/mono/glue/GodotSharp/GodotSharp/Core/StringExtensions.cs

+14
Original file line numberDiff line numberDiff line change
@@ -284,6 +284,20 @@ public static string Capitalize(this string instance)
284284
return cap;
285285
}
286286

287+
/// <summary>
288+
/// Adds underscores before uppercase characters (except for acronyms and the first character
289+
/// of the string). If <c>lowercase</c> is <c>true</c>, the string will be converted to lowercase.
290+
/// For <c>ToHtml5 ToHTMLElement toHtml to_html TO_HTML A_B-C.D</c>, it will return
291+
/// <c>to_html_5 _to_html_element to_html to_html _to__html _a__b-_c._d</c>.
292+
/// </summary>
293+
public static string CamelcaseToUnderscore(this string instance, bool lowercase = true)
294+
{
295+
return godot_icall_String_camelcase_to_underscore(instance, lowercase);
296+
}
297+
298+
[MethodImpl(MethodImplOptions.InternalCall)]
299+
internal extern static string godot_icall_String_camelcase_to_underscore(string str, bool lowercase);
300+
287301
/// <summary>
288302
/// Performs a case-sensitive comparison to another string, return -1 if less, 0 if equal and +1 if greater.
289303
/// </summary>

modules/mono/glue/string_glue.cpp

+6
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,11 @@
3636

3737
#include "../mono_gd/gd_mono_marshal.h"
3838

39+
MonoString *godot_icall_String_camelcase_to_underscore(MonoString *p_str, bool p_lowercase) {
40+
String ret = GDMonoMarshal::mono_string_to_godot(p_str).camelcase_to_underscore(p_lowercase);
41+
return GDMonoMarshal::mono_string_from_godot(ret);
42+
}
43+
3944
MonoArray *godot_icall_String_md5_buffer(MonoString *p_str) {
4045
Vector<uint8_t> ret = GDMonoMarshal::mono_string_to_godot(p_str).md5_buffer();
4146
// TODO Check possible Array/Vector<uint8_t> problem?
@@ -73,6 +78,7 @@ MonoString *godot_icall_String_simplify_path(MonoString *p_str) {
7378
}
7479

7580
void godot_register_string_icalls() {
81+
GDMonoUtils::add_internal_call("Godot.StringExtensions::godot_icall_String_camelcase_to_underscore", godot_icall_String_camelcase_to_underscore);
7682
GDMonoUtils::add_internal_call("Godot.StringExtensions::godot_icall_String_md5_buffer", godot_icall_String_md5_buffer);
7783
GDMonoUtils::add_internal_call("Godot.StringExtensions::godot_icall_String_md5_text", godot_icall_String_md5_text);
7884
GDMonoUtils::add_internal_call("Godot.StringExtensions::godot_icall_String_rfind", godot_icall_String_rfind);

0 commit comments

Comments
 (0)