diff --git a/core/string/ustring.cpp b/core/string/ustring.cpp index 1b3b070592b..6a59942a568 100644 --- a/core/string/ustring.cpp +++ b/core/string/ustring.cpp @@ -5034,6 +5034,14 @@ Vector String::to_utf32_buffer() const { return retval; } +Vector String::to_wchar_buffer() const { +#ifdef WINDOWS_ENABLED + return to_utf16_buffer(); +#else + return to_utf32_buffer(); +#endif +} + #ifdef TOOLS_ENABLED /** * "Tools TRanslate". Performs string replacement for internationalization diff --git a/core/string/ustring.h b/core/string/ustring.h index 1582504c57d..28e3af92c52 100644 --- a/core/string/ustring.h +++ b/core/string/ustring.h @@ -455,6 +455,7 @@ public: Vector to_utf8_buffer() const; Vector to_utf16_buffer() const; Vector to_utf32_buffer() const; + Vector to_wchar_buffer() const; String(const char *p_str); String(const wchar_t *p_str); diff --git a/core/variant/variant_call.cpp b/core/variant/variant_call.cpp index 0c0c8f657a2..ae151588362 100644 --- a/core/variant/variant_call.cpp +++ b/core/variant/variant_call.cpp @@ -700,6 +700,19 @@ struct _VariantCall { return s; } + static String func_PackedByteArray_get_string_from_wchar(PackedByteArray *p_instance) { + String s; + if (p_instance->size() > 0) { + const uint8_t *r = p_instance->ptr(); +#ifdef WINDOWS_ENABLED + s.parse_utf16((const char16_t *)r, floor((double)p_instance->size() / (double)sizeof(char16_t))); +#else + s = String((const char32_t *)r, floor((double)p_instance->size() / (double)sizeof(char32_t))); +#endif + } + return s; + } + static PackedByteArray func_PackedByteArray_compress(PackedByteArray *p_instance, int p_mode) { PackedByteArray compressed; @@ -1721,6 +1734,7 @@ static void _register_variant_builtin_methods() { bind_string_method(to_utf8_buffer, sarray(), varray()); bind_string_method(to_utf16_buffer, sarray(), varray()); bind_string_method(to_utf32_buffer, sarray(), varray()); + bind_string_method(to_wchar_buffer, sarray(), varray()); bind_static_method(String, num_scientific, sarray("number"), varray()); bind_static_method(String, num, sarray("number", "decimals"), varray(-1)); @@ -2258,6 +2272,7 @@ static void _register_variant_builtin_methods() { bind_function(PackedByteArray, get_string_from_utf8, _VariantCall::func_PackedByteArray_get_string_from_utf8, sarray(), varray()); bind_function(PackedByteArray, get_string_from_utf16, _VariantCall::func_PackedByteArray_get_string_from_utf16, sarray(), varray()); bind_function(PackedByteArray, get_string_from_utf32, _VariantCall::func_PackedByteArray_get_string_from_utf32, sarray(), varray()); + bind_function(PackedByteArray, get_string_from_wchar, _VariantCall::func_PackedByteArray_get_string_from_wchar, sarray(), varray()); bind_function(PackedByteArray, hex_encode, _VariantCall::func_PackedByteArray_hex_encode, sarray(), varray()); bind_function(PackedByteArray, compress, _VariantCall::func_PackedByteArray_compress, sarray("compression_mode"), varray(0)); bind_function(PackedByteArray, decompress, _VariantCall::func_PackedByteArray_decompress, sarray("buffer_size", "compression_mode"), varray(0)); diff --git a/doc/classes/PackedByteArray.xml b/doc/classes/PackedByteArray.xml index 081215f6b78..a3f23fa7ae4 100644 --- a/doc/classes/PackedByteArray.xml +++ b/doc/classes/PackedByteArray.xml @@ -328,6 +328,12 @@ Converts UTF-8 encoded array to [String]. Slower than [method get_string_from_ascii] but supports UTF-8 encoded data. Use this function if you are unsure about the source of the data. For user input this function should always be preferred. Returns empty string if source array is not valid UTF-8 string. + + + + Converts wide character ([code]wchar_t[/code], UTF-16 on Windows, UTF-32 on other platforms) encoded array to [String]. Returns empty string if source array is not valid wide string. + + diff --git a/doc/classes/String.xml b/doc/classes/String.xml index d629a31bca1..1f4ffa417d8 100644 --- a/doc/classes/String.xml +++ b/doc/classes/String.xml @@ -946,6 +946,12 @@ Converts the string to a [url=https://en.wikipedia.org/wiki/UTF-8]UTF-8[/url] encoded [PackedByteArray]. This method is slightly slower than [method to_ascii_buffer], but supports all UTF-8 characters. For most cases, prefer using this method. + + + + Converts the string to a [url=https://en.wikipedia.org/wiki/Wide_character]wide character[/url] ([code]wchar_t[/code], UTF-16 on Windows, UTF-32 on other platforms) encoded [PackedByteArray]. + + diff --git a/doc/classes/StringName.xml b/doc/classes/StringName.xml index 192cb1a6c2a..2140c53e209 100644 --- a/doc/classes/StringName.xml +++ b/doc/classes/StringName.xml @@ -853,6 +853,12 @@ Converts the string to a [url=https://en.wikipedia.org/wiki/UTF-8]UTF-8[/url] encoded [PackedByteArray]. This method is slightly slower than [method to_ascii_buffer], but supports all UTF-8 characters. For most cases, prefer using this method. + + + + Converts the string to a [url=https://en.wikipedia.org/wiki/Wide_character]wide character[/url] ([code]wchar_t[/code], UTF-16 on Windows, UTF-32 on other platforms) encoded [PackedByteArray]. + + diff --git a/editor/project_converter_3_to_4.cpp b/editor/project_converter_3_to_4.cpp index e4b6ef23b5e..cae055c6c57 100644 --- a/editor/project_converter_3_to_4.cpp +++ b/editor/project_converter_3_to_4.cpp @@ -1123,7 +1123,7 @@ bool ProjectConverter3To4::test_array_names() { // List of excluded functions from builtin types and global namespace, because currently it is not possible to get list of functions from them. // This will be available when https://github.com/godotengine/godot/pull/49053 or similar will be included into Godot. - static const char *builtin_types_excluded_functions[] = { "dict_to_inst", "inst_to_dict", "bytes_to_var", "bytes_to_var_with_objects", "db_to_linear", "deg_to_rad", "linear_to_db", "rad_to_deg", "randf_range", "snapped", "str_to_var", "var_to_str", "var_to_bytes", "var_to_bytes_with_objects", "move_toward", "uri_encode", "uri_decode", "remove_at", "get_rotation_quaternion", "limit_length", "grow_side", "is_absolute_path", "is_valid_int", "lerp", "to_ascii_buffer", "to_utf8_buffer", "to_utf32_buffer", "snapped", "remap", "rfind", nullptr }; + static const char *builtin_types_excluded_functions[] = { "dict_to_inst", "inst_to_dict", "bytes_to_var", "bytes_to_var_with_objects", "db_to_linear", "deg_to_rad", "linear_to_db", "rad_to_deg", "randf_range", "snapped", "str_to_var", "var_to_str", "var_to_bytes", "var_to_bytes_with_objects", "move_toward", "uri_encode", "uri_decode", "remove_at", "get_rotation_quaternion", "limit_length", "grow_side", "is_absolute_path", "is_valid_int", "lerp", "to_ascii_buffer", "to_utf8_buffer", "to_utf32_buffer", "to_wchar_buffer", "snapped", "remap", "rfind", nullptr }; for (int current_index = 0; builtin_types_excluded_functions[current_index]; current_index++) { all_functions.insert(builtin_types_excluded_functions[current_index]); } diff --git a/editor/renames_map_3_to_4.cpp b/editor/renames_map_3_to_4.cpp index a8c438b25a7..efd9d5f5887 100644 --- a/editor/renames_map_3_to_4.cpp +++ b/editor/renames_map_3_to_4.cpp @@ -595,6 +595,7 @@ const char *RenamesMap3To4::gdscript_function_renames[][2] = { { "find_last", "rfind" }, // Array, String { "to_ascii", "to_ascii_buffer" }, // String { "to_utf8", "to_utf8_buffer" }, // String + { "to_wchar", "to_wchar_buffer" }, // String // @GlobalScope // Remember to add them to the builtin_types_excluded_functions variable, because for now these functions cannot be listed.