| ... | @@ -0,0 +1,274 @@ |
| 1 | // zig patch: serenity support |
| 2 | // -*- C++ -*- |
| 3 | //===-----------------------------------------------------------------------===// |
| 4 | // |
| 5 | // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. |
| 6 | // See https://llvm.org/LICENSE.txt for license information. |
| 7 | // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
| 8 | // |
| 9 | //===----------------------------------------------------------------------===// |
| 10 | |
| 11 | #ifndef _LIBCPP___LOCALE_DIR_LOCALE_BASE_API_SERENITY_H |
| 12 | #define _LIBCPP___LOCALE_DIR_LOCALE_BASE_API_SERENITY_H |
| 13 | |
| 14 | #include <__config> |
| 15 | #include <__cstddef/size_t.h> |
| 16 | #include <__std_mbstate_t.h> |
| 17 | #include <__utility/forward.h> |
| 18 | #include <clocale> // std::lconv |
| 19 | #include <ctype.h> |
| 20 | #include <langinfo.h> |
| 21 | #include <stdio.h> |
| 22 | #include <stdlib.h> |
| 23 | #include <string.h> |
| 24 | #include <time.h> |
| 25 | #if _LIBCPP_HAS_WIDE_CHARACTERS |
| 26 | # include <wchar.h> |
| 27 | # include <wctype.h> |
| 28 | #endif |
| 29 | |
| 30 | #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER) |
| 31 | # pragma GCC system_header |
| 32 | #endif |
| 33 | |
| 34 | _LIBCPP_BEGIN_NAMESPACE_STD |
| 35 | namespace __locale { |
| 36 | |
| 37 | struct __locale_guard { |
| 38 | __locale_guard(locale_t& __loc) : __old_loc_(::uselocale(__loc)) {} |
| 39 | |
| 40 | ~__locale_guard() { |
| 41 | if (__old_loc_) |
| 42 | ::uselocale(__old_loc_); |
| 43 | } |
| 44 | |
| 45 | locale_t __old_loc_; |
| 46 | |
| 47 | __locale_guard(__locale_guard const&) = delete; |
| 48 | __locale_guard& operator=(__locale_guard const&) = delete; |
| 49 | }; |
| 50 | |
| 51 | // |
| 52 | // Locale management |
| 53 | // |
| 54 | #define _LIBCPP_COLLATE_MASK LC_COLLATE_MASK |
| 55 | #define _LIBCPP_CTYPE_MASK LC_CTYPE_MASK |
| 56 | #define _LIBCPP_MONETARY_MASK LC_MONETARY_MASK |
| 57 | #define _LIBCPP_NUMERIC_MASK LC_NUMERIC_MASK |
| 58 | #define _LIBCPP_TIME_MASK LC_TIME_MASK |
| 59 | #define _LIBCPP_MESSAGES_MASK LC_MESSAGES_MASK |
| 60 | #define _LIBCPP_ALL_MASK LC_ALL_MASK |
| 61 | #define _LIBCPP_LC_ALL LC_ALL |
| 62 | |
| 63 | using __locale_t _LIBCPP_NODEBUG = ::locale_t; |
| 64 | #if defined(_LIBCPP_BUILDING_LIBRARY) |
| 65 | using __lconv_t _LIBCPP_NODEBUG = std::lconv; |
| 66 | |
| 67 | inline __locale_t __newlocale(int __category_mask, const char* __locale, __locale_t __base) { |
| 68 | return ::newlocale(__category_mask, __locale, __base); |
| 69 | } |
| 70 | |
| 71 | inline void __freelocale(__locale_t __loc) { ::freelocale(__loc); } |
| 72 | |
| 73 | inline char* __setlocale(int __category, char const* __locale) { return ::setlocale(__category, __locale); } |
| 74 | |
| 75 | inline __lconv_t* __localeconv(__locale_t& __loc) { |
| 76 | __locale_guard __current(__loc); |
| 77 | return ::localeconv(); |
| 78 | } |
| 79 | #endif // _LIBCPP_BUILDING_LIBRARY |
| 80 | |
| 81 | // |
| 82 | // Strtonum functions |
| 83 | // |
| 84 | inline float __strtof(const char* __nptr, char** __endptr, __locale_t) { return ::strtof(__nptr, __endptr); } |
| 85 | |
| 86 | inline double __strtod(const char* __nptr, char** __endptr, __locale_t) { return ::strtod(__nptr, __endptr); } |
| 87 | |
| 88 | inline long double __strtold(const char* __nptr, char** __endptr, __locale_t) { return ::strtold(__nptr, __endptr); } |
| 89 | |
| 90 | // |
| 91 | // Character manipulation functions |
| 92 | // |
| 93 | #if defined(_LIBCPP_BUILDING_LIBRARY) |
| 94 | inline int __toupper(int __c, __locale_t __loc) { |
| 95 | __locale_guard __current(__loc); |
| 96 | return ::toupper(__c); |
| 97 | } |
| 98 | |
| 99 | inline int __tolower(int __c, __locale_t __loc) { |
| 100 | __locale_guard __current(__loc); |
| 101 | return ::tolower(__c); |
| 102 | } |
| 103 | |
| 104 | inline int __strcoll(const char* __s1, const char* __s2, __locale_t __loc) { |
| 105 | __locale_guard __current(__loc); |
| 106 | return ::strcoll(__s1, __s2); |
| 107 | } |
| 108 | |
| 109 | inline size_t __strxfrm(char* __dest, const char* __src, size_t __n, __locale_t __loc) { |
| 110 | __locale_guard __current(__loc); |
| 111 | return ::strxfrm(__dest, __src, __n); |
| 112 | } |
| 113 | |
| 114 | # if _LIBCPP_HAS_WIDE_CHARACTERS |
| 115 | inline int __iswctype(wint_t __c, wctype_t __type, __locale_t __loc) { |
| 116 | __locale_guard __current(__loc); |
| 117 | return ::iswctype(__c, __type); } |
| 118 | |
| 119 | inline int __iswspace(wint_t __c, __locale_t __loc) { |
| 120 | __locale_guard __current(__loc); |
| 121 | return ::iswspace(__c); } |
| 122 | |
| 123 | inline int __iswprint(wint_t __c, __locale_t __loc) { |
| 124 | __locale_guard __current(__loc); |
| 125 | return ::iswprint(__c); } |
| 126 | |
| 127 | inline int __iswcntrl(wint_t __c, __locale_t __loc) { |
| 128 | __locale_guard __current(__loc); |
| 129 | return ::iswcntrl(__c); } |
| 130 | |
| 131 | inline int __iswupper(wint_t __c, __locale_t __loc) { |
| 132 | __locale_guard __current(__loc); |
| 133 | return ::iswupper(__c); } |
| 134 | |
| 135 | inline int __iswlower(wint_t __c, __locale_t __loc) { |
| 136 | __locale_guard __current(__loc); |
| 137 | return ::iswlower(__c); } |
| 138 | |
| 139 | inline int __iswalpha(wint_t __c, __locale_t __loc) { |
| 140 | __locale_guard __current(__loc); |
| 141 | return ::iswalpha(__c); } |
| 142 | |
| 143 | inline int __iswblank(wint_t __c, __locale_t __loc) { |
| 144 | __locale_guard __current(__loc); |
| 145 | return ::iswblank(__c); } |
| 146 | |
| 147 | inline int __iswdigit(wint_t __c, __locale_t __loc) { |
| 148 | __locale_guard __current(__loc); |
| 149 | return ::iswdigit(__c); } |
| 150 | |
| 151 | inline int __iswpunct(wint_t __c, __locale_t __loc) { |
| 152 | __locale_guard __current(__loc); |
| 153 | return ::iswpunct(__c); } |
| 154 | |
| 155 | inline int __iswxdigit(wint_t __c, __locale_t __loc) { |
| 156 | __locale_guard __current(__loc); |
| 157 | return ::iswxdigit(__c); } |
| 158 | |
| 159 | inline wint_t __towupper(wint_t __c, __locale_t __loc) { |
| 160 | __locale_guard __current(__loc); |
| 161 | return ::towupper(__c); } |
| 162 | |
| 163 | inline wint_t __towlower(wint_t __c, __locale_t __loc) { |
| 164 | __locale_guard __current(__loc); |
| 165 | return ::towlower(__c); } |
| 166 | |
| 167 | inline int __wcscoll(const wchar_t* __ws1, const wchar_t* __ws2, __locale_t __loc) { |
| 168 | __locale_guard __current(__loc); |
| 169 | return ::wcscoll(__ws1, __ws2); |
| 170 | } |
| 171 | |
| 172 | inline size_t __wcsxfrm(wchar_t* __dest, const wchar_t* __src, size_t __n, __locale_t __loc) { |
| 173 | __locale_guard __current(__loc); |
| 174 | return ::wcsxfrm(__dest, __src, __n); |
| 175 | } |
| 176 | # endif // _LIBCPP_HAS_WIDE_CHARACTERS |
| 177 | |
| 178 | inline _LIBCPP_HIDE_FROM_ABI const char* __get_locale_encoding(__locale_t __loc) { |
| 179 | return ::nl_langinfo_l(CODESET, __loc); |
| 180 | } |
| 181 | |
| 182 | inline _LIBCPP_ATTRIBUTE_FORMAT(__strftime__, 3, 0) size_t |
| 183 | __strftime(char* __s, size_t __max, const char* __format, const struct tm* __tm, __locale_t __loc) { |
| 184 | __locale_guard __current(__loc); |
| 185 | return ::strftime(__s, __max, __format, __tm); |
| 186 | } |
| 187 | |
| 188 | // |
| 189 | // Other functions |
| 190 | // |
| 191 | inline decltype(MB_CUR_MAX) __mb_len_max(__locale_t __loc) { |
| 192 | __locale_guard __current(__loc); |
| 193 | return MB_CUR_MAX; |
| 194 | } |
| 195 | |
| 196 | # if _LIBCPP_HAS_WIDE_CHARACTERS |
| 197 | inline wint_t __btowc(int __c, __locale_t __loc) { |
| 198 | __locale_guard __current(__loc); |
| 199 | return btowc(__c); |
| 200 | } |
| 201 | |
| 202 | inline int __wctob(wint_t __c, __locale_t __loc) { |
| 203 | __locale_guard __current(__loc); |
| 204 | return wctob(__c); |
| 205 | } |
| 206 | |
| 207 | inline size_t |
| 208 | __wcsnrtombs(char* __dest, const wchar_t** __src, size_t __nwc, size_t __len, mbstate_t* __ps, __locale_t __loc) { |
| 209 | __locale_guard __current(__loc); |
| 210 | return wcsnrtombs(__dest, __src, __nwc, __len, __ps); // wcsnrtombs is a POSIX extension |
| 211 | } |
| 212 | |
| 213 | inline size_t __wcrtomb(char* __s, wchar_t __wc, mbstate_t* __ps, __locale_t __loc) { |
| 214 | __locale_guard __current(__loc); |
| 215 | return wcrtomb(__s, __wc, __ps); |
| 216 | } |
| 217 | |
| 218 | inline size_t |
| 219 | __mbsnrtowcs(wchar_t* __dest, const char** __src, size_t __nms, size_t __len, mbstate_t* __ps, __locale_t __loc) { |
| 220 | __locale_guard __current(__loc); |
| 221 | return mbsnrtowcs(__dest, __src, __nms, __len, __ps); // mbsnrtowcs is a POSIX extension |
| 222 | } |
| 223 | |
| 224 | inline size_t __mbrtowc(wchar_t* __pwc, const char* __s, size_t __n, mbstate_t* __ps, __locale_t __loc) { |
| 225 | __locale_guard __current(__loc); |
| 226 | return mbrtowc(__pwc, __s, __n, __ps); |
| 227 | } |
| 228 | |
| 229 | inline int __mbtowc(wchar_t* __pwc, const char* __pmb, size_t __max, __locale_t __loc) { |
| 230 | __locale_guard __current(__loc); |
| 231 | return mbtowc(__pwc, __pmb, __max); |
| 232 | } |
| 233 | |
| 234 | inline size_t __mbrlen(const char* __s, size_t __n, mbstate_t* __ps, __locale_t __loc) { |
| 235 | __locale_guard __current(__loc); |
| 236 | return mbrlen(__s, __n, __ps); |
| 237 | } |
| 238 | |
| 239 | inline size_t __mbsrtowcs(wchar_t* __dest, const char** __src, size_t __len, mbstate_t* __ps, __locale_t __loc) { |
| 240 | __locale_guard __current(__loc); |
| 241 | return mbsrtowcs(__dest, __src, __len, __ps); |
| 242 | } |
| 243 | # endif // _LIBCPP_HAS_WIDE_CHARACTERS |
| 244 | #endif // _LIBCPP_BUILDING_LIBRARY |
| 245 | |
| 246 | _LIBCPP_DIAGNOSTIC_PUSH |
| 247 | _LIBCPP_CLANG_DIAGNOSTIC_IGNORED("-Wgcc-compat") |
| 248 | _LIBCPP_GCC_DIAGNOSTIC_IGNORED("-Wformat-nonliteral") // GCC doesn't support [[gnu::format]] on variadic templates |
| 249 | #ifdef _LIBCPP_COMPILER_CLANG_BASED |
| 250 | # define _LIBCPP_VARIADIC_ATTRIBUTE_FORMAT(...) _LIBCPP_ATTRIBUTE_FORMAT(__VA_ARGS__) |
| 251 | #else |
| 252 | # define _LIBCPP_VARIADIC_ATTRIBUTE_FORMAT(...) /* nothing */ |
| 253 | #endif |
| 254 | |
| 255 | template <class... _Args> |
| 256 | _LIBCPP_VARIADIC_ATTRIBUTE_FORMAT(__printf__, 4, 5) |
| 257 | int __snprintf(char* __s, size_t __n, __locale_t __loc, const char* __format, _Args&&... __args) { |
| 258 | __locale_guard __current(__loc); |
| 259 | return ::snprintf(__s, __n, __format, std::forward<_Args>(__args)...); |
| 260 | } |
| 261 | |
| 262 | template <class... _Args> |
| 263 | _LIBCPP_VARIADIC_ATTRIBUTE_FORMAT(__printf__, 3, 4) |
| 264 | int __asprintf(char** __s, __locale_t __loc, const char* __format, _Args&&... __args) { |
| 265 | __locale_guard __current(__loc); |
| 266 | return ::asprintf(__s, __format, std::forward<_Args>(__args)...); // non-standard |
| 267 | } |
| 268 | _LIBCPP_DIAGNOSTIC_POP |
| 269 | #undef _LIBCPP_VARIADIC_ATTRIBUTE_FORMAT |
| 270 | |
| 271 | } // namespace __locale |
| 272 | _LIBCPP_END_NAMESPACE_STD |
| 273 | |
| 274 | #endif // _LIBCPP___LOCALE_DIR_LOCALE_BASE_API_SERENITY_H |