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

Clean up some uses of String::substr #102427

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
4 changes: 2 additions & 2 deletions core/config/project_settings.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -208,7 +208,7 @@ String ProjectSettings::localize_path(const String &p_path) const {
if (plocal[plocal.length() - 1] == '/') {
sep += 1;
}
return plocal + path.substr(sep, path.size() - sep);
return plocal + path.substr(sep);
}
}

Expand Down Expand Up @@ -1147,7 +1147,7 @@ Error ProjectSettings::save_custom(const String &p_path, const CustomMap &p_cust
category = "";
} else {
category = category.substr(0, div);
name = name.substr(div + 1, name.size());
name = name.substr(div + 1);
}
save_props[category].push_back(name);
}
Expand Down
2 changes: 1 addition & 1 deletion core/debugger/engine_debugger.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,7 @@ void EngineDebugger::initialize(const String &p_uri, bool p_skip_breakpoints, co
int sp = bp.rfind_char(':');
ERR_CONTINUE_MSG(sp == -1, vformat("Invalid breakpoint: '%s', expected file:line format.", bp));

singleton_script_debugger->insert_breakpoint(bp.substr(sp + 1, bp.length()).to_int(), bp.substr(0, sp));
singleton_script_debugger->insert_breakpoint(bp.substr(sp + 1).to_int(), bp.substr(0, sp));
}

allow_focus_steal_fn = p_allow_focus_steal_fn;
Expand Down
4 changes: 2 additions & 2 deletions core/doc_data.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ void DocData::return_doc_from_retinfo(DocData::MethodDoc &p_method, const Proper
} else if (p_retinfo.type == Variant::INT && p_retinfo.usage & (PROPERTY_USAGE_CLASS_IS_ENUM | PROPERTY_USAGE_CLASS_IS_BITFIELD)) {
p_method.return_enum = p_retinfo.class_name;
if (p_method.return_enum.begins_with("_")) { //proxy class
p_method.return_enum = p_method.return_enum.substr(1, p_method.return_enum.length());
p_method.return_enum = p_method.return_enum.substr(1);
}
p_method.return_is_bitfield = p_retinfo.usage & PROPERTY_USAGE_CLASS_IS_BITFIELD;
p_method.return_type = "int";
Expand Down Expand Up @@ -85,7 +85,7 @@ void DocData::argument_doc_from_arginfo(DocData::ArgumentDoc &p_argument, const
} else if (p_arginfo.type == Variant::INT && p_arginfo.usage & (PROPERTY_USAGE_CLASS_IS_ENUM | PROPERTY_USAGE_CLASS_IS_BITFIELD)) {
p_argument.enumeration = p_arginfo.class_name;
if (p_argument.enumeration.begins_with("_")) { //proxy class
p_argument.enumeration = p_argument.enumeration.substr(1, p_argument.enumeration.length());
p_argument.enumeration = p_argument.enumeration.substr(1);
}
p_argument.is_bitfield = p_arginfo.usage & PROPERTY_USAGE_CLASS_IS_BITFIELD;
p_argument.type = "int";
Expand Down
8 changes: 4 additions & 4 deletions core/extension/extension_api_dump.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1420,25 +1420,25 @@ static bool compare_dict_array(const Dictionary &p_old_api, const Dictionary &p_
bool optional = field.begins_with("*");
if (optional) {
// This is an optional field, but if exists it has to exist in both.
field = field.substr(1, field.length());
field = field.substr(1);
}

bool added = field.begins_with("+");
if (added) {
// Meaning this field must either exist or contents may not exist.
field = field.substr(1, field.length());
field = field.substr(1);
}

bool enum_values = field.begins_with("$");
if (enum_values) {
// Meaning this field is a list of enum values.
field = field.substr(1, field.length());
field = field.substr(1);
}

bool allow_name_change = field.begins_with("@");
if (allow_name_change) {
// Meaning that when structurally comparing the old and new value, the dictionary entry 'name' may change.
field = field.substr(1, field.length());
field = field.substr(1);
}

Variant old_value;
Expand Down
2 changes: 1 addition & 1 deletion core/input/input.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -219,7 +219,7 @@ void Input::get_argument_options(const StringName &p_function, int p_idx, List<S
continue;
}

String name = pi.name.substr(pi.name.find_char('/') + 1, pi.name.length());
String name = pi.name.substr(pi.name.find_char('/') + 1);
r_options->push_back(name.quote());
}
}
Expand Down
4 changes: 2 additions & 2 deletions core/input/input_map.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ void InputMap::get_argument_options(const StringName &p_function, int p_idx, Lis
continue;
}

String name = pi.name.substr(pi.name.find_char('/') + 1, pi.name.length());
String name = pi.name.substr(pi.name.find_char('/') + 1);
r_options->push_back(name.quote());
}
}
Expand Down Expand Up @@ -304,7 +304,7 @@ void InputMap::load_from_project_settings() {
continue;
}

