xref: /freebsd/include/_ctype.h (revision e0c4386e7e71d93b0edc0c8fa156263fc4a8b0b6)
1 /*-
2  * SPDX-License-Identifier: BSD-3-Clause
3  *
4  * Copyright (c) 1989, 1993
5  *	The Regents of the University of California.  All rights reserved.
6  * (c) UNIX System Laboratories, Inc.
7  * All or some portions of this file are derived from material licensed
8  * to the University of California by American Telephone and Telegraph
9  * Co. or Unix System Laboratories, Inc. and are reproduced herein with
10  * the permission of UNIX System Laboratories, Inc.
11  *
12  * This code is derived from software contributed to Berkeley by
13  * Paul Borman at Krystal Technologies.
14  *
15  * Redistribution and use in source and binary forms, with or without
16  * modification, are permitted provided that the following conditions
17  * are met:
18  * 1. Redistributions of source code must retain the above copyright
19  *    notice, this list of conditions and the following disclaimer.
20  * 2. Redistributions in binary form must reproduce the above copyright
21  *    notice, this list of conditions and the following disclaimer in the
22  *    documentation and/or other materials provided with the distribution.
23  * 3. Neither the name of the University nor the names of its contributors
24  *    may be used to endorse or promote products derived from this software
25  *    without specific prior written permission.
26  *
27  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
28  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
29  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
30  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
31  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
32  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
33  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
34  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
35  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
36  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
37  * SUCH DAMAGE.
38  * From FreeBSD: src/include/ctype.h,v 1.27 2004/06/23 07:11:39 tjr Exp
39  */
40 
41 #ifndef __CTYPE_H_
42 #define	__CTYPE_H_
43 
44 #include <sys/cdefs.h>
45 #include <sys/_types.h>
46 
47 #define	_CTYPE_A	0x00000100L		/* Alpha */
48 #define	_CTYPE_C	0x00000200L		/* Control */
49 #define	_CTYPE_D	0x00000400L		/* Digit */
50 #define	_CTYPE_G	0x00000800L		/* Graph */
51 #define	_CTYPE_L	0x00001000L		/* Lower */
52 #define	_CTYPE_P	0x00002000L		/* Punct */
53 #define	_CTYPE_S	0x00004000L		/* Space */
54 #define	_CTYPE_U	0x00008000L		/* Upper */
55 #define	_CTYPE_X	0x00010000L		/* X digit */
56 #define	_CTYPE_B	0x00020000L		/* Blank */
57 #define	_CTYPE_R	0x00040000L		/* Print */
58 #define	_CTYPE_I	0x00080000L		/* Ideogram */
59 #define	_CTYPE_T	0x00100000L		/* Special */
60 #define	_CTYPE_Q	0x00200000L		/* Phonogram */
61 #define	_CTYPE_N 	0x00400000L		/* Number (superset of digit) */
62 #define	_CTYPE_SW0	0x20000000L		/* 0 width character */
63 #define	_CTYPE_SW1	0x40000000L		/* 1 width character */
64 #define	_CTYPE_SW2	0x80000000L		/* 2 width character */
65 #define	_CTYPE_SW3	0xc0000000L		/* 3 width character */
66 #define	_CTYPE_SWM	0xe0000000L		/* Mask for screen width data */
67 #define	_CTYPE_SWS	30			/* Bits to shift to get width */
68 
69 /* See comments in <sys/_types.h> about __ct_rune_t. */
70 __BEGIN_DECLS
71 unsigned long	___runetype(__ct_rune_t) __pure;
72 __ct_rune_t	___tolower(__ct_rune_t) __pure;
73 __ct_rune_t	___toupper(__ct_rune_t) __pure;
74 __END_DECLS
75 
76 /*
77  * _EXTERNALIZE_CTYPE_INLINES_ is defined in locale/nomacros.c to tell us
78  * to generate code for extern versions of all our inline functions.
79  */
80 #ifdef _EXTERNALIZE_CTYPE_INLINES_
81 #define	_USE_CTYPE_INLINE_
82 #define	static
83 #define	__inline
84 #endif
85 
86 extern int __mb_sb_limit;
87 
88 /*
89  * Use inline functions if we are allowed to and the compiler supports them.
90  */
91 #if !defined(_DONT_USE_CTYPE_INLINE_) && \
92     (defined(_USE_CTYPE_INLINE_) || defined(__GNUC__) || defined(__cplusplus))
93 
94 #include <runetype.h>
95 
96 static __inline int
97 __maskrune(__ct_rune_t _c, unsigned long _f)
98 {
99 	return ((_c < 0 || _c >= _CACHED_RUNES) ? ___runetype(_c) :
100 		_CurrentRuneLocale->__runetype[_c]) & _f;
101 }
102 
103 static __inline int
104 __sbmaskrune(__ct_rune_t _c, unsigned long _f)
105 {
106 	return (_c < 0 || _c >= __mb_sb_limit) ? 0 :
107 	       _CurrentRuneLocale->__runetype[_c] & _f;
108 }
109 
110 static __inline int
111 __istype(__ct_rune_t _c, unsigned long _f)
112 {
113 	return (!!__maskrune(_c, _f));
114 }
115 
116 static __inline int
117 __sbistype(__ct_rune_t _c, unsigned long _f)
118 {
119 	return (!!__sbmaskrune(_c, _f));
120 }
121 
122 static __inline int
123 __isctype(__ct_rune_t _c, unsigned long _f)
124 {
125 	return (_c < 0 || _c >= 128) ? 0 :
126 	       !!(_DefaultRuneLocale.__runetype[_c] & _f);
127 }
128 
129 static __inline __ct_rune_t
130 __toupper(__ct_rune_t _c)
131 {
132 	return (_c < 0 || _c >= _CACHED_RUNES) ? ___toupper(_c) :
133 	       _CurrentRuneLocale->__mapupper[_c];
134 }
135 
136 static __inline __ct_rune_t
137 __sbtoupper(__ct_rune_t _c)
138 {
139 	return (_c < 0 || _c >= __mb_sb_limit) ? _c :
140 	       _CurrentRuneLocale->__mapupper[_c];
141 }
142 
143 static __inline __ct_rune_t
144 __tolower(__ct_rune_t _c)
145 {
146 	return (_c < 0 || _c >= _CACHED_RUNES) ? ___tolower(_c) :
147 	       _CurrentRuneLocale->__maplower[_c];
148 }
149 
150 static __inline __ct_rune_t
151 __sbtolower(__ct_rune_t _c)
152 {
153 	return (_c < 0 || _c >= __mb_sb_limit) ? _c :
154 	       _CurrentRuneLocale->__maplower[_c];
155 }
156 
157 static __inline int
158 __wcwidth(__ct_rune_t _c)
159 {
160 	unsigned int _x;
161 
162 	if (_c == 0)
163 		return (0);
164 	_x = (unsigned int)__maskrune(_c, _CTYPE_SWM|_CTYPE_R);
165 	if ((_x & _CTYPE_SWM) != 0)
166 		return ((_x & _CTYPE_SWM) >> _CTYPE_SWS);
167 	return ((_x & _CTYPE_R) != 0 ? 1 : -1);
168 }
169 
170 #else /* not using inlines */
171 
172 __BEGIN_DECLS
173 int		__maskrune(__ct_rune_t, unsigned long);
174 int		__sbmaskrune(__ct_rune_t, unsigned long);
175 int		__istype(__ct_rune_t, unsigned long);
176 int		__sbistype(__ct_rune_t, unsigned long);
177 int		__isctype(__ct_rune_t, unsigned long);
178 __ct_rune_t	__toupper(__ct_rune_t);
179 __ct_rune_t	__sbtoupper(__ct_rune_t);
180 __ct_rune_t	__tolower(__ct_rune_t);
181 __ct_rune_t	__sbtolower(__ct_rune_t);
182 int		__wcwidth(__ct_rune_t);
183 __END_DECLS
184 #endif /* using inlines */
185 
186 #endif /* !__CTYPE_H_ */
187