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