[llvm-branch-commits] [libcxx] f448524 - [libcxx] Handle backslash as path separator on windows

Martin Storsjö via llvm-branch-commits llvm-branch-commits at lists.llvm.org
Thu Jan 7 00:12:57 PST 2021


Author: Martin Storsjö
Date: 2021-01-07T10:02:47+02:00
New Revision: f4485240a2182050d6417947a407fe4c551e2011

URL: https://github.com/llvm/llvm-project/commit/f4485240a2182050d6417947a407fe4c551e2011
DIFF: https://github.com/llvm/llvm-project/commit/f4485240a2182050d6417947a407fe4c551e2011.diff

LOG: [libcxx] Handle backslash as path separator on windows

Differential Revision: https://reviews.llvm.org/D91138

Added: 
    

Modified: 
    libcxx/include/filesystem
    libcxx/src/filesystem/operations.cpp

Removed: 
    


################################################################################
diff  --git a/libcxx/include/filesystem b/libcxx/include/filesystem
index e39790c50955..c60020a3e893 100644
--- a/libcxx/include/filesystem
+++ b/libcxx/include/filesystem
@@ -1116,7 +1116,12 @@ public:
   _LIBCPP_INLINE_VISIBILITY
   void clear() noexcept { __pn_.clear(); }
 
-  path& make_preferred() { return *this; }
+  path& make_preferred() {
+#if defined(_LIBCPP_WIN32API)
+    _VSTD::replace(__pn_.begin(), __pn_.end(), L'/', L'\\');
+#endif
+    return *this;
+  }
 
   _LIBCPP_INLINE_VISIBILITY
   path& remove_filename() {

diff  --git a/libcxx/src/filesystem/operations.cpp b/libcxx/src/filesystem/operations.cpp
index 4d585af78f98..7db2d1ff0074 100644
--- a/libcxx/src/filesystem/operations.cpp
+++ b/libcxx/src/filesystem/operations.cpp
@@ -51,6 +51,17 @@
 _LIBCPP_BEGIN_NAMESPACE_FILESYSTEM
 
 namespace {
+
+bool isSeparator(path::value_type C) {
+  if (C == '/')
+    return true;
+#if defined(_LIBCPP_WIN32API)
+  if (C == '\\')
+    return true;
+#endif
+  return false;
+}
+
 namespace parser {
 
 using string_view_t = path::__string_view;
@@ -271,21 +282,21 @@ struct PathParser {
   }
 
   PosPtr consumeSeparator(PosPtr P, PosPtr End) const noexcept {
-    if (P == End || *P != '/')
+    if (P == End || !isSeparator(*P))
       return nullptr;
     const int Inc = P < End ? 1 : -1;
     P += Inc;
-    while (P != End && *P == '/')
+    while (P != End && isSeparator(*P))
       P += Inc;
     return P;
   }
 
   PosPtr consumeName(PosPtr P, PosPtr End) const noexcept {
-    if (P == End || *P == '/')
+    if (P == End || isSeparator(*P))
       return nullptr;
     const int Inc = P < End ? 1 : -1;
     P += Inc;
-    while (P != End && *P != '/')
+    while (P != End && !isSeparator(*P))
       P += Inc;
     return P;
   }
@@ -1393,7 +1404,7 @@ string_view_t path::__root_path_raw() const {
   auto PP = PathParser::CreateBegin(__pn_);
   if (PP.State == PathParser::PS_InRootName) {
     auto NextCh = PP.peek();
-    if (NextCh && *NextCh == '/') {
+    if (NextCh && isSeparator(*NextCh)) {
       ++PP;
       return createView(__pn_.data(), &PP.RawEntry.back());
     }
@@ -1491,6 +1502,10 @@ static PathPartKind ClassifyPathPart(string_view_t Part) {
     return PK_DotDot;
   if (Part == PS("/"))
     return PK_RootSep;
+#if defined(_LIBCPP_WIN32API)
+  if (Part == PS("\\"))
+    return PK_RootSep;
+#endif
   return PK_Filename;
 }
 


        


More information about the llvm-branch-commits mailing list