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