xref: /freebsd/contrib/llvm-project/libcxx/include/cwchar (revision a7b2d7f261b81d141195c4204fdc4a0dc97a146e)
10b57cec5SDimitry Andric// -*- C++ -*-
2349cc55cSDimitry Andric//===----------------------------------------------------------------------===//
30b57cec5SDimitry Andric//
40b57cec5SDimitry Andric// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
50b57cec5SDimitry Andric// See https://llvm.org/LICENSE.txt for license information.
60b57cec5SDimitry Andric// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
70b57cec5SDimitry Andric//
80b57cec5SDimitry Andric//===----------------------------------------------------------------------===//
90b57cec5SDimitry Andric
100b57cec5SDimitry Andric#ifndef _LIBCPP_CWCHAR
110b57cec5SDimitry Andric#define _LIBCPP_CWCHAR
120b57cec5SDimitry Andric
130b57cec5SDimitry Andric/*
140b57cec5SDimitry Andric    cwchar synopsis
150b57cec5SDimitry Andric
160b57cec5SDimitry AndricMacros:
170b57cec5SDimitry Andric
180b57cec5SDimitry Andric    NULL
190b57cec5SDimitry Andric    WCHAR_MAX
200b57cec5SDimitry Andric    WCHAR_MIN
210b57cec5SDimitry Andric    WEOF
220b57cec5SDimitry Andric
230b57cec5SDimitry Andricnamespace std
240b57cec5SDimitry Andric{
250b57cec5SDimitry Andric
260b57cec5SDimitry AndricTypes:
270b57cec5SDimitry Andric
280b57cec5SDimitry Andric    mbstate_t
290b57cec5SDimitry Andric    size_t
300b57cec5SDimitry Andric    tm
310b57cec5SDimitry Andric    wint_t
320b57cec5SDimitry Andric
330b57cec5SDimitry Andricint fwprintf(FILE* restrict stream, const wchar_t* restrict format, ...);
340b57cec5SDimitry Andricint fwscanf(FILE* restrict stream, const wchar_t* restrict format, ...);
350b57cec5SDimitry Andricint swprintf(wchar_t* restrict s, size_t n, const wchar_t* restrict format, ...);
360b57cec5SDimitry Andricint swscanf(const wchar_t* restrict s, const wchar_t* restrict format, ...);
370b57cec5SDimitry Andricint vfwprintf(FILE* restrict stream, const wchar_t* restrict format, va_list arg);
380b57cec5SDimitry Andricint vfwscanf(FILE* restrict stream, const wchar_t* restrict format, va_list arg);  // C99
390b57cec5SDimitry Andricint vswprintf(wchar_t* restrict s, size_t n, const wchar_t* restrict format, va_list arg);
400b57cec5SDimitry Andricint vswscanf(const wchar_t* restrict s, const wchar_t* restrict format, va_list arg);  // C99
410b57cec5SDimitry Andricint vwprintf(const wchar_t* restrict format, va_list arg);
420b57cec5SDimitry Andricint vwscanf(const wchar_t* restrict format, va_list arg);  // C99
430b57cec5SDimitry Andricint wprintf(const wchar_t* restrict format, ...);
440b57cec5SDimitry Andricint wscanf(const wchar_t* restrict format, ...);
450b57cec5SDimitry Andricwint_t fgetwc(FILE* stream);
460b57cec5SDimitry Andricwchar_t* fgetws(wchar_t* restrict s, int n, FILE* restrict stream);
470b57cec5SDimitry Andricwint_t fputwc(wchar_t c, FILE* stream);
480b57cec5SDimitry Andricint fputws(const wchar_t* restrict s, FILE* restrict stream);
490b57cec5SDimitry Andricint fwide(FILE* stream, int mode);
500b57cec5SDimitry Andricwint_t getwc(FILE* stream);
510b57cec5SDimitry Andricwint_t getwchar();
520b57cec5SDimitry Andricwint_t putwc(wchar_t c, FILE* stream);
530b57cec5SDimitry Andricwint_t putwchar(wchar_t c);
540b57cec5SDimitry Andricwint_t ungetwc(wint_t c, FILE* stream);
550b57cec5SDimitry Andricdouble wcstod(const wchar_t* restrict nptr, wchar_t** restrict endptr);
560b57cec5SDimitry Andricfloat wcstof(const wchar_t* restrict nptr, wchar_t** restrict endptr);         // C99
570b57cec5SDimitry Andriclong double wcstold(const wchar_t* restrict nptr, wchar_t** restrict endptr);  // C99
580b57cec5SDimitry Andriclong wcstol(const wchar_t* restrict nptr, wchar_t** restrict endptr, int base);
590b57cec5SDimitry Andriclong long wcstoll(const wchar_t* restrict nptr, wchar_t** restrict endptr, int base);  // C99
600b57cec5SDimitry Andricunsigned long wcstoul(const wchar_t* restrict nptr, wchar_t** restrict endptr, int base);
610b57cec5SDimitry Andricunsigned long long wcstoull(const wchar_t* restrict nptr, wchar_t** restrict endptr, int base);  // C99
620b57cec5SDimitry Andricwchar_t* wcscpy(wchar_t* restrict s1, const wchar_t* restrict s2);
630b57cec5SDimitry Andricwchar_t* wcsncpy(wchar_t* restrict s1, const wchar_t* restrict s2, size_t n);
640b57cec5SDimitry Andricwchar_t* wcscat(wchar_t* restrict s1, const wchar_t* restrict s2);
650b57cec5SDimitry Andricwchar_t* wcsncat(wchar_t* restrict s1, const wchar_t* restrict s2, size_t n);
660b57cec5SDimitry Andricint wcscmp(const wchar_t* s1, const wchar_t* s2);
670b57cec5SDimitry Andricint wcscoll(const wchar_t* s1, const wchar_t* s2);
680b57cec5SDimitry Andricint wcsncmp(const wchar_t* s1, const wchar_t* s2, size_t n);
690b57cec5SDimitry Andricsize_t wcsxfrm(wchar_t* restrict s1, const wchar_t* restrict s2, size_t n);
700b57cec5SDimitry Andricconst wchar_t* wcschr(const wchar_t* s, wchar_t c);
710b57cec5SDimitry Andric      wchar_t* wcschr(      wchar_t* s, wchar_t c);
720b57cec5SDimitry Andricsize_t wcscspn(const wchar_t* s1, const wchar_t* s2);
730b57cec5SDimitry Andricsize_t wcslen(const wchar_t* s);
740b57cec5SDimitry Andricconst wchar_t* wcspbrk(const wchar_t* s1, const wchar_t* s2);
750b57cec5SDimitry Andric      wchar_t* wcspbrk(      wchar_t* s1, const wchar_t* s2);
760b57cec5SDimitry Andricconst wchar_t* wcsrchr(const wchar_t* s, wchar_t c);
770b57cec5SDimitry Andric      wchar_t* wcsrchr(      wchar_t* s, wchar_t c);
780b57cec5SDimitry Andricsize_t wcsspn(const wchar_t* s1, const wchar_t* s2);
790b57cec5SDimitry Andricconst wchar_t* wcsstr(const wchar_t* s1, const wchar_t* s2);
800b57cec5SDimitry Andric      wchar_t* wcsstr(      wchar_t* s1, const wchar_t* s2);
810b57cec5SDimitry Andricwchar_t* wcstok(wchar_t* restrict s1, const wchar_t* restrict s2, wchar_t** restrict ptr);
820b57cec5SDimitry Andricconst wchar_t* wmemchr(const wchar_t* s, wchar_t c, size_t n);
830b57cec5SDimitry Andric      wchar_t* wmemchr(      wchar_t* s, wchar_t c, size_t n);
840b57cec5SDimitry Andricint wmemcmp(wchar_t* restrict s1, const wchar_t* restrict s2, size_t n);
850b57cec5SDimitry Andricwchar_t* wmemcpy(wchar_t* restrict s1, const wchar_t* restrict s2, size_t n);
860b57cec5SDimitry Andricwchar_t* wmemmove(wchar_t* s1, const wchar_t* s2, size_t n);
870b57cec5SDimitry Andricwchar_t* wmemset(wchar_t* s, wchar_t c, size_t n);
880b57cec5SDimitry Andricsize_t wcsftime(wchar_t* restrict s, size_t maxsize, const wchar_t* restrict format,
890b57cec5SDimitry Andric                const tm* restrict timeptr);
900b57cec5SDimitry Andricwint_t btowc(int c);
910b57cec5SDimitry Andricint wctob(wint_t c);
920b57cec5SDimitry Andricint mbsinit(const mbstate_t* ps);
930b57cec5SDimitry Andricsize_t mbrlen(const char* restrict s, size_t n, mbstate_t* restrict ps);
940b57cec5SDimitry Andricsize_t mbrtowc(wchar_t* restrict pwc, const char* restrict s, size_t n, mbstate_t* restrict ps);
950b57cec5SDimitry Andricsize_t wcrtomb(char* restrict s, wchar_t wc, mbstate_t* restrict ps);
960b57cec5SDimitry Andricsize_t mbsrtowcs(wchar_t* restrict dst, const char** restrict src, size_t len,
970b57cec5SDimitry Andric                 mbstate_t* restrict ps);
980b57cec5SDimitry Andricsize_t wcsrtombs(char* restrict dst, const wchar_t** restrict src, size_t len,
990b57cec5SDimitry Andric                 mbstate_t* restrict ps);
1000b57cec5SDimitry Andric
1010b57cec5SDimitry Andric}  // std
1020b57cec5SDimitry Andric
1030b57cec5SDimitry Andric*/
1040b57cec5SDimitry Andric
1050b57cec5SDimitry Andric#include <__config>
1060fca6ea1SDimitry Andric#include <__type_traits/copy_cv.h>
107bdd1243dSDimitry Andric#include <__type_traits/is_constant_evaluated.h>
10806c3fb27SDimitry Andric#include <__type_traits/is_equality_comparable.h>
10906c3fb27SDimitry Andric#include <__type_traits/is_same.h>
11006c3fb27SDimitry Andric#include <__type_traits/remove_cv.h>
1110b57cec5SDimitry Andric#include <cwctype>
112bdd1243dSDimitry Andric
1130b57cec5SDimitry Andric#include <wchar.h>
1140b57cec5SDimitry Andric
115bdd1243dSDimitry Andric#ifndef _LIBCPP_WCHAR_H
116bdd1243dSDimitry Andric#   error <cwchar> tried including <wchar.h> but didn't find libc++'s <wchar.h> header. \
117bdd1243dSDimitry Andric          This usually means that your header search paths are not configured properly. \
118bdd1243dSDimitry Andric          The header search paths should contain the C++ Standard Library headers before \
119bdd1243dSDimitry Andric          any C Standard Library, and you are probably using compiler flags that make that \
120bdd1243dSDimitry Andric          not be the case.
121bdd1243dSDimitry Andric#endif
122bdd1243dSDimitry Andric
1230b57cec5SDimitry Andric#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
1240b57cec5SDimitry Andric#  pragma GCC system_header
1250b57cec5SDimitry Andric#endif
1260b57cec5SDimitry Andric
1270b57cec5SDimitry Andric_LIBCPP_BEGIN_NAMESPACE_STD
1280b57cec5SDimitry Andric
129fe6060f1SDimitry Andricusing ::mbstate_t _LIBCPP_USING_IF_EXISTS;
130fe6060f1SDimitry Andricusing ::size_t _LIBCPP_USING_IF_EXISTS;
131fe6060f1SDimitry Andricusing ::tm _LIBCPP_USING_IF_EXISTS;
132fe6060f1SDimitry Andricusing ::wint_t _LIBCPP_USING_IF_EXISTS;
133fe6060f1SDimitry Andricusing ::FILE _LIBCPP_USING_IF_EXISTS;
134fe6060f1SDimitry Andricusing ::fwprintf _LIBCPP_USING_IF_EXISTS;
135fe6060f1SDimitry Andricusing ::fwscanf _LIBCPP_USING_IF_EXISTS;
136fe6060f1SDimitry Andricusing ::swprintf _LIBCPP_USING_IF_EXISTS;
137fe6060f1SDimitry Andricusing ::vfwprintf _LIBCPP_USING_IF_EXISTS;
138fe6060f1SDimitry Andricusing ::vswprintf _LIBCPP_USING_IF_EXISTS;
139fe6060f1SDimitry Andricusing ::swscanf _LIBCPP_USING_IF_EXISTS;
140fe6060f1SDimitry Andricusing ::vfwscanf _LIBCPP_USING_IF_EXISTS;
141fe6060f1SDimitry Andricusing ::vswscanf _LIBCPP_USING_IF_EXISTS;
142fe6060f1SDimitry Andricusing ::fgetwc _LIBCPP_USING_IF_EXISTS;
143fe6060f1SDimitry Andricusing ::fgetws _LIBCPP_USING_IF_EXISTS;
144fe6060f1SDimitry Andricusing ::fputwc _LIBCPP_USING_IF_EXISTS;
145fe6060f1SDimitry Andricusing ::fputws _LIBCPP_USING_IF_EXISTS;
146fe6060f1SDimitry Andricusing ::fwide _LIBCPP_USING_IF_EXISTS;
147fe6060f1SDimitry Andricusing ::getwc _LIBCPP_USING_IF_EXISTS;
148fe6060f1SDimitry Andricusing ::putwc _LIBCPP_USING_IF_EXISTS;
149fe6060f1SDimitry Andricusing ::ungetwc _LIBCPP_USING_IF_EXISTS;
150fe6060f1SDimitry Andricusing ::wcstod _LIBCPP_USING_IF_EXISTS;
151fe6060f1SDimitry Andricusing ::wcstof _LIBCPP_USING_IF_EXISTS;
152fe6060f1SDimitry Andricusing ::wcstold _LIBCPP_USING_IF_EXISTS;
153fe6060f1SDimitry Andricusing ::wcstol _LIBCPP_USING_IF_EXISTS;
154*6b4981dfSDimitry Andric#if defined(__FreeBSD__) && defined(__LONG_LONG_SUPPORTED)
155fe6060f1SDimitry Andricusing ::wcstoll _LIBCPP_USING_IF_EXISTS;
156*6b4981dfSDimitry Andric#endif
157fe6060f1SDimitry Andricusing ::wcstoul _LIBCPP_USING_IF_EXISTS;
158*6b4981dfSDimitry Andric#if defined(__FreeBSD__) && defined(__LONG_LONG_SUPPORTED)
159fe6060f1SDimitry Andricusing ::wcstoull _LIBCPP_USING_IF_EXISTS;
160*6b4981dfSDimitry Andric#endif
161fe6060f1SDimitry Andricusing ::wcscpy _LIBCPP_USING_IF_EXISTS;
162fe6060f1SDimitry Andricusing ::wcsncpy _LIBCPP_USING_IF_EXISTS;
163fe6060f1SDimitry Andricusing ::wcscat _LIBCPP_USING_IF_EXISTS;
164fe6060f1SDimitry Andricusing ::wcsncat _LIBCPP_USING_IF_EXISTS;
165fe6060f1SDimitry Andricusing ::wcscmp _LIBCPP_USING_IF_EXISTS;
166fe6060f1SDimitry Andricusing ::wcscoll _LIBCPP_USING_IF_EXISTS;
167fe6060f1SDimitry Andricusing ::wcsncmp _LIBCPP_USING_IF_EXISTS;
168fe6060f1SDimitry Andricusing ::wcsxfrm _LIBCPP_USING_IF_EXISTS;
169fe6060f1SDimitry Andricusing ::wcschr _LIBCPP_USING_IF_EXISTS;
170fe6060f1SDimitry Andricusing ::wcspbrk _LIBCPP_USING_IF_EXISTS;
171fe6060f1SDimitry Andricusing ::wcsrchr _LIBCPP_USING_IF_EXISTS;
172fe6060f1SDimitry Andricusing ::wcsstr _LIBCPP_USING_IF_EXISTS;
173fe6060f1SDimitry Andricusing ::wmemchr _LIBCPP_USING_IF_EXISTS;
174fe6060f1SDimitry Andricusing ::wcscspn _LIBCPP_USING_IF_EXISTS;
175fe6060f1SDimitry Andricusing ::wcslen _LIBCPP_USING_IF_EXISTS;
176fe6060f1SDimitry Andricusing ::wcsspn _LIBCPP_USING_IF_EXISTS;
177fe6060f1SDimitry Andricusing ::wcstok _LIBCPP_USING_IF_EXISTS;
178fe6060f1SDimitry Andricusing ::wmemcmp _LIBCPP_USING_IF_EXISTS;
179fe6060f1SDimitry Andricusing ::wmemcpy _LIBCPP_USING_IF_EXISTS;
180fe6060f1SDimitry Andricusing ::wmemmove _LIBCPP_USING_IF_EXISTS;
181fe6060f1SDimitry Andricusing ::wmemset _LIBCPP_USING_IF_EXISTS;
182fe6060f1SDimitry Andricusing ::wcsftime _LIBCPP_USING_IF_EXISTS;
183fe6060f1SDimitry Andricusing ::btowc _LIBCPP_USING_IF_EXISTS;
184fe6060f1SDimitry Andricusing ::wctob _LIBCPP_USING_IF_EXISTS;
185fe6060f1SDimitry Andricusing ::mbsinit _LIBCPP_USING_IF_EXISTS;
186fe6060f1SDimitry Andricusing ::mbrlen _LIBCPP_USING_IF_EXISTS;
187fe6060f1SDimitry Andricusing ::mbrtowc _LIBCPP_USING_IF_EXISTS;
188fe6060f1SDimitry Andricusing ::wcrtomb _LIBCPP_USING_IF_EXISTS;
189fe6060f1SDimitry Andricusing ::mbsrtowcs _LIBCPP_USING_IF_EXISTS;
190fe6060f1SDimitry Andricusing ::wcsrtombs _LIBCPP_USING_IF_EXISTS;
1910b57cec5SDimitry Andric
192fe6060f1SDimitry Andricusing ::getwchar _LIBCPP_USING_IF_EXISTS;
193fe6060f1SDimitry Andricusing ::vwscanf _LIBCPP_USING_IF_EXISTS;
194fe6060f1SDimitry Andricusing ::wscanf _LIBCPP_USING_IF_EXISTS;
1950b57cec5SDimitry Andric
196fe6060f1SDimitry Andricusing ::putwchar _LIBCPP_USING_IF_EXISTS;
197fe6060f1SDimitry Andricusing ::vwprintf _LIBCPP_USING_IF_EXISTS;
198fe6060f1SDimitry Andricusing ::wprintf _LIBCPP_USING_IF_EXISTS;
1990b57cec5SDimitry Andric
200bdd1243dSDimitry Andricinline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX14 size_t __constexpr_wcslen(const wchar_t* __str) {
201bdd1243dSDimitry Andric#if __has_builtin(__builtin_wcslen)
202bdd1243dSDimitry Andric  return __builtin_wcslen(__str);
203bdd1243dSDimitry Andric#else
204bdd1243dSDimitry Andric  if (!__libcpp_is_constant_evaluated())
205bdd1243dSDimitry Andric    return std::wcslen(__str);
206bdd1243dSDimitry Andric
207bdd1243dSDimitry Andric  size_t __len = 0;
208bdd1243dSDimitry Andric  for (; *__str != L'\0'; ++__str)
209bdd1243dSDimitry Andric    ++__len;
210bdd1243dSDimitry Andric  return __len;
211bdd1243dSDimitry Andric#endif
212bdd1243dSDimitry Andric}
213bdd1243dSDimitry Andric
214bdd1243dSDimitry Andricinline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX14 int
215bdd1243dSDimitry Andric__constexpr_wmemcmp(const wchar_t* __lhs, const wchar_t* __rhs, size_t __count) {
216bdd1243dSDimitry Andric#if __has_builtin(__builtin_wmemcmp)
217bdd1243dSDimitry Andric  return __builtin_wmemcmp(__lhs, __rhs, __count);
218bdd1243dSDimitry Andric#else
219bdd1243dSDimitry Andric  if (!__libcpp_is_constant_evaluated())
220bdd1243dSDimitry Andric    return std::wmemcmp(__lhs, __rhs, __count);
221bdd1243dSDimitry Andric
222bdd1243dSDimitry Andric  for (; __count; --__count, ++__lhs, ++__rhs) {
223bdd1243dSDimitry Andric    if (*__lhs < *__rhs)
224bdd1243dSDimitry Andric      return -1;
225bdd1243dSDimitry Andric    if (*__rhs < *__lhs)
226bdd1243dSDimitry Andric      return 1;
227bdd1243dSDimitry Andric  }
228bdd1243dSDimitry Andric  return 0;
229bdd1243dSDimitry Andric#endif
230bdd1243dSDimitry Andric}
231bdd1243dSDimitry Andric
23206c3fb27SDimitry Andrictemplate <class _Tp, class _Up>
23306c3fb27SDimitry Andric_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX14 _Tp* __constexpr_wmemchr(_Tp* __str, _Up __value, size_t __count) {
23406c3fb27SDimitry Andric  static_assert(sizeof(_Tp) == sizeof(wchar_t)&& _LIBCPP_ALIGNOF(_Tp) >= _LIBCPP_ALIGNOF(wchar_t) &&
23506c3fb27SDimitry Andric                    __libcpp_is_trivially_equality_comparable<_Tp, _Tp>::value,
23606c3fb27SDimitry Andric                "Calling wmemchr on non-trivially equality comparable types is unsafe.");
23706c3fb27SDimitry Andric
23806c3fb27SDimitry Andric#if __has_builtin(__builtin_wmemchr)
23906c3fb27SDimitry Andric  if (!__libcpp_is_constant_evaluated()) {
24006c3fb27SDimitry Andric    wchar_t __value_buffer = 0;
24106c3fb27SDimitry Andric    __builtin_memcpy(&__value_buffer, &__value, sizeof(wchar_t));
24206c3fb27SDimitry Andric    return reinterpret_cast<_Tp*>(
2430fca6ea1SDimitry Andric        __builtin_wmemchr(reinterpret_cast<__copy_cv_t<_Tp, wchar_t>*>(__str), __value_buffer, __count));
24406c3fb27SDimitry Andric  }
24506c3fb27SDimitry Andric#  if _LIBCPP_STD_VER >= 17
24606c3fb27SDimitry Andric  else if constexpr (is_same_v<remove_cv_t<_Tp>, wchar_t>)
24706c3fb27SDimitry Andric    return __builtin_wmemchr(__str, __value, __count);
24806c3fb27SDimitry Andric#  endif
24906c3fb27SDimitry Andric#endif // __has_builtin(__builtin_wmemchr)
250bdd1243dSDimitry Andric
251bdd1243dSDimitry Andric  for (; __count; --__count) {
25206c3fb27SDimitry Andric    if (*__str == __value)
253bdd1243dSDimitry Andric      return __str;
254bdd1243dSDimitry Andric    ++__str;
255bdd1243dSDimitry Andric  }
256bdd1243dSDimitry Andric  return nullptr;
257bdd1243dSDimitry Andric}
258bdd1243dSDimitry Andric
2590b57cec5SDimitry Andric_LIBCPP_END_NAMESPACE_STD
2600b57cec5SDimitry Andric
2610b57cec5SDimitry Andric#endif // _LIBCPP_CWCHAR
262