Skip to content

Commit

Permalink
Merge pull request #52744 from theraot/3.x
Browse files Browse the repository at this point in the history
[3.x] Fix get_base_dir windows top level directory logic
  • Loading branch information
akien-mga authored Sep 16, 2021
2 parents 1bbc7c9 + d03f7c0 commit 4850e7e
Showing 1 changed file with 28 additions and 12 deletions.
40 changes: 28 additions & 12 deletions core/ustring.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3967,26 +3967,42 @@ bool String::is_rel_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.find_last("/"), rs.find_last("\\"));
int sep = MAX(rs.rfind("/"), rs.rfind("\\"));
if (sep == -1) {
return base;
}
Expand Down

0 comments on commit 4850e7e

Please sign in to comment.