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

[3.x] Fix get_base_dir windows top level directory logic #52744

Merged
merged 1 commit into from
Sep 16, 2021
Merged
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
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