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