_ctype.h revision 130961
11539Srgrimes/*
21539Srgrimes * Copyright (c) 1989, 1993
31539Srgrimes *	The Regents of the University of California.  All rights reserved.
41539Srgrimes * (c) UNIX System Laboratories, Inc.
51539Srgrimes * All or some portions of this file are derived from material licensed
61539Srgrimes * to the University of California by American Telephone and Telegraph
71539Srgrimes * Co. or Unix System Laboratories, Inc. and are reproduced herein with
81539Srgrimes * the permission of UNIX System Laboratories, Inc.
91539Srgrimes *
101539Srgrimes * This code is derived from software contributed to Berkeley by
111539Srgrimes * Paul Borman at Krystal Technologies.
121539Srgrimes *
131539Srgrimes * Redistribution and use in source and binary forms, with or without
141539Srgrimes * modification, are permitted provided that the following conditions
151539Srgrimes * are met:
161539Srgrimes * 1. Redistributions of source code must retain the above copyright
171539Srgrimes *    notice, this list of conditions and the following disclaimer.
181539Srgrimes * 2. Redistributions in binary form must reproduce the above copyright
191539Srgrimes *    notice, this list of conditions and the following disclaimer in the
201539Srgrimes *    documentation and/or other materials provided with the distribution.
211539Srgrimes * 3. All advertising materials mentioning features or use of this software
221539Srgrimes *    must display the following acknowledgement:
231539Srgrimes *	This product includes software developed by the University of
241539Srgrimes *	California, Berkeley and its contributors.
251539Srgrimes * 4. Neither the name of the University nor the names of its contributors
261539Srgrimes *    may be used to endorse or promote products derived from this software
271539Srgrimes *    without specific prior written permission.
281539Srgrimes *
291539Srgrimes * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
301539Srgrimes * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
311539Srgrimes * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
321539Srgrimes * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
331539Srgrimes * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
341539Srgrimes * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
351539Srgrimes * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
361539Srgrimes * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
371539Srgrimes * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
381539Srgrimes * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
391539Srgrimes * SUCH DAMAGE.
401539Srgrimes *
411539Srgrimes *	@(#)ctype.h	8.4 (Berkeley) 1/21/94
4254746Sphantom *      $FreeBSD: head/include/_ctype.h 130961 2004-06-23 07:01:44Z tjr $
431539Srgrimes */
441539Srgrimes
457655Sbde#ifndef _CTYPE_H_
467655Sbde#define	_CTYPE_H_
471539Srgrimes
48103113Smike#include <sys/cdefs.h>
49103113Smike#include <sys/_types.h>
501539Srgrimes
5157035Sobrien#define	_CTYPE_A	0x00000100L		/* Alpha */
5257035Sobrien#define	_CTYPE_C	0x00000200L		/* Control */
5357035Sobrien#define	_CTYPE_D	0x00000400L		/* Digit */
5457035Sobrien#define	_CTYPE_G	0x00000800L		/* Graph */
5557035Sobrien#define	_CTYPE_L	0x00001000L		/* Lower */
5657035Sobrien#define	_CTYPE_P	0x00002000L		/* Punct */
5757035Sobrien#define	_CTYPE_S	0x00004000L		/* Space */
5857035Sobrien#define	_CTYPE_U	0x00008000L		/* Upper */
5957035Sobrien#define	_CTYPE_X	0x00010000L		/* X digit */
6057035Sobrien#define	_CTYPE_B	0x00020000L		/* Blank */
6157035Sobrien#define	_CTYPE_R	0x00040000L		/* Print */
6257035Sobrien#define	_CTYPE_I	0x00080000L		/* Ideogram */
6357035Sobrien#define	_CTYPE_T	0x00100000L		/* Special */
6457035Sobrien#define	_CTYPE_Q	0x00200000L		/* Phonogram */
65101984Skeichii#define	_CTYPE_SW0	0x20000000L		/* 0 width character */
66102093Sache#define	_CTYPE_SW1	0x40000000L		/* 1 width character */
67101984Skeichii#define	_CTYPE_SW2	0x80000000L		/* 2 width character */
68101984Skeichii#define	_CTYPE_SW3	0xc0000000L		/* 3 width character */
691539Srgrimes
707655Sbde__BEGIN_DECLS
7193032Simpint	isalnum(int);
7293032Simpint	isalpha(int);
7393032Simpint	iscntrl(int);
7493032Simpint	isdigit(int);
7593032Simpint	isgraph(int);
7693032Simpint	islower(int);
7793032Simpint	isprint(int);
7893032Simpint	ispunct(int);
7993032Simpint	isspace(int);
8093032Simpint	isupper(int);
8193032Simpint	isxdigit(int);
8293032Simpint	tolower(int);
8393032Simpint	toupper(int);
841539Srgrimes
85102998Smike#if __XSI_VISIBLE
86102998Smikeint	_tolower(int);
87102998Smikeint	_toupper(int);
88102998Smikeint	isascii(int);
89102998Smikeint	toascii(int);
90102998Smike#endif
91102998Smike
92128523Stjr#if __ISO_C_VISIBLE >= 1999
93128523Stjrint	isblank(int);
94128523Stjr#endif
95128523Stjr
96102998Smike#if __BSD_VISIBLE
9793032Simpint	digittoint(int);
9893032Simpint	ishexnumber(int);
9993032Simpint	isideogram(int);
10093032Simpint	isnumber(int);
10193032Simpint	isphonogram(int);
10293032Simpint	isrune(int);
10393032Simpint	isspecial(int);
1047655Sbde#endif
1057655Sbde__END_DECLS
1067655Sbde
10757035Sobrien#define	isalnum(c)	__istype((c), _CTYPE_A|_CTYPE_D)
10857035Sobrien#define	isalpha(c)	__istype((c), _CTYPE_A)
10957035Sobrien#define	iscntrl(c)	__istype((c), _CTYPE_C)
11057035Sobrien#define	isdigit(c)	__isctype((c), _CTYPE_D) /* ANSI -- locale independent */
11157035Sobrien#define	isgraph(c)	__istype((c), _CTYPE_G)
11257035Sobrien#define	islower(c)	__istype((c), _CTYPE_L)
11357035Sobrien#define	isprint(c)	__istype((c), _CTYPE_R)
11457035Sobrien#define	ispunct(c)	__istype((c), _CTYPE_P)
11557035Sobrien#define	isspace(c)	__istype((c), _CTYPE_S)
11657035Sobrien#define	isupper(c)	__istype((c), _CTYPE_U)
11757035Sobrien#define	isxdigit(c)	__isctype((c), _CTYPE_X) /* ANSI -- locale independent */
1187655Sbde#define	tolower(c)	__tolower(c)
1197655Sbde#define	toupper(c)	__toupper(c)
1207655Sbde
121102998Smike#if __XSI_VISIBLE
122102998Smike/*
123102998Smike * POSIX.1-2001 specifies _tolower() and _toupper() to be macros equivalent to
124102998Smike * tolower() and toupper() respectively, minus extra checking to ensure that
125102998Smike * the argument is a lower or uppercase letter respectively.  We've chosen to
126102998Smike * implement these macros with the same error checking as tolower() and
127102998Smike * toupper() since this doesn't violate the specification itself, only its
128102998Smike * intent.  We purposely leave _tolower() and _toupper() undocumented to
129102998Smike * discourage their use.
130102998Smike *
131102998Smike * XXX isascii() and toascii() should similarly be undocumented.
132102998Smike */
133102998Smike#define	_tolower(c)	__tolower(c)
134102998Smike#define	_toupper(c)	__toupper(c)
135102998Smike#define	isascii(c)	(((c) & ~0x7F) == 0)
136102998Smike#define	toascii(c)	((c) & 0x7F)
137102998Smike#endif
138102998Smike
139128523Stjr#if __ISO_C_VISIBLE >= 1999
140128523Stjr#define	isblank(c)	__istype((c), _CTYPE_B)
141128523Stjr#endif
142128523Stjr
143102998Smike#if __BSD_VISIBLE
14454746Sphantom#define	digittoint(c)	__maskrune((c), 0xFF)
14557035Sobrien#define	ishexnumber(c)	__istype((c), _CTYPE_X)
14657035Sobrien#define	isideogram(c)	__istype((c), _CTYPE_I)
14757035Sobrien#define	isnumber(c)	__istype((c), _CTYPE_D)
14857035Sobrien#define	isphonogram(c)	__istype((c), _CTYPE_Q)
1497655Sbde#define	isrune(c)	__istype((c), 0xFFFFFF00L)
15057035Sobrien#define	isspecial(c)	__istype((c), _CTYPE_T)
1511539Srgrimes#endif
1521539Srgrimes
153102998Smike/* See comments in <sys/_types.h> about __ct_rune_t. */
1541539Srgrimes__BEGIN_DECLS
155102227Smikeunsigned long	___runetype(__ct_rune_t);
156102227Smike__ct_rune_t	___tolower(__ct_rune_t);
157102227Smike__ct_rune_t	___toupper(__ct_rune_t);
1581539Srgrimes__END_DECLS
1591539Srgrimes
1601539Srgrimes/*
1617655Sbde * _EXTERNALIZE_CTYPE_INLINES_ is defined in locale/nomacros.c to tell us
1627655Sbde * to generate code for extern versions of all our inline functions.
1631539Srgrimes */
1647655Sbde#ifdef _EXTERNALIZE_CTYPE_INLINES_
1657655Sbde#define	_USE_CTYPE_INLINE_
1667655Sbde#define	static
1677655Sbde#define	__inline
1681539Srgrimes#endif
1691539Srgrimes
1707655Sbde/*
171103113Smike * <runetype.h> brings namespace pollution (struct member names).  This prevents
172103113Smike * us from using the inline optimizations in the more strict __POSIX_VISIBLE and
173103113Smike * __XSI_VISIBLE namespaces.  To fix this properly would require that we rename
174103113Smike * member names of long-standing structs, or something equally evil.
175103113Smike */
176103113Smike#if !__BSD_VISIBLE && !defined(_USE_CTYPE_INLINE_) && \
177103113Smike    !defined(_DONT_USE_CTYPE_INLINE_)
178103113Smike#define	_DONT_USE_CTYPE_INLINE_
179103113Smike#endif
180103113Smike
181103113Smike/*
1827655Sbde * Use inline functions if we are allowed to and the compiler supports them.
1837655Sbde */
1847655Sbde#if !defined(_DONT_USE_CTYPE_INLINE_) && \
1857655Sbde    (defined(_USE_CTYPE_INLINE_) || defined(__GNUC__) || defined(__cplusplus))
186103113Smike
187103113Smike#include <runetype.h>
188103113Smike
1891539Srgrimesstatic __inline int
190102227Smike__maskrune(__ct_rune_t _c, unsigned long _f)
1911539Srgrimes{
19229855Sache	return ((_c < 0 || _c >= _CACHED_RUNES) ? ___runetype(_c) :
193130961Stjr		_CurrentRuneLocale->__runetype[_c]) & _f;
1941539Srgrimes}
1951539Srgrimes
1961539Srgrimesstatic __inline int
197102227Smike__istype(__ct_rune_t _c, unsigned long _f)
19890231Sbbraun{
19990231Sbbraun	return (!!__maskrune(_c, _f));
20090231Sbbraun}
20190231Sbbraun
20290231Sbbraunstatic __inline int
203102227Smike__isctype(__ct_rune_t _c, unsigned long _f)
2041539Srgrimes{
20512028Sache	return (_c < 0 || _c >= _CACHED_RUNES) ? 0 :
206130961Stjr	       !!(_DefaultRuneLocale.__runetype[_c] & _f);
2071539Srgrimes}
2081539Srgrimes
209102227Smikestatic __inline __ct_rune_t
210102227Smike__toupper(__ct_rune_t _c)
2111539Srgrimes{
21214813Sache	return (_c < 0 || _c >= _CACHED_RUNES) ? ___toupper(_c) :
213130961Stjr	       _CurrentRuneLocale->__mapupper[_c];
2141539Srgrimes}
2151539Srgrimes
216102227Smikestatic __inline __ct_rune_t
217102227Smike__tolower(__ct_rune_t _c)
2181539Srgrimes{
21914813Sache	return (_c < 0 || _c >= _CACHED_RUNES) ? ___tolower(_c) :
220130961Stjr	       _CurrentRuneLocale->__maplower[_c];
2211539Srgrimes}
2221539Srgrimes
2237655Sbde#else /* not using inlines */
2241539Srgrimes
2251539Srgrimes__BEGIN_DECLS
226102227Smikeint		__maskrune(__ct_rune_t, unsigned long);
227102227Smikeint		__istype(__ct_rune_t, unsigned long);
228102227Smikeint		__isctype(__ct_rune_t, unsigned long);
229102227Smike__ct_rune_t	__toupper(__ct_rune_t);
230102227Smike__ct_rune_t	__tolower(__ct_rune_t);
2311539Srgrimes__END_DECLS
2327655Sbde#endif /* using inlines */
2331539Srgrimes
2341539Srgrimes#endif /* !_CTYPE_H_ */
235