[libcxx-commits] [PATCH] D91181: [24/N] [libcxx] Make generic_*string return paths with forward slashes on windows

Martin Storsjö via Phabricator via libcxx-commits libcxx-commits at lists.llvm.org
Tue Nov 10 08:56:27 PST 2020


mstorsjo created this revision.
mstorsjo added a reviewer: libc++.
Herald added a project: libc++.
Herald added 1 blocking reviewer(s): libc++.
mstorsjo requested review of this revision.

This matches what MS STL returns; in std::filesystem, forward slashes is considered a generic dir separator that is valid on all platforms.


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D91181

Files:
  libcxx/include/filesystem
  libcxx/test/std/input.output/filesystems/class.path/path.member/path.generic.obs/named_overloads.pass.cpp


Index: libcxx/test/std/input.output/filesystems/class.path/path.member/path.generic.obs/named_overloads.pass.cpp
===================================================================
--- libcxx/test/std/input.output/filesystems/class.path/path.member/path.generic.obs/named_overloads.pass.cpp
+++ libcxx/test/std/input.output/filesystems/class.path/path.member/path.generic.obs/named_overloads.pass.cpp
@@ -32,17 +32,24 @@
 #include "min_allocator.h"
 #include "filesystem_test_helper.h"
 
-MultiStringType longString = MKSTR("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ/123456789/abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ");
+MultiStringType input = MKSTR("c:\\foo\\bar");
+#ifdef _WIN32
+// On windows, the generic_* accessors return a path with forward slashes
+MultiStringType ref = MKSTR("c:/foo/bar");
+#else
+// On posix, the input string is returned as-is
+MultiStringType ref = MKSTR("c:\\foo\\bar");
+#endif
 
 int main(int, char**)
 {
   using namespace fs;
-  auto const& MS = longString;
-  const char* value = longString;
+  auto const& MS = ref;
+  const char* value = input;
   const path p(value);
   {
     std::string s = p.generic_string();
-    assert(s == value);
+    assert(s == (const char*)MS);
   }
   {
 #if TEST_STD_VER > 17 && defined(__cpp_char8_t)
Index: libcxx/include/filesystem
===================================================================
--- libcxx/include/filesystem
+++ libcxx/include/filesystem
@@ -1197,7 +1197,12 @@
 #if defined(_LIBCPP_WIN32API)
   _LIBCPP_INLINE_VISIBILITY std::wstring wstring() const { return __pn_; }
 
-  std::wstring generic_wstring() const { return __pn_; }
+  std::wstring generic_wstring() const {
+    std::wstring __s;
+    __s.resize(__pn_.size());
+    std::replace_copy(__pn_.begin(), __pn_.end(), __s.begin(), '\\', '/');
+    return __s;
+  }
 
 #if !defined(_LIBCPP_HAS_NO_LOCALIZATION)
   template <class _ECharT, class _Traits = char_traits<_ECharT>,
@@ -1234,13 +1239,21 @@
             class _Allocator = allocator<_ECharT> >
   basic_string<_ECharT, _Traits, _Allocator>
   generic_string(const _Allocator& __a = _Allocator()) const {
-    return string<_ECharT, _Traits, _Allocator>(__a);
+    using _Str = basic_string<_ECharT, _Traits, _Allocator>;
+    _Str __s = string<_ECharT, _Traits, _Allocator>(__a);
+    std::replace(__s.begin(), __s.end(),
+                 static_cast<_ECharT>('\\'), static_cast<_ECharT>('/'));
+    return __s;
   }
 
   std::string generic_string() const { return generic_string<char>(); }
   std::u16string generic_u16string() const { return generic_string<char16_t>(); }
   std::u32string generic_u32string() const { return generic_string<char32_t>(); }
-  __u8_string generic_u8string() const { return u8string(); }
+  __u8_string generic_u8string() const {
+    __u8_string __s = u8string();
+    std::replace(__s.begin(), __s.end(), '\\', '/');
+    return __s;
+  }
 #endif /* !_LIBCPP_HAS_NO_LOCALIZATION */
 #else /* !_WIN32 */
 


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D91181.304219.patch
Type: text/x-patch
Size: 3030 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/libcxx-commits/attachments/20201110/62af85e6/attachment.bin>


More information about the libcxx-commits mailing list