ctype.h revision 102998
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 102998 2002-09-06 04:22:54Z mike $
431539Srgrimes */
441539Srgrimes
457655Sbde#ifndef _CTYPE_H_
467655Sbde#define	_CTYPE_H_
471539Srgrimes
487655Sbde/*
49102998Smike * XXX <runetype.h> brings namespace pollution (struct member names).
507655Sbde */
511539Srgrimes#include <runetype.h>
521539Srgrimes
5357035Sobrien#define	_CTYPE_A	0x00000100L		/* Alpha */
5457035Sobrien#define	_CTYPE_C	0x00000200L		/* Control */
5557035Sobrien#define	_CTYPE_D	0x00000400L		/* Digit */
5657035Sobrien#define	_CTYPE_G	0x00000800L		/* Graph */
5757035Sobrien#define	_CTYPE_L	0x00001000L		/* Lower */
5857035Sobrien#define	_CTYPE_P	0x00002000L		/* Punct */
5957035Sobrien#define	_CTYPE_S	0x00004000L		/* Space */
6057035Sobrien#define	_CTYPE_U	0x00008000L		/* Upper */
6157035Sobrien#define	_CTYPE_X	0x00010000L		/* X digit */
6257035Sobrien#define	_CTYPE_B	0x00020000L		/* Blank */
6357035Sobrien#define	_CTYPE_R	0x00040000L		/* Print */
6457035Sobrien#define	_CTYPE_I	0x00080000L		/* Ideogram */
6557035Sobrien#define	_CTYPE_T	0x00100000L		/* Special */
6657035Sobrien#define	_CTYPE_Q	0x00200000L		/* Phonogram */
67101984Skeichii#define	_CTYPE_SW0	0x20000000L		/* 0 width character */
68102093Sache#define	_CTYPE_SW1	0x40000000L		/* 1 width character */
69101984Skeichii#define	_CTYPE_SW2	0x80000000L		/* 2 width character */
70101984Skeichii#define	_CTYPE_SW3	0xc0000000L		/* 3 width character */
711539Srgrimes
727655Sbde__BEGIN_DECLS
7393032Simpint	isalnum(int);
7493032Simpint	isalpha(int);
7593032Simpint	iscntrl(int);
7693032Simpint	isdigit(int);
7793032Simpint	isgraph(int);
7893032Simpint	islower(int);
7993032Simpint	isprint(int);
8093032Simpint	ispunct(int);
8193032Simpint	isspace(int);
8293032Simpint	isupper(int);
8393032Simpint	isxdigit(int);
8493032Simpint	tolower(int);
8593032Simpint	toupper(int);
861539Srgrimes
87102998Smike#if __XSI_VISIBLE
88102998Smikeint	_tolower(int);
89102998Smikeint	_toupper(int);
90102998Smikeint	isascii(int);
91102998Smikeint	toascii(int);
92102998Smike#endif
93102998Smike
94102998Smike#if __BSD_VISIBLE
9593032Simpint	digittoint(int);
9693032Simpint	isblank(int);
9793032Simpint	ishexnumber(int);
9893032Simpint	isideogram(int);
9993032Simpint	isnumber(int);
10093032Simpint	isphonogram(int);
10193032Simpint	isrune(int);
10293032Simpint	isspecial(int);
1037655Sbde#endif
1047655Sbde__END_DECLS
1057655Sbde
10657035Sobrien#define	isalnum(c)	__istype((c), _CTYPE_A|_CTYPE_D)
10757035Sobrien#define	isalpha(c)	__istype((c), _CTYPE_A)
10857035Sobrien#define	iscntrl(c)	__istype((c), _CTYPE_C)
10957035Sobrien#define	isdigit(c)	__isctype((c), _CTYPE_D) /* ANSI -- locale independent */
11057035Sobrien#define	isgraph(c)	__istype((c), _CTYPE_G)
11157035Sobrien#define	islower(c)	__istype((c), _CTYPE_L)
11257035Sobrien#define	isprint(c)	__istype((c), _CTYPE_R)
11357035Sobrien#define	ispunct(c)	__istype((c), _CTYPE_P)
11457035Sobrien#define	isspace(c)	__istype((c), _CTYPE_S)
11557035Sobrien#define	isupper(c)	__istype((c), _CTYPE_U)
11657035Sobrien#define	isxdigit(c)	__isctype((c), _CTYPE_X) /* ANSI -- locale independent */
1177655Sbde#define	tolower(c)	__tolower(c)
1187655Sbde#define	toupper(c)	__toupper(c)
1197655Sbde
120102998Smike#if __XSI_VISIBLE
121102998Smike/*
122102998Smike * POSIX.1-2001 specifies _tolower() and _toupper() to be macros equivalent to
123102998Smike * tolower() and toupper() respectively, minus extra checking to ensure that
124102998Smike * the argument is a lower or uppercase letter respectively.  We've chosen to
125102998Smike * implement these macros with the same error checking as tolower() and
126102998Smike * toupper() since this doesn't violate the specification itself, only its
127102998Smike * intent.  We purposely leave _tolower() and _toupper() undocumented to
128102998Smike * discourage their use.
129102998Smike *
130102998Smike * XXX isascii() and toascii() should similarly be undocumented.
131102998Smike */
132102998Smike#define	_tolower(c)	__tolower(c)
133102998Smike#define	_toupper(c)	__toupper(c)
134102998Smike#define	isascii(c)	(((c) & ~0x7F) == 0)
135102998Smike#define	toascii(c)	((c) & 0x7F)
136102998Smike#endif
137102998Smike
138102998Smike#if __BSD_VISIBLE
13954746Sphantom#define	digittoint(c)	__maskrune((c), 0xFF)
14057035Sobrien#define	isblank(c)	__istype((c), _CTYPE_B)
14157035Sobrien#define	ishexnumber(c)	__istype((c), _CTYPE_X)
14257035Sobrien#define	isideogram(c)	__istype((c), _CTYPE_I)
14357035Sobrien#define	isnumber(c)	__istype((c), _CTYPE_D)
14457035Sobrien#define	isphonogram(c)	__istype((c), _CTYPE_Q)
1457655Sbde#define	isrune(c)	__istype((c), 0xFFFFFF00L)
14657035Sobrien#define	isspecial(c)	__istype((c), _CTYPE_T)
1471539Srgrimes#endif
1481539Srgrimes
149102998Smike/* See comments in <sys/_types.h> about __ct_rune_t. */
1501539Srgrimes__BEGIN_DECLS
151102227Smikeunsigned long	___runetype(__ct_rune_t);
152102227Smike__ct_rune_t	___tolower(__ct_rune_t);
153102227Smike__ct_rune_t	___toupper(__ct_rune_t);
1541539Srgrimes__END_DECLS
1551539Srgrimes
1561539Srgrimes/*
1577655Sbde * _EXTERNALIZE_CTYPE_INLINES_ is defined in locale/nomacros.c to tell us
1587655Sbde * to generate code for extern versions of all our inline functions.
1591539Srgrimes */
1607655Sbde#ifdef _EXTERNALIZE_CTYPE_INLINES_
1617655Sbde#define	_USE_CTYPE_INLINE_
1627655Sbde#define	static
1637655Sbde#define	__inline
1641539Srgrimes#endif
1651539Srgrimes
1667655Sbde/*
1677655Sbde * Use inline functions if we are allowed to and the compiler supports them.
1687655Sbde */
1697655Sbde#if !defined(_DONT_USE_CTYPE_INLINE_) && \
1707655Sbde    (defined(_USE_CTYPE_INLINE_) || defined(__GNUC__) || defined(__cplusplus))
1711539Srgrimesstatic __inline int
172102227Smike__maskrune(__ct_rune_t _c, unsigned long _f)
1731539Srgrimes{
17429855Sache	return ((_c < 0 || _c >= _CACHED_RUNES) ? ___runetype(_c) :
17529855Sache		_CurrentRuneLocale->runetype[_c]) & _f;
1761539Srgrimes}
1771539Srgrimes
1781539Srgrimesstatic __inline int
179102227Smike__istype(__ct_rune_t _c, unsigned long _f)
18090231Sbbraun{
18190231Sbbraun	return (!!__maskrune(_c, _f));
18290231Sbbraun}
18390231Sbbraun
18490231Sbbraunstatic __inline int
185102227Smike__isctype(__ct_rune_t _c, unsigned long _f)
1861539Srgrimes{
18712028Sache	return (_c < 0 || _c >= _CACHED_RUNES) ? 0 :
18829883Sache	       !!(_DefaultRuneLocale.runetype[_c] & _f);
1891539Srgrimes}
1901539Srgrimes
191102227Smikestatic __inline __ct_rune_t
192102227Smike__toupper(__ct_rune_t _c)
1931539Srgrimes{
19414813Sache	return (_c < 0 || _c >= _CACHED_RUNES) ? ___toupper(_c) :
19512028Sache	       _CurrentRuneLocale->mapupper[_c];
1961539Srgrimes}
1971539Srgrimes
198102227Smikestatic __inline __ct_rune_t
199102227Smike__tolower(__ct_rune_t _c)
2001539Srgrimes{
20114813Sache	return (_c < 0 || _c >= _CACHED_RUNES) ? ___tolower(_c) :
20212028Sache	       _CurrentRuneLocale->maplower[_c];
2031539Srgrimes}
2041539Srgrimes
2057655Sbde#else /* not using inlines */
2061539Srgrimes
2071539Srgrimes__BEGIN_DECLS
208102227Smikeint		__maskrune(__ct_rune_t, unsigned long);
209102227Smikeint		__istype(__ct_rune_t, unsigned long);
210102227Smikeint		__isctype(__ct_rune_t, unsigned long);
211102227Smike__ct_rune_t	__toupper(__ct_rune_t);
212102227Smike__ct_rune_t	__tolower(__ct_rune_t);
2131539Srgrimes__END_DECLS
2147655Sbde#endif /* using inlines */
2151539Srgrimes
2161539Srgrimes#endif /* !_CTYPE_H_ */
217