String name = pi.name.substr(pi.name.find_char('/') + 1, pi.name.length());
String name = pi.name.substr(pi.name.find_char('/') + 1);

Dictionary action = GLOBAL_GET(pi.name);
float deadzone = action.has("deadzone") ? (float)action["deadzone"] : DEFAULT_DEADZONE;
Expand Down
2 changes: 1 addition & 1 deletion core/io/http_client.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ Dictionary HTTPClient::_get_response_headers_as_dictionary() {
continue;
}
String key = s.substr(0, sp).strip_edges();
String value = s.substr(sp + 1, s.length()).strip_edges();
String value = s.substr(sp + 1).strip_edges();
ret[key] = value;
}

Expand Down
8 changes: 4 additions & 4 deletions core/io/http_client_tcp.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -50,13 +50,13 @@ Error HTTPClientTCP::connect_to_host(const String &p_host, int p_port, Ref<TLSOp

String host_lower = conn_host.to_lower();
if (host_lower.begins_with("http://")) {
conn_host = conn_host.substr(7, conn_host.length() - 7);
conn_host = conn_host.substr(7);
tls_options.unref();
} else if (host_lower.begins_with("https://")) {
if (tls_options.is_null()) {
tls_options = TLSOptions::client();
}
conn_host = conn_host.substr(8, conn_host.length() - 8);
conn_host = conn_host.substr(8);
}

ERR_FAIL_COND_V(tls_options.is_valid() && tls_options->is_server(), ERR_INVALID_PARAMETER);
Expand Down Expand Up @@ -508,11 +508,11 @@ Error HTTPClientTCP::poll() {
continue;
}
if (s.begins_with("content-length:")) {
body_size = s.substr(s.find_char(':') + 1, s.length()).strip_edges().to_int();
body_size = s.substr(s.find_char(':') + 1).strip_edges().to_int();
body_left = body_size;

} else if (s.begins_with("transfer-encoding:")) {
String encoding = header.substr(header.find_char(':') + 1, header.length()).strip_edges();
String encoding = header.substr(header.find_char(':') + 1).strip_edges();
if (encoding == "chunked") {
chunked = true;
}
Expand Down
2 changes: 1 addition & 1 deletion core/io/ip_address.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,7 @@ void IPAddress::_parse_ipv6(const String &p_string) {
void IPAddress::_parse_ipv4(const String &p_string, int p_start, uint8_t *p_ret) {
String ip;
if (p_start != 0) {
ip = p_string.substr(p_start, p_string.length() - p_start);
ip = p_string.substr(p_start);
} else {
ip = p_string;
}
Expand Down
14 changes: 7 additions & 7 deletions core/io/translation_loader_po.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -189,7 +189,7 @@ Ref<Resource> TranslationLoaderPO::load_translation(Ref<FileAccess> f, Error *r_
}
}
msg_context = "";
l = l.substr(7, l.length()).strip_edges();
l = l.substr(7).strip_edges();
status = STATUS_READING_CONTEXT;
entered_context = true;
}
Expand All @@ -202,7 +202,7 @@ Ref<Resource> TranslationLoaderPO::load_translation(Ref<FileAccess> f, Error *r_
}
// We don't record the message in "msgid_plural" itself as tr_n(), TTRN(), RTRN() interfaces provide the plural string already.
// We just have to reset variables related to plurals for "msgstr[]" later on.
l = l.substr(12, l.length()).strip_edges();
l = l.substr(12).strip_edges();
plural_index = -1;
msgs_plural.clear();
msgs_plural.resize(plural_forms);
Expand Down Expand Up @@ -230,7 +230,7 @@ Ref<Resource> TranslationLoaderPO::load_translation(Ref<FileAccess> f, Error *r_
}
}

l = l.substr(5, l.length()).strip_edges();
l = l.substr(5).strip_edges();
status = STATUS_READING_ID;
// If we did not encounter msgctxt, we reset context to empty to reset it.
if (!entered_context) {
Expand All @@ -246,10 +246,10 @@ Ref<Resource> TranslationLoaderPO::load_translation(Ref<FileAccess> f, Error *r_
if (l.begins_with("msgstr[")) {
ERR_FAIL_COND_V_MSG(status != STATUS_READING_PLURAL, Ref<Resource>(), vformat("Unexpected 'msgstr[]', was expecting 'msgid_plural' before 'msgstr[]' while parsing: %s:%d.", path, line));
plural_index++; // Increment to add to the next slot in vector msgs_plural.
l = l.substr(9, l.length()).strip_edges();
l = l.substr(9).strip_edges();
} else if (l.begins_with("msgstr")) {
ERR_FAIL_COND_V_MSG(status != STATUS_READING_ID, Ref<Resource>(), vformat("Unexpected 'msgstr', was expecting 'msgid' before 'msgstr' while parsing: %s:%d.", path, line));
l = l.substr(6, l.length()).strip_edges();
l = l.substr(6).strip_edges();
status = STATUS_READING_STRING;
}

Expand All @@ -263,7 +263,7 @@ Ref<Resource> TranslationLoaderPO::load_translation(Ref<FileAccess> f, Error *r_

ERR_FAIL_COND_V_MSG(!l.begins_with("\"") || status == STATUS_NONE, Ref<Resource>(), vformat("Invalid line '%s' while parsing: %s:%d.", l, path, line));

l = l.substr(1, l.length());
l = l.substr(1);
// Find final quote, ignoring escaped ones (\").
// The escape_next logic is necessary to properly parse things like \\"
// where the backslash is the one being escaped, not the quote.
Expand Down Expand Up @@ -329,7 +329,7 @@ Ref<Resource> TranslationLoaderPO::load_translation(Ref<FileAccess> f, Error *r_
continue;
}
String prop = c.substr(0, p).strip_edges();
String value = c.substr(p + 1, c.length()).strip_edges();
String value = c.substr(p + 1).strip_edges();

if (prop == "X-Language" || prop == "Language") {
translation->set_locale(value);
Expand Down
2 changes: 1 addition & 1 deletion core/string/print_string.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ void __print_line_rich(const String &p_string) {
int brk_end = p_string.find_char(']', brk_pos + 1);

if (brk_end == -1) {
txt += p_string.substr(brk_pos, p_string.length() - brk_pos);
txt += p_string.substr(brk_pos);
output += txt;
break;
}
Expand Down
20 changes: 10 additions & 10 deletions core/string/ustring.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -238,7 +238,7 @@ Error String::parse_url(String &r_scheme, String &r_host, int &r_port, String &r
}
if (is_scheme_valid) {
r_scheme = base.substr(0, pos + 3).to_lower();
base = base.substr(pos + 3, base.length() - pos - 3);
base = base.substr(pos + 3);
}
}
pos = base.find_char('#');
Expand All @@ -250,14 +250,14 @@ Error String::parse_url(String &r_scheme, String &r_host, int &r_port, String &r
pos = base.find_char('/');
// Path
if (pos != -1) {
r_path = base.substr(pos, base.length() - pos);
r_path = base.substr(pos);
base = base.substr(0, pos);
}
// Host
pos = base.find_char('@');
if (pos != -1) {
// Strip credentials
base = base.substr(pos + 1, base.length() - pos - 1);
base = base.substr(pos + 1);
}
if (base.begins_with("[")) {
// Literal IPv6
Expand All @@ -266,7 +266,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) {
Expand All @@ -278,7 +278,7 @@ Error String::parse_url(String &r_scheme, String &r_host, int &r_port, String &r
base = "";
} else {
r_host = base.substr(0, pos);
base = base.substr(pos, base.length() - pos);
base = base.substr(pos);
}
}
if (r_host.is_empty()) {
Expand All @@ -287,7 +287,7 @@ Error String::parse_url(String &r_scheme, String &r_host, int &r_port, String &r
r_host = r_host.to_lower();
// Port
if (base.begins_with(":")) {
base = base.substr(1, base.length() - 1);
base = base.substr(1);
if (!base.is_valid_int()) {
return ERR_INVALID_PARAMETER;
}
Expand Down Expand Up @@ -4861,7 +4861,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.substr(p_prefix.length());
}
return s;
}
Expand All @@ -4870,7 +4870,7 @@ String String::trim_prefix(const char *p_prefix) const {
String s = *this;
if (s.begins_with(p_prefix)) {
int prefix_length = strlen(p_prefix);
return s.substr(prefix_length, s.length() - prefix_length);
return s.substr(prefix_length);
}
return s;
}
Expand Down Expand Up @@ -5025,8 +5025,8 @@ String String::path_to(const String &p_path) const {
return p_path; //impossible to do this
}

src = src.substr(src_begin.length(), src.length());
dst = dst.substr(dst_begin.length(), dst.length());
src = src.substr(src_begin.length());
dst = dst.substr(dst_begin.length());
}

//remove leading and trailing slash and split
Expand Down
2 changes: 1 addition & 1 deletion core/variant/variant_utility.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1678,7 +1678,7 @@ template <typename T>
static void register_utility_function(const String &p_name, const Vector<String> &argnames) {
String name = p_name;
if (name.begins_with("_")) {
name = name.substr(1, name.length() - 1);
name = name.substr(1);
}
StringName sname = name;
ERR_FAIL_COND(utility_function_table.has(sname));
Expand Down
2 changes: 1 addition & 1 deletion drivers/unix/dir_access_unix.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -383,7 +383,7 @@ String DirAccessUnix::get_current_dir(bool p_include_drive) const {
if (!base.is_empty()) {
String bd = current_dir.replace_first(base, "");
if (bd.begins_with("/")) {
return _get_root_string() + bd.substr(1, bd.length());
return _get_root_string() + bd.substr(1);
} else {
return _get_root_string() + bd;
}
Expand Down
2 changes: 1 addition & 1 deletion drivers/windows/dir_access_windows.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -220,7 +220,7 @@ String DirAccessWindows::get_current_dir(bool p_include_drive) const {
if (!base.is_empty()) {
String bd = cdir.replace_first(base, "");
if (bd.begins_with("/")) {
return _get_root_string() + bd.substr(1, bd.length());
return _get_root_string() + bd.substr(1);
} else {
return _get_root_string() + bd;
}
Expand Down
4 changes: 2 additions & 2 deletions editor/animation_track_editor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4662,7 +4662,7 @@ AnimationTrackEditor::TrackIndices AnimationTrackEditor::_confirm_insert(InsertD
for (int i = 0; i < subindices.size(); i++) {
InsertData id = p_id;
id.type = Animation::TYPE_BEZIER;
id.value = subindices[i].is_empty() ? p_id.value : p_id.value.get(subindices[i].substr(1, subindices[i].length()));
id.value = subindices[i].is_empty() ? p_id.value : p_id.value.get(subindices[i].substr(1));
id.path = String(p_id.path) + subindices[i];
p_next_tracks = _confirm_insert(id, p_next_tracks, p_reset_wanted, p_reset_anim, false);
}
Expand Down Expand Up @@ -6515,7 +6515,7 @@ void AnimationTrackEditor::_edit_menu_pressed(int p_option) {
text = path;
int sep = text.find_char(':');
if (sep != -1) {
text = text.substr(sep + 1, text.length());
text = text.substr(sep + 1);
}
}

Expand Down
2 changes: 1 addition & 1 deletion editor/debugger/editor_visual_profiler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -362,7 +362,7 @@ void EditorVisualProfiler::_update_frame(bool p_focus_selected) {
stack.push_back(category);
categories.push_back(category);

name = name.substr(1, name.length());
name = name.substr(1);

category->set_text(0, name);
category->set_metadata(1, cpu_time);
Expand Down
6 changes: 3 additions & 3 deletions editor/editor_autoload_settings.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -242,7 +242,7 @@ void EditorAutoloadSettings::_autoload_edited() {
String scr_path = GLOBAL_GET(base);

if (scr_path.begins_with("*")) {
scr_path = scr_path.substr(1, scr_path.length());
scr_path = scr_path.substr(1);
}

// Singleton autoloads are represented with a leading "*" in their path.
Expand Down Expand Up @@ -494,7 +494,7 @@ void EditorAutoloadSettings::update_autoload() {
info.is_singleton = scr_path.begins_with("*");

if (info.is_singleton) {
scr_path = scr_path.substr(1, scr_path.length());
scr_path = scr_path.substr(1);
}

info.name = name;
Expand Down Expand Up @@ -873,7 +873,7 @@ EditorAutoloadSettings::EditorAutoloadSettings() {
info.is_singleton = scr_path.begins_with("*");

if (info.is_singleton) {
scr_path = scr_path.substr(1, scr_path.length());
scr_path = scr_path.substr(1);
}

info.name = name;
Expand Down
4 changes: 2 additions & 2 deletions editor/editor_file_system.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1783,7 +1783,7 @@ bool EditorFileSystem::_find_file(const String &p_file, EditorFileSystemDirector
if (!f.begins_with("res://")) {
return false;
}
f = f.substr(6, f.length());
f = f.substr(6);
f = f.replace("\\", "/");

Vector<String> path = f.split("/");
Expand Down Expand Up @@ -1899,7 +1899,7 @@ EditorFileSystemDirectory *EditorFileSystem::get_filesystem_path(const String &p
return nullptr;
}

f = f.substr(6, f.length());
f = f.substr(6);
f = f.replace("\\", "/");
if (f.is_empty()) {
return filesystem;
Expand Down
Loading