xref: /freebsd/include/_ctype.h (revision 5a1d14419a5b620430949a46cb6ee63148a43cb9)
1*2321c474SPedro F. Giffuni /*-
2*2321c474SPedro F. Giffuni  * SPDX-License-Identifier: BSD-3-Clause
3*2321c474SPedro F. Giffuni  *
459deaec5SRodney W. Grimes  * Copyright (c) 1989, 1993
559deaec5SRodney W. Grimes  *	The Regents of the University of California.  All rights reserved.
659deaec5SRodney W. Grimes  * (c) UNIX System Laboratories, Inc.
759deaec5SRodney W. Grimes  * All or some portions of this file are derived from material licensed
859deaec5SRodney W. Grimes  * to the University of California by American Telephone and Telegraph
959deaec5SRodney W. Grimes  * Co. or Unix System Laboratories, Inc. and are reproduced herein with
1059deaec5SRodney W. Grimes  * the permission of UNIX System Laboratories, Inc.
1159deaec5SRodney W. Grimes  *
1259deaec5SRodney W. Grimes  * This code is derived from software contributed to Berkeley by
1359deaec5SRodney W. Grimes  * Paul Borman at Krystal Technologies.
1459deaec5SRodney W. Grimes  *
1559deaec5SRodney W. Grimes  * Redistribution and use in source and binary forms, with or without
1659deaec5SRodney W. Grimes  * modification, are permitted provided that the following conditions
1759deaec5SRodney W. Grimes  * are met:
1859deaec5SRodney W. Grimes  * 1. Redistributions of source code must retain the above copyright
1959deaec5SRodney W. Grimes  *    notice, this list of conditions and the following disclaimer.
2059deaec5SRodney W. Grimes  * 2. Redistributions in binary form must reproduce the above copyright
2159deaec5SRodney W. Grimes  *    notice, this list of conditions and the following disclaimer in the
2259deaec5SRodney W. Grimes  *    documentation and/or other materials provided with the distribution.
23f2556687SWarner Losh  * 3. Neither the name of the University nor the names of its contributors
2459deaec5SRodney W. Grimes  *    may be used to endorse or promote products derived from this software
2559deaec5SRodney W. Grimes  *    without specific prior written permission.
2659deaec5SRodney W. Grimes  *
2759deaec5SRodney W. Grimes  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
2859deaec5SRodney W. Grimes  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
2959deaec5SRodney W. Grimes  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
3059deaec5SRodney W. Grimes  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
3159deaec5SRodney W. Grimes  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
3259deaec5SRodney W. Grimes  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
3359deaec5SRodney W. Grimes  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
3459deaec5SRodney W. Grimes  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
3559deaec5SRodney W. Grimes  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
3659deaec5SRodney W. Grimes  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
3759deaec5SRodney W. Grimes  * SUCH DAMAGE.
3816133e15STim J. Robbins  * From FreeBSD: src/include/ctype.h,v 1.27 2004/06/23 07:11:39 tjr Exp
3959deaec5SRodney W. Grimes  */
4059deaec5SRodney W. Grimes 
4116133e15STim J. Robbins #ifndef __CTYPE_H_
4216133e15STim J. Robbins #define	__CTYPE_H_
4359deaec5SRodney W. Grimes 
4486eedb4eSMike Barcroft #include <sys/cdefs.h>
4586eedb4eSMike Barcroft #include <sys/_types.h>
4659deaec5SRodney W. Grimes 
47f506ed74SDavid E. O'Brien #define	_CTYPE_A	0x00000100L		/* Alpha */
48f506ed74SDavid E. O'Brien #define	_CTYPE_C	0x00000200L		/* Control */
49f506ed74SDavid E. O'Brien #define	_CTYPE_D	0x00000400L		/* Digit */
50f506ed74SDavid E. O'Brien #define	_CTYPE_G	0x00000800L		/* Graph */
51f506ed74SDavid E. O'Brien #define	_CTYPE_L	0x00001000L		/* Lower */
52f506ed74SDavid E. O'Brien #define	_CTYPE_P	0x00002000L		/* Punct */
53f506ed74SDavid E. O'Brien #define	_CTYPE_S	0x00004000L		/* Space */
54f506ed74SDavid E. O'Brien #define	_CTYPE_U	0x00008000L		/* Upper */
55f506ed74SDavid E. O'Brien #define	_CTYPE_X	0x00010000L		/* X digit */
56f506ed74SDavid E. O'Brien #define	_CTYPE_B	0x00020000L		/* Blank */
57f506ed74SDavid E. O'Brien #define	_CTYPE_R	0x00040000L		/* Print */
58f506ed74SDavid E. O'Brien #define	_CTYPE_I	0x00080000L		/* Ideogram */
59f506ed74SDavid E. O'Brien #define	_CTYPE_T	0x00100000L		/* Special */
60f506ed74SDavid E. O'Brien #define	_CTYPE_Q	0x00200000L		/* Phonogram */
61f5dde016SBaptiste Daroussin #define	_CTYPE_N 	0x00400000L		/* Number (superset of digit) */
62c3121a34SMichael C . Wu #define	_CTYPE_SW0	0x20000000L		/* 0 width character */
63dcc3da58SAndrey A. Chernov #define	_CTYPE_SW1	0x40000000L		/* 1 width character */
64c3121a34SMichael C . Wu #define	_CTYPE_SW2	0x80000000L		/* 2 width character */
65c3121a34SMichael C . Wu #define	_CTYPE_SW3	0xc0000000L		/* 3 width character */
66de6c9c9dSTim J. Robbins #define	_CTYPE_SWM	0xe0000000L		/* Mask for screen width data */
67de6c9c9dSTim J. Robbins #define	_CTYPE_SWS	30			/* Bits to shift to get width */
6859deaec5SRodney W. Grimes 
69423eb945SMike Barcroft /* See comments in <sys/_types.h> about __ct_rune_t. */
7059deaec5SRodney W. Grimes __BEGIN_DECLS
7117211a5fSTim J. Robbins unsigned long	___runetype(__ct_rune_t) __pure;
7217211a5fSTim J. Robbins __ct_rune_t	___tolower(__ct_rune_t) __pure;
7317211a5fSTim J. Robbins __ct_rune_t	___toupper(__ct_rune_t) __pure;
7459deaec5SRodney W. Grimes __END_DECLS
7559deaec5SRodney W. Grimes 
7659deaec5SRodney W. Grimes /*
7718c34920SBruce Evans  * _EXTERNALIZE_CTYPE_INLINES_ is defined in locale/nomacros.c to tell us
7818c34920SBruce Evans  * to generate code for extern versions of all our inline functions.
7959deaec5SRodney W. Grimes  */
8018c34920SBruce Evans #ifdef _EXTERNALIZE_CTYPE_INLINES_
8118c34920SBruce Evans #define	_USE_CTYPE_INLINE_
8218c34920SBruce Evans #define	static
8318c34920SBruce Evans #define	__inline
8459deaec5SRodney W. Grimes #endif
8559deaec5SRodney W. Grimes 
86367ed4e1SAndrey A. Chernov extern int __mb_sb_limit;
87367ed4e1SAndrey A. Chernov 
8818c34920SBruce Evans /*
8918c34920SBruce Evans  * Use inline functions if we are allowed to and the compiler supports them.
9018c34920SBruce Evans  */
9118c34920SBruce Evans #if !defined(_DONT_USE_CTYPE_INLINE_) && \
9218c34920SBruce Evans     (defined(_USE_CTYPE_INLINE_) || defined(__GNUC__) || defined(__cplusplus))
9386eedb4eSMike Barcroft 
9486eedb4eSMike Barcroft #include <runetype.h>
9586eedb4eSMike Barcroft 
9659deaec5SRodney W. Grimes static __inline int
__maskrune(__ct_rune_t _c,unsigned long _f)97abbd8902SMike Barcroft __maskrune(__ct_rune_t _c, unsigned long _f)
9859deaec5SRodney W. Grimes {
9980fd9251SAndrey A. Chernov 	return ((_c < 0 || _c >= _CACHED_RUNES) ? ___runetype(_c) :
100ddc1ededSTim J. Robbins 		_CurrentRuneLocale->__runetype[_c]) & _f;
10159deaec5SRodney W. Grimes }
10259deaec5SRodney W. Grimes 
10359deaec5SRodney W. Grimes static __inline int
__sbmaskrune(__ct_rune_t _c,unsigned long _f)104367ed4e1SAndrey A. Chernov __sbmaskrune(__ct_rune_t _c, unsigned long _f)
105367ed4e1SAndrey A. Chernov {
106367ed4e1SAndrey A. Chernov 	return (_c < 0 || _c >= __mb_sb_limit) ? 0 :
107367ed4e1SAndrey A. Chernov 	       _CurrentRuneLocale->__runetype[_c] & _f;
108367ed4e1SAndrey A. Chernov }
109367ed4e1SAndrey A. Chernov 
110367ed4e1SAndrey A. Chernov static __inline int
__istype(__ct_rune_t _c,unsigned long _f)111abbd8902SMike Barcroft __istype(__ct_rune_t _c, unsigned long _f)
112509853bbSRob Braun {
113509853bbSRob Braun 	return (!!__maskrune(_c, _f));
114509853bbSRob Braun }
115509853bbSRob Braun 
116509853bbSRob Braun static __inline int
__sbistype(__ct_rune_t _c,unsigned long _f)117367ed4e1SAndrey A. Chernov __sbistype(__ct_rune_t _c, unsigned long _f)
118367ed4e1SAndrey A. Chernov {
119367ed4e1SAndrey A. Chernov 	return (!!__sbmaskrune(_c, _f));
120367ed4e1SAndrey A. Chernov }
121367ed4e1SAndrey A. Chernov 
122367ed4e1SAndrey A. Chernov static __inline int
__isctype(__ct_rune_t _c,unsigned long _f)123abbd8902SMike Barcroft __isctype(__ct_rune_t _c, unsigned long _f)
12459deaec5SRodney W. Grimes {
12570e7b073SAndrey A. Chernov 	return (_c < 0 || _c >= 128) ? 0 :
126ddc1ededSTim J. Robbins 	       !!(_DefaultRuneLocale.__runetype[_c] & _f);
12759deaec5SRodney W. Grimes }
12859deaec5SRodney W. Grimes 
129abbd8902SMike Barcroft static __inline __ct_rune_t
__toupper(__ct_rune_t _c)130abbd8902SMike Barcroft __toupper(__ct_rune_t _c)
13159deaec5SRodney W. Grimes {
132f905bd50SAndrey A. Chernov 	return (_c < 0 || _c >= _CACHED_RUNES) ? ___toupper(_c) :
133ddc1ededSTim J. Robbins 	       _CurrentRuneLocale->__mapupper[_c];
13459deaec5SRodney W. Grimes }
13559deaec5SRodney W. Grimes 
136abbd8902SMike Barcroft static __inline __ct_rune_t
__sbtoupper(__ct_rune_t _c)137367ed4e1SAndrey A. Chernov __sbtoupper(__ct_rune_t _c)
138367ed4e1SAndrey A. Chernov {
139367ed4e1SAndrey A. Chernov 	return (_c < 0 || _c >= __mb_sb_limit) ? _c :
140367ed4e1SAndrey A. Chernov 	       _CurrentRuneLocale->__mapupper[_c];
141367ed4e1SAndrey A. Chernov }
142367ed4e1SAndrey A. Chernov 
143367ed4e1SAndrey A. Chernov static __inline __ct_rune_t
__tolower(__ct_rune_t _c)144abbd8902SMike Barcroft __tolower(__ct_rune_t _c)
14518c34920SBruce Evans {
146f905bd50SAndrey A. Chernov 	return (_c < 0 || _c >= _CACHED_RUNES) ? ___tolower(_c) :
147ddc1ededSTim J. Robbins 	       _CurrentRuneLocale->__maplower[_c];
14818c34920SBruce Evans }
14918c34920SBruce Evans 
150367ed4e1SAndrey A. Chernov static __inline __ct_rune_t
__sbtolower(__ct_rune_t _c)151367ed4e1SAndrey A. Chernov __sbtolower(__ct_rune_t _c)
152367ed4e1SAndrey A. Chernov {
153367ed4e1SAndrey A. Chernov 	return (_c < 0 || _c >= __mb_sb_limit) ? _c :
154367ed4e1SAndrey A. Chernov 	       _CurrentRuneLocale->__maplower[_c];
155367ed4e1SAndrey A. Chernov }
156367ed4e1SAndrey A. Chernov 
157de6c9c9dSTim J. Robbins static __inline int
__wcwidth(__ct_rune_t _c)158de6c9c9dSTim J. Robbins __wcwidth(__ct_rune_t _c)
159de6c9c9dSTim J. Robbins {
160de6c9c9dSTim J. Robbins 	unsigned int _x;
161de6c9c9dSTim J. Robbins 
162de6c9c9dSTim J. Robbins 	if (_c == 0)
163de6c9c9dSTim J. Robbins 		return (0);
164de6c9c9dSTim J. Robbins 	_x = (unsigned int)__maskrune(_c, _CTYPE_SWM|_CTYPE_R);
165de6c9c9dSTim J. Robbins 	if ((_x & _CTYPE_SWM) != 0)
166de6c9c9dSTim J. Robbins 		return ((_x & _CTYPE_SWM) >> _CTYPE_SWS);
167de6c9c9dSTim J. Robbins 	return ((_x & _CTYPE_R) != 0 ? 1 : -1);
168de6c9c9dSTim J. Robbins }
169de6c9c9dSTim J. Robbins 
17018c34920SBruce Evans #else /* not using inlines */
17159deaec5SRodney W. Grimes 
17259deaec5SRodney W. Grimes __BEGIN_DECLS
173abbd8902SMike Barcroft int		__maskrune(__ct_rune_t, unsigned long);
174367ed4e1SAndrey A. Chernov int		__sbmaskrune(__ct_rune_t, unsigned long);
175abbd8902SMike Barcroft int		__istype(__ct_rune_t, unsigned long);
176367ed4e1SAndrey A. Chernov int		__sbistype(__ct_rune_t, unsigned long);
177abbd8902SMike Barcroft int		__isctype(__ct_rune_t, unsigned long);
178abbd8902SMike Barcroft __ct_rune_t	__toupper(__ct_rune_t);
179367ed4e1SAndrey A. Chernov __ct_rune_t	__sbtoupper(__ct_rune_t);
180abbd8902SMike Barcroft __ct_rune_t	__tolower(__ct_rune_t);
181367ed4e1SAndrey A. Chernov __ct_rune_t	__sbtolower(__ct_rune_t);
182de6c9c9dSTim J. Robbins int		__wcwidth(__ct_rune_t);
18359deaec5SRodney W. Grimes __END_DECLS
18418c34920SBruce Evans #endif /* using inlines */
18559deaec5SRodney W. Grimes 
18616133e15STim J. Robbins #endif /* !__CTYPE_H_ */
187