From d03f7c059897aad30685e51aca445839e6c86d6e Mon Sep 17 00:00:00 2001 From: Theraot Date: Thu, 16 Sep 2021 06:38:29 -0500 Subject: [PATCH] [3.x] Fix get_base_dir windows top level directory logic This is a fix for https://github.com/godotengine/godot/issues/52048 --- core/ustring.cpp | 48 ++++++++++++++++++++++++++++++++---------------- 1 file changed, 32 insertions(+), 16 deletions(-) diff --git a/core/ustring.cpp b/core/ustring.cpp index ee5617728d2..0d0311f1721 100644 --- a/core/ustring.cpp +++ b/core/ustring.cpp @@ -3967,26 +3967,42 @@ bool String::is_rel_path() const { } String String::get_base_dir() const { - int basepos = find(":/"); - if (basepos == -1) { - basepos = find(":\\"); - } - String rs; - String base; + int end = 0; + + // url scheme style base + int basepos = find("://"); if (basepos != -1) { - int end = basepos + 3; - rs = substr(end, length()); - base = substr(0, end); - } else { - if (begins_with("/")) { - rs = substr(1, length()); - base = "/"; - } else { - rs = *this; + end = basepos + 3; + } + + // windows top level directory base + if (end == 0) { + basepos = find(":/"); + if (basepos == -1) { + basepos = find(":\\"); + } + 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) { return base; }