From 910c67b268a44eb27e57fee8902d7d738009f06a Mon Sep 17 00:00:00 2001 From: Kirill Diduk Date: Sat, 16 Jul 2022 16:31:32 +0200 Subject: [PATCH] [GDNative] Expose `String::join()` to GDNative Core API v1.3 --- core/ustring.cpp | 2 +- core/ustring.h | 2 +- modules/gdnative/gdnative/string.cpp | 17 +++++++++++++++++ modules/gdnative/gdnative_api.json | 8 ++++++++ modules/gdnative/include/gdnative/string.h | 2 ++ 5 files changed, 29 insertions(+), 2 deletions(-) diff --git a/core/ustring.cpp b/core/ustring.cpp index 4afb2883840e..57dc752f7945 100644 --- a/core/ustring.cpp +++ b/core/ustring.cpp @@ -1005,7 +1005,7 @@ Vector String::split_ints_mk(const Vector &p_splitters, bool p_allo return ret; } -String String::join(Vector parts) { +String String::join(const Vector &parts) const { String ret; for (int i = 0; i < parts.size(); ++i) { if (i > 0) { diff --git a/core/ustring.h b/core/ustring.h index 4adfab2b36e7..dee9018dd3b3 100644 --- a/core/ustring.h +++ b/core/ustring.h @@ -274,7 +274,7 @@ class String { Vector split_ints(const String &p_splitter, bool p_allow_empty = true) const; Vector split_ints_mk(const Vector &p_splitters, bool p_allow_empty = true) const; - String join(Vector parts); + String join(const Vector &parts) const; static CharType char_uppercase(CharType p_char); static CharType char_lowercase(CharType p_char); diff --git a/modules/gdnative/gdnative/string.cpp b/modules/gdnative/gdnative/string.cpp index 178b4619dd18..f07d16743a8c 100644 --- a/modules/gdnative/gdnative/string.cpp +++ b/modules/gdnative/gdnative/string.cpp @@ -846,6 +846,23 @@ godot_array GDAPI godot_string_split_spaces(const godot_string *p_self) { return result; } +godot_string GDAPI godot_string_join(const godot_string *p_self, const godot_array *p_parts) { + const String *self = (const String *)p_self; + + const Array *parts_proxy = (const Array *)p_parts; + Vector parts; + parts.resize(parts_proxy->size()); + for (int i = 0; i < parts_proxy->size(); i++) { + parts.write[i] = (*parts_proxy)[i]; + } + + godot_string str; + String *s = (String *)&str; + memnew_placement(s, String); + *s = self->join(parts); + return str; +} + godot_int GDAPI godot_string_get_slice_count(const godot_string *p_self, godot_string p_splitter) { const String *self = (const String *)p_self; String *splitter = (String *)&p_splitter; diff --git a/modules/gdnative/gdnative_api.json b/modules/gdnative/gdnative_api.json index 9f90743f94f5..f94c65147092 100644 --- a/modules/gdnative/gdnative_api.json +++ b/modules/gdnative/gdnative_api.json @@ -137,6 +137,14 @@ "arguments": [ ["godot_pool_color_array *", "p_self"] ] + }, + { + "name": "godot_string_join", + "return_type": "godot_string", + "arguments": [ + ["const godot_string *", "p_self"], + ["const godot_array *", "p_parts"] + ] } ] }, diff --git a/modules/gdnative/include/gdnative/string.h b/modules/gdnative/include/gdnative/string.h index f44f95ae009f..ce8a5e504291 100644 --- a/modules/gdnative/include/gdnative/string.h +++ b/modules/gdnative/include/gdnative/string.h @@ -179,6 +179,8 @@ godot_array GDAPI godot_string_split_ints_mk(const godot_string *p_self, const g godot_array GDAPI godot_string_split_ints_mk_allows_empty(const godot_string *p_self, const godot_array *p_splitters); godot_array GDAPI godot_string_split_spaces(const godot_string *p_self); +godot_string GDAPI godot_string_join(const godot_string *p_self, const godot_array *p_parts); + wchar_t GDAPI godot_string_char_lowercase(wchar_t p_char); wchar_t GDAPI godot_string_char_uppercase(wchar_t p_char); godot_string GDAPI godot_string_to_lower(const godot_string *p_self);