diff --git a/core/io/dir_access.cpp b/core/io/dir_access.cpp index 40c1a539588b..9749a2e3e734 100644 --- a/core/io/dir_access.cpp +++ b/core/io/dir_access.cpp @@ -159,11 +159,11 @@ Error DirAccess::make_dir_recursive(String p_dir) { ERR_FAIL_COND_V(pos < 0, ERR_INVALID_PARAMETER); pos = full_dir.find("/", pos + 1); ERR_FAIL_COND_V(pos < 0, ERR_INVALID_PARAMETER); - base = full_dir.substr(0, pos + 1); + base = full_dir.left(pos + 1); } else if (full_dir.begins_with("/")) { base = "/"; } else if (full_dir.contains(":/")) { - base = full_dir.substr(0, full_dir.find(":/") + 2); + base = full_dir.left(full_dir.find(":/") + 2); } else { ERR_FAIL_V(ERR_INVALID_PARAMETER); } diff --git a/core/io/file_access.cpp b/core/io/file_access.cpp index 6026dbf8962f..a6f2288a977f 100644 --- a/core/io/file_access.cpp +++ b/core/io/file_access.cpp @@ -417,7 +417,7 @@ Vector FileAccess::get_csv_line(const String &p_delim) const { } while (qc % 2); // Remove the extraneous newline we've added above. - line = line.substr(0, line.length() - 1); + line = line.left(-1); Vector strings; diff --git a/core/io/file_access_compressed.cpp b/core/io/file_access_compressed.cpp index 0f00bd292cdd..755d293f9c6c 100644 --- a/core/io/file_access_compressed.cpp +++ b/core/io/file_access_compressed.cpp @@ -34,7 +34,7 @@ void FileAccessCompressed::configure(const String &p_magic, Compression::Mode p_mode, uint32_t p_block_size) { magic = p_magic.ascii().get_data(); - magic = (magic + " ").substr(0, 4); + magic = magic.rpad(4, " "); cmode = p_mode; block_size = p_block_size; diff --git a/core/io/http_client.cpp b/core/io/http_client.cpp index 09505ea05da5..0a81b5a99b56 100644 --- a/core/io/http_client.cpp +++ b/core/io/http_client.cpp @@ -94,7 +94,7 @@ String HTTPClient::query_string_from_dict(const Dictionary &p_dict) { } } } - return query.substr(1); + return query.right(-1); } Error HTTPClient::verify_headers(const Vector &p_headers) { @@ -117,8 +117,8 @@ Dictionary HTTPClient::_get_response_headers_as_dictionary() { if (sp == -1) { continue; } - String key = s.substr(0, sp).strip_edges(); - String value = s.substr(sp + 1, s.length()).strip_edges(); + String key = s.left(sp).strip_edges(); + String value = s.substr(sp + 1).strip_edges(); ret[key] = value; } diff --git a/core/string/node_path.cpp b/core/string/node_path.cpp index 32e4564c5e1a..212412a75107 100644 --- a/core/string/node_path.cpp +++ b/core/string/node_path.cpp @@ -393,7 +393,7 @@ NodePath::NodePath(const String &p_path) { } } - path = path.substr(0, subpath_pos); + path = path.left(subpath_pos); } for (int i = (int)absolute; i < path.length(); i++) { diff --git a/core/string/translation.cpp b/core/string/translation.cpp index a443ed308d62..130401f6e149 100644 --- a/core/string/translation.cpp +++ b/core/string/translation.cpp @@ -1018,9 +1018,9 @@ void TranslationServer::_bind_methods() { void TranslationServer::load_translations() { _load_translations("internationalization/locale/translations"); //all - _load_translations("internationalization/locale/translations_" + locale.substr(0, 2)); + _load_translations("internationalization/locale/translations_" + locale.left(2)); - if (locale.substr(0, 2) != locale) { + if (locale.left(2) != locale) { _load_translations("internationalization/locale/translations_" + locale); } } diff --git a/core/string/translation_po.cpp b/core/string/translation_po.cpp index 6b1595174ad5..5f2d8ba0a39a 100644 --- a/core/string/translation_po.cpp +++ b/core/string/translation_po.cpp @@ -172,10 +172,10 @@ void TranslationPO::_cache_plural_tests(const String &p_plural_rule) { return; } - String equi_test = p_plural_rule.substr(0, first_ques_mark).strip_edges(); + String equi_test = p_plural_rule.left(first_ques_mark).strip_edges(); equi_tests.push_back(equi_test); - String after_colon = p_plural_rule.substr(p_plural_rule.find(":") + 1, p_plural_rule.length()); + String after_colon = p_plural_rule.substr(p_plural_rule.find(":") + 1); _cache_plural_tests(after_colon); } diff --git a/core/string/ustring.cpp b/core/string/ustring.cpp index be829502efc1..941dd9112018 100644 --- a/core/string/ustring.cpp +++ b/core/string/ustring.cpp @@ -231,20 +231,20 @@ Error String::parse_url(String &r_scheme, String &r_host, int &r_port, String &r int pos = base.find("://"); // Scheme if (pos != -1) { - r_scheme = base.substr(0, pos + 3).to_lower(); - base = base.substr(pos + 3, base.length() - pos - 3); + r_scheme = base.left(pos + 3).to_lower(); + base = base.substr(pos + 3); } pos = base.find("/"); // Path if (pos != -1) { - r_path = base.substr(pos, base.length() - pos); - base = base.substr(0, pos); + r_path = base.right(-pos); + base = base.left(pos); } // Host pos = base.find("@"); if (pos != -1) { - // Strip credentials - base = base.substr(pos + 1, base.length() - pos - 1); + // Strip credentials. + base = base.substr(pos + 1); } if (base.begins_with("[")) { // Literal IPv6 @@ -253,7 +253,7 @@ Error String::parse_url(String &r_scheme, String &r_host, int &r_port, String &r return ERR_INVALID_PARAMETER; } r_host = base.substr(1, pos - 1); - base = base.substr(pos + 1, base.length() - pos - 1); + base = base.substr(pos + 1); } else { // Anything else if (base.get_slice_count(":") > 2) { @@ -264,8 +264,8 @@ Error String::parse_url(String &r_scheme, String &r_host, int &r_port, String &r r_host = base; base = ""; } else { - r_host = base.substr(0, pos); - base = base.substr(pos, base.length() - pos); + r_host = base.left(pos); + base = base.right(-pos); } } if (r_host.is_empty()) { @@ -1297,9 +1297,9 @@ Vector String::rsplit(const String &p_splitter, bool p_allow_empty, int while (true) { if (remaining_len < p_splitter.length() || (p_maxsplit > 0 && p_maxsplit == ret.size())) { - // no room for another splitter or hit max splits, push what's left and we're done + // No room for another splitter or hit max splits, push what's left and we're done. if (p_allow_empty || remaining_len > 0) { - ret.push_back(substr(0, remaining_len)); + ret.push_back(left(remaining_len)); } break; } @@ -1315,8 +1315,8 @@ Vector String::rsplit(const String &p_splitter, bool p_allow_empty, int } if (left_edge < 0) { - // no more splitters, we're done - ret.push_back(substr(0, remaining_len)); + // No more splitters, we're done. + ret.push_back(left(remaining_len)); break; } @@ -2389,7 +2389,7 @@ int64_t String::to_int(const char *p_str, int p_len) { char c = p_str[i]; if (is_digit(c)) { bool overflow = (integer > INT64_MAX / 10) || (integer == INT64_MAX / 10 && ((sign == 1 && c > '7') || (sign == -1 && c > '8'))); - ERR_FAIL_COND_V_MSG(overflow, sign == 1 ? INT64_MAX : INT64_MIN, "Cannot represent " + String(p_str).substr(0, to) + " as a 64-bit signed integer, since the value is " + (sign == 1 ? "too large." : "too small.")); + ERR_FAIL_COND_V_MSG(overflow, sign == 1 ? INT64_MAX : INT64_MIN, "Cannot represent " + String(p_str).left(to) + " as a 64-bit signed integer, since the value is " + (sign == 1 ? "too large." : "too small.")); integer *= 10; integer += c - '0'; @@ -2420,7 +2420,7 @@ int64_t String::to_int(const wchar_t *p_str, int p_len) { wchar_t c = p_str[i]; if (is_digit(c)) { bool overflow = (integer > INT64_MAX / 10) || (integer == INT64_MAX / 10 && ((sign == 1 && c > '7') || (sign == -1 && c > '8'))); - ERR_FAIL_COND_V_MSG(overflow, sign == 1 ? INT64_MAX : INT64_MIN, "Cannot represent " + String(p_str).substr(0, to) + " as a 64-bit signed integer, since the value is " + (sign == 1 ? "too large." : "too small.")); + ERR_FAIL_COND_V_MSG(overflow, sign == 1 ? INT64_MAX : INT64_MIN, "Cannot represent " + String(p_str).left(to) + " as a 64-bit signed integer, since the value is " + (sign == 1 ? "too large." : "too small.")); integer *= 10; integer += c - '0'; @@ -2949,21 +2949,15 @@ String String::insert(int p_at_pos, const String &p_string) const { return *this; } - if (p_at_pos > length()) { - p_at_pos = length(); + if (p_at_pos >= length()) { + return *this + p_string; } - String pre; - if (p_at_pos > 0) { - pre = substr(0, p_at_pos); + if (p_at_pos == 0) { + return p_string + *this; } - String post; - if (p_at_pos < length()) { - post = substr(p_at_pos, length() - p_at_pos); - } - - return pre + p_string + post; + return left(p_at_pos) + p_string + right(-p_at_pos); } String String::erase(int p_pos, int p_chars) const { @@ -3579,7 +3573,7 @@ String String::replace(const String &p_key, const String &p_with) const { return *this; } - new_string += substr(search_from, length() - search_from); + new_string += right(-search_from); return new_string; } @@ -3603,7 +3597,7 @@ String String::replace(const char *p_key, const char *p_with) const { return *this; } - new_string += substr(search_from, length() - search_from); + new_string += right(-search_from); return new_string; } @@ -3611,7 +3605,7 @@ String String::replace(const char *p_key, const char *p_with) const { String String::replace_first(const String &p_key, const String &p_with) const { int pos = find(p_key); if (pos >= 0) { - return substr(0, pos) + p_with + substr(pos + p_key.length(), length()); + return left(pos) + p_with + substr(pos + p_key.length(), length()); } return *this; @@ -3632,7 +3626,7 @@ String String::replacen(const String &p_key, const String &p_with) const { return *this; } - new_string += substr(search_from, length() - search_from); + new_string += right(-search_from); return new_string; } @@ -3682,7 +3676,7 @@ String String::reverse() const { String String::left(int p_len) const { if (p_len < 0) { - p_len = length() + p_len; + p_len += length(); } if (p_len <= 0) { @@ -3700,7 +3694,7 @@ String String::left(int p_len) const { String String::right(int p_len) const { if (p_len < 0) { - p_len = length() + p_len; + p_len += length(); } if (p_len <= 0) { @@ -3737,7 +3731,7 @@ String String::indent(const String &p_prefix) const { } } if (line_start != length()) { - new_string += p_prefix + substr(line_start); + new_string += p_prefix + right(-line_start); } return new_string; } @@ -3779,7 +3773,7 @@ String String::dedent() const { } if (has_text) { - new_string += substr(indent_stop, length() - indent_stop); + new_string += right(-indent_stop); } return new_string; @@ -3856,11 +3850,7 @@ String String::rstrip(const String &p_chars) const { } } - if (end == len - 1) { - return *this; - } - - return substr(0, end + 1); + return left(end + 1); } bool String::is_network_share_path() const { @@ -3884,19 +3874,19 @@ String String::simplify_path() const { } if (only_chars) { found = true; - drive = s.substr(0, p + 3); + drive = s.left(p + 3); s = s.substr(p + 3); } } if (!found) { if (is_network_share_path()) { // Network path, beginning with // or \\. - drive = s.substr(0, 2); - s = s.substr(2); + drive = s.left(2); + s = s.right(-2); } else if (s.begins_with("/") || s.begins_with("\\")) { // Absolute path. - drive = s.substr(0, 1); - s = s.substr(1); + drive = s.left(1); + s = s.right(-1); } else { // Windows-style drive path, like C:/ or C:\. p = s.find(":/"); @@ -3904,7 +3894,7 @@ String String::simplify_path() const { p = s.find(":\\"); } if (p != -1 && p < s.find("/")) { - drive = s.substr(0, p + 2); + drive = s.left(p + 2); s = s.substr(p + 2); } } @@ -4302,12 +4292,12 @@ String String::pad_decimals(int p_digits) const { c = s.length() - 1; } else { if (p_digits <= 0) { - return s.substr(0, c); + return s.left(c); } } if (s.length() - (c + 1) > p_digits) { - return s.substr(0, c + p_digits + 1); + return s.left(c + p_digits + 1); } else { int zeros_to_add = p_digits - s.length() + (c + 1); return s + String("0").repeat(zeros_to_add); @@ -4344,7 +4334,7 @@ String String::pad_zeros(int p_digits) const { String String::trim_prefix(const String &p_prefix) const { String s = *this; if (s.begins_with(p_prefix)) { - return s.substr(p_prefix.length(), s.length() - p_prefix.length()); + return s.right(-p_prefix.length()); } return s; } @@ -4352,7 +4342,7 @@ String String::trim_prefix(const String &p_prefix) const { String String::trim_suffix(const String &p_suffix) const { String s = *this; if (s.ends_with(p_suffix)) { - return s.substr(0, s.length() - p_suffix.length()); + return s.left(-p_suffix.length()); } return s; } @@ -4371,7 +4361,7 @@ bool String::is_valid_int() const { for (int i = from; i < len; i++) { if (!is_digit(operator[](i))) { - return false; // no start with number plz + return false; // I hereby desire to not start with a number. } } @@ -4495,11 +4485,11 @@ String String::path_to(const String &p_path) const { dst = dst.substr(dst_begin.length(), dst.length()); } - //remove leading and trailing slash and split + // Remove leading and trailing slash and split. Vector src_dirs = src.substr(1, src.length() - 2).split("/"); Vector dst_dirs = dst.substr(1, dst.length() - 2).split("/"); - //find common parent + // Find common parent. int common_parent = 0; while (true) { @@ -4660,8 +4650,8 @@ String String::get_base_dir() const { String rs; String base; if (end != 0) { - rs = substr(end, length()); - base = substr(0, end); + rs = right(-end); + base = left(end); } else { rs = *this; } @@ -4671,7 +4661,7 @@ String String::get_base_dir() const { return base; } - return base + rs.substr(0, sep); + return base + rs.left(sep); } String String::get_file() const { @@ -4680,7 +4670,7 @@ String String::get_file() const { return *this; } - return substr(sep + 1, length()); + return substr(sep + 1); } String String::get_extension() const { @@ -4689,7 +4679,7 @@ String String::get_extension() const { return ""; } - return substr(pos + 1, length()); + return substr(pos + 1); } String String::path_join(const String &p_file) const { @@ -4788,7 +4778,7 @@ String String::get_basename() const { return *this; } - return substr(0, pos); + return left(pos); } String itos(int64_t p_val) { @@ -5201,7 +5191,9 @@ String String::unquote() const { return *this; } - return substr(1, length() - 2); + String s; + s.copy_from_unchecked(&get_data()[1], length() - 2); + return s; } Vector String::to_ascii_buffer() const { diff --git a/editor/code_editor.cpp b/editor/code_editor.cpp index 9a107669001a..6c8f89c4bc1e 100644 --- a/editor/code_editor.cpp +++ b/editor/code_editor.cpp @@ -1118,7 +1118,7 @@ void CodeTextEditor::trim_trailing_whitespace() { break; } } - text_editor->set_line(i, line.substr(0, end)); + text_editor->set_line(i, line.left(end)); } } diff --git a/editor/editor_help.cpp b/editor/editor_help.cpp index 10ab733c2bd5..bd39e94571a1 100644 --- a/editor/editor_help.cpp +++ b/editor/editor_help.cpp @@ -2130,7 +2130,7 @@ static void _add_text_to_rt(const String &p_bbcode, RichTextLabel *p_rt, Control } else if (tag.begins_with("method ") || tag.begins_with("constructor ") || tag.begins_with("operator ") || tag.begins_with("member ") || tag.begins_with("signal ") || tag.begins_with("enum ") || tag.begins_with("constant ") || tag.begins_with("annotation ") || tag.begins_with("theme_item ")) { const int tag_end = tag.find(" "); - const String link_tag = tag.substr(0, tag_end); + const String link_tag = tag.left(tag_end); const String link_target = tag.substr(tag_end + 1, tag.length()).lstrip(" "); // Use monospace font to make clickable references @@ -2299,7 +2299,7 @@ static void _add_text_to_rt(const String &p_bbcode, RichTextLabel *p_rt, Control const String &expr = subtags[i]; int value_pos = expr.find("="); if (value_pos > -1) { - bbcode_options[expr.substr(0, value_pos)] = expr.substr(value_pos + 1).unquote(); + bbcode_options[expr.left(value_pos)] = expr.substr(value_pos + 1).unquote(); } } HashMap::Iterator width_option = bbcode_options.find("width"); diff --git a/editor/editor_settings.cpp b/editor/editor_settings.cpp index 1eaeee97a578..9612df558c76 100644 --- a/editor/editor_settings.cpp +++ b/editor/editor_settings.cpp @@ -1367,7 +1367,7 @@ bool EditorSettings::save_text_editor_theme_as(String p_file) { if (_save_text_editor_theme(p_file)) { // switch to theme is saved in the theme directory list_text_editor_themes(); - String theme_name = p_file.substr(0, p_file.length() - 4).get_file(); + String theme_name = p_file.left(-4).get_file(); if (p_file.get_base_dir() == EditorPaths::get_singleton()->get_text_editor_themes_dir()) { _initial_set("text_editor/theme/color_theme", theme_name); diff --git a/modules/mono/utils/path_utils.cpp b/modules/mono/utils/path_utils.cpp index aa975346756c..6f2346ba66dc 100644 --- a/modules/mono/utils/path_utils.cpp +++ b/modules/mono/utils/path_utils.cpp @@ -213,7 +213,7 @@ String relative_to_impl(const String &p_path, const String &p_relative_to) { String get_drive_letter(const String &p_norm_path) { int idx = p_norm_path.find(":/"); if (idx != -1 && idx < p_norm_path.find("/")) { - return p_norm_path.substr(0, idx + 1); + return p_norm_path.left(idx + 1); } return String(); } diff --git a/scene/gui/code_edit.cpp b/scene/gui/code_edit.cpp index 291d578add17..d6f749404e76 100644 --- a/scene/gui/code_edit.cpp +++ b/scene/gui/code_edit.cpp @@ -145,7 +145,7 @@ void CodeEdit::_notification(int p_what) { for (int j = 0; j < code_completion_options[l].matches.size(); j++) { Pair match_segment = code_completion_options[l].matches[j]; - int match_offset = theme_cache.font->get_string_size(code_completion_options[l].display.substr(0, match_segment.first), HORIZONTAL_ALIGNMENT_LEFT, -1, theme_cache.font_size).width; + int match_offset = theme_cache.font->get_string_size(code_completion_options[l].display.left(match_segment.first), HORIZONTAL_ALIGNMENT_LEFT, -1, theme_cache.font_size).width; int match_len = theme_cache.font->get_string_size(code_completion_options[l].display.substr(match_segment.first, match_segment.second), HORIZONTAL_ALIGNMENT_LEFT, -1, theme_cache.font_size).width; draw_rect(Rect2(match_pos + Point2(match_offset, 0), Size2(match_len, row_height)), theme_cache.code_completion_existing_color); @@ -176,7 +176,7 @@ void CodeEdit::_notification(int p_what) { } Size2 minsize = theme_cache.code_hint_style->get_minimum_size() + Size2(max_width, line_count * font_height + (theme_cache.line_spacing * line_count - 1)); - int offset = theme_cache.font->get_string_size(code_hint_lines[0].substr(0, code_hint_lines[0].find(String::chr(0xFFFF))), HORIZONTAL_ALIGNMENT_LEFT, -1, theme_cache.font_size).x; + int offset = theme_cache.font->get_string_size(code_hint_lines[0].get_slice(String::chr(0xFFFF), 0), HORIZONTAL_ALIGNMENT_LEFT, -1, theme_cache.font_size).x; if (code_hint_xpos == -0xFFFF) { code_hint_xpos = get_caret_draw_pos().x - offset; } @@ -196,8 +196,8 @@ void CodeEdit::_notification(int p_what) { int begin = 0; int end = 0; if (line.contains(String::chr(0xFFFF))) { - begin = theme_cache.font->get_string_size(line.substr(0, line.find(String::chr(0xFFFF))), HORIZONTAL_ALIGNMENT_LEFT, -1, theme_cache.font_size).x; - end = theme_cache.font->get_string_size(line.substr(0, line.rfind(String::chr(0xFFFF))), HORIZONTAL_ALIGNMENT_LEFT, -1, theme_cache.font_size).x; + begin = theme_cache.font->get_string_size(line.left(line.find(String::chr(0xFFFF))), HORIZONTAL_ALIGNMENT_LEFT, -1, theme_cache.font_size).x; + end = theme_cache.font->get_string_size(line.left(line.rfind(String::chr(0xFFFF))), HORIZONTAL_ALIGNMENT_LEFT, -1, theme_cache.font_size).x; } Point2 round_ofs = hint_ofs + theme_cache.code_hint_style->get_offset() + Vector2(0, theme_cache.font->get_ascent(theme_cache.font_size) + font_height * i + yofs); @@ -914,7 +914,7 @@ void CodeEdit::unindent_lines() { String line_text = get_line(i); if (line_text.begins_with("\t")) { - line_text = line_text.substr(1, line_text.length()); + line_text = line_text.right(-1); set_line(i, line_text); removed_characters = 1; @@ -929,7 +929,7 @@ void CodeEdit::unindent_lines() { // Here we remove only enough spaces to align text to nearest full multiple of indentation_size. // In case where selection begins at the start of indentation_size multiple we remove whole indentation level. int spaces_to_remove = _calculate_spaces_till_next_left_indent(get_first_non_whitespace_column(i)); - line_text = line_text.substr(spaces_to_remove, line_text.length()); + line_text = line_text.substr(spaces_to_remove); set_line(i, line_text); removed_characters = spaces_to_remove; @@ -2067,10 +2067,10 @@ String CodeEdit::get_text_for_code_completion() const { String line = get_line(i); if (i == get_caret_line()) { - completion_text += line.substr(0, get_caret_column()); + completion_text += line.left(get_caret_column()); /* Not unicode, represents the caret. */ completion_text += String::chr(0xFFFF); - completion_text += line.substr(get_caret_column(), line.size()); + completion_text += line.substr(get_caret_column()); } else { completion_text += line; } @@ -2262,7 +2262,7 @@ void CodeEdit::confirm_code_completion(bool p_replace) { set_caret_column(get_caret_column(i) - code_completion_base.length(), false, i); // Merge with text. - insert_text_at_caret(insert_text.substr(0, code_completion_base.length()), i); + insert_text_at_caret(insert_text.left(code_completion_base.length()), i); set_caret_column(caret_col, false, i); insert_text_at_caret(insert_text.substr(matching_chars), i); } @@ -2364,10 +2364,10 @@ String CodeEdit::get_text_with_cursor_char(int p_line, int p_column) const { for (int i = 0; i < text_size; i++) { String line_text = get_line(i); if (i == p_line && p_column >= 0 && p_column <= line_text.size()) { - result += line_text.substr(0, p_column); + result += line_text.left(p_column); /* Not unicode, represents the cursor. */ result += String::chr(0xFFFF); - result += line_text.substr(p_column, line_text.size()); + result += line_text.substr(p_column); } else { result += line_text; } diff --git a/scene/gui/file_dialog.cpp b/scene/gui/file_dialog.cpp index d721ee3ec3e8..d54956758714 100644 --- a/scene/gui/file_dialog.cpp +++ b/scene/gui/file_dialog.cpp @@ -800,8 +800,8 @@ void FileDialog::set_current_path(const String &p_path) { if (pos == -1) { set_current_file(p_path); } else { - String path_dir = p_path.substr(0, pos); - String path_file = p_path.substr(pos + 1, p_path.length()); + String path_dir = p_path.left(pos); + String path_file = p_path.substr(pos + 1); set_current_dir(path_dir); set_current_file(path_file); } diff --git a/scene/gui/label.cpp b/scene/gui/label.cpp index 2fbd29b04801..692c551fccbf 100644 --- a/scene/gui/label.cpp +++ b/scene/gui/label.cpp @@ -119,7 +119,7 @@ void Label::_shape() { ERR_FAIL_COND(font.is_null()); String txt = (uppercase) ? TS->string_to_upper(xl_text, language) : xl_text; if (visible_chars >= 0 && visible_chars_behavior == TextServer::VC_CHARS_BEFORE_SHAPING) { - txt = txt.substr(0, visible_chars); + txt = txt.left(visible_chars); } if (dirty) { TS->shaped_text_add_string(text_rid, txt, font->get_rids(), font_size, font->get_opentype_features(), language); diff --git a/scene/gui/line_edit.cpp b/scene/gui/line_edit.cpp index 5ed1a9d5e3ab..fc3dd7ec740c 100644 --- a/scene/gui/line_edit.cpp +++ b/scene/gui/line_edit.cpp @@ -141,7 +141,7 @@ void LineEdit::_backspace(bool p_word, bool p_all_to_left) { if (p_all_to_left) { deselect(); - text = text.substr(0, caret_column); + text = text.left(caret_column); _text_changed(); return; } @@ -177,7 +177,7 @@ void LineEdit::_delete(bool p_word, bool p_all_to_right) { if (p_all_to_right) { deselect(); - text = text.substr(caret_column, text.length() - caret_column); + text = text.substr(caret_column); _shape(); set_caret_column(0); _text_changed(); @@ -1739,11 +1739,11 @@ void LineEdit::insert_text_at_caret(String p_text) { int available_chars = max_length - text.length(); if (p_text.length() > available_chars) { emit_signal(SNAME("text_change_rejected"), p_text.substr(available_chars)); - p_text = p_text.substr(0, available_chars); + p_text = p_text.left(available_chars); } } - String pre = text.substr(0, caret_column); - String post = text.substr(caret_column, text.length() - caret_column); + String pre = text.left(caret_column); + String post = text.substr(caret_column); text = pre + p_text + post; _shape(); TextServer::Direction dir = TS->shaped_text_get_dominant_direction_in_range(text_rid, caret_column, caret_column + p_text.length()); @@ -2295,7 +2295,7 @@ void LineEdit::_shape() { t = secret.repeat(text.length() + ime_text.length()); } else { if (ime_text.length() > 0) { - t = text.substr(0, caret_column) + ime_text + text.substr(caret_column, text.length()); + t = text.left(caret_column) + ime_text + text.substr(caret_column); } else { t = text; } diff --git a/scene/gui/rich_text_label.cpp b/scene/gui/rich_text_label.cpp index dc96f8a59484..9807d9ffa91c 100644 --- a/scene/gui/rich_text_label.cpp +++ b/scene/gui/rich_text_label.cpp @@ -574,7 +574,7 @@ float RichTextLabel::_shape_line(ItemFrame *p_frame, int p_line, const Ref String lang = _find_language(it); String tx = t->text; if (visible_chars_behavior == TextServer::VC_CHARS_BEFORE_SHAPING && visible_characters >= 0 && remaining_characters >= 0) { - tx = tx.substr(0, remaining_characters); + tx = tx.left(remaining_characters); } remaining_characters -= tx.length(); @@ -4041,7 +4041,7 @@ void RichTextLabel::append_text(const String &p_bbcode) { if (brk_end == -1) { //no close, add the rest - txt += p_bbcode.substr(brk_pos, p_bbcode.length() - brk_pos); + txt += p_bbcode.substr(brk_pos); add_text(txt); break; } @@ -4059,7 +4059,7 @@ void RichTextLabel::append_text(const String &p_bbcode) { const String &expr = split_tag_block[i]; int value_pos = expr.find("="); if (value_pos > -1) { - bbcode_options[expr.substr(0, value_pos)] = expr.substr(value_pos + 1).unquote(); + bbcode_options[expr.left(value_pos)] = expr.substr(value_pos + 1).unquote(); } } } else { @@ -4071,11 +4071,11 @@ void RichTextLabel::append_text(const String &p_bbcode) { int main_value_pos = bbcode_name.find("="); if (main_value_pos > -1) { bbcode_value = bbcode_name.substr(main_value_pos + 1); - bbcode_name = bbcode_name.substr(0, main_value_pos); + bbcode_name = bbcode_name.left(main_value_pos); } if (tag.begins_with("/") && tag_stack.size()) { - bool tag_ok = tag_stack.size() && tag_stack.front()->get() == tag.substr(1, tag.length()); + bool tag_ok = tag_stack.size() && tag_stack.front()->get() == tag.right(-1); if (tag_stack.front()->get() == "b") { in_bold = false; @@ -4403,7 +4403,7 @@ void RichTextLabel::append_text(const String &p_bbcode) { pos = brk_end + 1; tag_stack.push_front(tag); } else if (tag.begins_with("lang=")) { - String lang = tag.substr(5, tag.length()).unquote(); + String lang = tag.right(-5).unquote(); push_language(lang); pos = brk_end + 1; tag_stack.push_front("lang"); @@ -4412,7 +4412,7 @@ void RichTextLabel::append_text(const String &p_bbcode) { pos = brk_end + 1; tag_stack.push_front("p"); } else if (tag.begins_with("p ")) { - Vector subtag = tag.substr(2, tag.length()).split(" "); + Vector subtag = tag.right(-2).split(" "); _normalize_subtags(subtag); HorizontalAlignment alignment = HORIZONTAL_ALIGNMENT_LEFT; @@ -4504,17 +4504,17 @@ void RichTextLabel::append_text(const String &p_bbcode) { tag_stack.push_front(tag); } else if (tag.begins_with("url=")) { - String url = tag.substr(4, tag.length()).unquote(); + String url = tag.right(-4).unquote(); push_meta(url); pos = brk_end + 1; tag_stack.push_front("url"); } else if (tag.begins_with("hint=")) { - String description = tag.substr(5, tag.length()).unquote(); + String description = tag.right(-5).unquote(); push_hint(description); pos = brk_end + 1; tag_stack.push_front("hint"); } else if (tag.begins_with("dropcap")) { - Vector subtag = tag.substr(5, tag.length()).split(" "); + Vector subtag = tag.substr(-5).split(" "); _normalize_subtags(subtag); int fs = theme_cache.normal_font_size * 3; @@ -4570,7 +4570,7 @@ void RichTextLabel::append_text(const String &p_bbcode) { } else if (tag.begins_with("img")) { int alignment = INLINE_ALIGNMENT_CENTER; if (tag.begins_with("img=")) { - Vector subtag = tag.substr(4, tag.length()).split(","); + Vector subtag = tag.right(-4).split(","); _normalize_subtags(subtag); if (subtag.size() > 1) { @@ -4638,7 +4638,7 @@ void RichTextLabel::append_text(const String &p_bbcode) { if (sep == -1) { width = bbcode_value.to_int(); } else { - width = bbcode_value.substr(0, sep).to_int(); + width = bbcode_value.left(sep).to_int(); height = bbcode_value.substr(sep + 1).to_int(); } } else { @@ -4675,21 +4675,21 @@ void RichTextLabel::append_text(const String &p_bbcode) { pos = end; tag_stack.push_front(bbcode_name); } else if (tag.begins_with("color=")) { - String color_str = tag.substr(6, tag.length()).unquote(); + String color_str = tag.right(-6).unquote(); Color color = Color::from_string(color_str, theme_cache.default_color); push_color(color); pos = brk_end + 1; tag_stack.push_front("color"); } else if (tag.begins_with("outline_color=")) { - String color_str = tag.substr(14, tag.length()).unquote(); + String color_str = tag.right(-14).unquote(); Color color = Color::from_string(color_str, theme_cache.default_color); push_outline_color(color); pos = brk_end + 1; tag_stack.push_front("outline_color"); } else if (tag.begins_with("font_size=")) { - int fnt_size = tag.substr(10, tag.length()).to_int(); + int fnt_size = tag.right(-10).to_int(); push_font_size(fnt_size); pos = brk_end + 1; tag_stack.push_front("font_size"); @@ -4736,10 +4736,10 @@ void RichTextLabel::append_text(const String &p_bbcode) { } } pos = brk_end + 1; - tag_stack.push_front(tag.substr(0, value_pos)); + tag_stack.push_front(tag.left(value_pos)); } else if (tag.begins_with("font=")) { - String fnt = tag.substr(5, tag.length()).unquote(); + String fnt = tag.right(-5).unquote(); Ref fc = ResourceLoader::load(fnt, "Font"); if (fc.is_valid()) { @@ -4750,7 +4750,7 @@ void RichTextLabel::append_text(const String &p_bbcode) { tag_stack.push_front("font"); } else if (tag.begins_with("font ")) { - Vector subtag = tag.substr(2, tag.length()).split(" "); + Vector subtag = tag.right(-2).split(" "); _normalize_subtags(subtag); Ref font = theme_cache.normal_font; @@ -4848,7 +4848,7 @@ void RichTextLabel::append_text(const String &p_bbcode) { tag_stack.push_front("font"); } else if (tag.begins_with("outline_size=")) { - int fnt_size = tag.substr(13, tag.length()).to_int(); + int fnt_size = tag.right(-13).to_int(); if (fnt_size > 0) { push_outline_size(fnt_size); } @@ -4987,7 +4987,7 @@ void RichTextLabel::append_text(const String &p_bbcode) { tag_stack.push_front("pulse"); set_process_internal(true); } else if (tag.begins_with("bgcolor=")) { - String color_str = tag.substr(8, tag.length()).unquote(); + String color_str = tag.right(-8).unquote(); Color color = Color::from_string(color_str, theme_cache.default_color); push_bgcolor(color); @@ -4995,7 +4995,7 @@ void RichTextLabel::append_text(const String &p_bbcode) { tag_stack.push_front("bgcolor"); } else if (tag.begins_with("fgcolor=")) { - String color_str = tag.substr(8, tag.length()).unquote(); + String color_str = tag.right(-8).unquote(); Color color = Color::from_string(color_str, theme_cache.default_color); push_fgcolor(color); @@ -5424,10 +5424,10 @@ String RichTextLabel::_get_line_text(ItemFrame *p_frame, int p_line, Selection p } } if ((l.from != nullptr) && (p_frame == p_selection.to_frame) && (p_selection.to_item != nullptr) && (p_selection.to_item->index >= l.from->index) && (p_selection.to_item->index < end_idx)) { - txt = txt.substr(0, p_selection.to_char); + txt = txt.left(p_selection.to_char); } if ((l.from != nullptr) && (p_frame == p_selection.from_frame) && (p_selection.from_item != nullptr) && (p_selection.from_item->index >= l.from->index) && (p_selection.from_item->index < end_idx)) { - txt = txt.substr(p_selection.from_char, -1); + txt = txt.substr(p_selection.from_char); } return txt; } @@ -6357,7 +6357,7 @@ Dictionary RichTextLabel::parse_expressions_for_values(Vector p_expressi a.append(Color::html(values[j])); } else if (!nodepath.search(values[j]).is_null()) { if (values[j].begins_with("$")) { - String v = values[j].substr(1, values[j].length()); + String v = values[j].right(-1); a.append(NodePath(v)); } } else if (!boolean.search(values[j]).is_null()) { diff --git a/scene/gui/text_edit.cpp b/scene/gui/text_edit.cpp index 86e726d9dac6..6d6652113423 100644 --- a/scene/gui/text_edit.cpp +++ b/scene/gui/text_edit.cpp @@ -1567,7 +1567,7 @@ void TextEdit::_notification(int p_what) { for (int i = 0; i < carets.size(); i++) { String t; if (get_caret_column(i) >= 0) { - t = text[get_caret_line(i)].substr(0, get_caret_column(i)) + ime_text + text[get_caret_line(i)].substr(get_caret_column(i), text[get_caret_line(i)].length()); + t = text[get_caret_line(i)].insert(get_caret_column(i), ime_text); } else { t = ime_text; } @@ -3565,7 +3565,7 @@ void TextEdit::insert_text_at_caret(const String &p_text, int p_caret) { for (int i = 0; i < carets.size(); i++) { String t; if (get_caret_column(i) >= 0) { - t = text[get_caret_line(i)].substr(0, get_caret_column(i)) + ime_text + text[get_caret_line(i)].substr(get_caret_column(i), text[get_caret_line(i)].length()); + t = text[get_caret_line(i)].insert(get_caret_column(i), ime_text); } else { t = ime_text; } @@ -7782,9 +7782,9 @@ void TextEdit::_base_insert_text(int p_line, int p_char, const String &p_text, i } /* STEP 3: Separate dest string in pre and post text. */ - String postinsert_text = text[p_line].substr(p_char, text[p_line].size()); + String postinsert_text = text[p_line].substr(p_char); - substrings.write[0] = text[p_line].substr(0, p_char) + substrings[0]; + substrings.write[0] = text[p_line].left(p_char) + substrings[0]; substrings.write[substrings.size() - 1] += postinsert_text; Vector bidi_override; @@ -7850,8 +7850,8 @@ void TextEdit::_base_remove_text(int p_from_line, int p_from_column, int p_to_li ERR_FAIL_COND(p_to_line < p_from_line); // 'from > to'. ERR_FAIL_COND(p_to_line == p_from_line && p_to_column < p_from_column); // 'from > to'. - String pre_text = text[p_from_line].substr(0, p_from_column); - String post_text = text[p_to_line].substr(p_to_column, text[p_to_line].length()); + String pre_text = text[p_from_line].left(p_from_column); + String post_text = text[p_to_line].substr(p_to_column); text.remove_range(p_from_line, p_to_line); text.set(p_from_line, pre_text + post_text, structured_text_parser(st_parser, st_args, pre_text + post_text)); diff --git a/scene/main/node.cpp b/scene/main/node.cpp index e730f47607ec..57a57ca9d4c3 100644 --- a/scene/main/node.cpp +++ b/scene/main/node.cpp @@ -1319,7 +1319,7 @@ void Node::_generate_serial_child_name(const Node *p_child, StringName &name) co for (int i = name_string.length() - 1; i >= 0; i--) { char32_t n = name_string[i]; if (is_digit(n)) { - nums = String::chr(name_string[i]) + nums; + nums = String::chr(n) + nums; } else { break; } @@ -1328,9 +1328,9 @@ void Node::_generate_serial_child_name(const Node *p_child, StringName &name) co String nnsep = _get_name_num_separator(); int name_last_index = name_string.length() - nnsep.length() - nums.length(); - // Assign the base name + separator to name if we have numbers preceded by a separator + // Assign the base name + separator to name if we have numbers preceded by a separator. if (nums.length() > 0 && name_string.substr(name_last_index, nnsep.length()) == nnsep) { - name_string = name_string.substr(0, name_last_index + nnsep.length()); + name_string = name_string.left(name_last_index + nnsep.length()); } else { nums = ""; } diff --git a/scene/resources/packed_scene.cpp b/scene/resources/packed_scene.cpp index 1f6e453e88e5..5c11f28b328e 100644 --- a/scene/resources/packed_scene.cpp +++ b/scene/resources/packed_scene.cpp @@ -699,12 +699,12 @@ Error SceneState::_parse_node(Node *p_owner, Node *p_node, int p_parent_idx, Has } else if (E.type == Variant::ARRAY && E.hint == PROPERTY_HINT_TYPE_STRING) { int hint_subtype_separator = E.hint_string.find(":"); if (hint_subtype_separator >= 0) { - String subtype_string = E.hint_string.substr(0, hint_subtype_separator); + String subtype_string = E.hint_string.left(hint_subtype_separator); int slash_pos = subtype_string.find("/"); PropertyHint subtype_hint = PropertyHint::PROPERTY_HINT_NONE; if (slash_pos >= 0) { subtype_hint = PropertyHint(subtype_string.get_slice("/", 1).to_int()); - subtype_string = subtype_string.substr(0, slash_pos); + subtype_string = subtype_string.left(slash_pos); } Variant::Type subtype = Variant::Type(subtype_string.to_int()); diff --git a/servers/rendering/shader_preprocessor.cpp b/servers/rendering/shader_preprocessor.cpp index dbd1374941b2..f3a3bbff1f17 100644 --- a/servers/rendering/shader_preprocessor.cpp +++ b/servers/rendering/shader_preprocessor.cpp @@ -674,7 +674,7 @@ void ShaderPreprocessor::process_include(Tokenizer *p_tokenizer) { break; } } - path = path.substr(0, path.length() - 1); + path = path.left(-1); if (path.is_empty() || !p_tokenizer->consume_empty_line()) { set_error(RTR("Invalid path."), line); @@ -956,7 +956,7 @@ Error ShaderPreprocessor::expand_condition(const String &p_string, int p_line, S String body = state->defines.has(vector_to_string(text)) ? "true" : "false"; String temp = result; - result = result.substr(0, index) + body; + result = result.left(index) + body; index_start = result.length(); if (index_end > 0) { result += temp.substr(index_end); @@ -1074,7 +1074,7 @@ bool ShaderPreprocessor::expand_macros_once(const String &p_line, int p_line_num int arg_index_start = 0; int arg_index = 0; while (find_match(body, arg_name, arg_index, arg_index_start)) { - body = body.substr(0, arg_index) + args[i] + body.substr(arg_index + arg_name.length(), body.length() - (arg_index + arg_name.length())); + body = body.left(arg_index) + args[i] + body.substr(arg_index + arg_name.length()); // Manually reset arg_index_start to where the arg value of the define finishes. // This ensures we don't skip the other args of this macro in the string. arg_index_start = arg_index + args[i].length() + 1; @@ -1083,11 +1083,11 @@ bool ShaderPreprocessor::expand_macros_once(const String &p_line, int p_line_num concatenate_macro_body(body); - result = result.substr(0, index) + " " + body + " " + result.substr(args_end + 1, result.length()); + result = result.left(index) + " " + body + " " + result.substr(args_end + 1); } else { concatenate_macro_body(body); - result = result.substr(0, index) + " " + body + " " + result.substr(index + key.length(), result.length() - (index + key.length())); + result = result.left(index) + " " + body + " " + result.substr(index + key.length()); } r_expanded = result; @@ -1154,7 +1154,7 @@ void ShaderPreprocessor::concatenate_macro_body(String &r_body) { index_start--; } - r_body = r_body.substr(0, index_start) + r_body.substr(index_end, r_body.length() - index_end); + r_body = r_body.left(index_start) + r_body.substr(index_end); index_start = r_body.find("##", index_start); }