summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--binding_generator.py2
-rw-r--r--gdextension/gdextension_interface.h19
-rw-r--r--include/godot_cpp/godot.hpp1
-rw-r--r--src/godot.cpp2
-rw-r--r--src/variant/char_string.cpp4
-rw-r--r--test/project/main.gd3
-rw-r--r--test/src/example.cpp11
-rw-r--r--test/src/example.h1
8 files changed, 43 insertions, 0 deletions
diff --git a/binding_generator.py b/binding_generator.py
index 6e47e7b..0a6580e 100644
--- a/binding_generator.py
+++ b/binding_generator.py
@@ -366,6 +366,7 @@ def generate_builtin_class_header(builtin_api, size, used_classes, fully_used_cl
if class_name == "String":
result.append("#include <godot_cpp/variant/char_string.hpp>")
result.append("#include <godot_cpp/variant/char_utils.hpp>")
+ result.append("#include <godot_cpp/classes/global_constants.hpp>")
if class_name == "PackedStringArray":
result.append("#include <godot_cpp/variant/string.hpp>")
@@ -552,6 +553,7 @@ def generate_builtin_class_header(builtin_api, size, used_classes, fully_used_cl
result.append("\tChar32String utf32() const;")
result.append("\tCharWideString wide_string() const;")
result.append("\tstatic String num_real(double p_num, bool p_trailing = true);")
+ result.append("\tError resize(int p_size);")
if "members" in builtin_api:
for member in builtin_api["members"]:
diff --git a/gdextension/gdextension_interface.h b/gdextension/gdextension_interface.h
index 43931cc..6c05f39 100644
--- a/gdextension/gdextension_interface.h
+++ b/gdextension/gdextension_interface.h
@@ -1526,6 +1526,25 @@ typedef void (*GDExtensionInterfaceStringOperatorPlusEqWcstr)(GDExtensionStringP
*/
typedef void (*GDExtensionInterfaceStringOperatorPlusEqC32str)(GDExtensionStringPtr p_self, const char32_t *p_b);
+/**
+ * @name string_resize
+ * @since 4.2
+ *
+ * Resizes the underlying string data to the given number of characters.
+ *
+ * Space needs to be allocated for the null terminating character ('\0') which
+ * also must be added manually, in order for all string functions to work correctly.
+ *
+ * Warning: This is an error-prone operation - only use it if there's no other
+ * efficient way to accomplish your goal.
+ *
+ * @param p_self A pointer to the String.
+ * @param p_resize The new length for the String.
+ *
+ * @return Error code signifying if the operation successful.
+ */
+typedef GDExtensionInt (*GDExtensionInterfaceStringResize)(GDExtensionStringPtr p_self, GDExtensionInt p_resize);
+
/* INTERFACE: XMLParser Utilities */
/**
diff --git a/include/godot_cpp/godot.hpp b/include/godot_cpp/godot.hpp
index 59aa150..fb89b55 100644
--- a/include/godot_cpp/godot.hpp
+++ b/include/godot_cpp/godot.hpp
@@ -123,6 +123,7 @@ extern "C" GDExtensionInterfaceStringOperatorPlusEqChar gdextension_interface_st
extern "C" GDExtensionInterfaceStringOperatorPlusEqCstr gdextension_interface_string_operator_plus_eq_cstr;
extern "C" GDExtensionInterfaceStringOperatorPlusEqWcstr gdextension_interface_string_operator_plus_eq_wcstr;
extern "C" GDExtensionInterfaceStringOperatorPlusEqC32str gdextension_interface_string_operator_plus_eq_c32str;
+extern "C" GDExtensionInterfaceStringResize gdextension_interface_string_resize;
extern "C" GDExtensionInterfaceXmlParserOpenBuffer gdextension_interface_xml_parser_open_buffer;
extern "C" GDExtensionInterfaceFileAccessStoreBuffer gdextension_interface_file_access_store_buffer;
extern "C" GDExtensionInterfaceFileAccessGetBuffer gdextension_interface_file_access_get_buffer;
diff --git a/src/godot.cpp b/src/godot.cpp
index c85667f..4a92fad 100644
--- a/src/godot.cpp
+++ b/src/godot.cpp
@@ -128,6 +128,7 @@ GDExtensionInterfaceStringOperatorPlusEqChar gdextension_interface_string_operat
GDExtensionInterfaceStringOperatorPlusEqCstr gdextension_interface_string_operator_plus_eq_cstr = nullptr;
GDExtensionInterfaceStringOperatorPlusEqWcstr gdextension_interface_string_operator_plus_eq_wcstr = nullptr;
GDExtensionInterfaceStringOperatorPlusEqC32str gdextension_interface_string_operator_plus_eq_c32str = nullptr;
+GDExtensionInterfaceStringResize gdextension_interface_string_resize = nullptr;
GDExtensionInterfaceXmlParserOpenBuffer gdextension_interface_xml_parser_open_buffer = nullptr;
GDExtensionInterfaceFileAccessStoreBuffer gdextension_interface_file_access_store_buffer = nullptr;
GDExtensionInterfaceFileAccessGetBuffer gdextension_interface_file_access_get_buffer = nullptr;
@@ -311,6 +312,7 @@ GDExtensionBool GDExtensionBinding::init(GDExtensionInterfaceGetProcAddress p_ge
LOAD_PROC_ADDRESS(string_operator_plus_eq_cstr, GDExtensionInterfaceStringOperatorPlusEqCstr);
LOAD_PROC_ADDRESS(string_operator_plus_eq_wcstr, GDExtensionInterfaceStringOperatorPlusEqWcstr);
LOAD_PROC_ADDRESS(string_operator_plus_eq_c32str, GDExtensionInterfaceStringOperatorPlusEqC32str);
+ LOAD_PROC_ADDRESS(string_resize, GDExtensionInterfaceStringResize);
LOAD_PROC_ADDRESS(xml_parser_open_buffer, GDExtensionInterfaceXmlParserOpenBuffer);
LOAD_PROC_ADDRESS(file_access_store_buffer, GDExtensionInterfaceFileAccessStoreBuffer);
LOAD_PROC_ADDRESS(file_access_get_buffer, GDExtensionInterfaceFileAccessGetBuffer);
diff --git a/src/variant/char_string.cpp b/src/variant/char_string.cpp
index 856037c..fc8845e 100644
--- a/src/variant/char_string.cpp
+++ b/src/variant/char_string.cpp
@@ -289,6 +289,10 @@ CharWideString String::wide_string() const {
return str;
}
+Error String::resize(int p_size) {
+ return (Error)internal::gdextension_interface_string_resize(_native_ptr(), p_size);
+}
+
String &String::operator=(const char *p_str) {
*this = String(p_str);
return *this;
diff --git a/test/project/main.gd b/test/project/main.gd
index cedd512..9716bc0 100644
--- a/test/project/main.gd
+++ b/test/project/main.gd
@@ -86,6 +86,9 @@ func _ready():
assert_equal(example.test_string_is_fourty_two("blah"), false)
assert_equal(example.test_string_is_fourty_two("fourty two"), true)
+ # String::resize().
+ assert_equal(example.test_string_resize("What"), "What!?")
+
# PackedArray iterators
assert_equal(example.test_vector_ops(), 105)
diff --git a/test/src/example.cpp b/test/src/example.cpp
index fb47dd8..af3837a 100644
--- a/test/src/example.cpp
+++ b/test/src/example.cpp
@@ -139,6 +139,7 @@ void Example::_bind_methods() {
ClassDB::bind_method(D_METHOD("test_string_ops"), &Example::test_string_ops);
ClassDB::bind_method(D_METHOD("test_str_utility"), &Example::test_str_utility);
ClassDB::bind_method(D_METHOD("test_string_is_fourty_two"), &Example::test_string_is_fourty_two);
+ ClassDB::bind_method(D_METHOD("test_string_resize"), &Example::test_string_resize);
ClassDB::bind_method(D_METHOD("test_vector_ops"), &Example::test_vector_ops);
ClassDB::bind_method(D_METHOD("test_bitfield", "flags"), &Example::test_bitfield);
@@ -304,6 +305,16 @@ bool Example::test_string_is_fourty_two(const String &p_string) const {
return strcmp(p_string.utf8().ptr(), "fourty two") == 0;
}
+String Example::test_string_resize(String p_string) const {
+ int orig_len = p_string.length();
+ p_string.resize(orig_len + 3);
+ char32_t *data = p_string.ptrw();
+ data[orig_len + 0] = '!';
+ data[orig_len + 1] = '?';
+ data[orig_len + 2] = '\0';
+ return p_string;
+}
+
int Example::test_vector_ops() const {
PackedInt32Array arr;
arr.push_back(10);
diff --git a/test/src/example.h b/test/src/example.h
index a84efed..ce64c31 100644
--- a/test/src/example.h
+++ b/test/src/example.h
@@ -118,6 +118,7 @@ public:
String test_string_ops() const;
String test_str_utility() const;
bool test_string_is_fourty_two(const String &p_str) const;
+ String test_string_resize(String p_original) const;
int test_vector_ops() const;
BitField<Flags> test_bitfield(BitField<Flags> flags);