_ctype.h revision 232498
1231673Stheraven/*-
2231673Stheraven * Copyright (c) 2011 The FreeBSD Foundation
3231673Stheraven * All rights reserved.
4231673Stheraven *
5231673Stheraven * This software was developed by David Chisnall under sponsorship from
6231673Stheraven * the FreeBSD Foundation.
7231673Stheraven *
8231673Stheraven * Redistribution and use in source and binary forms, with or without
9231673Stheraven * modification, are permitted provided that the following conditions
10231673Stheraven * are met:
11231673Stheraven * 1. Redistributions of source code must retain the above copyright
12231673Stheraven *    notice, this list of conditions and the following disclaimer.
13231673Stheraven * 2. Redistributions in binary form must reproduce the above copyright
14231673Stheraven *    notice, this list of conditions and the following disclaimer in the
15231673Stheraven *    documentation and/or other materials provided with the distribution.
16231673Stheraven *
17231673Stheraven * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
18231673Stheraven * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19231673Stheraven * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20231673Stheraven * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
21231673Stheraven * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22231673Stheraven * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23231673Stheraven * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24231673Stheraven * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25231673Stheraven * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26231673Stheraven * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27231673Stheraven * SUCH DAMAGE.
28231673Stheraven *
29231673Stheraven * $FreeBSD: head/include/xlocale/_ctype.h 232498 2012-03-04 15:31:13Z theraven $
30231673Stheraven */
31231673Stheraven
32231673Stheraven
33231673Stheraven#if	(defined(_XLOCALE_WCTYPES) && !defined(_XLOCALE_WCTYPE_H)) || \
34231673Stheraven	(!defined(_XLOCALE_WCTYPES) && !defined(_XLOCALE_CTYPE_H))
35231673Stheraven
36231673Stheraven#ifdef _XLOCALE_WCTYPES
37231673Stheraven#define _XLOCALE_WCTYPE_H
38231673Stheraven#else
39231673Stheraven#define _XLOCALE_CTYPE_H
40231673Stheraven#endif
41231673Stheraven
42231673Stheraven#ifndef _LOCALE_T_DEFINED
43231673Stheraven#define _LOCALE_T_DEFINED
44231673Stheraventypedef struct	_xlocale *locale_t;
45231673Stheraven#endif
46231673Stheraven
47232498Stheraven#ifndef _XLOCALE_RUN_FUNCTIONS_DEFINED
48232498Stheraven#define _XLOCALE_RUN_FUNCTIONS_DEFINED 1
49231673Stheravenunsigned long	 ___runetype_l(__ct_rune_t, locale_t) __pure;
50231673Stheraven__ct_rune_t	 ___tolower_l(__ct_rune_t, locale_t) __pure;
51231673Stheraven__ct_rune_t	 ___toupper_l(__ct_rune_t, locale_t) __pure;
52231673Stheraven_RuneLocale	*__runes_for_locale(locale_t, int*);
53231673Stheraven#endif
54231673Stheraven
55231673Stheraven#ifndef _XLOCALE_INLINE
56231673Stheraven#if __GNUC__ && !__GNUC_STDC_INLINE__
57231673Stheraven/* GNU89 inline has nonstandard semantics. */
58231673Stheraven#define _XLOCALE_INLINE extern inline
59231673Stheraven#else
60231673Stheraven/* Hack to work around people who define inline away */
61231673Stheraven#ifdef inline
62231673Stheraven#define _XLOCALE_INLINE __inline static
63231673Stheraven#else
64231673Stheraven/* Define with C++ / C99 compatible semantics */
65231673Stheraven#define _XLOCALE_INLINE inline
66231673Stheraven#endif
67231673Stheraven#endif
68231673Stheraven#endif /* _XLOCALE_INLINE */
69231673Stheraven
70231673Stheraven#ifdef _XLOCALE_WCTYPES
71231673Stheravenstatic __inline int
72231673Stheraven__maskrune_l(__ct_rune_t _c, unsigned long _f, locale_t locale)
73231673Stheraven{
74231673Stheraven	int mb_sb_limit;
75231673Stheraven	_RuneLocale *runes = __runes_for_locale(locale, &mb_sb_limit);
76231673Stheraven	return (_c < 0 || _c >= _CACHED_RUNES) ? ___runetype_l(_c, locale) :
77231673Stheraven	       runes->__runetype[_c] & _f;
78231673Stheraven}
79231673Stheraven
80231673Stheravenstatic __inline int
81231673Stheraven__istype_l(__ct_rune_t _c, unsigned long _f, locale_t locale)
82231673Stheraven{
83231673Stheraven	return (!!__maskrune_l(_c, _f, locale));
84231673Stheraven}
85231673Stheraven
86231673Stheraven#define XLOCALE_ISCTYPE(fname, cat) \
87232498Stheraven		_XLOCALE_INLINE int isw##fname##_l(int, locale_t);\
88232498Stheraven		_XLOCALE_INLINE int isw##fname##_l(int __c, locale_t __l)\
89232498Stheraven		{ return __istype_l(__c, cat, __l); }
90231673Stheraven#else
91231673Stheravenstatic __inline int
92231673Stheraven__sbmaskrune_l(__ct_rune_t _c, unsigned long _f, locale_t locale)
93231673Stheraven{
94231673Stheraven	int mb_sb_limit;
95231673Stheraven	_RuneLocale *runes = __runes_for_locale(locale, &mb_sb_limit);
96231673Stheraven	return (_c < 0 || _c >= mb_sb_limit) ? 0 :
97231673Stheraven	       runes->__runetype[_c] & _f;
98231673Stheraven}
99231673Stheraven
100231673Stheravenstatic __inline int
101231673Stheraven__sbistype_l(__ct_rune_t _c, unsigned long _f, locale_t locale)
102231673Stheraven{
103231673Stheraven	return (!!__sbmaskrune_l(_c, _f, locale));
104231673Stheraven}
105231673Stheraven
106231673Stheraven#define XLOCALE_ISCTYPE(fname, cat) \
107232498Stheraven		_XLOCALE_INLINE int is##fname##_l(int c, locale_t l); \
108231673Stheraven		_XLOCALE_INLINE int is##fname##_l(int c, locale_t l)\
109231673Stheraven		{ return __sbistype_l(c, cat, l); }
110231673Stheraven#endif
111231673Stheraven
112231673StheravenXLOCALE_ISCTYPE(alnum, _CTYPE_A|_CTYPE_D)
113231673StheravenXLOCALE_ISCTYPE(alpha, _CTYPE_A)
114231673StheravenXLOCALE_ISCTYPE(blank, _CTYPE_B)
115231673StheravenXLOCALE_ISCTYPE(cntrl, _CTYPE_C)
116231673StheravenXLOCALE_ISCTYPE(digit, _CTYPE_D)
117231673StheravenXLOCALE_ISCTYPE(graph, _CTYPE_G)
118231673StheravenXLOCALE_ISCTYPE(hexnumber, _CTYPE_X)
119231673StheravenXLOCALE_ISCTYPE(ideogram, _CTYPE_I)
120231673StheravenXLOCALE_ISCTYPE(lower, _CTYPE_L)
121231673StheravenXLOCALE_ISCTYPE(number, _CTYPE_D)
122231673StheravenXLOCALE_ISCTYPE(phonogram, _CTYPE_Q)
123231673StheravenXLOCALE_ISCTYPE(print, _CTYPE_R)
124231673StheravenXLOCALE_ISCTYPE(punct, _CTYPE_P)
125231673StheravenXLOCALE_ISCTYPE(rune, 0xFFFFFF00L)
126231673StheravenXLOCALE_ISCTYPE(space, _CTYPE_S)
127231673StheravenXLOCALE_ISCTYPE(special, _CTYPE_T)
128231673StheravenXLOCALE_ISCTYPE(upper, _CTYPE_U)
129231673StheravenXLOCALE_ISCTYPE(xdigit, _CTYPE_X)
130231673Stheraven#undef XLOCALE_ISCTYPE
131231673Stheraven
132231673Stheraven#ifdef _XLOCALE_WCTYPES
133232498Stheraven_XLOCALE_INLINE int towlower_l(int, locale_t);
134232498Stheraven_XLOCALE_INLINE int __wcwidth_l(__ct_rune_t, locale_t);
135232498Stheraven_XLOCALE_INLINE int towupper_l(int, locale_t);
136232498Stheraven
137232498Stheraven_XLOCALE_INLINE int towlower_l(int __c, locale_t __l)
138231673Stheraven{
139231673Stheraven	int mb_sb_limit;
140232498Stheraven	_RuneLocale *__runes = __runes_for_locale(__l, &mb_sb_limit);
141232498Stheraven	return (__c < 0 || __c >= _CACHED_RUNES) ? ___tolower_l(__c, __l) :
142232498Stheraven	       __runes->__maplower[__c];
143231673Stheraven}
144232498Stheraven_XLOCALE_INLINE int towupper_l(int __c, locale_t __l)
145231673Stheraven{
146231673Stheraven	int mb_sb_limit;
147232498Stheraven	_RuneLocale *__runes = __runes_for_locale(__l, &mb_sb_limit);
148232498Stheraven	return (__c < 0 || __c >= _CACHED_RUNES) ? ___toupper_l(__c, __l) :
149232498Stheraven	       __runes->__mapupper[__c];
150231673Stheraven}
151231673Stheraven_XLOCALE_INLINE int
152232498Stheraven__wcwidth_l(__ct_rune_t _c, locale_t __l)
153231673Stheraven{
154231673Stheraven	unsigned int _x;
155231673Stheraven
156231673Stheraven	if (_c == 0)
157231673Stheraven		return (0);
158232498Stheraven	_x = (unsigned int)__maskrune_l(_c, _CTYPE_SWM|_CTYPE_R, __l);
159231673Stheraven	if ((_x & _CTYPE_SWM) != 0)
160231673Stheraven		return ((_x & _CTYPE_SWM) >> _CTYPE_SWS);
161231673Stheraven	return ((_x & _CTYPE_R) != 0 ? 1 : -1);
162231673Stheraven}
163232498Stheravenint iswctype_l(wint_t __wc, wctype_t __charclass, locale_t __l);
164232498Stheravenwctype_t wctype_l(const char *property, locale_t __l);
165232498Stheravenwint_t towctrans_l(wint_t __wc, wctrans_t desc, locale_t __l);
166232498Stheravenwint_t nextwctype_l(wint_t __wc, wctype_t wct, locale_t __l);
167232498Stheravenwctrans_t wctrans_l(const char *__charclass, locale_t __l);
168231673Stheraven#undef _XLOCALE_WCTYPES
169231673Stheraven#else
170232498Stheraven_XLOCALE_INLINE int digittoint_l(int, locale_t);
171232498Stheraven_XLOCALE_INLINE int tolower_l(int, locale_t);
172232498Stheraven_XLOCALE_INLINE int toupper_l(int, locale_t);
173231673Stheraven
174232498Stheraven_XLOCALE_INLINE int digittoint_l(int __c, locale_t __l)
175232498Stheraven{ return __sbmaskrune_l((__c), 0xFF, __l); }
176232498Stheraven
177232498Stheraven_XLOCALE_INLINE int tolower_l(int __c, locale_t __l)
178231673Stheraven{
179232498Stheraven	int __limit;
180232498Stheraven	_RuneLocale *__runes = __runes_for_locale(__l, &__limit);
181232498Stheraven	return (__c < 0 || __c >= __limit) ? __c :
182232498Stheraven	       __runes->__maplower[__c];
183231673Stheraven}
184232498Stheraven_XLOCALE_INLINE int toupper_l(int __c, locale_t __l)
185231673Stheraven{
186232498Stheraven	int __limit;
187232498Stheraven	_RuneLocale *__runes = __runes_for_locale(__l, &__limit);
188232498Stheraven	return (__c < 0 || __c >= __limit) ? __c :
189232498Stheraven	       __runes->__mapupper[__c];
190231673Stheraven}
191231673Stheraven#endif
192231673Stheraven#endif /* (defined(_XLOCALE_WCTYPES) && !defined(_XLOCALE_WCTYPE_H)) || \
193231673Stheraven	(!defined(_XLOCALE_WCTYPES) && !defined(_XLOCALE_CTYPE_H)) */
194