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) {
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;

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 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) {

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 {
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);
if (tnode.is_leaf()) {
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 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 {

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 {
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();
String new_string = *this;
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) {
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 {
@ -4192,12 +4198,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 {
@ -4376,11 +4378,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] + "/";
@ -4669,11 +4668,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;
}
@ -4682,11 +4678,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;
}

View file

@ -1144,11 +1144,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());

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()) {
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");
}

View file

@ -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));

View file

@ -3098,12 +3098,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
@ -3150,12 +3145,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;
}

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 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();

View file

@ -522,12 +522,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

View file

@ -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) {