Merge pull request #72288 from MewPurPur/use-string-repeat

Use `String.repeat()` to optimize several String methods
This commit is contained in:
Clay John 2023-05-05 09:56:48 -07:00 committed by GitHub
commit 610877e326
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
12 changed files with 37 additions and 94 deletions

View file

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

View file

@ -47,13 +47,7 @@ const char *JSON::tk_name[TK_MAX] = {
}; };
String JSON::_make_indent(const String &p_indent, int p_size) { String JSON::_make_indent(const String &p_indent, int p_size) {
String indent_text = ""; return p_indent.repeat(p_size);
if (!p_indent.is_empty()) {
for (int i = 0; i < p_size; i++) {
indent_text += p_indent;
}
}
return indent_text;
} }
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) { 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) {

View file

@ -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 { void _debug_recursive_print_tree_node(uint32_t p_node_id, int depth = 0) const {
const TNode &tnode = _nodes[p_node_id]; const TNode &tnode = _nodes[p_node_id];
String sz = ""; String sz = String("\t").repeat(depth) + itos(p_node_id);
for (int n = 0; n < depth; n++) {
sz += "\t";
}
sz += itos(p_node_id);
if (tnode.is_leaf()) { if (tnode.is_leaf()) {
sz += " L"; sz += " L";

View file

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

View file

@ -3524,6 +3524,14 @@ String String::replacen(const String &p_key, const String &p_with) const {
String String::repeat(int p_count) const { String String::repeat(int p_count) const {
ERR_FAIL_COND_V_MSG(p_count < 0, "", "Parameter count should be a positive number."); ERR_FAIL_COND_V_MSG(p_count < 0, "", "Parameter count should be a positive number.");
if (p_count == 0) {
return "";
}
if (p_count == 1) {
return *this;
}
int len = length(); int len = length();
String new_string = *this; String new_string = *this;
new_string.resize(p_count * len + 1); new_string.resize(p_count * len + 1);
@ -4161,13 +4169,11 @@ String String::pad_decimals(int p_digits) const {
} }
if (s.length() - (c + 1) > p_digits) { if (s.length() - (c + 1) > p_digits) {
s = s.substr(0, c + p_digits + 1); return s.substr(0, c + p_digits + 1);
} else { } else {
while (s.length() - (c + 1) < p_digits) { int zeros_to_add = p_digits - s.length() + (c + 1);
s += "0"; return s + String("0").repeat(zeros_to_add);
}
} }
return s;
} }
String String::pad_zeros(int p_digits) const { String String::pad_zeros(int p_digits) const {
@ -4192,12 +4198,8 @@ String String::pad_zeros(int p_digits) const {
return s; return s;
} }
while (end - begin < p_digits) { int zeros_to_add = p_digits - (end - begin);
s = s.insert(begin, "0"); return s.insert(begin, String("0").repeat(zeros_to_add));
end++;
}
return s;
} }
String String::trim_prefix(const String &p_prefix) const { String String::trim_prefix(const String &p_prefix) const {
@ -4376,11 +4378,8 @@ String String::path_to(const String &p_path) const {
common_parent--; common_parent--;
String dir; int dirs_to_backtrack = (src_dirs.size() - 1) - common_parent;
String dir = String("../").repeat(dirs_to_backtrack);
for (int i = src_dirs.size() - 1; i > common_parent; i--) {
dir += "../";
}
for (int i = common_parent + 1; i < dst_dirs.size(); i++) { for (int i = common_parent + 1; i < dst_dirs.size(); i++) {
dir += dst_dirs[i] + "/"; dir += dst_dirs[i] + "/";
@ -4669,11 +4668,8 @@ String String::rpad(int min_length, const String &character) const {
String s = *this; String s = *this;
int padding = min_length - s.length(); int padding = min_length - s.length();
if (padding > 0) { if (padding > 0) {
for (int i = 0; i < padding; i++) { s += character.repeat(padding);
s = s + character;
}
} }
return s; return s;
} }
@ -4682,11 +4678,8 @@ String String::lpad(int min_length, const String &character) const {
String s = *this; String s = *this;
int padding = min_length - s.length(); int padding = min_length - s.length();
if (padding > 0) { if (padding > 0) {
for (int i = 0; i < padding; i++) { s = character.repeat(padding) + s;
s = character + s;
}
} }
return s; return s;
} }

View file

@ -1144,11 +1144,7 @@ void CodeTextEditor::insert_final_newline() {
void CodeTextEditor::convert_indent_to_spaces() { void CodeTextEditor::convert_indent_to_spaces() {
int indent_size = EDITOR_GET("text_editor/behavior/indent/size"); int indent_size = EDITOR_GET("text_editor/behavior/indent/size");
String indent = ""; String indent = String(" ").repeat(indent_size);
for (int i = 0; i < indent_size; i++) {
indent += " ";
}
Vector<int> cursor_columns; Vector<int> cursor_columns;
cursor_columns.resize(text_editor->get_caret_count()); cursor_columns.resize(text_editor->get_caret_count());

View file

@ -1372,10 +1372,7 @@ static void _write_string(Ref<FileAccess> f, int p_tablevel, const String &p_str
if (p_string.is_empty()) { if (p_string.is_empty()) {
return; return;
} }
String tab; String tab = String("\t").repeat(p_tablevel);
for (int i = 0; i < p_tablevel; i++) {
tab += "\t";
}
f->store_string(tab + p_string + "\n"); f->store_string(tab + p_string + "\n");
} }

View file

@ -894,9 +894,9 @@ ScriptLanguage::ScriptTemplate ScriptCreateDialog::_parse_template(const ScriptL
if (line.begins_with("space-indent")) { if (line.begins_with("space-indent")) {
String indent_value = line.substr(17, -1).strip_edges(); String indent_value = line.substr(17, -1).strip_edges();
if (indent_value.is_valid_int()) { if (indent_value.is_valid_int()) {
space_indent = ""; int indent_size = indent_value.to_int();
for (int i = 0; i < indent_value.to_int(); i++) { if (indent_size >= 0) {
space_indent += " "; space_indent = String(" ").repeat(indent_size);
} }
} else { } else {
WARN_PRINT(vformat("Template meta-use_space_indent need to be a valid integer value. Found %s.", indent_value)); WARN_PRINT(vformat("Template meta-use_space_indent need to be a valid integer value. Found %s.", indent_value));

View file

@ -3098,12 +3098,7 @@ String GDScriptLanguage::_get_indentation() const {
if (use_space_indentation) { if (use_space_indentation) {
int indent_size = EDITOR_GET("text_editor/behavior/indent/size"); int indent_size = EDITOR_GET("text_editor/behavior/indent/size");
return String(" ").repeat(indent_size);
String space_indent = "";
for (int i = 0; i < indent_size; i++) {
space_indent += " ";
}
return space_indent;
} }
} }
#endif #endif
@ -3150,12 +3145,7 @@ void GDScriptLanguage::auto_indent_code(String &p_code, int p_from_line, int p_t
} }
if (i >= p_from_line) { if (i >= p_from_line) {
l = ""; l = indent.repeat(indent_stack.size()) + st;
for (int j = 0; j < indent_stack.size(); j++) {
l += indent;
}
l += st;
} else if (i > p_to_line) { } else if (i > p_to_line) {
break; break;
} }

View file

@ -7190,9 +7190,9 @@ PackedByteArray GLTFDocument::_serialize_glb_buffer(Ref<GLTFState> p_state, Erro
const int32_t header_size = 12; const int32_t header_size = 12;
const int32_t chunk_header_size = 8; const int32_t chunk_header_size = 8;
for (int32_t pad_i = 0; pad_i < (chunk_header_size + json.utf8().length()) % 4; pad_i++) { int32_t padding = (chunk_header_size + json.utf8().length()) % 4;
json += " "; json += String(" ").repeat(padding);
}
CharString cs = json.utf8(); CharString cs = json.utf8();
const uint32_t text_chunk_length = cs.length(); const uint32_t text_chunk_length = cs.length();

View file

@ -522,12 +522,7 @@ String CSharpLanguage::_get_indentation() const {
if (use_space_indentation) { if (use_space_indentation) {
int indent_size = EDITOR_GET("text_editor/behavior/indent/size"); int indent_size = EDITOR_GET("text_editor/behavior/indent/size");
return String(" ").repeat(indent_size);
String space_indent = "";
for (int i = 0; i < indent_size; i++) {
space_indent += " ";
}
return space_indent;
} }
} }
#endif #endif

View file

@ -38,12 +38,7 @@
#define SL ShaderLanguage #define SL ShaderLanguage
static String _mktab(int p_level) { static String _mktab(int p_level) {
String tb; return String("\t").repeat(p_level);
for (int i = 0; i < p_level; i++) {
tb += "\t";
}
return tb;
} }
static String _typestr(SL::DataType p_type) { static String _typestr(SL::DataType p_type) {