1b096817theraven/*-
2d3308bcpfg * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
3d3308bcpfg *
4b096817theraven * Copyright (c) 2011 The FreeBSD Foundation
5b096817theraven * All rights reserved.
6b096817theraven *
7b096817theraven * This software was developed by David Chisnall under sponsorship from
8b096817theraven * the FreeBSD Foundation.
9b096817theraven *
10b096817theraven * Redistribution and use in source and binary forms, with or without
11b096817theraven * modification, are permitted provided that the following conditions
12b096817theraven * are met:
13b096817theraven * 1. Redistributions of source code must retain the above copyright
14b096817theraven *    notice, this list of conditions and the following disclaimer.
15b096817theraven * 2. Redistributions in binary form must reproduce the above copyright
16b096817theraven *    notice, this list of conditions and the following disclaimer in the
17b096817theraven *    documentation and/or other materials provided with the distribution.
18b096817theraven *
19b096817theraven * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
20b096817theraven * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21b096817theraven * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22b096817theraven * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
23b096817theraven * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24b096817theraven * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25b096817theraven * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26b096817theraven * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27b096817theraven * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28b096817theraven * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29b096817theraven * SUCH DAMAGE.
30b096817theraven *
31b096817theraven * $FreeBSD$
32b096817theraven */
33b096817theraven
34b096817theraven
35b096817theraven#if	(defined(_XLOCALE_WCTYPES) && !defined(_XLOCALE_WCTYPE_H)) || \
36b096817theraven	(!defined(_XLOCALE_WCTYPES) && !defined(_XLOCALE_CTYPE_H))
37b096817theraven
38b096817theraven#ifdef _XLOCALE_WCTYPES
39b096817theraven#define _XLOCALE_WCTYPE_H
40b096817theraven#else
41b096817theraven#define _XLOCALE_CTYPE_H
42b096817theraven#endif
43b096817theraven
44b096817theraven#ifndef _LOCALE_T_DEFINED
45b096817theraven#define _LOCALE_T_DEFINED
46b096817theraventypedef struct	_xlocale *locale_t;
47b096817theraven#endif
48b096817theraven
49b096817theraven#ifndef _XLOCALE_RUN_FUNCTIONS_DEFINED
50b096817theraven#define _XLOCALE_RUN_FUNCTIONS_DEFINED 1
51b096817theravenunsigned long	 ___runetype_l(__ct_rune_t, locale_t) __pure;
52b096817theraven__ct_rune_t	 ___tolower_l(__ct_rune_t, locale_t) __pure;
53b096817theraven__ct_rune_t	 ___toupper_l(__ct_rune_t, locale_t) __pure;
54b096817theraven_RuneLocale	*__runes_for_locale(locale_t, int*);
55b096817theraven#endif
56b096817theraven
57b096817theraven#ifndef _XLOCALE_INLINE
5818400f0demon#if defined(__GNUC__) && !defined(__GNUC_STDC_INLINE__)
59b096817theraven/* GNU89 inline has nonstandard semantics. */
60b8d73c5dim#define _XLOCALE_INLINE extern __inline
61b096817theraven#else
62b096817theraven/* Hack to work around people who define inline away */
63b096817theraven#ifdef inline
64b8d73c5dim#define _XLOCALE_INLINE static __inline
65b096817theraven#else
66b096817theraven/* Define with C++ / C99 compatible semantics */
67b096817theraven#define _XLOCALE_INLINE inline
68b096817theraven#endif
69b096817theraven#endif
70b096817theraven#endif /* _XLOCALE_INLINE */
71b096817theraven
72b096817theraven#ifdef _XLOCALE_WCTYPES
7375aeb0ctheraven_XLOCALE_INLINE int
742895bb9theraven__maskrune_l(__ct_rune_t __c, unsigned long __f, locale_t __loc);
7598f4b90theraven_XLOCALE_INLINE int
762895bb9theraven__istype_l(__ct_rune_t __c, unsigned long __f, locale_t __loc);
772895bb9theraven
782895bb9theraven_XLOCALE_INLINE int
7975aeb0ctheraven__maskrune_l(__ct_rune_t __c, unsigned long __f, locale_t __loc)
80b096817theraven{
8175aeb0ctheraven	int __limit;
8275aeb0ctheraven	_RuneLocale *runes = __runes_for_locale(__loc, &__limit);
83e2f6a7etheraven	return ((__c < 0 || __c >= _CACHED_RUNES) ? ___runetype_l(__c, __loc) :
84e2f6a7etheraven	        runes->__runetype[__c]) & __f;
85b096817theraven}
86b096817theraven
8798f4b90theraven_XLOCALE_INLINE int
8875aeb0ctheraven__istype_l(__ct_rune_t __c, unsigned long __f, locale_t __loc)
89b096817theraven{
9075aeb0ctheraven	return (!!__maskrune_l(__c, __f, __loc));
91b096817theraven}
92b096817theraven
93b096817theraven#define XLOCALE_ISCTYPE(fname, cat) \
94b096817theraven		_XLOCALE_INLINE int isw##fname##_l(int, locale_t);\
95b096817theraven		_XLOCALE_INLINE int isw##fname##_l(int __c, locale_t __l)\
96b096817theraven		{ return __istype_l(__c, cat, __l); }
97b096817theraven#else
9875aeb0ctheraven_XLOCALE_INLINE int
995c44253theraven__sbmaskrune_l(__ct_rune_t __c, unsigned long __f, locale_t __loc);
1005c44253theraven_XLOCALE_INLINE int
1015c44253theraven__sbistype_l(__ct_rune_t __c, unsigned long __f, locale_t __loc);
1025c44253theraven
1035c44253theraven_XLOCALE_INLINE int
10475aeb0ctheraven__sbmaskrune_l(__ct_rune_t __c, unsigned long __f, locale_t __loc)
105b096817theraven{
10675aeb0ctheraven	int __limit;
10775aeb0ctheraven	_RuneLocale *runes = __runes_for_locale(__loc, &__limit);
10875aeb0ctheraven	return (__c < 0 || __c >= __limit) ? 0 :
10975aeb0ctheraven	       runes->__runetype[__c] & __f;
110b096817theraven}
111b096817theraven
11275aeb0ctheraven_XLOCALE_INLINE int
11375aeb0ctheraven__sbistype_l(__ct_rune_t __c, unsigned long __f, locale_t __loc)
114b096817theraven{
11575aeb0ctheraven	return (!!__sbmaskrune_l(__c, __f, __loc));
116b096817theraven}
117b096817theraven
11875aeb0ctheraven#define XLOCALE_ISCTYPE(__fname, __cat) \
11975aeb0ctheraven		_XLOCALE_INLINE int is##__fname##_l(int, locale_t); \
12075aeb0ctheraven		_XLOCALE_INLINE int is##__fname##_l(int __c, locale_t __l)\
12175aeb0ctheraven		{ return __sbistype_l(__c, __cat, __l); }
122b096817theraven#endif
123b096817theraven
124979669dbaptXLOCALE_ISCTYPE(alnum, _CTYPE_A|_CTYPE_D|_CTYPE_N)
125b096817theravenXLOCALE_ISCTYPE(alpha, _CTYPE_A)
126b096817theravenXLOCALE_ISCTYPE(blank, _CTYPE_B)
127b096817theravenXLOCALE_ISCTYPE(cntrl, _CTYPE_C)
128b096817theravenXLOCALE_ISCTYPE(digit, _CTYPE_D)
129b096817theravenXLOCALE_ISCTYPE(graph, _CTYPE_G)
130b096817theravenXLOCALE_ISCTYPE(hexnumber, _CTYPE_X)
131b096817theravenXLOCALE_ISCTYPE(ideogram, _CTYPE_I)
132b096817theravenXLOCALE_ISCTYPE(lower, _CTYPE_L)
133979669dbaptXLOCALE_ISCTYPE(number, _CTYPE_D|_CTYPE_N)
134b096817theravenXLOCALE_ISCTYPE(phonogram, _CTYPE_Q)
135b096817theravenXLOCALE_ISCTYPE(print, _CTYPE_R)
136b096817theravenXLOCALE_ISCTYPE(punct, _CTYPE_P)
137b096817theravenXLOCALE_ISCTYPE(rune, 0xFFFFFF00L)
138b096817theravenXLOCALE_ISCTYPE(space, _CTYPE_S)
139b096817theravenXLOCALE_ISCTYPE(special, _CTYPE_T)
140b096817theravenXLOCALE_ISCTYPE(upper, _CTYPE_U)
141b096817theravenXLOCALE_ISCTYPE(xdigit, _CTYPE_X)
142b096817theraven#undef XLOCALE_ISCTYPE
143b096817theraven
144b096817theraven#ifdef _XLOCALE_WCTYPES
145b096817theraven_XLOCALE_INLINE int towlower_l(int, locale_t);
146b096817theraven_XLOCALE_INLINE int __wcwidth_l(__ct_rune_t, locale_t);
147b096817theraven_XLOCALE_INLINE int towupper_l(int, locale_t);
148b096817theraven
149b096817theraven_XLOCALE_INLINE int towlower_l(int __c, locale_t __l)
150b096817theraven{
15175aeb0ctheraven	int __limit;
15275aeb0ctheraven	_RuneLocale *__runes = __runes_for_locale(__l, &__limit);
153b096817theraven	return (__c < 0 || __c >= _CACHED_RUNES) ? ___tolower_l(__c, __l) :
154b096817theraven	       __runes->__maplower[__c];
155b096817theraven}
156b096817theraven_XLOCALE_INLINE int towupper_l(int __c, locale_t __l)
157b096817theraven{
15875aeb0ctheraven	int __limit;
15975aeb0ctheraven	_RuneLocale *__runes = __runes_for_locale(__l, &__limit);
160b096817theraven	return (__c < 0 || __c >= _CACHED_RUNES) ? ___toupper_l(__c, __l) :
161b096817theraven	       __runes->__mapupper[__c];
162b096817theraven}
163b096817theraven_XLOCALE_INLINE int
164b096817theraven__wcwidth_l(__ct_rune_t _c, locale_t __l)
165b096817theraven{
166b096817theraven	unsigned int _x;
167b096817theraven
168b096817theraven	if (_c == 0)
169b096817theraven		return (0);
170b096817theraven	_x = (unsigned int)__maskrune_l(_c, _CTYPE_SWM|_CTYPE_R, __l);
171b096817theraven	if ((_x & _CTYPE_SWM) != 0)
172b096817theraven		return ((_x & _CTYPE_SWM) >> _CTYPE_SWS);
173b096817theraven	return ((_x & _CTYPE_R) != 0 ? 1 : -1);
174b096817theraven}
175b096817theravenint iswctype_l(wint_t __wc, wctype_t __charclass, locale_t __l);
176b096817theravenwctype_t wctype_l(const char *property, locale_t __l);
177b096817theravenwint_t towctrans_l(wint_t __wc, wctrans_t desc, locale_t __l);
178b096817theravenwint_t nextwctype_l(wint_t __wc, wctype_t wct, locale_t __l);
179b096817theravenwctrans_t wctrans_l(const char *__charclass, locale_t __l);
180b096817theraven#undef _XLOCALE_WCTYPES
181b096817theraven#else
182b096817theraven_XLOCALE_INLINE int digittoint_l(int, locale_t);
183b096817theraven_XLOCALE_INLINE int tolower_l(int, locale_t);
184b096817theraven_XLOCALE_INLINE int toupper_l(int, locale_t);
185b096817theraven
186b096817theraven_XLOCALE_INLINE int digittoint_l(int __c, locale_t __l)
187b096817theraven{ return __sbmaskrune_l((__c), 0xFF, __l); }
188b096817theraven
189b096817theraven_XLOCALE_INLINE int tolower_l(int __c, locale_t __l)
190b096817theraven{
191b096817theraven	int __limit;
192b096817theraven	_RuneLocale *__runes = __runes_for_locale(__l, &__limit);
193b096817theraven	return (__c < 0 || __c >= __limit) ? __c :
194b096817theraven	       __runes->__maplower[__c];
195b096817theraven}
196b096817theraven_XLOCALE_INLINE int toupper_l(int __c, locale_t __l)
197b096817theraven{
198b096817theraven	int __limit;
199b096817theraven	_RuneLocale *__runes = __runes_for_locale(__l, &__limit);
200b096817theraven	return (__c < 0 || __c >= __limit) ? __c :
201b096817theraven	       __runes->__mapupper[__c];
202b096817theraven}
203b096817theraven#endif
204b096817theraven#endif /* (defined(_XLOCALE_WCTYPES) && !defined(_XLOCALE_WCTYPE_H)) || \
205b096817theraven	(!defined(_XLOCALE_WCTYPES) && !defined(_XLOCALE_CTYPE_H)) */
206