Skip to content

Commit 8bc1c1d

Browse files
committed
Implement String::resize()
1 parent c5d8447 commit 8bc1c1d

File tree

8 files changed

+43
-0
lines changed

8 files changed

+43
-0
lines changed

binding_generator.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -366,6 +366,7 @@ def generate_builtin_class_header(builtin_api, size, used_classes, fully_used_cl
366366
if class_name == "String":
367367
result.append("#include <godot_cpp/variant/char_string.hpp>")
368368
result.append("#include <godot_cpp/variant/char_utils.hpp>")
369+
result.append("#include <godot_cpp/classes/global_constants.hpp>")
369370

370371
if class_name == "PackedStringArray":
371372
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
552553
result.append("\tChar32String utf32() const;")
553554
result.append("\tCharWideString wide_string() const;")
554555
result.append("\tstatic String num_real(double p_num, bool p_trailing = true);")
556+
result.append("\tError resize(int p_size);")
555557

556558
if "members" in builtin_api:
557559
for member in builtin_api["members"]:

gdextension/gdextension_interface.h

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1526,6 +1526,25 @@ typedef void (*GDExtensionInterfaceStringOperatorPlusEqWcstr)(GDExtensionStringP
15261526
*/
15271527
typedef void (*GDExtensionInterfaceStringOperatorPlusEqC32str)(GDExtensionStringPtr p_self, const char32_t *p_b);
15281528

1529+
/**
1530+
* @name string_resize
1531+
* @since 4.2
1532+
*
1533+
* Resizes the underlying string data to the given number of characters.
1534+
*
1535+
* Space needs to be allocated for the null terminating character ('\0') which
1536+
* also must be added manually, in order for all string functions to work correctly.
1537+
*
1538+
* Warning: This is an error-prone operation - only use it if there's no other
1539+
* efficient way to accomplish your goal.
1540+
*
1541+
* @param p_self A pointer to the String.
1542+
* @param p_resize The new length for the String.
1543+
*
1544+
* @return Error code signifying if the operation successful.
1545+
*/
1546+
typedef GDExtensionInt (*GDExtensionInterfaceStringResize)(GDExtensionStringPtr p_self, GDExtensionInt p_resize);
1547+
15291548
/* INTERFACE: XMLParser Utilities */
15301549

15311550
/**

include/godot_cpp/godot.hpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,7 @@ extern "C" GDExtensionInterfaceStringOperatorPlusEqChar gdextension_interface_st
123123
extern "C" GDExtensionInterfaceStringOperatorPlusEqCstr gdextension_interface_string_operator_plus_eq_cstr;
124124
extern "C" GDExtensionInterfaceStringOperatorPlusEqWcstr gdextension_interface_string_operator_plus_eq_wcstr;
125125
extern "C" GDExtensionInterfaceStringOperatorPlusEqC32str gdextension_interface_string_operator_plus_eq_c32str;
126+
extern "C" GDExtensionInterfaceStringResize gdextension_interface_string_resize;
126127
extern "C" GDExtensionInterfaceXmlParserOpenBuffer gdextension_interface_xml_parser_open_buffer;
127128
extern "C" GDExtensionInterfaceFileAccessStoreBuffer gdextension_interface_file_access_store_buffer;
128129
extern "C" GDExtensionInterfaceFileAccessGetBuffer gdextension_interface_file_access_get_buffer;

src/godot.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,7 @@ GDExtensionInterfaceStringOperatorPlusEqChar gdextension_interface_string_operat
128128
GDExtensionInterfaceStringOperatorPlusEqCstr gdextension_interface_string_operator_plus_eq_cstr = nullptr;
129129
GDExtensionInterfaceStringOperatorPlusEqWcstr gdextension_interface_string_operator_plus_eq_wcstr = nullptr;
130130
GDExtensionInterfaceStringOperatorPlusEqC32str gdextension_interface_string_operator_plus_eq_c32str = nullptr;
131+
GDExtensionInterfaceStringResize gdextension_interface_string_resize = nullptr;
131132
GDExtensionInterfaceXmlParserOpenBuffer gdextension_interface_xml_parser_open_buffer = nullptr;
132133
GDExtensionInterfaceFileAccessStoreBuffer gdextension_interface_file_access_store_buffer = nullptr;
133134
GDExtensionInterfaceFileAccessGetBuffer gdextension_interface_file_access_get_buffer = nullptr;
@@ -311,6 +312,7 @@ GDExtensionBool GDExtensionBinding::init(GDExtensionInterfaceGetProcAddress p_ge
311312
LOAD_PROC_ADDRESS(string_operator_plus_eq_cstr, GDExtensionInterfaceStringOperatorPlusEqCstr);
312313
LOAD_PROC_ADDRESS(string_operator_plus_eq_wcstr, GDExtensionInterfaceStringOperatorPlusEqWcstr);
313314
LOAD_PROC_ADDRESS(string_operator_plus_eq_c32str, GDExtensionInterfaceStringOperatorPlusEqC32str);
315+
LOAD_PROC_ADDRESS(string_resize, GDExtensionInterfaceStringResize);
314316
LOAD_PROC_ADDRESS(xml_parser_open_buffer, GDExtensionInterfaceXmlParserOpenBuffer);
315317
LOAD_PROC_ADDRESS(file_access_store_buffer, GDExtensionInterfaceFileAccessStoreBuffer);
316318
LOAD_PROC_ADDRESS(file_access_get_buffer, GDExtensionInterfaceFileAccessGetBuffer);

src/variant/char_string.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -289,6 +289,10 @@ CharWideString String::wide_string() const {
289289
return str;
290290
}
291291

292+
Error String::resize(int p_size) {
293+
return (Error)internal::gdextension_interface_string_resize(_native_ptr(), p_size);
294+
}
295+
292296
String &String::operator=(const char *p_str) {
293297
*this = String(p_str);
294298
return *this;

test/project/main.gd

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,9 @@ func _ready():
8686
assert_equal(example.test_string_is_fourty_two("blah"), false)
8787
assert_equal(example.test_string_is_fourty_two("fourty two"), true)
8888

89+
# String::resize().
90+
assert_equal(example.test_string_resize("What"), "What!?")
91+
8992
# PackedArray iterators
9093
assert_equal(example.test_vector_ops(), 105)
9194

test/src/example.cpp

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -139,6 +139,7 @@ void Example::_bind_methods() {
139139
ClassDB::bind_method(D_METHOD("test_string_ops"), &Example::test_string_ops);
140140
ClassDB::bind_method(D_METHOD("test_str_utility"), &Example::test_str_utility);
141141
ClassDB::bind_method(D_METHOD("test_string_is_fourty_two"), &Example::test_string_is_fourty_two);
142+
ClassDB::bind_method(D_METHOD("test_string_resize"), &Example::test_string_resize);
142143
ClassDB::bind_method(D_METHOD("test_vector_ops"), &Example::test_vector_ops);
143144

144145
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 {
304305
return strcmp(p_string.utf8().ptr(), "fourty two") == 0;
305306
}
306307

308+
String Example::test_string_resize(String p_string) const {
309+
int orig_len = p_string.length();
310+
p_string.resize(orig_len + 3);
311+
char32_t *data = p_string.ptrw();
312+
data[orig_len + 0] = '!';
313+
data[orig_len + 1] = '?';
314+
data[orig_len + 2] = '\0';
315+
return p_string;
316+
}
317+
307318
int Example::test_vector_ops() const {
308319
PackedInt32Array arr;
309320
arr.push_back(10);

test/src/example.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,7 @@ class Example : public Control {
118118
String test_string_ops() const;
119119
String test_str_utility() const;
120120
bool test_string_is_fourty_two(const String &p_str) const;
121+
String test_string_resize(String p_original) const;
121122
int test_vector_ops() const;
122123

123124
BitField<Flags> test_bitfield(BitField<Flags> flags);

0 commit comments

Comments
 (0)