Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use String.repeat() to optimize several String methods #72288

Merged
merged 1 commit into from
May 5, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 1 addition & 7 deletions core/io/file_access_compressed.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,13 +34,7 @@

void FileAccessCompressed::configure(const String &p_magic, Compression::Mode p_mode, uint32_t p_block_size) {
magic = p_magic.ascii().get_data();
if (magic.length() > 4) {
magic = magic.substr(0, 4);
} else {
while (magic.length() < 4) {
magic += " ";
}
}
magic = (magic + " ").substr(0, 4);

cmode = p_mode;
block_size = p_block_size;
Expand Down
8 changes: 1 addition & 7 deletions core/io/json.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -47,13 +47,7 @@ const char *JSON::tk_name[TK_MAX] = {
};

String JSON::_make_indent(const String &p_indent, int p_size) {
String indent_text = "";
if (!p_indent.is_empty()) {
for (int i = 0; i < p_size; i++) {
indent_text += p_indent;
}
}
return indent_text;
return p_indent.repeat(p_size);
}

String JSON::_stringify(const Variant &p_var, const String &p_indent, int p_cur_indent, bool p_sort_keys, HashSet<const void *> &p_markers, bool p_full_precision) {
Expand Down
6 changes: 1 addition & 5 deletions core/math/bvh_debug.inc
Original file line number Diff line number Diff line change
Expand Up @@ -30,11 +30,7 @@ String _debug_aabb_to_string(const BVHABB_CLASS &aabb) const {
void _debug_recursive_print_tree_node(uint32_t p_node_id, int depth = 0) const {
const TNode &tnode = _nodes[p_node_id];

String sz = "";
for (int n = 0; n < depth; n++) {
sz += "\t";
}
sz += itos(p_node_id);
String sz = String("\t").repeat(depth) + itos(p_node_id);

clayjohn marked this conversation as resolved.
Show resolved Hide resolved
if (tnode.is_leaf()) {
sz += " L";
Expand Down
17 changes: 5 additions & 12 deletions core/string/translation.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -909,18 +909,11 @@ String TranslationServer::wrap_with_fakebidi_characters(String &p_message) const
}

String TranslationServer::add_padding(const String &p_message, int p_length) const {
String res;
String prefix = pseudolocalization_prefix;
String suffix;
for (int i = 0; i < p_length * expansion_ratio / 2; i++) {
prefix += "_";
suffix += "_";
}
suffix += pseudolocalization_suffix;
res += prefix;
res += p_message;
res += suffix;
return res;
String underscores = String("_").repeat(p_length * expansion_ratio / 2);
String prefix = pseudolocalization_prefix + underscores;
String suffix = underscores + pseudolocalization_suffix;

return prefix + p_message + suffix;
}

const char32_t *TranslationServer::get_accented_version(char32_t p_character) const {
Expand Down
41 changes: 17 additions & 24 deletions core/string/ustring.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3470,6 +3470,14 @@ String String::replacen(const String &p_key, const String &p_with) const {
String String::repeat(int p_count) const {
ERR_FAIL_COND_V_MSG(p_count < 0, "", "Parameter count should be a positive number.");

if (p_count == 0) {
return "";
}

if (p_count == 1) {
MewPurPur marked this conversation as resolved.
Show resolved Hide resolved
return *this;
}

int len = length();
String new_string = *this;
new_string.resize(p_count * len + 1);
Expand Down Expand Up @@ -4107,13 +4115,11 @@ String String::pad_decimals(int p_digits) const {
}

if (s.length() - (c + 1) > p_digits) {
s = s.substr(0, c + p_digits + 1);
return s.substr(0, c + p_digits + 1);
} else {
while (s.length() - (c + 1) < p_digits) {
s += "0";
}
int zeros_to_add = p_digits - s.length() + (c + 1);
return s + String("0").repeat(zeros_to_add);
}
return s;
}

String String::pad_zeros(int p_digits) const {
Expand All @@ -4138,12 +4144,8 @@ String String::pad_zeros(int p_digits) const {
return s;
}

while (end - begin < p_digits) {
s = s.insert(begin, "0");
end++;
}

return s;
int zeros_to_add = p_digits - (end - begin);
return s.insert(begin, String("0").repeat(zeros_to_add));
}

String String::trim_prefix(const String &p_prefix) const {
Expand Down Expand Up @@ -4322,11 +4324,8 @@ String String::path_to(const String &p_path) const {

common_parent--;

String dir;

for (int i = src_dirs.size() - 1; i > common_parent; i--) {
dir += "../";
}
int dirs_to_backtrack = (src_dirs.size() - 1) - common_parent;
String dir = String("../").repeat(dirs_to_backtrack);

for (int i = common_parent + 1; i < dst_dirs.size(); i++) {
dir += dst_dirs[i] + "/";
Expand Down Expand Up @@ -4547,11 +4546,8 @@ String String::rpad(int min_length, const String &character) const {
String s = *this;
int padding = min_length - s.length();
if (padding > 0) {
for (int i = 0; i < padding; i++) {
s = s + character;
}
s += character.repeat(padding);
}

return s;
}

Expand All @@ -4560,11 +4556,8 @@ String String::lpad(int min_length, const String &character) const {
String s = *this;
int padding = min_length - s.length();
if (padding > 0) {
for (int i = 0; i < padding; i++) {
s = character + s;
}
s = character.repeat(padding) + s;
}

return s;
}

Expand Down
6 changes: 1 addition & 5 deletions editor/code_editor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1138,11 +1138,7 @@ void CodeTextEditor::insert_final_newline() {

void CodeTextEditor::convert_indent_to_spaces() {
int indent_size = EDITOR_GET("text_editor/behavior/indent/size");
String indent = "";

for (int i = 0; i < indent_size; i++) {
indent += " ";
}
String indent = String(" ").repeat(indent_size);

Vector<int> cursor_columns;
cursor_columns.resize(text_editor->get_caret_count());
Expand Down
5 changes: 1 addition & 4 deletions editor/doc_tools.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1353,10 +1353,7 @@ static void _write_string(Ref<FileAccess> f, int p_tablevel, const String &p_str
if (p_string.is_empty()) {
return;
}
String tab;
for (int i = 0; i < p_tablevel; i++) {
tab += "\t";
}
String tab = String("\t").repeat(p_tablevel);
f->store_string(tab + p_string + "\n");
}

Expand Down
6 changes: 3 additions & 3 deletions editor/script_create_dialog.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -894,9 +894,9 @@ ScriptLanguage::ScriptTemplate ScriptCreateDialog::_parse_template(const ScriptL
if (line.begins_with("space-indent")) {
String indent_value = line.substr(17, -1).strip_edges();
if (indent_value.is_valid_int()) {
space_indent = "";
for (int i = 0; i < indent_value.to_int(); i++) {
space_indent += " ";
int indent_size = indent_value.to_int();
if (indent_size >= 0) {
space_indent = String(" ").repeat(indent_size);
}
} else {
WARN_PRINT(vformat("Template meta-use_space_indent need to be a valid integer value. Found %s.", indent_value));
Expand Down
14 changes: 2 additions & 12 deletions modules/gdscript/gdscript_editor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3040,12 +3040,7 @@ String GDScriptLanguage::_get_indentation() const {

if (use_space_indentation) {
int indent_size = EDITOR_GET("text_editor/behavior/indent/size");

String space_indent = "";
for (int i = 0; i < indent_size; i++) {
space_indent += " ";
}
return space_indent;
return String(" ").repeat(indent_size);
}
}
#endif
Expand Down Expand Up @@ -3092,12 +3087,7 @@ void GDScriptLanguage::auto_indent_code(String &p_code, int p_from_line, int p_t
}

if (i >= p_from_line) {
l = "";
for (int j = 0; j < indent_stack.size(); j++) {
l += indent;
}
l += st;

l = indent.repeat(indent_stack.size()) + st;
} else if (i > p_to_line) {
break;
}
Expand Down
6 changes: 3 additions & 3 deletions modules/gltf/gltf_document.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6839,9 +6839,9 @@ PackedByteArray GLTFDocument::_serialize_glb_buffer(Ref<GLTFState> p_state, Erro
const int32_t header_size = 12;
const int32_t chunk_header_size = 8;

for (int32_t pad_i = 0; pad_i < (chunk_header_size + json.utf8().length()) % 4; pad_i++) {
json += " ";
}
int32_t padding = (chunk_header_size + json.utf8().length()) % 4;
json += String(" ").repeat(padding);

CharString cs = json.utf8();
const uint32_t text_chunk_length = cs.length();

Expand Down
7 changes: 1 addition & 6 deletions modules/mono/csharp_script.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -524,12 +524,7 @@ String CSharpLanguage::_get_indentation() const {

if (use_space_indentation) {
int indent_size = EDITOR_GET("text_editor/behavior/indent/size");

String space_indent = "";
for (int i = 0; i < indent_size; i++) {
space_indent += " ";
}
return space_indent;
return String(" ").repeat(indent_size);
}
}
#endif
Expand Down
7 changes: 1 addition & 6 deletions servers/rendering/shader_compiler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -38,12 +38,7 @@
#define SL ShaderLanguage

static String _mktab(int p_level) {
String tb;
for (int i = 0; i < p_level; i++) {
tb += "\t";
}

return tb;
return String("\t").repeat(p_level);
}

static String _typestr(SL::DataType p_type) {
Expand Down