cwchar 10.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258
  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_CWCHAR
  10. #define _LIBCPP_CWCHAR
  11. /*
  12. cwchar synopsis
  13. Macros:
  14. NULL
  15. WCHAR_MAX
  16. WCHAR_MIN
  17. WEOF
  18. namespace std
  19. {
  20. Types:
  21. mbstate_t
  22. size_t
  23. tm
  24. wint_t
  25. int fwprintf(FILE* restrict stream, const wchar_t* restrict format, ...);
  26. int fwscanf(FILE* restrict stream, const wchar_t* restrict format, ...);
  27. int swprintf(wchar_t* restrict s, size_t n, const wchar_t* restrict format, ...);
  28. int swscanf(const wchar_t* restrict s, const wchar_t* restrict format, ...);
  29. int vfwprintf(FILE* restrict stream, const wchar_t* restrict format, va_list arg);
  30. int vfwscanf(FILE* restrict stream, const wchar_t* restrict format, va_list arg); // C99
  31. int vswprintf(wchar_t* restrict s, size_t n, const wchar_t* restrict format, va_list arg);
  32. int vswscanf(const wchar_t* restrict s, const wchar_t* restrict format, va_list arg); // C99
  33. int vwprintf(const wchar_t* restrict format, va_list arg);
  34. int vwscanf(const wchar_t* restrict format, va_list arg); // C99
  35. int wprintf(const wchar_t* restrict format, ...);
  36. int wscanf(const wchar_t* restrict format, ...);
  37. wint_t fgetwc(FILE* stream);
  38. wchar_t* fgetws(wchar_t* restrict s, int n, FILE* restrict stream);
  39. wint_t fputwc(wchar_t c, FILE* stream);
  40. int fputws(const wchar_t* restrict s, FILE* restrict stream);
  41. int fwide(FILE* stream, int mode);
  42. wint_t getwc(FILE* stream);
  43. wint_t getwchar();
  44. wint_t putwc(wchar_t c, FILE* stream);
  45. wint_t putwchar(wchar_t c);
  46. wint_t ungetwc(wint_t c, FILE* stream);
  47. double wcstod(const wchar_t* restrict nptr, wchar_t** restrict endptr);
  48. float wcstof(const wchar_t* restrict nptr, wchar_t** restrict endptr); // C99
  49. long double wcstold(const wchar_t* restrict nptr, wchar_t** restrict endptr); // C99
  50. long wcstol(const wchar_t* restrict nptr, wchar_t** restrict endptr, int base);
  51. long long wcstoll(const wchar_t* restrict nptr, wchar_t** restrict endptr, int base); // C99
  52. unsigned long wcstoul(const wchar_t* restrict nptr, wchar_t** restrict endptr, int base);
  53. unsigned long long wcstoull(const wchar_t* restrict nptr, wchar_t** restrict endptr, int base); // C99
  54. wchar_t* wcscpy(wchar_t* restrict s1, const wchar_t* restrict s2);
  55. wchar_t* wcsncpy(wchar_t* restrict s1, const wchar_t* restrict s2, size_t n);
  56. wchar_t* wcscat(wchar_t* restrict s1, const wchar_t* restrict s2);
  57. wchar_t* wcsncat(wchar_t* restrict s1, const wchar_t* restrict s2, size_t n);
  58. int wcscmp(const wchar_t* s1, const wchar_t* s2);
  59. int wcscoll(const wchar_t* s1, const wchar_t* s2);
  60. int wcsncmp(const wchar_t* s1, const wchar_t* s2, size_t n);
  61. size_t wcsxfrm(wchar_t* restrict s1, const wchar_t* restrict s2, size_t n);
  62. const wchar_t* wcschr(const wchar_t* s, wchar_t c);
  63. wchar_t* wcschr( wchar_t* s, wchar_t c);
  64. size_t wcscspn(const wchar_t* s1, const wchar_t* s2);
  65. size_t wcslen(const wchar_t* s);
  66. const wchar_t* wcspbrk(const wchar_t* s1, const wchar_t* s2);
  67. wchar_t* wcspbrk( wchar_t* s1, const wchar_t* s2);
  68. const wchar_t* wcsrchr(const wchar_t* s, wchar_t c);
  69. wchar_t* wcsrchr( wchar_t* s, wchar_t c);
  70. size_t wcsspn(const wchar_t* s1, const wchar_t* s2);
  71. const wchar_t* wcsstr(const wchar_t* s1, const wchar_t* s2);
  72. wchar_t* wcsstr( wchar_t* s1, const wchar_t* s2);
  73. wchar_t* wcstok(wchar_t* restrict s1, const wchar_t* restrict s2, wchar_t** restrict ptr);
  74. const wchar_t* wmemchr(const wchar_t* s, wchar_t c, size_t n);
  75. wchar_t* wmemchr( wchar_t* s, wchar_t c, size_t n);
  76. int wmemcmp(wchar_t* restrict s1, const wchar_t* restrict s2, size_t n);
  77. wchar_t* wmemcpy(wchar_t* restrict s1, const wchar_t* restrict s2, size_t n);
  78. wchar_t* wmemmove(wchar_t* s1, const wchar_t* s2, size_t n);
  79. wchar_t* wmemset(wchar_t* s, wchar_t c, size_t n);
  80. size_t wcsftime(wchar_t* restrict s, size_t maxsize, const wchar_t* restrict format,
  81. const tm* restrict timeptr);
  82. wint_t btowc(int c);
  83. int wctob(wint_t c);
  84. int mbsinit(const mbstate_t* ps);
  85. size_t mbrlen(const char* restrict s, size_t n, mbstate_t* restrict ps);
  86. size_t mbrtowc(wchar_t* restrict pwc, const char* restrict s, size_t n, mbstate_t* restrict ps);
  87. size_t wcrtomb(char* restrict s, wchar_t wc, mbstate_t* restrict ps);
  88. size_t mbsrtowcs(wchar_t* restrict dst, const char** restrict src, size_t len,
  89. mbstate_t* restrict ps);
  90. size_t wcsrtombs(char* restrict dst, const wchar_t** restrict src, size_t len,
  91. mbstate_t* restrict ps);
  92. } // std
  93. */
  94. #include <__assert> // all public C++ headers provide the assertion handler
  95. #include <__config>
  96. #include <__type_traits/apply_cv.h>
  97. #include <__type_traits/is_constant_evaluated.h>
  98. #include <__type_traits/is_equality_comparable.h>
  99. #include <__type_traits/is_same.h>
  100. #include <__type_traits/remove_cv.h>
  101. #include <cwctype>
  102. #include <wchar.h>
  103. #ifndef _LIBCPP_WCHAR_H
  104. # error <cwchar> tried including <wchar.h> but didn't find libc++'s <wchar.h> header. \
  105. This usually means that your header search paths are not configured properly. \
  106. The header search paths should contain the C++ Standard Library headers before \
  107. any C Standard Library, and you are probably using compiler flags that make that \
  108. not be the case.
  109. #endif
  110. #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
  111. # pragma GCC system_header
  112. #endif
  113. _LIBCPP_BEGIN_NAMESPACE_STD
  114. using ::mbstate_t _LIBCPP_USING_IF_EXISTS;
  115. using ::size_t _LIBCPP_USING_IF_EXISTS;
  116. using ::tm _LIBCPP_USING_IF_EXISTS;
  117. using ::wint_t _LIBCPP_USING_IF_EXISTS;
  118. using ::FILE _LIBCPP_USING_IF_EXISTS;
  119. using ::fwprintf _LIBCPP_USING_IF_EXISTS;
  120. using ::fwscanf _LIBCPP_USING_IF_EXISTS;
  121. using ::swprintf _LIBCPP_USING_IF_EXISTS;
  122. using ::vfwprintf _LIBCPP_USING_IF_EXISTS;
  123. using ::vswprintf _LIBCPP_USING_IF_EXISTS;
  124. using ::swscanf _LIBCPP_USING_IF_EXISTS;
  125. using ::vfwscanf _LIBCPP_USING_IF_EXISTS;
  126. using ::vswscanf _LIBCPP_USING_IF_EXISTS;
  127. using ::fgetwc _LIBCPP_USING_IF_EXISTS;
  128. using ::fgetws _LIBCPP_USING_IF_EXISTS;
  129. using ::fputwc _LIBCPP_USING_IF_EXISTS;
  130. using ::fputws _LIBCPP_USING_IF_EXISTS;
  131. using ::fwide _LIBCPP_USING_IF_EXISTS;
  132. using ::getwc _LIBCPP_USING_IF_EXISTS;
  133. using ::putwc _LIBCPP_USING_IF_EXISTS;
  134. using ::ungetwc _LIBCPP_USING_IF_EXISTS;
  135. using ::wcstod _LIBCPP_USING_IF_EXISTS;
  136. using ::wcstof _LIBCPP_USING_IF_EXISTS;
  137. using ::wcstold _LIBCPP_USING_IF_EXISTS;
  138. using ::wcstol _LIBCPP_USING_IF_EXISTS;
  139. using ::wcstoll _LIBCPP_USING_IF_EXISTS;
  140. using ::wcstoul _LIBCPP_USING_IF_EXISTS;
  141. using ::wcstoull _LIBCPP_USING_IF_EXISTS;
  142. using ::wcscpy _LIBCPP_USING_IF_EXISTS;
  143. using ::wcsncpy _LIBCPP_USING_IF_EXISTS;
  144. using ::wcscat _LIBCPP_USING_IF_EXISTS;
  145. using ::wcsncat _LIBCPP_USING_IF_EXISTS;
  146. using ::wcscmp _LIBCPP_USING_IF_EXISTS;
  147. using ::wcscoll _LIBCPP_USING_IF_EXISTS;
  148. using ::wcsncmp _LIBCPP_USING_IF_EXISTS;
  149. using ::wcsxfrm _LIBCPP_USING_IF_EXISTS;
  150. using ::wcschr _LIBCPP_USING_IF_EXISTS;
  151. using ::wcspbrk _LIBCPP_USING_IF_EXISTS;
  152. using ::wcsrchr _LIBCPP_USING_IF_EXISTS;
  153. using ::wcsstr _LIBCPP_USING_IF_EXISTS;
  154. using ::wmemchr _LIBCPP_USING_IF_EXISTS;
  155. using ::wcscspn _LIBCPP_USING_IF_EXISTS;
  156. using ::wcslen _LIBCPP_USING_IF_EXISTS;
  157. using ::wcsspn _LIBCPP_USING_IF_EXISTS;
  158. using ::wcstok _LIBCPP_USING_IF_EXISTS;
  159. using ::wmemcmp _LIBCPP_USING_IF_EXISTS;
  160. using ::wmemcpy _LIBCPP_USING_IF_EXISTS;
  161. using ::wmemmove _LIBCPP_USING_IF_EXISTS;
  162. using ::wmemset _LIBCPP_USING_IF_EXISTS;
  163. using ::wcsftime _LIBCPP_USING_IF_EXISTS;
  164. using ::btowc _LIBCPP_USING_IF_EXISTS;
  165. using ::wctob _LIBCPP_USING_IF_EXISTS;
  166. using ::mbsinit _LIBCPP_USING_IF_EXISTS;
  167. using ::mbrlen _LIBCPP_USING_IF_EXISTS;
  168. using ::mbrtowc _LIBCPP_USING_IF_EXISTS;
  169. using ::wcrtomb _LIBCPP_USING_IF_EXISTS;
  170. using ::mbsrtowcs _LIBCPP_USING_IF_EXISTS;
  171. using ::wcsrtombs _LIBCPP_USING_IF_EXISTS;
  172. using ::getwchar _LIBCPP_USING_IF_EXISTS;
  173. using ::vwscanf _LIBCPP_USING_IF_EXISTS;
  174. using ::wscanf _LIBCPP_USING_IF_EXISTS;
  175. using ::putwchar _LIBCPP_USING_IF_EXISTS;
  176. using ::vwprintf _LIBCPP_USING_IF_EXISTS;
  177. using ::wprintf _LIBCPP_USING_IF_EXISTS;
  178. inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX14 size_t __constexpr_wcslen(const wchar_t* __str) {
  179. #if __has_builtin(__builtin_wcslen)
  180. return __builtin_wcslen(__str);
  181. #else
  182. if (!__libcpp_is_constant_evaluated())
  183. return std::wcslen(__str);
  184. size_t __len = 0;
  185. for (; *__str != L'\0'; ++__str)
  186. ++__len;
  187. return __len;
  188. #endif
  189. }
  190. inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX14 int
  191. __constexpr_wmemcmp(const wchar_t* __lhs, const wchar_t* __rhs, size_t __count) {
  192. #if __has_builtin(__builtin_wmemcmp)
  193. return __builtin_wmemcmp(__lhs, __rhs, __count);
  194. #else
  195. if (!__libcpp_is_constant_evaluated())
  196. return std::wmemcmp(__lhs, __rhs, __count);
  197. for (; __count; --__count, ++__lhs, ++__rhs) {
  198. if (*__lhs < *__rhs)
  199. return -1;
  200. if (*__rhs < *__lhs)
  201. return 1;
  202. }
  203. return 0;
  204. #endif
  205. }
  206. template <class _Tp, class _Up>
  207. _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX14 _Tp* __constexpr_wmemchr(_Tp* __str, _Up __value, size_t __count) {
  208. static_assert(sizeof(_Tp) == sizeof(wchar_t)&& _LIBCPP_ALIGNOF(_Tp) >= _LIBCPP_ALIGNOF(wchar_t) &&
  209. __libcpp_is_trivially_equality_comparable<_Tp, _Tp>::value,
  210. "Calling wmemchr on non-trivially equality comparable types is unsafe.");
  211. #if __has_builtin(__builtin_wmemchr)
  212. if (!__libcpp_is_constant_evaluated()) {
  213. wchar_t __value_buffer = 0;
  214. __builtin_memcpy(&__value_buffer, &__value, sizeof(wchar_t));
  215. return reinterpret_cast<_Tp*>(
  216. __builtin_wmemchr(reinterpret_cast<__apply_cv_t<_Tp, wchar_t>*>(__str), __value_buffer, __count));
  217. }
  218. # if _LIBCPP_STD_VER >= 17
  219. else if constexpr (is_same_v<remove_cv_t<_Tp>, wchar_t>)
  220. return __builtin_wmemchr(__str, __value, __count);
  221. # endif
  222. #endif // __has_builtin(__builtin_wmemchr)
  223. for (; __count; --__count) {
  224. if (*__str == __value)
  225. return __str;
  226. ++__str;
  227. }
  228. return nullptr;
  229. }
  230. _LIBCPP_END_NAMESPACE_STD
  231. #endif // _LIBCPP_CWCHAR