Skip to content

Commit

Permalink
[win] Keep initial double backslashes "\\" for network paths
Browse files Browse the repository at this point in the history
  • Loading branch information
dacap committed Aug 21, 2024
1 parent a971ccd commit f645a69
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 2 deletions.
31 changes: 29 additions & 2 deletions base/fs.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -249,7 +249,22 @@ std::string fix_path_separators(const std::string& filename)
{
std::string result;
result.reserve(filename.size());
for (auto chr : filename) {

size_t i = 0;

#if LAF_WINDOWS
// Network paths can start with two backslashes
if (filename.size() >= 2 &&
filename[0] == path_separator && // Check for equality to backslash (\),
filename[1] == path_separator) { // no for is_path_separator()
result.push_back(path_separator);
result.push_back(path_separator);
i += 2;
}
#endif

for (; i<filename.size(); ++i) {
const auto chr = filename[i];
if (is_path_separator(chr)) {
if (result.empty() || !is_path_separator(result.back()))
result.push_back(path_separator);
Expand All @@ -272,9 +287,21 @@ std::string normalize_path(const std::string& _path)
std::string path = fix_path_separators(_path);

std::string fn;
if (!path.empty() && path[0] == path_separator)
fn.reserve(path.size());

// Add the first separator for absolute paths.
if (!path.empty() && path[0] == path_separator) {
fn.push_back(path_separator);

#if LAF_WINDOWS
// Add the second separator for network paths.
if (path.size() >= 2 &&
path[1] == path_separator) {
fn.push_back(path_separator);
}
#endif
}

std::vector<std::string> parts;
split_string(path, parts, path_separators);

Expand Down
11 changes: 11 additions & 0 deletions base/fs_tests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,13 @@ TEST(FS, FixPathSeparators)
const std::string sep(1, path_separator);
EXPECT_EQ(sep, fix_path_separators("/"));
EXPECT_EQ(sep, fix_path_separators("///"));
EXPECT_EQ(sep+"a"+sep, fix_path_separators("//a/"));
EXPECT_EQ("a"+sep+"b"+sep, fix_path_separators("a///b/"));

#if LAF_WINDOWS
EXPECT_EQ("\\\\hostname\\a\\b", fix_path_separators("\\\\hostname\\\\a/b"));
EXPECT_EQ("\\\\hostname\\b", fix_path_separators("\\\\/hostname\\b"));
#endif
}

TEST(FS, MakeDirectory)
Expand Down Expand Up @@ -281,6 +287,11 @@ TEST(FS, NormalizePath)
EXPECT_EQ(".."+sep+"..", normalize_path("../a/../.."));
EXPECT_EQ("..", normalize_path("a/../.."));
EXPECT_EQ(sep+"b", normalize_path("/a/../b"));

#if LAF_WINDOWS
EXPECT_EQ("\\\\hostname\\b", normalize_path("\\\\hostname\\\\a/../b"));
EXPECT_EQ("\\\\hostname\\b\\a", normalize_path("\\\\/hostname\\b/a"));
#endif
}

#if COMPARE_WITH_STD_FS
Expand Down

0 comments on commit f645a69

Please sign in to comment.