From e9f723006ac6212fb7f1a8645159749e006ff69c Mon Sep 17 00:00:00 2001 From: Hugo Locurcio Date: Mon, 7 Aug 2023 10:37:41 +0200 Subject: [PATCH] Use compile-time Unicode string conversion Thanks to this syntax introduced in C++11, this reduces the amount of work that needs to be performed at run-time while making the code more terse. --- editor/editor_property_name_processor.cpp | 4 ++-- editor/gui/scene_tree_editor.cpp | 2 +- editor/plugins/bit_map_editor_plugin.cpp | 2 +- editor/plugins/canvas_item_editor_plugin.cpp | 8 ++++---- .../plugins/gpu_particles_collision_sdf_editor_plugin.cpp | 4 ++-- editor/plugins/node_3d_editor_plugin.cpp | 2 +- editor/plugins/sprite_frames_editor_plugin.cpp | 2 +- editor/plugins/voxel_gi_editor_plugin.cpp | 4 ++-- editor/script_create_dialog.cpp | 2 +- scene/gui/rich_text_label.h | 2 +- tests/core/string/test_string.h | 6 +++--- 11 files changed, 19 insertions(+), 19 deletions(-) diff --git a/editor/editor_property_name_processor.cpp b/editor/editor_property_name_processor.cpp index fd14136fc7c..a6cd07dd8b9 100644 --- a/editor/editor_property_name_processor.cpp +++ b/editor/editor_property_name_processor.cpp @@ -269,9 +269,9 @@ EditorPropertyNameProcessor::EditorPropertyNameProcessor() { capitalize_string_remaps["uri"] = "URI"; capitalize_string_remaps["url"] = "URL"; capitalize_string_remaps["urls"] = "URLs"; - capitalize_string_remaps["us"] = String::utf8("(µs)"); // Unit. + capitalize_string_remaps["us"] = U"(µs)"; // Unit. capitalize_string_remaps["usb"] = "USB"; - capitalize_string_remaps["usec"] = String::utf8("(µsec)"); // Unit. + capitalize_string_remaps["usec"] = U"(µsec)"; // Unit. capitalize_string_remaps["uuid"] = "UUID"; capitalize_string_remaps["uv"] = "UV"; capitalize_string_remaps["uv1"] = "UV1"; diff --git a/editor/gui/scene_tree_editor.cpp b/editor/gui/scene_tree_editor.cpp index 7ca24d6168a..d07e5dfa1a0 100644 --- a/editor/gui/scene_tree_editor.cpp +++ b/editor/gui/scene_tree_editor.cpp @@ -294,7 +294,7 @@ void SceneTreeEditor::_add_nodes(Node *p_node, TreeItem *p_parent) { } // Improve looks on tooltip, extra spacing on non-bullet point newlines. - const String bullet_point = String::utf8("• "); + const String bullet_point = U"• "; int next_newline = 0; while (next_newline != -1) { next_newline = conf_warning.find("\n", next_newline + 2); diff --git a/editor/plugins/bit_map_editor_plugin.cpp b/editor/plugins/bit_map_editor_plugin.cpp index 3388cab0062..f2423cb803b 100644 --- a/editor/plugins/bit_map_editor_plugin.cpp +++ b/editor/plugins/bit_map_editor_plugin.cpp @@ -37,7 +37,7 @@ void BitMapEditor::setup(const Ref &p_bitmap) { texture_rect->set_texture(ImageTexture::create_from_image(p_bitmap->convert_to_image())); - size_label->set_text(vformat(String::utf8("%s×%s"), p_bitmap->get_size().width, p_bitmap->get_size().height)); + size_label->set_text(vformat(U"%s×%s", p_bitmap->get_size().width, p_bitmap->get_size().height)); } BitMapEditor::BitMapEditor() { diff --git a/editor/plugins/canvas_item_editor_plugin.cpp b/editor/plugins/canvas_item_editor_plugin.cpp index 0386a05cc28..486fbdad3e5 100644 --- a/editor/plugins/canvas_item_editor_plugin.cpp +++ b/editor/plugins/canvas_item_editor_plugin.cpp @@ -3027,8 +3027,8 @@ void CanvasItemEditor::_draw_ruler_tool() { Point2 v_angle_text_pos; v_angle_text_pos.x = CLAMP(begin.x - angle_text_width / 2, angle_text_width / 2, viewport->get_rect().size.x - angle_text_width); v_angle_text_pos.y = begin.y < end.y ? MIN(text_pos2.y - 2 * text_height, begin.y - text_height * 0.5) : MAX(text_pos2.y + text_height * 3, begin.y + text_height * 1.5); - viewport->draw_string_outline(font, v_angle_text_pos, TS->format_number(vformat(String::utf8("%d°"), vertical_angle)), HORIZONTAL_ALIGNMENT_LEFT, -1, font_size, outline_size, outline_color); - viewport->draw_string(font, v_angle_text_pos, TS->format_number(vformat(String::utf8("%d°"), vertical_angle)), HORIZONTAL_ALIGNMENT_LEFT, -1, font_size, font_secondary_color); + viewport->draw_string_outline(font, v_angle_text_pos, TS->format_number(vformat(U"%d°", vertical_angle)), HORIZONTAL_ALIGNMENT_LEFT, -1, font_size, outline_size, outline_color); + viewport->draw_string(font, v_angle_text_pos, TS->format_number(vformat(U"%d°", vertical_angle)), HORIZONTAL_ALIGNMENT_LEFT, -1, font_size, font_secondary_color); text_pos2 = text_pos; text_pos2.y = end.y < text_pos.y ? MIN(text_pos.y - text_height * 2, end.y - text_height / 2) : MAX(text_pos.y + text_height * 2, end.y - text_height / 2); @@ -3050,8 +3050,8 @@ void CanvasItemEditor::_draw_ruler_tool() { h_angle_text_pos.y = MIN(text_pos.y - height_multiplier * text_height, MIN(end.y - text_height * 0.5, text_pos2.y - height_multiplier * text_height)); } } - viewport->draw_string_outline(font, h_angle_text_pos, TS->format_number(vformat(String::utf8("%d°"), horizontal_angle)), HORIZONTAL_ALIGNMENT_LEFT, -1, font_size, outline_size, outline_color); - viewport->draw_string(font, h_angle_text_pos, TS->format_number(vformat(String::utf8("%d°"), horizontal_angle)), HORIZONTAL_ALIGNMENT_LEFT, -1, font_size, font_secondary_color); + viewport->draw_string_outline(font, h_angle_text_pos, TS->format_number(vformat(U"%d°", horizontal_angle)), HORIZONTAL_ALIGNMENT_LEFT, -1, font_size, outline_size, outline_color); + viewport->draw_string(font, h_angle_text_pos, TS->format_number(vformat(U"%d°", horizontal_angle)), HORIZONTAL_ALIGNMENT_LEFT, -1, font_size, font_secondary_color); } if (grid_snap_active) { diff --git a/editor/plugins/gpu_particles_collision_sdf_editor_plugin.cpp b/editor/plugins/gpu_particles_collision_sdf_editor_plugin.cpp index 9582f0caa59..938a541a221 100644 --- a/editor/plugins/gpu_particles_collision_sdf_editor_plugin.cpp +++ b/editor/plugins/gpu_particles_collision_sdf_editor_plugin.cpp @@ -93,8 +93,8 @@ void GPUParticlesCollisionSDF3DEditorPlugin::_notification(int p_what) { } String text; - text += vformat(TTR("Subdivisions: %s"), vformat(String::utf8("%d × %d × %d"), size.x, size.y, size.z)) + "\n"; - text += vformat(TTR("Cell size: %s"), vformat(String::utf8("%.3f × %.3f × %.3f"), extents.x / size.x, extents.y / size.y, extents.z / size.z)) + "\n"; + text += vformat(TTR("Subdivisions: %s"), vformat(U"%d × %d × %d", size.x, size.y, size.z)) + "\n"; + text += vformat(TTR("Cell size: %s"), vformat(U"%.3f × %.3f × %.3f", extents.x / size.x, extents.y / size.y, extents.z / size.z)) + "\n"; text += vformat(TTR("Video RAM size: %s MB (%s)"), String::num(size_mb, 2), size_quality); // Only update the tooltip when needed to avoid constant redrawing. diff --git a/editor/plugins/node_3d_editor_plugin.cpp b/editor/plugins/node_3d_editor_plugin.cpp index 78805324c9d..bed73d02e1b 100644 --- a/editor/plugins/node_3d_editor_plugin.cpp +++ b/editor/plugins/node_3d_editor_plugin.cpp @@ -2822,7 +2822,7 @@ void Node3DEditorViewport::_notification(int p_what) { } if (show_info) { - const String viewport_size = vformat(String::utf8("%d × %d"), viewport->get_size().x, viewport->get_size().y); + const String viewport_size = vformat(U"%d × %d", viewport->get_size().x, viewport->get_size().y); String text; text += vformat(TTR("X: %s\n"), rtos(current_camera->get_position().x).pad_decimals(1)); text += vformat(TTR("Y: %s\n"), rtos(current_camera->get_position().y).pad_decimals(1)); diff --git a/editor/plugins/sprite_frames_editor_plugin.cpp b/editor/plugins/sprite_frames_editor_plugin.cpp index 7b353514578..720cfb59285 100644 --- a/editor/plugins/sprite_frames_editor_plugin.cpp +++ b/editor/plugins/sprite_frames_editor_plugin.cpp @@ -1282,7 +1282,7 @@ void SpriteFramesEditor::_update_library(bool p_skip_selector) { // Frame is often saved as an AtlasTexture subresource within a scene/resource file, // thus its path might be not what the user is looking for. So we're also showing // subsequent source texture paths. - String prefix = String::utf8("┖╴"); + String prefix = U"┖╴"; Ref at = texture; while (at.is_valid() && at->get_atlas().is_valid()) { tooltip += "\n" + prefix + at->get_atlas()->get_path(); diff --git a/editor/plugins/voxel_gi_editor_plugin.cpp b/editor/plugins/voxel_gi_editor_plugin.cpp index 43b133f4b53..af4a027f78a 100644 --- a/editor/plugins/voxel_gi_editor_plugin.cpp +++ b/editor/plugins/voxel_gi_editor_plugin.cpp @@ -119,8 +119,8 @@ void VoxelGIEditorPlugin::_notification(int p_what) { } String text; - text += vformat(TTR("Subdivisions: %s"), vformat(String::utf8("%d × %d × %d"), cell_size.x, cell_size.y, cell_size.z)) + "\n"; - text += vformat(TTR("Cell size: %s"), vformat(String::utf8("%.3f × %.3f × %.3f"), half_size.x / cell_size.x, half_size.y / cell_size.y, half_size.z / cell_size.z)) + "\n"; + text += vformat(TTR("Subdivisions: %s"), vformat(U"%d × %d × %d", cell_size.x, cell_size.y, cell_size.z)) + "\n"; + text += vformat(TTR("Cell size: %s"), vformat(U"%.3f × %.3f × %.3f", half_size.x / cell_size.x, half_size.y / cell_size.y, half_size.z / cell_size.z)) + "\n"; text += vformat(TTR("Video RAM size: %s MB (%s)"), String::num(size_mb, 2), size_quality); // Only update the tooltip when needed to avoid constant redrawing. diff --git a/editor/script_create_dialog.cpp b/editor/script_create_dialog.cpp index a4eabf409ac..058f4c8d914 100644 --- a/editor/script_create_dialog.cpp +++ b/editor/script_create_dialog.cpp @@ -348,7 +348,7 @@ void ScriptCreateDialog::_template_changed(int p_template) { } } // Update template label information. - String template_info = String::utf8("• "); + String template_info = U"• "; template_info += TTR("Template:"); template_info += " " + sinfo.name; if (!sinfo.description.is_empty()) { diff --git a/scene/gui/rich_text_label.h b/scene/gui/rich_text_label.h index 46bf5641bc5..0d0917a9d8c 100644 --- a/scene/gui/rich_text_label.h +++ b/scene/gui/rich_text_label.h @@ -256,7 +256,7 @@ private: ListType list_type = LIST_DOTS; bool capitalize = false; int level = 0; - String bullet = String::utf8("•"); + String bullet = U"•"; ItemList() { type = ITEM_LIST; } }; diff --git a/tests/core/string/test_string.h b/tests/core/string/test_string.h index 93b237788db..fc1e0590fc5 100644 --- a/tests/core/string/test_string.h +++ b/tests/core/string/test_string.h @@ -1372,8 +1372,8 @@ TEST_CASE("[String] Ensuring empty string into parse_utf8 passes empty string") } TEST_CASE("[String] Cyrillic to_lower()") { - String upper = String::utf8("АБВГДЕЁЖЗИЙКЛМНОПРСТУФХЦЧШЩЪЫЬЭЮЯ"); - String lower = String::utf8("абвгдеёжзийклмнопрстуфхцчшщъыьэюя"); + String upper = U"АБВГДЕЁЖЗИЙКЛМНОПРСТУФХЦЧШЩЪЫЬЭЮЯ"; + String lower = U"абвгдеёжзийклмнопрстуфхцчшщъыьэюя"; String test = upper.to_lower(); @@ -1700,7 +1700,7 @@ TEST_CASE("[String] validate_identifier") { String name_with_spaces = "Name with spaces"; CHECK(name_with_spaces.validate_identifier() == "Name_with_spaces"); - String name_with_invalid_chars = String::utf8("Invalid characters:@*#&世界"); + String name_with_invalid_chars = U"Invalid characters:@*#&世界"; CHECK(name_with_invalid_chars.validate_identifier() == "Invalid_characters_______"); }