0001: /* Copyright (C) 1991,92,93,95,96,97,98,99,2001,2002,2004,2007,2008,2009,2011 0002: Free Software Foundation, Inc. 0003: This file is part of the GNU C Library. 0004: 0005: The GNU C Library is free software; you can redistribute it and/or 0006: modify it under the terms of the GNU Lesser General Public 0007: License as published by the Free Software Foundation; either 0008: version 2.1 of the License, or (at your option) any later version. 0009: 0010: The GNU C Library is distributed in the hope that it will be useful, 0011: but WITHOUT ANY WARRANTY; without even the implied warranty of 0012: MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 0013: Lesser General Public License for more details. 0014: 0015: You should have received a copy of the GNU Lesser General Public 0016: License along with the GNU C Library; if not, write to the Free 0017: Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 0018: 02111-1307 USA. */ 0019: 0020: /* 0021: * ISO C99 Standard 7.4: Character handling <ctype.h> 0022: */ 0023: 0024: #ifndef _CTYPE_H 0025: #define _CTYPE_H 1 0026: 0027: #include <features.h> 0028: #include <bits/types.h> 0029: 0030: __BEGIN_DECLS 0031: 0032: #ifndef _ISbit 0033: /* These are all the characteristics of characters. 0034: If there get to be more than 16 distinct characteristics, 0035: many things must be changed that use `unsigned short int's. 0036: 0037: The characteristics are stored always in network byte order (big 0038: endian). We define the bit value interpretations here dependent on the 0039: machine's byte order. */ 0040: 0041: # include <endian.h> 0042: # if __BYTE_ORDER == __BIG_ENDIAN 0043: # define _ISbit(bit) (1 << (bit)) 0044: # else /* __BYTE_ORDER == __LITTLE_ENDIAN */ 0045: # define _ISbit(bit) ((bit) < 8 ? ((1 << (bit)) << 8) : ((1 << (bit)) >> 8)) 0046: # endif 0047: 0048: enum 0049: { 0050: _ISupper = _ISbit (0), /* UPPERCASE. */ 0051: _ISlower = _ISbit (1), /* lowercase. */ 0052: _ISalpha = _ISbit (2), /* Alphabetic. */ 0053: _ISdigit = _ISbit (3), /* Numeric. */ 0054: _ISxdigit = _ISbit (4), /* Hexadecimal numeric. */ 0055: _ISspace = _ISbit (5), /* Whitespace. */ 0056: _ISprint = _ISbit (6), /* Printing. */ 0057: _ISgraph = _ISbit (7), /* Graphical. */ 0058: _ISblank = _ISbit (8), /* Blank (usually SPC and TAB). */ 0059: _IScntrl = _ISbit (9), /* Control character. */ 0060: _ISpunct = _ISbit (10), /* Punctuation. */ 0061: _ISalnum = _ISbit (11) /* Alphanumeric. */ 0062: }; 0063: #endif /* ! _ISbit */ 0064: 0065: /* These are defined in ctype-info.c. 0066: The declarations here must match those in localeinfo.h. 0067: 0068: In the thread-specific locale model (see `uselocale' in <locale.h>) 0069: we cannot use global variables for these as was done in the past. 0070: Instead, the following accessor functions return the address of 0071: each variable, which is local to the current thread if multithreaded. 0072: 0073: These point into arrays of 384, so they can be indexed by any `unsigned 0074: char' value [0,255]; by EOF (-1); or by any `signed char' value 0075: [-128,-1). ISO C requires that the ctype functions work for `unsigned 0076: char' values and for EOF; we also support negative `signed char' values 0077: for broken old programs. The case conversion arrays are of `int's 0078: rather than `unsigned char's because tolower (EOF) must be EOF, which 0079: doesn't fit into an `unsigned char'. But today more important is that 0080: the arrays are also used for multi-byte character sets. */ 0081: extern __const unsigned short int **__ctype_b_loc (void) 0082: __THROW __attribute__ ((__const)); 0083: extern __const __int32_t **__ctype_tolower_loc (void) 0084: __THROW __attribute__ ((__const)); 0085: extern __const __int32_t **__ctype_toupper_loc (void) 0086: __THROW __attribute__ ((__const)); 0087: 0088: 0089: #ifndef __cplusplus 0090: # define __isctype(c, type) \ 0091: ((*__ctype_b_loc ())[(int) (c)] & (unsigned short int) type) 0092: #elif defined __USE_EXTERN_INLINES 0093: # define __isctype_f(type) \ 0094: __extern_inline int \ 0095: is##type (int __c) __THROW \ 0096: { \ 0097: return (*__ctype_b_loc ())[(int) (__c)] & (unsigned short int) _IS##type; \ 0098: } 0099: #endif 0100: 0101: #define __isascii(c) (((c) & ~0x7f) == 0) /* If C is a 7 bit value. */ 0102: #define __toascii(c) ((c) & 0x7f) /* Mask off high bits. */ 0103: 0104: #define __exctype(name) extern int name (int) __THROW 0105: 0106: __BEGIN_NAMESPACE_STD 0107: 0108: /* The following names are all functions: 0109: int isCHARACTERISTIC(int c); 0110: which return nonzero iff C has CHARACTERISTIC. 0111: For the meaning of the characteristic names, see the `enum' above. */ 0112: __exctype (isalnum); 0113: __exctype (isalpha); 0114: __exctype (iscntrl); 0115: __exctype (isdigit); 0116: __exctype (islower); 0117: __exctype (isgraph); 0118: __exctype (isprint); 0119: __exctype (ispunct); 0120: __exctype (isspace); 0121: __exctype (isupper); 0122: __exctype (isxdigit); 0123: 0124: 0125: /* Return the lowercase version of C. */ 0126: extern int tolower (int __c) __THROW; 0127: 0128: /* Return the uppercase version of C. */ 0129: extern int toupper (int __c) __THROW; 0130: 0131: __END_NAMESPACE_STD 0132: 0133: 0134: /* ISO C99 introduced one new function. */ 0135: #ifdef __USE_ISOC99 0136: __BEGIN_NAMESPACE_C99 0137: 0138: __exctype (isblank); 0139: 0140: __END_NAMESPACE_C99 0141: #endif 0142: 0143: #ifdef __USE_GNU 0144: /* Test C for a set of character classes according to MASK. */ 0145: extern int isctype (int __c, int __mask) __THROW; 0146: #endif 0147: 0148: #if defined __USE_SVID || defined __USE_MISC || defined __USE_XOPEN 0149: 0150: /* Return nonzero iff C is in the ASCII set 0151: (i.e., is no more than 7 bits wide). */ 0152: extern int isascii (int __c) __THROW; 0153: 0154: /* Return the part of C that is in the ASCII set 0155: (i.e., the low-order 7 bits of C). */ 0156: extern int toascii (int __c) __THROW; 0157: 0158: /* These are the same as `toupper' and `tolower' except that they do not 0159: check the argument for being in the range of a `char'. */ 0160: __exctype (_toupper); 0161: __exctype (_tolower); 0162: #endif /* Use SVID or use misc. */ 0163: 0164: /* This code is needed for the optimized mapping functions. */ 0165: #define __tobody(c, f, a, args) \ 0166: (__extension__ \ 0167: ({ int __res; \ 0168: if (sizeof (c) > 1) \ 0169: { \ 0170: if (__builtin_constant_p (c)) \ 0171: { \ 0172: int __c = (c); \ 0173: __res = __c < -128 || __c > 255 ? __c : (a)[__c]; \ 0174: } \ 0175: else \ 0176: __res = f args; \ 0177: } \ 0178: else \ 0179: __res = (a)[(int) (c)]; \ 0180: __res; })) 0181: 0182: #if !defined __NO_CTYPE 0183: # ifdef __isctype_f 0184: __isctype_f (alnum) 0185: __isctype_f (alpha) 0186: __isctype_f (cntrl) 0187: __isctype_f (digit) 0188: __isctype_f (lower) 0189: __isctype_f (graph) 0190: __isctype_f (print) 0191: __isctype_f (punct) 0192: __isctype_f (space) 0193: __isctype_f (upper) 0194: __isctype_f (xdigit) 0195: # ifdef __USE_ISOC99 0196: __isctype_f (blank) 0197: # endif 0198: # elif defined __isctype 0199: # define isalnum(c) __isctype((c), _ISalnum) 0200: # define isalpha(c) __isctype((c), _ISalpha) 0201: # define iscntrl(c) __isctype((c), _IScntrl) 0202: # define isdigit(c) __isctype((c), _ISdigit) 0203: # define islower(c) __isctype((c), _ISlower) 0204: # define isgraph(c) __isctype((c), _ISgraph) 0205: # define isprint(c) __isctype((c), _ISprint) 0206: # define ispunct(c) __isctype((c), _ISpunct) 0207: # define isspace(c) __isctype((c), _ISspace) 0208: # define isupper(c) __isctype((c), _ISupper) 0209: # define isxdigit(c) __isctype((c), _ISxdigit) 0210: # ifdef __USE_ISOC99 0211: # define isblank(c) __isctype((c), _ISblank) 0212: # endif 0213: # endif 0214: 0215: # ifdef __USE_EXTERN_INLINES 0216: __extern_inline int 0217: __NTH (tolower (int __c)) 0218: { 0219: return __c >= -128 && __c < 256 ? (*__ctype_tolower_loc ())[__c] : __c; 0220: } 0221: 0222: __extern_inline int 0223: __NTH (toupper (int __c)) 0224: { 0225: return __c >= -128 && __c < 256 ? (*__ctype_toupper_loc ())[__c] : __c; 0226: } 0227: # endif 0228: 0229: # if __GNUC__ >= 2 && defined __OPTIMIZE__ && !defined __cplusplus 0230: # define tolower(c) __tobody (c, tolower, *__ctype_tolower_loc (), (c)) 0231: # define toupper(c) __tobody (c, toupper, *__ctype_toupper_loc (), (c)) 0232: # endif /* Optimizing gcc */ 0233: 0234: # if defined __USE_SVID || defined __USE_MISC || defined __USE_XOPEN 0235: # define isascii(c) __isascii (c) 0236: # define toascii(c) __toascii (c) 0237: 0238: # define _tolower(c) ((int) (*__ctype_tolower_loc ())[(int) (c)]) 0239: # define _toupper(c) ((int) (*__ctype_toupper_loc ())[(int) (c)]) 0240: # endif 0241: 0242: #endif /* Not __NO_CTYPE. */ 0243: 0244: 0245: #ifdef __USE_XOPEN2K8 0246: /* The concept of one static locale per category is not very well 0247: thought out. Many applications will need to process its data using 0248: information from several different locales. Another application is 0249: the implementation of the internationalization handling in the 0250: upcoming ISO C++ standard library. To support this another set of 0251: the functions using locale data exist which have an additional 0252: argument. 0253: 0254: Attention: all these functions are *not* standardized in any form. 0255: This is a proof-of-concept implementation. */ 0256: 0257: /* Structure for reentrant locale using functions. This is an 0258: (almost) opaque type for the user level programs. */ 0259: # include <xlocale.h> 0260: 0261: /* These definitions are similar to the ones above but all functions 0262: take as an argument a handle for the locale which shall be used. */ 0263: # define __isctype_l(c, type, locale) \ 0264: ((locale)->__ctype_b[(int) (c)] & (unsigned short int) type) 0265: 0266: # define __exctype_l(name) \ 0267: extern int name (int, __locale_t) __THROW 0268: 0269: /* The following names are all functions: 0270: int isCHARACTERISTIC(int c, locale_t *locale); 0271: which return nonzero iff C has CHARACTERISTIC. 0272: For the meaning of the characteristic names, see the `enum' above. */ 0273: __exctype_l (isalnum_l); 0274: __exctype_l (isalpha_l); 0275: __exctype_l (iscntrl_l); 0276: __exctype_l (isdigit_l); 0277: __exctype_l (islower_l); 0278: __exctype_l (isgraph_l); 0279: __exctype_l (isprint_l); 0280: __exctype_l (ispunct_l); 0281: __exctype_l (isspace_l); 0282: __exctype_l (isupper_l); 0283: __exctype_l (isxdigit_l); 0284: 0285: __exctype_l (isblank_l); 0286: 0287: 0288: /* Return the lowercase version of C in locale L. */ 0289: extern int __tolower_l (int __c, __locale_t __l) __THROW; 0290: extern int tolower_l (int __c, __locale_t __l) __THROW; 0291: 0292: /* Return the uppercase version of C. */ 0293: extern int __toupper_l (int __c, __locale_t __l) __THROW; 0294: extern int toupper_l (int __c, __locale_t __l) __THROW; 0295: 0296: # if __GNUC__ >= 2 && defined __OPTIMIZE__ && !defined __cplusplus 0297: # define __tolower_l(c, locale) \ 0298: __tobody (c, __tolower_l, (locale)->__ctype_tolower, (c, locale)) 0299: # define __toupper_l(c, locale) \ 0300: __tobody (c, __toupper_l, (locale)->__ctype_toupper, (c, locale)) 0301: # define tolower_l(c, locale) __tolower_l ((c), (locale)) 0302: # define toupper_l(c, locale) __toupper_l ((c), (locale)) 0303: # endif /* Optimizing gcc */ 0304: 0305: 0306: # ifndef __NO_CTYPE 0307: # define __isalnum_l(c,l) __isctype_l((c), _ISalnum, (l)) 0308: # define __isalpha_l(c,l) __isctype_l((c), _ISalpha, (l)) 0309: # define __iscntrl_l(c,l) __isctype_l((c), _IScntrl, (l)) 0310: # define __isdigit_l(c,l) __isctype_l((c), _ISdigit, (l)) 0311: # define __islower_l(c,l) __isctype_l((c), _ISlower, (l)) 0312: # define __isgraph_l(c,l) __isctype_l((c), _ISgraph, (l)) 0313: # define __isprint_l(c,l) __isctype_l((c), _ISprint, (l)) 0314: # define __ispunct_l(c,l) __isctype_l((c), _ISpunct, (l)) 0315: # define __isspace_l(c,l) __isctype_l((c), _ISspace, (l)) 0316: # define __isupper_l(c,l) __isctype_l((c), _ISupper, (l)) 0317: # define __isxdigit_l(c,l) __isctype_l((c), _ISxdigit, (l)) 0318: 0319: # define __isblank_l(c,l) __isctype_l((c), _ISblank, (l)) 0320: 0321: # if defined __USE_SVID || defined __USE_MISC 0322: # define __isascii_l(c,l) ((l), __isascii (c)) 0323: # define __toascii_l(c,l) ((l), __toascii (c)) 0324: # endif 0325: 0326: # define isalnum_l(c,l) __isalnum_l ((c), (l)) 0327: # define isalpha_l(c,l) __isalpha_l ((c), (l)) 0328: # define iscntrl_l(c,l) __iscntrl_l ((c), (l)) 0329: # define isdigit_l(c,l) __isdigit_l ((c), (l)) 0330: # define islower_l(c,l) __islower_l ((c), (l)) 0331: # define isgraph_l(c,l) __isgraph_l ((c), (l)) 0332: # define isprint_l(c,l) __isprint_l ((c), (l)) 0333: # define ispunct_l(c,l) __ispunct_l ((c), (l)) 0334: # define isspace_l(c,l) __isspace_l ((c), (l)) 0335: # define isupper_l(c,l) __isupper_l ((c), (l)) 0336: # define isxdigit_l(c,l) __isxdigit_l ((c), (l)) 0337: 0338: # define isblank_l(c,l) __isblank_l ((c), (l)) 0339: 0340: # if defined __USE_SVID || defined __USE_MISC 0341: # define isascii_l(c,l) __isascii_l ((c), (l)) 0342: # define toascii_l(c,l) __toascii_l ((c), (l)) 0343: # endif 0344: 0345: # endif /* Not __NO_CTYPE. */ 0346: 0347: #endif /* Use POSIX 2008. */ 0348: 0349: __END_DECLS 0350: 0351: #endif /* ctype.h */