u8path.h 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. // -*- C++ -*-
  2. //===----------------------------------------------------------------------===//
  3. //
  4. // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
  5. // See https://llvm.org/LICENSE.txt for license information.
  6. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  7. //
  8. //===----------------------------------------------------------------------===//
  9. #ifndef _LIBCPP___FILESYSTEM_U8PATH_H
  10. #define _LIBCPP___FILESYSTEM_U8PATH_H
  11. #include <__algorithm/unwrap_iter.h>
  12. #include <__availability>
  13. #include <__config>
  14. #include <__filesystem/path.h>
  15. #include <string>
  16. // Only required on Windows for __widen_from_utf8, and included conservatively
  17. // because it requires support for localization.
  18. #if defined(_LIBCPP_WIN32API)
  19. # include <locale>
  20. #endif
  21. #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
  22. # pragma GCC system_header
  23. #endif
  24. #ifndef _LIBCPP_CXX03_LANG
  25. _LIBCPP_BEGIN_NAMESPACE_FILESYSTEM
  26. _LIBCPP_AVAILABILITY_FILESYSTEM_LIBRARY_PUSH
  27. template <class _InputIt, __enable_if_t<__is_pathable<_InputIt>::value, int> = 0>
  28. _LIBCPP_INLINE_VISIBILITY _LIBCPP_DEPRECATED_WITH_CHAR8_T
  29. path
  30. u8path(_InputIt __f, _InputIt __l) {
  31. static_assert(
  32. #ifndef _LIBCPP_HAS_NO_CHAR8_T
  33. is_same<typename __is_pathable<_InputIt>::__char_type, char8_t>::value ||
  34. #endif
  35. is_same<typename __is_pathable<_InputIt>::__char_type, char>::value,
  36. "u8path(Iter, Iter) requires Iter have a value_type of type 'char'"
  37. " or 'char8_t'");
  38. #if defined(_LIBCPP_WIN32API)
  39. string __tmp(__f, __l);
  40. using _CVT = __widen_from_utf8<sizeof(wchar_t) * __CHAR_BIT__>;
  41. _VSTD::wstring __w;
  42. __w.reserve(__tmp.size());
  43. _CVT()(back_inserter(__w), __tmp.data(), __tmp.data() + __tmp.size());
  44. return path(__w);
  45. #else
  46. return path(__f, __l);
  47. #endif /* !_LIBCPP_WIN32API */
  48. }
  49. #if defined(_LIBCPP_WIN32API)
  50. template <class _InputIt, __enable_if_t<__is_pathable<_InputIt>::value, int> = 0>
  51. _LIBCPP_INLINE_VISIBILITY _LIBCPP_DEPRECATED_WITH_CHAR8_T
  52. path
  53. u8path(_InputIt __f, _NullSentinel) {
  54. static_assert(
  55. #ifndef _LIBCPP_HAS_NO_CHAR8_T
  56. is_same<typename __is_pathable<_InputIt>::__char_type, char8_t>::value ||
  57. #endif
  58. is_same<typename __is_pathable<_InputIt>::__char_type, char>::value,
  59. "u8path(Iter, Iter) requires Iter have a value_type of type 'char'"
  60. " or 'char8_t'");
  61. string __tmp;
  62. const char __sentinel = char{};
  63. for (; *__f != __sentinel; ++__f)
  64. __tmp.push_back(*__f);
  65. using _CVT = __widen_from_utf8<sizeof(wchar_t) * __CHAR_BIT__>;
  66. _VSTD::wstring __w;
  67. __w.reserve(__tmp.size());
  68. _CVT()(back_inserter(__w), __tmp.data(), __tmp.data() + __tmp.size());
  69. return path(__w);
  70. }
  71. #endif /* _LIBCPP_WIN32API */
  72. template <class _Source, __enable_if_t<__is_pathable<_Source>::value, int> = 0>
  73. _LIBCPP_INLINE_VISIBILITY _LIBCPP_DEPRECATED_WITH_CHAR8_T
  74. path
  75. u8path(const _Source& __s) {
  76. static_assert(
  77. #ifndef _LIBCPP_HAS_NO_CHAR8_T
  78. is_same<typename __is_pathable<_Source>::__char_type, char8_t>::value ||
  79. #endif
  80. is_same<typename __is_pathable<_Source>::__char_type, char>::value,
  81. "u8path(Source const&) requires Source have a character type of type "
  82. "'char' or 'char8_t'");
  83. #if defined(_LIBCPP_WIN32API)
  84. using _Traits = __is_pathable<_Source>;
  85. return u8path(_VSTD::__unwrap_iter(_Traits::__range_begin(__s)), _VSTD::__unwrap_iter(_Traits::__range_end(__s)));
  86. #else
  87. return path(__s);
  88. #endif
  89. }
  90. _LIBCPP_AVAILABILITY_FILESYSTEM_LIBRARY_POP
  91. _LIBCPP_END_NAMESPACE_FILESYSTEM
  92. #endif // _LIBCPP_CXX03_LANG
  93. #endif // _LIBCPP___FILESYSTEM_U8PATH_H