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

Replace String.substr() with simpler functions where possible #82412

Closed
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
17 changes: 6 additions & 11 deletions core/config/project_settings.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -197,7 +197,7 @@ String ProjectSettings::localize_path(const String &p_path) const {
return "res://" + path;
}

String parent = path.substr(0, sep);
String parent = path.left(sep);
MewPurPur marked this conversation as resolved.
Show resolved Hide resolved

String plocal = localize_path(parent);
if (plocal.is_empty()) {
Expand Down Expand Up @@ -426,9 +426,8 @@ void ProjectSettings::_get_property_list(List<PropertyInfo> *p_list) const {

for (const _VCSort &E : vclist) {
String prop_info_name = E.name;
int dot = prop_info_name.find(".");
if (dot != -1 && !custom_prop_info.has(prop_info_name)) {
prop_info_name = prop_info_name.substr(0, dot);
if (!custom_prop_info.has(prop_info_name)) {
prop_info_name = prop_info_name.get_slice(".", 0);
MewPurPur marked this conversation as resolved.
Show resolved Hide resolved
}

if (custom_prop_info.has(prop_info_name)) {
Expand Down Expand Up @@ -522,9 +521,7 @@ Error ProjectSettings::_setup(const String &p_path, const String &p_main_pack, b
// OS will call ProjectSettings->get_resource_path which will be empty if not overridden!
// If the OS would rather use a specific location, then it will not be empty.
resource_path = OS::get_singleton()->get_resource_dir().replace("\\", "/");
if (!resource_path.is_empty() && resource_path[resource_path.length() - 1] == '/') {
resource_path = resource_path.substr(0, resource_path.length() - 1); // Chop end.
}
resource_path = resource_path.trim_suffix("/");
MewPurPur marked this conversation as resolved.
Show resolved Hide resolved
}

// Attempt with a user-defined main pack first
Expand Down Expand Up @@ -645,9 +642,7 @@ Error ProjectSettings::_setup(const String &p_path, const String &p_main_pack, b
return err;
}

if (resource_path.length() && resource_path[resource_path.length() - 1] == '/') {
resource_path = resource_path.substr(0, resource_path.length() - 1); // Chop end.
}
resource_path = resource_path.trim_suffix("/");
MewPurPur marked this conversation as resolved.
Show resolved Hide resolved

return OK;
}
Expand Down Expand Up @@ -1060,7 +1055,7 @@ Error ProjectSettings::save_custom(const String &p_path, const CustomMap &p_cust
if (div < 0) {
category = "";
} else {
category = category.substr(0, div);
category = category.left(div);
MewPurPur marked this conversation as resolved.
Show resolved Hide resolved
name = name.substr(div + 1, name.size());
}
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 @@ -138,7 +138,7 @@ void EngineDebugger::initialize(const String &p_uri, bool p_skip_breakpoints, Ve
// Tell the OS that we want to handle termination signals.
OS::get_singleton()->initialize_debugging();
} else if (p_uri.find("://") >= 0) {
const String proto = p_uri.substr(0, p_uri.find("://") + 3);
const String proto = p_uri.left(p_uri.find("://") + 3);
MewPurPur marked this conversation as resolved.
Show resolved Hide resolved
if (!protocols.has(proto)) {
return;
}
Expand Down
6 changes: 3 additions & 3 deletions core/debugger/remote_debugger.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,7 @@ void RemoteDebugger::_print_handler(void *p_this, const String &p_string, bool p
}

if (allowed_chars < s.length()) {
s = s.substr(0, allowed_chars);
s = s.left(allowed_chars);
MewPurPur marked this conversation as resolved.
Show resolved Hide resolved
}

MutexLock lock(rd->mutex);
Expand Down Expand Up @@ -341,7 +341,7 @@ Error RemoteDebugger::_try_capture(const String &p_msg, const Array &p_data, boo
if (idx < 0) { // No prefix, unknown message.
return OK;
}
const String cap = p_msg.substr(0, idx);
const String cap = p_msg.left(idx);
MewPurPur marked this conversation as resolved.
Show resolved Hide resolved
if (!has_capture(cap)) {
return ERR_UNAVAILABLE; // Unknown message...
}
Expand Down Expand Up @@ -581,7 +581,7 @@ void RemoteDebugger::poll_events(bool p_is_idle) {
continue;
}

const String cap = cmd.substr(0, idx);
const String cap = cmd.left(idx);
MewPurPur marked this conversation as resolved.
Show resolved Hide resolved
if (!has_capture(cap)) {
continue; // Unknown message...
}
Expand Down
2 changes: 1 addition & 1 deletion core/debugger/remote_debugger_peer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -227,7 +227,7 @@ RemoteDebuggerPeer *RemoteDebuggerPeerTCP::create(const String &p_uri) {
if (debug_host.contains(":")) {
int sep_pos = debug_host.rfind(":");
debug_port = debug_host.substr(sep_pos + 1).to_int();
debug_host = debug_host.substr(0, sep_pos);
debug_host = debug_host.left(sep_pos);
MewPurPur marked this conversation as resolved.
Show resolved Hide resolved
}

RemoteDebuggerPeerTCP *peer = memnew(RemoteDebuggerPeerTCP);
Expand Down
4 changes: 2 additions & 2 deletions core/io/dir_access.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
MewPurPur marked this conversation as resolved.
Show resolved Hide resolved
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);
MewPurPur marked this conversation as resolved.
Show resolved Hide resolved
base = full_dir.left(full_dir.find(":/") + 2);
} else {
ERR_FAIL_V(ERR_INVALID_PARAMETER);
}
Expand Down
2 changes: 1 addition & 1 deletion core/io/file_access.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -417,7 +417,7 @@ Vector<String> 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<String> strings;

Expand Down
2 changes: 1 addition & 1 deletion core/io/file_access_compressed.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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, " ");
MewPurPur marked this conversation as resolved.
Show resolved Hide resolved

cmode = p_mode;
block_size = p_block_size;
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 @@ -117,7 +117,7 @@ Dictionary HTTPClient::_get_response_headers_as_dictionary() {
if (sp == -1) {
continue;
}
String key = s.substr(0, sp).strip_edges();
String key = s.left(sp).strip_edges();
MewPurPur marked this conversation as resolved.
Show resolved Hide resolved
String value = s.substr(sp + 1, s.length()).strip_edges();
ret[key] = value;
}
Expand Down
4 changes: 2 additions & 2 deletions core/io/translation_loader_po.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -286,7 +286,7 @@ Ref<Resource> TranslationLoaderPO::load_translation(Ref<FileAccess> f, Error *r_

ERR_FAIL_COND_V_MSG(end_pos == -1, Ref<Resource>(), "Expected '\"' at end of message while parsing: " + path + ":" + itos(line));

l = l.substr(0, end_pos);
l = l.left(end_pos);
MewPurPur marked this conversation as resolved.
Show resolved Hide resolved
l = l.c_unescape();

if (status == STATUS_READING_ID) {
Expand Down Expand Up @@ -329,7 +329,7 @@ Ref<Resource> TranslationLoaderPO::load_translation(Ref<FileAccess> f, Error *r_
if (p == -1) {
continue;
}
String prop = c.substr(0, p).strip_edges();
String prop = c.left(p).strip_edges();
MewPurPur marked this conversation as resolved.
Show resolved Hide resolved
String value = c.substr(p + 1, c.length()).strip_edges();

if (prop == "X-Language" || prop == "Language") {
Expand Down
4 changes: 2 additions & 2 deletions core/string/node_path.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -362,7 +362,7 @@ NodePath::NodePath(const NodePath &p_path) {
}

NodePath::NodePath(const String &p_path) {
if (p_path.length() == 0) {
if (p_path.is_empty()) {
return;
}

Expand Down Expand Up @@ -393,7 +393,7 @@ NodePath::NodePath(const String &p_path) {
}
}

path = path.substr(0, subpath_pos);
path = path.left(subpath_pos);
MewPurPur marked this conversation as resolved.
Show resolved Hide resolved
}

for (int i = (int)absolute; i < path.length(); i++) {
Expand Down
4 changes: 2 additions & 2 deletions core/string/translation.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1016,9 +1016,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);
}
}
Expand Down
2 changes: 1 addition & 1 deletion core/string/translation_po.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -172,7 +172,7 @@ 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();
MewPurPur marked this conversation as resolved.
Show resolved Hide resolved
equi_tests.push_back(equi_test);

String after_colon = p_plural_rule.substr(p_plural_rule.find(":") + 1, p_plural_rule.length());
Expand Down
Loading
Loading