Back to index

glibc  2.9
ctype.h
Go to the documentation of this file.
00001 /* Copyright (C) 1991,92,93,95,96,97,98,99,2001,2002,2004,2007,2008
00002        Free Software Foundation, Inc.
00003    This file is part of the GNU C Library.
00004 
00005    The GNU C Library is free software; you can redistribute it and/or
00006    modify it under the terms of the GNU Lesser General Public
00007    License as published by the Free Software Foundation; either
00008    version 2.1 of the License, or (at your option) any later version.
00009 
00010    The GNU C Library is distributed in the hope that it will be useful,
00011    but WITHOUT ANY WARRANTY; without even the implied warranty of
00012    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00013    Lesser General Public License for more details.
00014 
00015    You should have received a copy of the GNU Lesser General Public
00016    License along with the GNU C Library; if not, write to the Free
00017    Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
00018    02111-1307 USA.  */
00019 
00020 /*
00021  *     ISO C99 Standard 7.4: Character handling  <ctype.h>
00022  */
00023 
00024 #ifndef       _CTYPE_H
00025 #define       _CTYPE_H      1
00026 
00027 #include <features.h>
00028 #include <bits/types.h>
00029 
00030 __BEGIN_DECLS
00031 
00032 #ifndef _ISbit
00033 /* These are all the characteristics of characters.
00034    If there get to be more than 16 distinct characteristics,
00035    many things must be changed that use `unsigned short int's.
00036 
00037    The characteristics are stored always in network byte order (big
00038    endian).  We define the bit value interpretations here dependent on the
00039    machine's byte order.  */
00040 
00041 # include <endian.h>
00042 # if __BYTE_ORDER == __BIG_ENDIAN
00043 #  define _ISbit(bit)       (1 << (bit))
00044 # else /* __BYTE_ORDER == __LITTLE_ENDIAN */
00045 #  define _ISbit(bit)       ((bit) < 8 ? ((1 << (bit)) << 8) : ((1 << (bit)) >> 8))
00046 # endif
00047 
00048 enum
00049 {
00050   _ISupper = _ISbit (0),    /* UPPERCASE.  */
00051   _ISlower = _ISbit (1),    /* lowercase.  */
00052   _ISalpha = _ISbit (2),    /* Alphabetic.  */
00053   _ISdigit = _ISbit (3),    /* Numeric.  */
00054   _ISxdigit = _ISbit (4),   /* Hexadecimal numeric.  */
00055   _ISspace = _ISbit (5),    /* Whitespace.  */
00056   _ISprint = _ISbit (6),    /* Printing.  */
00057   _ISgraph = _ISbit (7),    /* Graphical.  */
00058   _ISblank = _ISbit (8),    /* Blank (usually SPC and TAB).  */
00059   _IScntrl = _ISbit (9),    /* Control character.  */
00060   _ISpunct = _ISbit (10),   /* Punctuation.  */
00061   _ISalnum = _ISbit (11)    /* Alphanumeric.  */
00062 };
00063 #endif /* ! _ISbit  */
00064 
00065 /* These are defined in ctype-info.c.
00066    The declarations here must match those in localeinfo.h.
00067 
00068    In the thread-specific locale model (see `uselocale' in <locale.h>)
00069    we cannot use global variables for these as was done in the past.
00070    Instead, the following accessor functions return the address of
00071    each variable, which is local to the current thread if multithreaded.
00072 
00073    These point into arrays of 384, so they can be indexed by any `unsigned
00074    char' value [0,255]; by EOF (-1); or by any `signed char' value
00075    [-128,-1).  ISO C requires that the ctype functions work for `unsigned
00076    char' values and for EOF; we also support negative `signed char' values
00077    for broken old programs.  The case conversion arrays are of `int's
00078    rather than `unsigned char's because tolower (EOF) must be EOF, which
00079    doesn't fit into an `unsigned char'.  But today more important is that
00080    the arrays are also used for multi-byte character sets.  */
00081 extern __const unsigned short int **__ctype_b_loc (void)
00082      __THROW __attribute__ ((__const));
00083 extern __const __int32_t **__ctype_tolower_loc (void)
00084      __THROW __attribute__ ((__const));
00085 extern __const __int32_t **__ctype_toupper_loc (void)
00086      __THROW __attribute__ ((__const));
00087 
00088 #define __isctype(c, type) \
00089   ((*__ctype_b_loc ())[(int) (c)] & (unsigned short int) type)
00090 
00091 #define       __isascii(c)  (((c) & ~0x7f) == 0) /* If C is a 7 bit value.  */
00092 #define       __toascii(c)  ((c) & 0x7f)         /* Mask off high bits.  */
00093 
00094 #define       __exctype(name)      extern int name (int) __THROW
00095 
00096 __BEGIN_NAMESPACE_STD
00097 
00098 /* The following names are all functions:
00099      int isCHARACTERISTIC(int c);
00100    which return nonzero iff C has CHARACTERISTIC.
00101    For the meaning of the characteristic names, see the `enum' above.  */
00102 __exctype (isalnum);
00103 __exctype (isalpha);
00104 __exctype (iscntrl);
00105 __exctype (isdigit);
00106 __exctype (islower);
00107 __exctype (isgraph);
00108 __exctype (isprint);
00109 __exctype (ispunct);
00110 __exctype (isspace);
00111 __exctype (isupper);
00112 __exctype (isxdigit);
00113 
00114 
00115 /* Return the lowercase version of C.  */
00116 extern int tolower (int __c) __THROW;
00117 
00118 /* Return the uppercase version of C.  */
00119 extern int toupper (int __c) __THROW;
00120 
00121 __END_NAMESPACE_STD
00122 
00123 
00124 /* ISO C99 introduced one new function.  */
00125 #ifdef __USE_ISOC99
00126 __BEGIN_NAMESPACE_C99
00127 
00128 __exctype (isblank);
00129 
00130 __END_NAMESPACE_C99
00131 #endif
00132 
00133 #ifdef __USE_GNU
00134 /* Test C for a set of character classes according to MASK.  */
00135 extern int isctype (int __c, int __mask) __THROW;
00136 #endif
00137 
00138 #if defined __USE_SVID || defined __USE_MISC || defined __USE_XOPEN
00139 
00140 /* Return nonzero iff C is in the ASCII set
00141    (i.e., is no more than 7 bits wide).  */
00142 extern int isascii (int __c) __THROW;
00143 
00144 /* Return the part of C that is in the ASCII set
00145    (i.e., the low-order 7 bits of C).  */
00146 extern int toascii (int __c) __THROW;
00147 
00148 /* These are the same as `toupper' and `tolower' except that they do not
00149    check the argument for being in the range of a `char'.  */
00150 __exctype (_toupper);
00151 __exctype (_tolower);
00152 #endif /* Use SVID or use misc.  */
00153 
00154 /* This code is needed for the optimized mapping functions.  */
00155 #define __tobody(c, f, a, args) \
00156   (__extension__                                                     \
00157    ({ int __res;                                                     \
00158       if (sizeof (c) > 1)                                            \
00159        {                                                             \
00160          if (__builtin_constant_p (c))                                      \
00161            {                                                         \
00162              int __c = (c);                                          \
00163              __res = __c < -128 || __c > 255 ? __c : (a)[__c];              \
00164            }                                                         \
00165          else                                                        \
00166            __res = f args;                                           \
00167        }                                                             \
00168       else                                                           \
00169        __res = (a)[(int) (c)];                                              \
00170       __res; }))
00171 
00172 #if !defined __NO_CTYPE && !defined __cplusplus
00173 # define isalnum(c)  __isctype((c), _ISalnum)
00174 # define isalpha(c)  __isctype((c), _ISalpha)
00175 # define iscntrl(c)  __isctype((c), _IScntrl)
00176 # define isdigit(c)  __isctype((c), _ISdigit)
00177 # define islower(c)  __isctype((c), _ISlower)
00178 # define isgraph(c)  __isctype((c), _ISgraph)
00179 # define isprint(c)  __isctype((c), _ISprint)
00180 # define ispunct(c)  __isctype((c), _ISpunct)
00181 # define isspace(c)  __isctype((c), _ISspace)
00182 # define isupper(c)  __isctype((c), _ISupper)
00183 # define isxdigit(c) __isctype((c), _ISxdigit)
00184 
00185 # ifdef __USE_ISOC99
00186 #  define isblank(c) __isctype((c), _ISblank)
00187 # endif
00188 
00189 # ifdef __USE_EXTERN_INLINES
00190 __extern_inline int
00191 __NTH (tolower (int __c))
00192 {
00193   return __c >= -128 && __c < 256 ? (*__ctype_tolower_loc ())[__c] : __c;
00194 }
00195 
00196 __extern_inline int
00197 __NTH (toupper (int __c))
00198 {
00199   return __c >= -128 && __c < 256 ? (*__ctype_toupper_loc ())[__c] : __c;
00200 }
00201 # endif
00202 
00203 # if __GNUC__ >= 2 && defined __OPTIMIZE__ && !defined __cplusplus
00204 #  define tolower(c) __tobody (c, tolower, *__ctype_tolower_loc (), (c))
00205 #  define toupper(c) __tobody (c, toupper, *__ctype_toupper_loc (), (c))
00206 # endif /* Optimizing gcc */
00207 
00208 # if defined __USE_SVID || defined __USE_MISC || defined __USE_XOPEN
00209 #  define isascii(c) __isascii (c)
00210 #  define toascii(c) __toascii (c)
00211 
00212 #  define _tolower(c)       ((int) (*__ctype_tolower_loc ())[(int) (c)])
00213 #  define _toupper(c)       ((int) (*__ctype_toupper_loc ())[(int) (c)])
00214 # endif
00215 
00216 #endif /* Not __NO_CTYPE.  */
00217 
00218 
00219 #ifdef __USE_GNU
00220 /* The concept of one static locale per category is not very well
00221    thought out.  Many applications will need to process its data using
00222    information from several different locales.  Another application is
00223    the implementation of the internationalization handling in the
00224    upcoming ISO C++ standard library.  To support this another set of
00225    the functions using locale data exist which have an additional
00226    argument.
00227 
00228    Attention: all these functions are *not* standardized in any form.
00229    This is a proof-of-concept implementation.  */
00230 
00231 /* Structure for reentrant locale using functions.  This is an
00232    (almost) opaque type for the user level programs.  */
00233 # include <xlocale.h>
00234 
00235 /* These definitions are similar to the ones above but all functions
00236    take as an argument a handle for the locale which shall be used.  */
00237 #  define __isctype_l(c, type, locale) \
00238   ((locale)->__ctype_b[(int) (c)] & (unsigned short int) type)
00239 
00240 # define __exctype_l(name)                                           \
00241   extern int name (int, __locale_t) __THROW
00242 
00243 /* The following names are all functions:
00244      int isCHARACTERISTIC(int c, locale_t *locale);
00245    which return nonzero iff C has CHARACTERISTIC.
00246    For the meaning of the characteristic names, see the `enum' above.  */
00247 __exctype_l (isalnum_l);
00248 __exctype_l (isalpha_l);
00249 __exctype_l (iscntrl_l);
00250 __exctype_l (isdigit_l);
00251 __exctype_l (islower_l);
00252 __exctype_l (isgraph_l);
00253 __exctype_l (isprint_l);
00254 __exctype_l (ispunct_l);
00255 __exctype_l (isspace_l);
00256 __exctype_l (isupper_l);
00257 __exctype_l (isxdigit_l);
00258 
00259 __exctype_l (isblank_l);
00260 
00261 
00262 /* Return the lowercase version of C in locale L.  */
00263 extern int __tolower_l (int __c, __locale_t __l) __THROW;
00264 extern int tolower_l (int __c, __locale_t __l) __THROW;
00265 
00266 /* Return the uppercase version of C.  */
00267 extern int __toupper_l (int __c, __locale_t __l) __THROW;
00268 extern int toupper_l (int __c, __locale_t __l) __THROW;
00269 
00270 # if __GNUC__ >= 2 && defined __OPTIMIZE__ && !defined __cplusplus
00271 #  define __tolower_l(c, locale) \
00272   __tobody (c, __tolower_l, (locale)->__ctype_tolower, (c, locale))
00273 #  define __toupper_l(c, locale) \
00274   __tobody (c, __toupper_l, (locale)->__ctype_toupper, (c, locale))
00275 #  define tolower_l(c, locale)     __tolower_l ((c), (locale))
00276 #  define toupper_l(c, locale)     __toupper_l ((c), (locale))
00277 # endif       /* Optimizing gcc */
00278 
00279 
00280 # ifndef __NO_CTYPE
00281 #  define __isalnum_l(c,l)  __isctype_l((c), _ISalnum, (l))
00282 #  define __isalpha_l(c,l)  __isctype_l((c), _ISalpha, (l))
00283 #  define __iscntrl_l(c,l)  __isctype_l((c), _IScntrl, (l))
00284 #  define __isdigit_l(c,l)  __isctype_l((c), _ISdigit, (l))
00285 #  define __islower_l(c,l)  __isctype_l((c), _ISlower, (l))
00286 #  define __isgraph_l(c,l)  __isctype_l((c), _ISgraph, (l))
00287 #  define __isprint_l(c,l)  __isctype_l((c), _ISprint, (l))
00288 #  define __ispunct_l(c,l)  __isctype_l((c), _ISpunct, (l))
00289 #  define __isspace_l(c,l)  __isctype_l((c), _ISspace, (l))
00290 #  define __isupper_l(c,l)  __isctype_l((c), _ISupper, (l))
00291 #  define __isxdigit_l(c,l) __isctype_l((c), _ISxdigit, (l))
00292 
00293 #  define __isblank_l(c,l)  __isctype_l((c), _ISblank, (l))
00294 
00295 #  if defined __USE_SVID || defined __USE_MISC || defined __USE_XOPEN
00296 #   define __isascii_l(c,l) ((l), __isascii (c))
00297 #   define __toascii_l(c,l) ((l), __toascii (c))
00298 #  endif
00299 
00300 #  define isalnum_l(c,l)    __isalnum_l ((c), (l))
00301 #  define isalpha_l(c,l)    __isalpha_l ((c), (l))
00302 #  define iscntrl_l(c,l)    __iscntrl_l ((c), (l))
00303 #  define isdigit_l(c,l)    __isdigit_l ((c), (l))
00304 #  define islower_l(c,l)    __islower_l ((c), (l))
00305 #  define isgraph_l(c,l)    __isgraph_l ((c), (l))
00306 #  define isprint_l(c,l)    __isprint_l ((c), (l))
00307 #  define ispunct_l(c,l)    __ispunct_l ((c), (l))
00308 #  define isspace_l(c,l)    __isspace_l ((c), (l))
00309 #  define isupper_l(c,l)    __isupper_l ((c), (l))
00310 #  define isxdigit_l(c,l)   __isxdigit_l ((c), (l))
00311 
00312 #  define isblank_l(c,l)    __isblank_l ((c), (l))
00313 
00314 #  if defined __USE_SVID || defined __USE_MISC || defined __USE_XOPEN
00315 #   define isascii_l(c,l)   __isascii_l ((c), (l))
00316 #   define toascii_l(c,l)   __toascii_l ((c), (l))
00317 #  endif
00318 
00319 # endif /* Not __NO_CTYPE.  */
00320 
00321 #endif /* Use GNU.  */
00322 
00323 __END_DECLS
00324 
00325 #endif /* ctype.h  */