xref: /freebsd/include/xlocale/_ctype.h (revision b3e76948)
1a8ed63bbSDavid Chisnall /*-
24d846d26SWarner Losh  * SPDX-License-Identifier: BSD-2-Clause
3e58eb3c4SPedro F. Giffuni  *
4a8ed63bbSDavid Chisnall  * Copyright (c) 2011 The FreeBSD Foundation
5a8ed63bbSDavid Chisnall  *
6a8ed63bbSDavid Chisnall  * This software was developed by David Chisnall under sponsorship from
7a8ed63bbSDavid Chisnall  * the FreeBSD Foundation.
8a8ed63bbSDavid Chisnall  *
9a8ed63bbSDavid Chisnall  * Redistribution and use in source and binary forms, with or without
10a8ed63bbSDavid Chisnall  * modification, are permitted provided that the following conditions
11a8ed63bbSDavid Chisnall  * are met:
12a8ed63bbSDavid Chisnall  * 1. Redistributions of source code must retain the above copyright
13a8ed63bbSDavid Chisnall  *    notice, this list of conditions and the following disclaimer.
14a8ed63bbSDavid Chisnall  * 2. Redistributions in binary form must reproduce the above copyright
15a8ed63bbSDavid Chisnall  *    notice, this list of conditions and the following disclaimer in the
16a8ed63bbSDavid Chisnall  *    documentation and/or other materials provided with the distribution.
17a8ed63bbSDavid Chisnall  *
18a8ed63bbSDavid Chisnall  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
19a8ed63bbSDavid Chisnall  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20a8ed63bbSDavid Chisnall  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21a8ed63bbSDavid Chisnall  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
22a8ed63bbSDavid Chisnall  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23a8ed63bbSDavid Chisnall  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
24a8ed63bbSDavid Chisnall  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25a8ed63bbSDavid Chisnall  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26a8ed63bbSDavid Chisnall  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
27a8ed63bbSDavid Chisnall  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28a8ed63bbSDavid Chisnall  * SUCH DAMAGE.
29a8ed63bbSDavid Chisnall  */
30a8ed63bbSDavid Chisnall 
31a8ed63bbSDavid Chisnall 
32a8ed63bbSDavid Chisnall #if	(defined(_XLOCALE_WCTYPES) && !defined(_XLOCALE_WCTYPE_H)) || \
33a8ed63bbSDavid Chisnall 	(!defined(_XLOCALE_WCTYPES) && !defined(_XLOCALE_CTYPE_H))
34a8ed63bbSDavid Chisnall 
35a8ed63bbSDavid Chisnall #ifdef _XLOCALE_WCTYPES
36a8ed63bbSDavid Chisnall #define _XLOCALE_WCTYPE_H
37a8ed63bbSDavid Chisnall #else
38a8ed63bbSDavid Chisnall #define _XLOCALE_CTYPE_H
39a8ed63bbSDavid Chisnall #endif
40a8ed63bbSDavid Chisnall 
41a8ed63bbSDavid Chisnall #ifndef _LOCALE_T_DEFINED
42a8ed63bbSDavid Chisnall #define _LOCALE_T_DEFINED
43a8ed63bbSDavid Chisnall typedef struct	_xlocale *locale_t;
44a8ed63bbSDavid Chisnall #endif
45a8ed63bbSDavid Chisnall 
46a8ed63bbSDavid Chisnall #ifndef _XLOCALE_RUN_FUNCTIONS_DEFINED
47a8ed63bbSDavid Chisnall #define _XLOCALE_RUN_FUNCTIONS_DEFINED 1
48a8ed63bbSDavid Chisnall unsigned long	 ___runetype_l(__ct_rune_t, locale_t) __pure;
49a8ed63bbSDavid Chisnall __ct_rune_t	 ___tolower_l(__ct_rune_t, locale_t) __pure;
50a8ed63bbSDavid Chisnall __ct_rune_t	 ___toupper_l(__ct_rune_t, locale_t) __pure;
51a8ed63bbSDavid Chisnall _RuneLocale	*__runes_for_locale(locale_t, int*);
52a8ed63bbSDavid Chisnall #endif
53a8ed63bbSDavid Chisnall 
54a8ed63bbSDavid Chisnall #ifndef _XLOCALE_INLINE
5540455cf3SDmitry Sivachenko #if defined(__GNUC__) && !defined(__GNUC_STDC_INLINE__)
56a8ed63bbSDavid Chisnall /* GNU89 inline has nonstandard semantics. */
57b1e45245SDimitry Andric #define _XLOCALE_INLINE extern __inline
58a8ed63bbSDavid Chisnall #else
59a8ed63bbSDavid Chisnall /* Hack to work around people who define inline away */
60a8ed63bbSDavid Chisnall #ifdef inline
61b1e45245SDimitry Andric #define _XLOCALE_INLINE static __inline
62a8ed63bbSDavid Chisnall #else
63a8ed63bbSDavid Chisnall /* Define with C++ / C99 compatible semantics */
64a8ed63bbSDavid Chisnall #define _XLOCALE_INLINE inline
65a8ed63bbSDavid Chisnall #endif
66a8ed63bbSDavid Chisnall #endif
67a8ed63bbSDavid Chisnall #endif /* _XLOCALE_INLINE */
68a8ed63bbSDavid Chisnall 
69a8ed63bbSDavid Chisnall #ifdef _XLOCALE_WCTYPES
704d7815ecSDavid Chisnall _XLOCALE_INLINE int
719fa42222SDavid Chisnall __maskrune_l(__ct_rune_t __c, unsigned long __f, locale_t __loc);
72c5c1d739SDavid Chisnall _XLOCALE_INLINE int
739fa42222SDavid Chisnall __istype_l(__ct_rune_t __c, unsigned long __f, locale_t __loc);
749fa42222SDavid Chisnall 
759fa42222SDavid Chisnall _XLOCALE_INLINE int
__maskrune_l(__ct_rune_t __c,unsigned long __f,locale_t __loc)764d7815ecSDavid Chisnall __maskrune_l(__ct_rune_t __c, unsigned long __f, locale_t __loc)
77a8ed63bbSDavid Chisnall {
784d7815ecSDavid Chisnall 	int __limit;
794d7815ecSDavid Chisnall 	_RuneLocale *runes = __runes_for_locale(__loc, &__limit);
80e36c7d52SDavid Chisnall 	return ((__c < 0 || __c >= _CACHED_RUNES) ? ___runetype_l(__c, __loc) :
81e36c7d52SDavid Chisnall 	        runes->__runetype[__c]) & __f;
82a8ed63bbSDavid Chisnall }
83a8ed63bbSDavid Chisnall 
84c5c1d739SDavid Chisnall _XLOCALE_INLINE int
__istype_l(__ct_rune_t __c,unsigned long __f,locale_t __loc)854d7815ecSDavid Chisnall __istype_l(__ct_rune_t __c, unsigned long __f, locale_t __loc)
86a8ed63bbSDavid Chisnall {
874d7815ecSDavid Chisnall 	return (!!__maskrune_l(__c, __f, __loc));
88a8ed63bbSDavid Chisnall }
89a8ed63bbSDavid Chisnall 
90a8ed63bbSDavid Chisnall #define XLOCALE_ISCTYPE(fname, cat) \
91a8ed63bbSDavid Chisnall 		_XLOCALE_INLINE int isw##fname##_l(int, locale_t);\
92a8ed63bbSDavid Chisnall 		_XLOCALE_INLINE int isw##fname##_l(int __c, locale_t __l)\
93a8ed63bbSDavid Chisnall 		{ return __istype_l(__c, cat, __l); }
94a8ed63bbSDavid Chisnall #else
954d7815ecSDavid Chisnall _XLOCALE_INLINE int
963fad4c34SDavid Chisnall __sbmaskrune_l(__ct_rune_t __c, unsigned long __f, locale_t __loc);
973fad4c34SDavid Chisnall _XLOCALE_INLINE int
983fad4c34SDavid Chisnall __sbistype_l(__ct_rune_t __c, unsigned long __f, locale_t __loc);
993fad4c34SDavid Chisnall 
1003fad4c34SDavid Chisnall _XLOCALE_INLINE int
__sbmaskrune_l(__ct_rune_t __c,unsigned long __f,locale_t __loc)1014d7815ecSDavid Chisnall __sbmaskrune_l(__ct_rune_t __c, unsigned long __f, locale_t __loc)
102a8ed63bbSDavid Chisnall {
1034d7815ecSDavid Chisnall 	int __limit;
1044d7815ecSDavid Chisnall 	_RuneLocale *runes = __runes_for_locale(__loc, &__limit);
1054d7815ecSDavid Chisnall 	return (__c < 0 || __c >= __limit) ? 0 :
1064d7815ecSDavid Chisnall 	       runes->__runetype[__c] & __f;
107a8ed63bbSDavid Chisnall }
108a8ed63bbSDavid Chisnall 
1094d7815ecSDavid Chisnall _XLOCALE_INLINE int
__sbistype_l(__ct_rune_t __c,unsigned long __f,locale_t __loc)1104d7815ecSDavid Chisnall __sbistype_l(__ct_rune_t __c, unsigned long __f, locale_t __loc)
111a8ed63bbSDavid Chisnall {
1124d7815ecSDavid Chisnall 	return (!!__sbmaskrune_l(__c, __f, __loc));
113a8ed63bbSDavid Chisnall }
114a8ed63bbSDavid Chisnall 
1154d7815ecSDavid Chisnall #define XLOCALE_ISCTYPE(__fname, __cat) \
1164d7815ecSDavid Chisnall 		_XLOCALE_INLINE int is##__fname##_l(int, locale_t); \
1174d7815ecSDavid Chisnall 		_XLOCALE_INLINE int is##__fname##_l(int __c, locale_t __l)\
1184d7815ecSDavid Chisnall 		{ return __sbistype_l(__c, __cat, __l); }
119a8ed63bbSDavid Chisnall #endif
120a8ed63bbSDavid Chisnall 
121f5dde016SBaptiste Daroussin XLOCALE_ISCTYPE(alnum, _CTYPE_A|_CTYPE_D|_CTYPE_N)
122a8ed63bbSDavid Chisnall XLOCALE_ISCTYPE(alpha, _CTYPE_A)
123a8ed63bbSDavid Chisnall XLOCALE_ISCTYPE(blank, _CTYPE_B)
124a8ed63bbSDavid Chisnall XLOCALE_ISCTYPE(cntrl, _CTYPE_C)
125a8ed63bbSDavid Chisnall XLOCALE_ISCTYPE(digit, _CTYPE_D)
126a8ed63bbSDavid Chisnall XLOCALE_ISCTYPE(graph, _CTYPE_G)
127a8ed63bbSDavid Chisnall XLOCALE_ISCTYPE(hexnumber, _CTYPE_X)
128a8ed63bbSDavid Chisnall XLOCALE_ISCTYPE(ideogram, _CTYPE_I)
129a8ed63bbSDavid Chisnall XLOCALE_ISCTYPE(lower, _CTYPE_L)
130f5dde016SBaptiste Daroussin XLOCALE_ISCTYPE(number, _CTYPE_D|_CTYPE_N)
131a8ed63bbSDavid Chisnall XLOCALE_ISCTYPE(phonogram, _CTYPE_Q)
132a8ed63bbSDavid Chisnall XLOCALE_ISCTYPE(print, _CTYPE_R)
133a8ed63bbSDavid Chisnall XLOCALE_ISCTYPE(punct, _CTYPE_P)
134a8ed63bbSDavid Chisnall XLOCALE_ISCTYPE(rune, 0xFFFFFF00L)
135a8ed63bbSDavid Chisnall XLOCALE_ISCTYPE(space, _CTYPE_S)
136a8ed63bbSDavid Chisnall XLOCALE_ISCTYPE(special, _CTYPE_T)
137a8ed63bbSDavid Chisnall XLOCALE_ISCTYPE(upper, _CTYPE_U)
138a8ed63bbSDavid Chisnall XLOCALE_ISCTYPE(xdigit, _CTYPE_X)
139a8ed63bbSDavid Chisnall #undef XLOCALE_ISCTYPE
140a8ed63bbSDavid Chisnall 
141a8ed63bbSDavid Chisnall #ifdef _XLOCALE_WCTYPES
142a8ed63bbSDavid Chisnall _XLOCALE_INLINE int towlower_l(int, locale_t);
143a8ed63bbSDavid Chisnall _XLOCALE_INLINE int __wcwidth_l(__ct_rune_t, locale_t);
144a8ed63bbSDavid Chisnall _XLOCALE_INLINE int towupper_l(int, locale_t);
145a8ed63bbSDavid Chisnall 
towlower_l(int __c,locale_t __l)146a8ed63bbSDavid Chisnall _XLOCALE_INLINE int towlower_l(int __c, locale_t __l)
147a8ed63bbSDavid Chisnall {
1484d7815ecSDavid Chisnall 	int __limit;
1494d7815ecSDavid Chisnall 	_RuneLocale *__runes = __runes_for_locale(__l, &__limit);
150a8ed63bbSDavid Chisnall 	return (__c < 0 || __c >= _CACHED_RUNES) ? ___tolower_l(__c, __l) :
151a8ed63bbSDavid Chisnall 	       __runes->__maplower[__c];
152a8ed63bbSDavid Chisnall }
towupper_l(int __c,locale_t __l)153a8ed63bbSDavid Chisnall _XLOCALE_INLINE int towupper_l(int __c, locale_t __l)
154a8ed63bbSDavid Chisnall {
1554d7815ecSDavid Chisnall 	int __limit;
1564d7815ecSDavid Chisnall 	_RuneLocale *__runes = __runes_for_locale(__l, &__limit);
157a8ed63bbSDavid Chisnall 	return (__c < 0 || __c >= _CACHED_RUNES) ? ___toupper_l(__c, __l) :
158a8ed63bbSDavid Chisnall 	       __runes->__mapupper[__c];
159a8ed63bbSDavid Chisnall }
160a8ed63bbSDavid Chisnall _XLOCALE_INLINE int
__wcwidth_l(__ct_rune_t _c,locale_t __l)161a8ed63bbSDavid Chisnall __wcwidth_l(__ct_rune_t _c, locale_t __l)
162a8ed63bbSDavid Chisnall {
163a8ed63bbSDavid Chisnall 	unsigned int _x;
164a8ed63bbSDavid Chisnall 
165a8ed63bbSDavid Chisnall 	if (_c == 0)
166a8ed63bbSDavid Chisnall 		return (0);
167a8ed63bbSDavid Chisnall 	_x = (unsigned int)__maskrune_l(_c, _CTYPE_SWM|_CTYPE_R, __l);
168a8ed63bbSDavid Chisnall 	if ((_x & _CTYPE_SWM) != 0)
169a8ed63bbSDavid Chisnall 		return ((_x & _CTYPE_SWM) >> _CTYPE_SWS);
170a8ed63bbSDavid Chisnall 	return ((_x & _CTYPE_R) != 0 ? 1 : -1);
171a8ed63bbSDavid Chisnall }
172a8ed63bbSDavid Chisnall int iswctype_l(wint_t __wc, wctype_t __charclass, locale_t __l);
173a8ed63bbSDavid Chisnall wctype_t wctype_l(const char *property, locale_t __l);
174a8ed63bbSDavid Chisnall wint_t towctrans_l(wint_t __wc, wctrans_t desc, locale_t __l);
175a8ed63bbSDavid Chisnall wint_t nextwctype_l(wint_t __wc, wctype_t wct, locale_t __l);
176a8ed63bbSDavid Chisnall wctrans_t wctrans_l(const char *__charclass, locale_t __l);
177a8ed63bbSDavid Chisnall #undef _XLOCALE_WCTYPES
178a8ed63bbSDavid Chisnall #else
179a8ed63bbSDavid Chisnall _XLOCALE_INLINE int digittoint_l(int, locale_t);
180a8ed63bbSDavid Chisnall _XLOCALE_INLINE int tolower_l(int, locale_t);
181a8ed63bbSDavid Chisnall _XLOCALE_INLINE int toupper_l(int, locale_t);
182a8ed63bbSDavid Chisnall 
183a8ed63bbSDavid Chisnall _XLOCALE_INLINE int digittoint_l(int __c, locale_t __l)
184a8ed63bbSDavid Chisnall { return __sbmaskrune_l((__c), 0xFF, __l); }
185a8ed63bbSDavid Chisnall 
186a8ed63bbSDavid Chisnall _XLOCALE_INLINE int tolower_l(int __c, locale_t __l)
187a8ed63bbSDavid Chisnall {
188a8ed63bbSDavid Chisnall 	int __limit;
189a8ed63bbSDavid Chisnall 	_RuneLocale *__runes = __runes_for_locale(__l, &__limit);
190a8ed63bbSDavid Chisnall 	return (__c < 0 || __c >= __limit) ? __c :
191a8ed63bbSDavid Chisnall 	       __runes->__maplower[__c];
192a8ed63bbSDavid Chisnall }
193a8ed63bbSDavid Chisnall _XLOCALE_INLINE int toupper_l(int __c, locale_t __l)
194a8ed63bbSDavid Chisnall {
195a8ed63bbSDavid Chisnall 	int __limit;
196a8ed63bbSDavid Chisnall 	_RuneLocale *__runes = __runes_for_locale(__l, &__limit);
197a8ed63bbSDavid Chisnall 	return (__c < 0 || __c >= __limit) ? __c :
198a8ed63bbSDavid Chisnall 	       __runes->__mapupper[__c];
199a8ed63bbSDavid Chisnall }
200a8ed63bbSDavid Chisnall #endif
201a8ed63bbSDavid Chisnall #endif /* (defined(_XLOCALE_WCTYPES) && !defined(_XLOCALE_WCTYPE_H)) || \
202a8ed63bbSDavid Chisnall 	(!defined(_XLOCALE_WCTYPES) && !defined(_XLOCALE_CTYPE_H)) */
203