u8path.h 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  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 <__availability>
  12. #include <__config>
  13. #include <__filesystem/path.h>
  14. #include <type_traits>
  15. #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
  16. # pragma GCC system_header
  17. #endif
  18. #ifndef _LIBCPP_CXX03_LANG
  19. _LIBCPP_BEGIN_NAMESPACE_FILESYSTEM
  20. _LIBCPP_AVAILABILITY_FILESYSTEM_PUSH
  21. template <class _InputIt>
  22. _LIBCPP_INLINE_VISIBILITY _LIBCPP_DEPRECATED_WITH_CHAR8_T
  23. typename enable_if<__is_pathable<_InputIt>::value, path>::type
  24. u8path(_InputIt __f, _InputIt __l) {
  25. static_assert(
  26. #ifndef _LIBCPP_HAS_NO_CHAR8_T
  27. is_same<typename __is_pathable<_InputIt>::__char_type, char8_t>::value ||
  28. #endif
  29. is_same<typename __is_pathable<_InputIt>::__char_type, char>::value,
  30. "u8path(Iter, Iter) requires Iter have a value_type of type 'char'"
  31. " or 'char8_t'");
  32. #if defined(_LIBCPP_WIN32API)
  33. string __tmp(__f, __l);
  34. using _CVT = __widen_from_utf8<sizeof(wchar_t) * __CHAR_BIT__>;
  35. _VSTD::wstring __w;
  36. __w.reserve(__tmp.size());
  37. _CVT()(back_inserter(__w), __tmp.data(), __tmp.data() + __tmp.size());
  38. return path(__w);
  39. #else
  40. return path(__f, __l);
  41. #endif /* !_LIBCPP_WIN32API */
  42. }
  43. #if defined(_LIBCPP_WIN32API)
  44. template <class _InputIt>
  45. _LIBCPP_INLINE_VISIBILITY _LIBCPP_DEPRECATED_WITH_CHAR8_T
  46. typename enable_if<__is_pathable<_InputIt>::value, path>::type
  47. u8path(_InputIt __f, _NullSentinel) {
  48. static_assert(
  49. #ifndef _LIBCPP_HAS_NO_CHAR8_T
  50. is_same<typename __is_pathable<_InputIt>::__char_type, char8_t>::value ||
  51. #endif
  52. is_same<typename __is_pathable<_InputIt>::__char_type, char>::value,
  53. "u8path(Iter, Iter) requires Iter have a value_type of type 'char'"
  54. " or 'char8_t'");
  55. string __tmp;
  56. const char __sentinel = char{};
  57. for (; *__f != __sentinel; ++__f)
  58. __tmp.push_back(*__f);
  59. using _CVT = __widen_from_utf8<sizeof(wchar_t) * __CHAR_BIT__>;
  60. _VSTD::wstring __w;
  61. __w.reserve(__tmp.size());
  62. _CVT()(back_inserter(__w), __tmp.data(), __tmp.data() + __tmp.size());
  63. return path(__w);
  64. }
  65. #endif /* _LIBCPP_WIN32API */
  66. template <class _Source>
  67. _LIBCPP_INLINE_VISIBILITY _LIBCPP_DEPRECATED_WITH_CHAR8_T
  68. typename enable_if<__is_pathable<_Source>::value, path>::type
  69. 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(_VSTD::__unwrap_iter(_Traits::__range_begin(__s)), _VSTD::__unwrap_iter(_Traits::__range_end(__s)));
  80. #else
  81. return path(__s);
  82. #endif
  83. }
  84. _LIBCPP_AVAILABILITY_FILESYSTEM_POP
  85. _LIBCPP_END_NAMESPACE_FILESYSTEM
  86. #endif // _LIBCPP_CXX03_LANG
  87. #endif // _LIBCPP___FILESYSTEM_U8PATH_H