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