Merge pull request #52049 from theraot/master

This commit is contained in:
Rémi Verschelde 2021-09-13 16:49:24 +02:00 committed by GitHub
commit 41562b9198
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 34 additions and 18 deletions

View file

@ -4329,23 +4329,39 @@ bool String::is_relative_path() const {
}
String String::get_base_dir() const {
int basepos = find(":/");
if (basepos == -1) {
basepos = find(":\\");
int end = 0;
// url scheme style base
int basepos = find("://");
if (basepos != -1) {
end = basepos + 3;
}
// windows top level directory base
if (end == 0) {
basepos = find(":/");
if (basepos == -1) {
basepos = find(":\\");
}
if (basepos != -1) {
end = basepos + 2;
}
}
// unix root directory base
if (end == 0) {
if (begins_with("/")) {
end = 1;
}
}
String rs;
String base;
if (basepos != -1) {
int end = basepos + 3;
if (end != 0) {
rs = substr(end, length());
base = substr(0, end);
} else {
if (begins_with("/")) {
rs = substr(1, length());
base = "/";
} else {
rs = *this;
}
rs = *this;
}
int sep = MAX(rs.rfind("/"), rs.rfind("\\"));

View file

@ -1142,14 +1142,14 @@ TEST_CASE("[String] dedent") {
}
TEST_CASE("[String] Path functions") {
static const char *path[4] = { "C:\\Godot\\project\\test.tscn", "/Godot/project/test.xscn", "../Godot/project/test.scn", "Godot\\test.doc" };
static const char *base_dir[4] = { "C:\\Godot\\project", "/Godot/project", "../Godot/project", "Godot" };
static const char *base_name[4] = { "C:\\Godot\\project\\test", "/Godot/project/test", "../Godot/project/test", "Godot\\test" };
static const char *ext[4] = { "tscn", "xscn", "scn", "doc" };
static const char *file[4] = { "test.tscn", "test.xscn", "test.scn", "test.doc" };
static const bool abs[4] = { true, true, false, false };
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 };
for (int i = 0; i < 4; i++) {
for (int i = 0; i < 7; 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]);