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