Merge pull request #67251 from groud/simplify_path_solve_urls

Make String.simplify_path keep the protocol identifier for urls
This commit is contained in:
Rémi Verschelde 2022-10-13 15:24:18 +02:00
commit f12c4e8b9a
2 changed files with 45 additions and 32 deletions

View file

@ -3667,32 +3667,43 @@ bool String::is_network_share_path() const {
String String::simplify_path() const {
String s = *this;
String drive;
if (s.begins_with("local://")) {
drive = "local://";
s = s.substr(8);
} else if (s.begins_with("res://")) {
drive = "res://";
s = s.substr(6);
} else if (s.begins_with("user://")) {
drive = "user://";
s = s.substr(7);
} else if (s.begins_with("uid://")) {
drive = "uid://";
s = s.substr(6);
} else if (is_network_share_path()) {
drive = s.substr(0, 2);
s = s.substr(2, s.length() - 2);
} else if (s.begins_with("/") || s.begins_with("\\")) {
drive = s.substr(0, 1);
s = s.substr(1, s.length() - 1);
} else {
int p = s.find(":/");
if (p == -1) {
p = s.find(":\\");
// Check if we have a special path (like res://) or a protocol identifier.
int p = s.find("://");
bool found = false;
if (p > 0) {
bool only_chars = true;
for (int i = 0; i < p; i++) {
if (!is_ascii_char(s[i])) {
only_chars = false;
break;
}
}
if (p != -1 && p < s.find("/")) {
drive = s.substr(0, p + 2);
s = s.substr(p + 2);
if (only_chars) {
found = true;
drive = s.substr(0, 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);
} else if (s.begins_with("/") || s.begins_with("\\")) {
// Absolute path.
drive = s.substr(0, 1);
s = s.substr(1);
} else {
// Windows-style drive path, like C:/ or C:\.
p = s.find(":/");
if (p == -1) {
p = s.find(":\\");
}
if (p != -1 && p < s.find("/")) {
drive = s.substr(0, p + 2);
s = s.substr(p + 2);
}
}
}

View file

@ -1424,20 +1424,22 @@ TEST_CASE("[String] dedent") {
}
TEST_CASE("[String] Path functions") {
static const char *path[7] = { "C:\\Godot\\project\\test.tscn", "/Godot/project/test.xscn", "../Godot/project/test.scn", "Godot\\test.doc", "C:\\test.", "res://test", "/.test" };
static const char *base_dir[7] = { "C:\\Godot\\project", "/Godot/project", "../Godot/project", "Godot", "C:\\", "res://", "/" };
static const char *base_name[7] = { "C:\\Godot\\project\\test", "/Godot/project/test", "../Godot/project/test", "Godot\\test", "C:\\test", "res://test", "/" };
static const char *ext[7] = { "tscn", "xscn", "scn", "doc", "", "", "test" };
static const char *file[7] = { "test.tscn", "test.xscn", "test.scn", "test.doc", "test.", "test", ".test" };
static const bool abs[7] = { true, true, false, false, true, true, true };
static const char *path[8] = { "C:\\Godot\\project\\test.tscn", "/Godot/project/test.xscn", "../Godot/project/test.scn", "Godot\\test.doc", "C:\\test.", "res://test", "user://test", "/.test" };
static const char *base_dir[8] = { "C:\\Godot\\project", "/Godot/project", "../Godot/project", "Godot", "C:\\", "res://", "user://", "/" };
static const char *base_name[8] = { "C:\\Godot\\project\\test", "/Godot/project/test", "../Godot/project/test", "Godot\\test", "C:\\test", "res://test", "user://test", "/" };
static const char *ext[8] = { "tscn", "xscn", "scn", "doc", "", "", "", "test" };
static const char *file[8] = { "test.tscn", "test.xscn", "test.scn", "test.doc", "test.", "test", "test", ".test" };
static const char *simplified[8] = { "C:/Godot/project/test.tscn", "/Godot/project/test.xscn", "Godot/project/test.scn", "Godot/test.doc", "C:/test.", "res://test", "user://test", "/.test" };
static const bool abs[8] = { true, true, false, false, true, true, true, true };
for (int i = 0; i < 7; i++) {
for (int i = 0; i < 8; i++) {
CHECK(String(path[i]).get_base_dir() == base_dir[i]);
CHECK(String(path[i]).get_basename() == base_name[i]);
CHECK(String(path[i]).get_extension() == ext[i]);
CHECK(String(path[i]).get_file() == file[i]);
CHECK(String(path[i]).is_absolute_path() == abs[i]);
CHECK(String(path[i]).is_relative_path() != abs[i]);
CHECK(String(path[i]).simplify_path() == String(simplified[i]));
CHECK(String(path[i]).simplify_path().get_base_dir().path_join(file[i]) == String(path[i]).simplify_path());
}