Implement `String::resize()`

pull/1166/head
David Snopek 2023-07-07 08:26:55 -05:00
parent c5d8447861
commit 8bc1c1dbeb
8 changed files with 43 additions and 0 deletions

View File

@ -366,6 +366,7 @@ def generate_builtin_class_header(builtin_api, size, used_classes, fully_used_cl
if class_name == "String": if class_name == "String":
result.append("#include <godot_cpp/variant/char_string.hpp>") result.append("#include <godot_cpp/variant/char_string.hpp>")
result.append("#include <godot_cpp/variant/char_utils.hpp>") result.append("#include <godot_cpp/variant/char_utils.hpp>")
result.append("#include <godot_cpp/classes/global_constants.hpp>")
if class_name == "PackedStringArray": if class_name == "PackedStringArray":
result.append("#include <godot_cpp/variant/string.hpp>") 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("\tChar32String utf32() const;")
result.append("\tCharWideString wide_string() const;") result.append("\tCharWideString wide_string() const;")
result.append("\tstatic String num_real(double p_num, bool p_trailing = true);") 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: if "members" in builtin_api:
for member in builtin_api["members"]: for member in builtin_api["members"]:

View File

@ -1526,6 +1526,25 @@ typedef void (*GDExtensionInterfaceStringOperatorPlusEqWcstr)(GDExtensionStringP
*/ */
typedef void (*GDExtensionInterfaceStringOperatorPlusEqC32str)(GDExtensionStringPtr p_self, const char32_t *p_b); 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 */ /* INTERFACE: XMLParser Utilities */
/** /**

View File

@ -123,6 +123,7 @@ extern "C" GDExtensionInterfaceStringOperatorPlusEqChar gdextension_interface_st
extern "C" GDExtensionInterfaceStringOperatorPlusEqCstr gdextension_interface_string_operator_plus_eq_cstr; extern "C" GDExtensionInterfaceStringOperatorPlusEqCstr gdextension_interface_string_operator_plus_eq_cstr;
extern "C" GDExtensionInterfaceStringOperatorPlusEqWcstr gdextension_interface_string_operator_plus_eq_wcstr; extern "C" GDExtensionInterfaceStringOperatorPlusEqWcstr gdextension_interface_string_operator_plus_eq_wcstr;
extern "C" GDExtensionInterfaceStringOperatorPlusEqC32str gdextension_interface_string_operator_plus_eq_c32str; 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" GDExtensionInterfaceXmlParserOpenBuffer gdextension_interface_xml_parser_open_buffer;
extern "C" GDExtensionInterfaceFileAccessStoreBuffer gdextension_interface_file_access_store_buffer; extern "C" GDExtensionInterfaceFileAccessStoreBuffer gdextension_interface_file_access_store_buffer;
extern "C" GDExtensionInterfaceFileAccessGetBuffer gdextension_interface_file_access_get_buffer; extern "C" GDExtensionInterfaceFileAccessGetBuffer gdextension_interface_file_access_get_buffer;

View File

@ -128,6 +128,7 @@ GDExtensionInterfaceStringOperatorPlusEqChar gdextension_interface_string_operat
GDExtensionInterfaceStringOperatorPlusEqCstr gdextension_interface_string_operator_plus_eq_cstr = nullptr; GDExtensionInterfaceStringOperatorPlusEqCstr gdextension_interface_string_operator_plus_eq_cstr = nullptr;
GDExtensionInterfaceStringOperatorPlusEqWcstr gdextension_interface_string_operator_plus_eq_wcstr = nullptr; GDExtensionInterfaceStringOperatorPlusEqWcstr gdextension_interface_string_operator_plus_eq_wcstr = nullptr;
GDExtensionInterfaceStringOperatorPlusEqC32str gdextension_interface_string_operator_plus_eq_c32str = nullptr; GDExtensionInterfaceStringOperatorPlusEqC32str gdextension_interface_string_operator_plus_eq_c32str = nullptr;
GDExtensionInterfaceStringResize gdextension_interface_string_resize = nullptr;
GDExtensionInterfaceXmlParserOpenBuffer gdextension_interface_xml_parser_open_buffer = nullptr; GDExtensionInterfaceXmlParserOpenBuffer gdextension_interface_xml_parser_open_buffer = nullptr;
GDExtensionInterfaceFileAccessStoreBuffer gdextension_interface_file_access_store_buffer = nullptr; GDExtensionInterfaceFileAccessStoreBuffer gdextension_interface_file_access_store_buffer = nullptr;
GDExtensionInterfaceFileAccessGetBuffer gdextension_interface_file_access_get_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_cstr, GDExtensionInterfaceStringOperatorPlusEqCstr);
LOAD_PROC_ADDRESS(string_operator_plus_eq_wcstr, GDExtensionInterfaceStringOperatorPlusEqWcstr); LOAD_PROC_ADDRESS(string_operator_plus_eq_wcstr, GDExtensionInterfaceStringOperatorPlusEqWcstr);
LOAD_PROC_ADDRESS(string_operator_plus_eq_c32str, GDExtensionInterfaceStringOperatorPlusEqC32str); 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(xml_parser_open_buffer, GDExtensionInterfaceXmlParserOpenBuffer);
LOAD_PROC_ADDRESS(file_access_store_buffer, GDExtensionInterfaceFileAccessStoreBuffer); LOAD_PROC_ADDRESS(file_access_store_buffer, GDExtensionInterfaceFileAccessStoreBuffer);
LOAD_PROC_ADDRESS(file_access_get_buffer, GDExtensionInterfaceFileAccessGetBuffer); LOAD_PROC_ADDRESS(file_access_get_buffer, GDExtensionInterfaceFileAccessGetBuffer);

View File

@ -289,6 +289,10 @@ CharWideString String::wide_string() const {
return str; 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) { String &String::operator=(const char *p_str) {
*this = String(p_str); *this = String(p_str);
return *this; return *this;

View File

@ -86,6 +86,9 @@ func _ready():
assert_equal(example.test_string_is_fourty_two("blah"), false) assert_equal(example.test_string_is_fourty_two("blah"), false)
assert_equal(example.test_string_is_fourty_two("fourty two"), true) assert_equal(example.test_string_is_fourty_two("fourty two"), true)
# String::resize().
assert_equal(example.test_string_resize("What"), "What!?")
# PackedArray iterators # PackedArray iterators
assert_equal(example.test_vector_ops(), 105) assert_equal(example.test_vector_ops(), 105)

View File

@ -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_string_ops"), &Example::test_string_ops);
ClassDB::bind_method(D_METHOD("test_str_utility"), &Example::test_str_utility); 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_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_vector_ops"), &Example::test_vector_ops);
ClassDB::bind_method(D_METHOD("test_bitfield", "flags"), &Example::test_bitfield); 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; 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 { int Example::test_vector_ops() const {
PackedInt32Array arr; PackedInt32Array arr;
arr.push_back(10); arr.push_back(10);

View File

@ -118,6 +118,7 @@ public:
String test_string_ops() const; String test_string_ops() const;
String test_str_utility() const; String test_str_utility() const;
bool test_string_is_fourty_two(const String &p_str) 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; int test_vector_ops() const;
BitField<Flags> test_bitfield(BitField<Flags> flags); BitField<Flags> test_bitfield(BitField<Flags> flags);