xref: /freebsd/include/_ctype.h (revision f39bffc62c1395bde25d152c7f68fdf7cbaab414)
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  *
39  * From @(#)ctype.h	8.4 (Berkeley) 1/21/94
40  * From FreeBSD: src/include/ctype.h,v 1.27 2004/06/23 07:11:39 tjr Exp
41  * $FreeBSD$
42  */
43 
44 #ifndef __CTYPE_H_
45 #define	__CTYPE_H_
46 
47 #include <sys/cdefs.h>
48 #include <sys/_types.h>
49 
50 #define	_CTYPE_A	0x00000100L		/* Alpha */
51 #define	_CTYPE_C	0x00000200L		/* Control */
52 #define	_CTYPE_D	0x00000400L		/* Digit */
53 #define	_CTYPE_G	0x00000800L		/* Graph */
54 #define	_CTYPE_L	0x00001000L		/* Lower */
55 #define	_CTYPE_P	0x00002000L		/* Punct */
56 #define	_CTYPE_S	0x00004000L		/* Space */
57 #define	_CTYPE_U	0x00008000L		/* Upper */
58 #define	_CTYPE_X	0x00010000L		/* X digit */
59 #define	_CTYPE_B	0x00020000L		/* Blank */
60 #define	_CTYPE_R	0x00040000L		/* Print */
61 #define	_CTYPE_I	0x00080000L		/* Ideogram */
62 #define	_CTYPE_T	0x00100000L		/* Special */
63 #define	_CTYPE_Q	0x00200000L		/* Phonogram */
64 #define	_CTYPE_N 	0x00400000L		/* Number (superset of digit) */
65 #define	_CTYPE_SW0	0x20000000L		/* 0 width character */
66 #define	_CTYPE_SW1	0x40000000L		/* 1 width character */
67 #define	_CTYPE_SW2	0x80000000L		/* 2 width character */
68 #define	_CTYPE_SW3	0xc0000000L		/* 3 width character */
69 #define	_CTYPE_SWM	0xe0000000L		/* Mask for screen width data */
70 #define	_CTYPE_SWS	30			/* Bits to shift to get width */
71 
72 /* See comments in <sys/_types.h> about __ct_rune_t. */
73 __BEGIN_DECLS
74 unsigned long	___runetype(__ct_rune_t) __pure;
75 __ct_rune_t	___tolower(__ct_rune_t) __pure;
76 __ct_rune_t	___toupper(__ct_rune_t) __pure;
77 __END_DECLS
78 
79 /*
80  * _EXTERNALIZE_CTYPE_INLINES_ is defined in locale/nomacros.c to tell us
81  * to generate code for extern versions of all our inline functions.
82  */
83 #ifdef _EXTERNALIZE_CTYPE_INLINES_
84 #define	_USE_CTYPE_INLINE_
85 #define	static
86 #define	__inline
87 #endif
88 
89 extern int __mb_sb_limit;
90 
91 /*
92  * Use inline functions if we are allowed to and the compiler supports them.
93  */
94 #if !defined(_DONT_USE_CTYPE_INLINE_) && \
95     (defined(_USE_CTYPE_INLINE_) || defined(__GNUC__) || defined(__cplusplus))
96 
97 #include <runetype.h>
98 
99 static __inline int
100 __maskrune(__ct_rune_t _c, unsigned long _f)
101 {
102 	return ((_c < 0 || _c >= _CACHED_RUNES) ? ___runetype(_c) :
103 		_CurrentRuneLocale->__runetype[_c]) & _f;
104 }
105 
106 static __inline int
107 __sbmaskrune(__ct_rune_t _c, unsigned long _f)
108 {
109 	return (_c < 0 || _c >= __mb_sb_limit) ? 0 :
110 	       _CurrentRuneLocale->__runetype[_c] & _f;
111 }
112 
113 static __inline int
114 __istype(__ct_rune_t _c, unsigned long _f)
115 {
116 	return (!!__maskrune(_c, _f));
117 }
118 
119 static __inline int
120 __sbistype(__ct_rune_t _c, unsigned long _f)
121 {
122 	return (!!__sbmaskrune(_c, _f));
123 }
124 
125 static __inline int
126 __isctype(__ct_rune_t _c, unsigned long _f)
127 {
128 	return (_c < 0 || _c >= 128) ? 0 :
129 	       !!(_DefaultRuneLocale.__runetype[_c] & _f);
130 }
131 
132 static __inline __ct_rune_t
133 __toupper(__ct_rune_t _c)
134 {
135 	return (_c < 0 || _c >= _CACHED_RUNES) ? ___toupper(_c) :
136 	       _CurrentRuneLocale->__mapupper[_c];
137 }
138 
139 static __inline __ct_rune_t
140 __sbtoupper(__ct_rune_t _c)
141 {
142 	return (_c < 0 || _c >= __mb_sb_limit) ? _c :
143 	       _CurrentRuneLocale->__mapupper[_c];
144 }
145 
146 static __inline __ct_rune_t
147 __tolower(__ct_rune_t _c)
148 {
149 	return (_c < 0 || _c >= _CACHED_RUNES) ? ___tolower(_c) :
150 	       _CurrentRuneLocale->__maplower[_c];
151 }
152 
153 static __inline __ct_rune_t
154 __sbtolower(__ct_rune_t _c)
155 {
156 	return (_c < 0 || _c >= __mb_sb_limit) ? _c :
157 	       _CurrentRuneLocale->__maplower[_c];
158 }
159 
160 static __inline int
161 __wcwidth(__ct_rune_t _c)
162 {
163 	unsigned int _x;
164 
165 	if (_c == 0)
166 		return (0);
167 	_x = (unsigned int)__maskrune(_c, _CTYPE_SWM|_CTYPE_R);
168 	if ((_x & _CTYPE_SWM) != 0)
169 		return ((_x & _CTYPE_SWM) >> _CTYPE_SWS);
170 	return ((_x & _CTYPE_R) != 0 ? 1 : -1);
171 }
172 
173 #else /* not using inlines */
174 
175 __BEGIN_DECLS
176 int		__maskrune(__ct_rune_t, unsigned long);
177 int		__sbmaskrune(__ct_rune_t, unsigned long);
178 int		__istype(__ct_rune_t, unsigned long);
179 int		__sbistype(__ct_rune_t, unsigned long);
180 int		__isctype(__ct_rune_t, unsigned long);
181 __ct_rune_t	__toupper(__ct_rune_t);
182 __ct_rune_t	__sbtoupper(__ct_rune_t);
183 __ct_rune_t	__tolower(__ct_rune_t);
184 __ct_rune_t	__sbtolower(__ct_rune_t);
185 int		__wcwidth(__ct_rune_t);
186 __END_DECLS
187 #endif /* using inlines */
188 
189 #endif /* !__CTYPE_H_ */
190