Skip to content

Commit

Permalink
Merge pull request #1166 from dsnopek/string-resize
Browse files Browse the repository at this point in the history
Implement `String::resize()` in godot-cpp
  • Loading branch information
dsnopek authored Jul 31, 2023
2 parents 845226d + 8bc1c1d commit 5834e16
Show file tree
Hide file tree
Showing 8 changed files with 43 additions and 0 deletions.
2 changes: 2 additions & 0 deletions binding_generator.py
Original file line number Diff line number Diff line change
Expand Up @@ -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>")
Expand Down Expand Up @@ -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"]:
Expand Down
19 changes: 19 additions & 0 deletions gdextension/gdextension_interface.h
Original file line number Diff line number Diff line change
Expand Up @@ -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 */

/**
Expand Down
1 change: 1 addition & 0 deletions include/godot_cpp/godot.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
2 changes: 2 additions & 0 deletions src/godot.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
Expand Down
4 changes: 4 additions & 0 deletions src/variant/char_string.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
3 changes: 3 additions & 0 deletions test/project/main.gd
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down
11 changes: 11 additions & 0 deletions test/src/example.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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);
Expand Down
1 change: 1 addition & 0 deletions test/src/example.h
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,7 @@ class Example : public Control {
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);
Expand Down

0 comments on commit 5834e16

Please sign in to comment.