u8path.h 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  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. #if _LIBCPP_STD_VER >= 17
  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_HIDE_FROM_ABI _LIBCPP_DEPRECATED_WITH_CHAR8_T path u8path(_InputIt __f, _InputIt __l) {
  29. static_assert(
  30. # ifndef _LIBCPP_HAS_NO_CHAR8_T
  31. is_same<typename __is_pathable<_InputIt>::__char_type, char8_t>::value ||
  32. # endif
  33. is_same<typename __is_pathable<_InputIt>::__char_type, char>::value,
  34. "u8path(Iter, Iter) requires Iter have a value_type of type 'char'"
  35. " or 'char8_t'");
  36. # if defined(_LIBCPP_WIN32API)
  37. string __tmp(__f, __l);
  38. using _CVT = __widen_from_utf8<sizeof(wchar_t) * __CHAR_BIT__>;
  39. std::wstring __w;
  40. __w.reserve(__tmp.size());
  41. _CVT()(back_inserter(__w), __tmp.data(), __tmp.data() + __tmp.size());
  42. return path(__w);
  43. # else
  44. return path(__f, __l);
  45. # endif /* !_LIBCPP_WIN32API */
  46. }
  47. # if defined(_LIBCPP_WIN32API)
  48. template <class _InputIt, __enable_if_t<__is_pathable<_InputIt>::value, int> = 0>
  49. _LIBCPP_HIDE_FROM_ABI _LIBCPP_DEPRECATED_WITH_CHAR8_T path u8path(_InputIt __f, _NullSentinel) {
  50. static_assert(
  51. # ifndef _LIBCPP_HAS_NO_CHAR8_T
  52. is_same<typename __is_pathable<_InputIt>::__char_type, char8_t>::value ||
  53. # endif
  54. is_same<typename __is_pathable<_InputIt>::__char_type, char>::value,
  55. "u8path(Iter, Iter) requires Iter have a value_type of type 'char'"
  56. " or 'char8_t'");
  57. string __tmp;
  58. const char __sentinel = char{};
  59. for (; *__f != __sentinel; ++__f)
  60. __tmp.push_back(*__f);
  61. using _CVT = __widen_from_utf8<sizeof(wchar_t) * __CHAR_BIT__>;
  62. std::wstring __w;
  63. __w.reserve(__tmp.size());
  64. _CVT()(back_inserter(__w), __tmp.data(), __tmp.data() + __tmp.size());
  65. return path(__w);
  66. }
  67. # endif /* _LIBCPP_WIN32API */
  68. template <class _Source, __enable_if_t<__is_pathable<_Source>::value, int> = 0>
  69. _LIBCPP_HIDE_FROM_ABI _LIBCPP_DEPRECATED_WITH_CHAR8_T path u8path(const _Source& __s) {
  70. static_assert(
  71. # ifndef _LIBCPP_HAS_NO_CHAR8_T
  72. is_same<typename __is_pathable<_Source>::__char_type, char8_t>::value ||
  73. # endif
  74. is_same<typename __is_pathable<_Source>::__char_type, char>::value,
  75. "u8path(Source const&) requires Source have a character type of type "
  76. "'char' or 'char8_t'");
  77. # if defined(_LIBCPP_WIN32API)
  78. using _Traits = __is_pathable<_Source>;
  79. return u8path(std::__unwrap_iter(_Traits::__range_begin(__s)), std::__unwrap_iter(_Traits::__range_end(__s)));
  80. # else
  81. return path(__s);
  82. # endif
  83. }
  84. _LIBCPP_AVAILABILITY_FILESYSTEM_LIBRARY_POP
  85. _LIBCPP_END_NAMESPACE_FILESYSTEM
  86. #endif // _LIBCPP_STD_VER >= 17
  87. #endif // _LIBCPP___FILESYSTEM_U8PATH_H