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 10581ad6265SDimitry Andric#include <__assert> // all public C++ headers provide the assertion handler 1060b57cec5SDimitry Andric#include <__config> 107*bdd1243dSDimitry Andric#include <__type_traits/is_constant_evaluated.h> 1080b57cec5SDimitry Andric#include <cwctype> 109*bdd1243dSDimitry Andric 1100b57cec5SDimitry Andric#include <wchar.h> 1110b57cec5SDimitry Andric 112*bdd1243dSDimitry Andric#ifndef _LIBCPP_WCHAR_H 113*bdd1243dSDimitry Andric# error <cwchar> tried including <wchar.h> but didn't find libc++'s <wchar.h> header. \ 114*bdd1243dSDimitry Andric This usually means that your header search paths are not configured properly. \ 115*bdd1243dSDimitry Andric The header search paths should contain the C++ Standard Library headers before \ 116*bdd1243dSDimitry Andric any C Standard Library, and you are probably using compiler flags that make that \ 117*bdd1243dSDimitry Andric not be the case. 118*bdd1243dSDimitry Andric#endif 119*bdd1243dSDimitry Andric 1200b57cec5SDimitry Andric#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER) 1210b57cec5SDimitry Andric# pragma GCC system_header 1220b57cec5SDimitry Andric#endif 1230b57cec5SDimitry Andric 1240b57cec5SDimitry Andric_LIBCPP_BEGIN_NAMESPACE_STD 1250b57cec5SDimitry Andric 126fe6060f1SDimitry Andricusing ::mbstate_t _LIBCPP_USING_IF_EXISTS; 127fe6060f1SDimitry Andricusing ::size_t _LIBCPP_USING_IF_EXISTS; 128fe6060f1SDimitry Andricusing ::tm _LIBCPP_USING_IF_EXISTS; 129fe6060f1SDimitry Andricusing ::wint_t _LIBCPP_USING_IF_EXISTS; 130fe6060f1SDimitry Andricusing ::FILE _LIBCPP_USING_IF_EXISTS; 131fe6060f1SDimitry Andricusing ::fwprintf _LIBCPP_USING_IF_EXISTS; 132fe6060f1SDimitry Andricusing ::fwscanf _LIBCPP_USING_IF_EXISTS; 133fe6060f1SDimitry Andricusing ::swprintf _LIBCPP_USING_IF_EXISTS; 134fe6060f1SDimitry Andricusing ::vfwprintf _LIBCPP_USING_IF_EXISTS; 135fe6060f1SDimitry Andricusing ::vswprintf _LIBCPP_USING_IF_EXISTS; 136fe6060f1SDimitry Andricusing ::swscanf _LIBCPP_USING_IF_EXISTS; 137fe6060f1SDimitry Andricusing ::vfwscanf _LIBCPP_USING_IF_EXISTS; 138fe6060f1SDimitry Andricusing ::vswscanf _LIBCPP_USING_IF_EXISTS; 139fe6060f1SDimitry Andricusing ::fgetwc _LIBCPP_USING_IF_EXISTS; 140fe6060f1SDimitry Andricusing ::fgetws _LIBCPP_USING_IF_EXISTS; 141fe6060f1SDimitry Andricusing ::fputwc _LIBCPP_USING_IF_EXISTS; 142fe6060f1SDimitry Andricusing ::fputws _LIBCPP_USING_IF_EXISTS; 143fe6060f1SDimitry Andricusing ::fwide _LIBCPP_USING_IF_EXISTS; 144fe6060f1SDimitry Andricusing ::getwc _LIBCPP_USING_IF_EXISTS; 145fe6060f1SDimitry Andricusing ::putwc _LIBCPP_USING_IF_EXISTS; 146fe6060f1SDimitry Andricusing ::ungetwc _LIBCPP_USING_IF_EXISTS; 147fe6060f1SDimitry Andricusing ::wcstod _LIBCPP_USING_IF_EXISTS; 148fe6060f1SDimitry Andricusing ::wcstof _LIBCPP_USING_IF_EXISTS; 149fe6060f1SDimitry Andricusing ::wcstold _LIBCPP_USING_IF_EXISTS; 150fe6060f1SDimitry Andricusing ::wcstol _LIBCPP_USING_IF_EXISTS; 151fe6060f1SDimitry Andricusing ::wcstoll _LIBCPP_USING_IF_EXISTS; 152fe6060f1SDimitry Andricusing ::wcstoul _LIBCPP_USING_IF_EXISTS; 153fe6060f1SDimitry Andricusing ::wcstoull _LIBCPP_USING_IF_EXISTS; 154fe6060f1SDimitry Andricusing ::wcscpy _LIBCPP_USING_IF_EXISTS; 155fe6060f1SDimitry Andricusing ::wcsncpy _LIBCPP_USING_IF_EXISTS; 156fe6060f1SDimitry Andricusing ::wcscat _LIBCPP_USING_IF_EXISTS; 157fe6060f1SDimitry Andricusing ::wcsncat _LIBCPP_USING_IF_EXISTS; 158fe6060f1SDimitry Andricusing ::wcscmp _LIBCPP_USING_IF_EXISTS; 159fe6060f1SDimitry Andricusing ::wcscoll _LIBCPP_USING_IF_EXISTS; 160fe6060f1SDimitry Andricusing ::wcsncmp _LIBCPP_USING_IF_EXISTS; 161fe6060f1SDimitry Andricusing ::wcsxfrm _LIBCPP_USING_IF_EXISTS; 162fe6060f1SDimitry Andricusing ::wcschr _LIBCPP_USING_IF_EXISTS; 163fe6060f1SDimitry Andricusing ::wcspbrk _LIBCPP_USING_IF_EXISTS; 164fe6060f1SDimitry Andricusing ::wcsrchr _LIBCPP_USING_IF_EXISTS; 165fe6060f1SDimitry Andricusing ::wcsstr _LIBCPP_USING_IF_EXISTS; 166fe6060f1SDimitry Andricusing ::wmemchr _LIBCPP_USING_IF_EXISTS; 167fe6060f1SDimitry Andricusing ::wcscspn _LIBCPP_USING_IF_EXISTS; 168fe6060f1SDimitry Andricusing ::wcslen _LIBCPP_USING_IF_EXISTS; 169fe6060f1SDimitry Andricusing ::wcsspn _LIBCPP_USING_IF_EXISTS; 170fe6060f1SDimitry Andricusing ::wcstok _LIBCPP_USING_IF_EXISTS; 171fe6060f1SDimitry Andricusing ::wmemcmp _LIBCPP_USING_IF_EXISTS; 172fe6060f1SDimitry Andricusing ::wmemcpy _LIBCPP_USING_IF_EXISTS; 173fe6060f1SDimitry Andricusing ::wmemmove _LIBCPP_USING_IF_EXISTS; 174fe6060f1SDimitry Andricusing ::wmemset _LIBCPP_USING_IF_EXISTS; 175fe6060f1SDimitry Andricusing ::wcsftime _LIBCPP_USING_IF_EXISTS; 176fe6060f1SDimitry Andricusing ::btowc _LIBCPP_USING_IF_EXISTS; 177fe6060f1SDimitry Andricusing ::wctob _LIBCPP_USING_IF_EXISTS; 178fe6060f1SDimitry Andricusing ::mbsinit _LIBCPP_USING_IF_EXISTS; 179fe6060f1SDimitry Andricusing ::mbrlen _LIBCPP_USING_IF_EXISTS; 180fe6060f1SDimitry Andricusing ::mbrtowc _LIBCPP_USING_IF_EXISTS; 181fe6060f1SDimitry Andricusing ::wcrtomb _LIBCPP_USING_IF_EXISTS; 182fe6060f1SDimitry Andricusing ::mbsrtowcs _LIBCPP_USING_IF_EXISTS; 183fe6060f1SDimitry Andricusing ::wcsrtombs _LIBCPP_USING_IF_EXISTS; 1840b57cec5SDimitry Andric 185fe6060f1SDimitry Andricusing ::getwchar _LIBCPP_USING_IF_EXISTS; 186fe6060f1SDimitry Andricusing ::vwscanf _LIBCPP_USING_IF_EXISTS; 187fe6060f1SDimitry Andricusing ::wscanf _LIBCPP_USING_IF_EXISTS; 1880b57cec5SDimitry Andric 189fe6060f1SDimitry Andricusing ::putwchar _LIBCPP_USING_IF_EXISTS; 190fe6060f1SDimitry Andricusing ::vwprintf _LIBCPP_USING_IF_EXISTS; 191fe6060f1SDimitry Andricusing ::wprintf _LIBCPP_USING_IF_EXISTS; 1920b57cec5SDimitry Andric 193*bdd1243dSDimitry Andricinline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX14 size_t __constexpr_wcslen(const wchar_t* __str) { 194*bdd1243dSDimitry Andric#if __has_builtin(__builtin_wcslen) 195*bdd1243dSDimitry Andric return __builtin_wcslen(__str); 196*bdd1243dSDimitry Andric#else 197*bdd1243dSDimitry Andric if (!__libcpp_is_constant_evaluated()) 198*bdd1243dSDimitry Andric return std::wcslen(__str); 199*bdd1243dSDimitry Andric 200*bdd1243dSDimitry Andric size_t __len = 0; 201*bdd1243dSDimitry Andric for (; *__str != L'\0'; ++__str) 202*bdd1243dSDimitry Andric ++__len; 203*bdd1243dSDimitry Andric return __len; 204*bdd1243dSDimitry Andric#endif 205*bdd1243dSDimitry Andric} 206*bdd1243dSDimitry Andric 207*bdd1243dSDimitry Andricinline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX14 int 208*bdd1243dSDimitry Andric__constexpr_wmemcmp(const wchar_t* __lhs, const wchar_t* __rhs, size_t __count) { 209*bdd1243dSDimitry Andric#if __has_builtin(__builtin_wmemcmp) 210*bdd1243dSDimitry Andric return __builtin_wmemcmp(__lhs, __rhs, __count); 211*bdd1243dSDimitry Andric#else 212*bdd1243dSDimitry Andric if (!__libcpp_is_constant_evaluated()) 213*bdd1243dSDimitry Andric return std::wmemcmp(__lhs, __rhs, __count); 214*bdd1243dSDimitry Andric 215*bdd1243dSDimitry Andric for (; __count; --__count, ++__lhs, ++__rhs) { 216*bdd1243dSDimitry Andric if (*__lhs < *__rhs) 217*bdd1243dSDimitry Andric return -1; 218*bdd1243dSDimitry Andric if (*__rhs < *__lhs) 219*bdd1243dSDimitry Andric return 1; 220*bdd1243dSDimitry Andric } 221*bdd1243dSDimitry Andric return 0; 222*bdd1243dSDimitry Andric#endif 223*bdd1243dSDimitry Andric} 224*bdd1243dSDimitry Andric 225*bdd1243dSDimitry Andricinline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX14 const wchar_t* 226*bdd1243dSDimitry Andric__constexpr_wmemchr(const wchar_t* __str, wchar_t __char, size_t __count) { 227*bdd1243dSDimitry Andric#if __has_feature(cxx_constexpr_string_builtins) 228*bdd1243dSDimitry Andric return __builtin_wmemchr(__str, __char, __count); 229*bdd1243dSDimitry Andric#else 230*bdd1243dSDimitry Andric if (!__libcpp_is_constant_evaluated()) 231*bdd1243dSDimitry Andric return std::wmemchr(__str, __char, __count); 232*bdd1243dSDimitry Andric 233*bdd1243dSDimitry Andric for (; __count; --__count) { 234*bdd1243dSDimitry Andric if (*__str == __char) 235*bdd1243dSDimitry Andric return __str; 236*bdd1243dSDimitry Andric ++__str; 237*bdd1243dSDimitry Andric } 238*bdd1243dSDimitry Andric return nullptr; 239*bdd1243dSDimitry Andric#endif 240*bdd1243dSDimitry Andric} 241*bdd1243dSDimitry Andric 2420b57cec5SDimitry Andric_LIBCPP_END_NAMESPACE_STD 2430b57cec5SDimitry Andric 2440b57cec5SDimitry Andric#endif // _LIBCPP_CWCHAR 245