Merge pull request #52744 from theraot/3.x

[3.x] Fix get_base_dir windows top level directory logic
This commit is contained in:
Rémi Verschelde 2021-09-16 14:12:57 +02:00 committed by GitHub
commit 4850e7eaca
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 32 additions and 16 deletions

View File

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