Skip to content

Commit f448524

Browse files
committed
[libcxx] Handle backslash as path separator on windows
Differential Revision: https://reviews.llvm.org/D91138
1 parent 248e345 commit f448524

File tree

2 files changed

+26
-6
lines changed

2 files changed

+26
-6
lines changed

libcxx/include/filesystem

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1116,7 +1116,12 @@ public:
11161116
_LIBCPP_INLINE_VISIBILITY
11171117
void clear() noexcept { __pn_.clear(); }
11181118

1119-
path& make_preferred() { return *this; }
1119+
path& make_preferred() {
1120+
#if defined(_LIBCPP_WIN32API)
1121+
_VSTD::replace(__pn_.begin(), __pn_.end(), L'/', L'\\');
1122+
#endif
1123+
return *this;
1124+
}
11201125

11211126
_LIBCPP_INLINE_VISIBILITY
11221127
path& remove_filename() {

libcxx/src/filesystem/operations.cpp

Lines changed: 20 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,17 @@
5151
_LIBCPP_BEGIN_NAMESPACE_FILESYSTEM
5252

5353
namespace {
54+
55+
bool isSeparator(path::value_type C) {
56+
if (C == '/')
57+
return true;
58+
#if defined(_LIBCPP_WIN32API)
59+
if (C == '\\')
60+
return true;
61+
#endif
62+
return false;
63+
}
64+
5465
namespace parser {
5566

5667
using string_view_t = path::__string_view;
@@ -271,21 +282,21 @@ struct PathParser {
271282
}
272283

273284
PosPtr consumeSeparator(PosPtr P, PosPtr End) const noexcept {
274-
if (P == End || *P != '/')
285+
if (P == End || !isSeparator(*P))
275286
return nullptr;
276287
const int Inc = P < End ? 1 : -1;
277288
P += Inc;
278-
while (P != End && *P == '/')
289+
while (P != End && isSeparator(*P))
279290
P += Inc;
280291
return P;
281292
}
282293

283294
PosPtr consumeName(PosPtr P, PosPtr End) const noexcept {
284-
if (P == End || *P == '/')
295+
if (P == End || isSeparator(*P))
285296
return nullptr;
286297
const int Inc = P < End ? 1 : -1;
287298
P += Inc;
288-
while (P != End && *P != '/')
299+
while (P != End && !isSeparator(*P))
289300
P += Inc;
290301
return P;
291302
}
@@ -1393,7 +1404,7 @@ string_view_t path::__root_path_raw() const {
13931404
auto PP = PathParser::CreateBegin(__pn_);
13941405
if (PP.State == PathParser::PS_InRootName) {
13951406
auto NextCh = PP.peek();
1396-
if (NextCh && *NextCh == '/') {
1407+
if (NextCh && isSeparator(*NextCh)) {
13971408
++PP;
13981409
return createView(__pn_.data(), &PP.RawEntry.back());
13991410
}
@@ -1491,6 +1502,10 @@ static PathPartKind ClassifyPathPart(string_view_t Part) {
14911502
return PK_DotDot;
14921503
if (Part == PS("/"))
14931504
return PK_RootSep;
1505+
#if defined(_LIBCPP_WIN32API)
1506+
if (Part == PS("\\"))
1507+
return PK_RootSep;
1508+
#endif
14941509
return PK_Filename;
14951510
}
14961511

0 commit comments

Comments
 (0)