root/lib/c-ctype.h

/* [<][>][^][v][top][bottom][index][help] */

INCLUDED FROM


DEFINITIONS

This source file includes following definitions.
  1. c_isalnum
  2. c_isalpha
  3. c_isascii
  4. c_isblank
  5. c_iscntrl
  6. c_isdigit
  7. c_isgraph
  8. c_islower
  9. c_isprint
  10. c_ispunct
  11. c_isspace
  12. c_isupper
  13. c_isxdigit
  14. c_tolower
  15. c_toupper

     1 /* Character handling in C locale.
     2 
     3    These functions work like the corresponding functions in <ctype.h>,
     4    except that they have the C (POSIX) locale hardwired, whereas the
     5    <ctype.h> functions' behaviour depends on the current locale set via
     6    setlocale.
     7 
     8    Copyright (C) 2000-2003, 2006, 2008-2023 Free Software Foundation,
     9    Inc.
    10 
    11    This file is free software: you can redistribute it and/or modify
    12    it under the terms of the GNU Lesser General Public License as
    13    published by the Free Software Foundation; either version 2.1 of the
    14    License, or (at your option) any later version.
    15 
    16    This file is distributed in the hope that it will be useful,
    17    but WITHOUT ANY WARRANTY; without even the implied warranty of
    18    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    19    GNU Lesser General Public License for more details.
    20 
    21    You should have received a copy of the GNU Lesser General Public License
    22    along with this program.  If not, see <https://www.gnu.org/licenses/>.  */
    23 
    24 #ifndef C_CTYPE_H
    25 #define C_CTYPE_H
    26 
    27 #ifndef _GL_INLINE_HEADER_BEGIN
    28  #error "Please include config.h first."
    29 #endif
    30 _GL_INLINE_HEADER_BEGIN
    31 #ifndef C_CTYPE_INLINE
    32 # define C_CTYPE_INLINE _GL_INLINE
    33 #endif
    34 
    35 #ifdef __cplusplus
    36 extern "C" {
    37 #endif
    38 
    39 
    40 /* The functions defined in this file assume the "C" locale and a character
    41    set without diacritics (ASCII-US or EBCDIC-US or something like that).
    42    Even if the "C" locale on a particular system is an extension of the ASCII
    43    character set (like on BeOS, where it is UTF-8, or on AmigaOS, where it
    44    is ISO-8859-1), the functions in this file recognize only the ASCII
    45    characters.  */
    46 
    47 
    48 #if (' ' == 32) && ('!' == 33) && ('"' == 34) && ('#' == 35) \
    49     && ('%' == 37) && ('&' == 38) && ('\'' == 39) && ('(' == 40) \
    50     && (')' == 41) && ('*' == 42) && ('+' == 43) && (',' == 44) \
    51     && ('-' == 45) && ('.' == 46) && ('/' == 47) && ('0' == 48) \
    52     && ('1' == 49) && ('2' == 50) && ('3' == 51) && ('4' == 52) \
    53     && ('5' == 53) && ('6' == 54) && ('7' == 55) && ('8' == 56) \
    54     && ('9' == 57) && (':' == 58) && (';' == 59) && ('<' == 60) \
    55     && ('=' == 61) && ('>' == 62) && ('?' == 63) && ('A' == 65) \
    56     && ('B' == 66) && ('C' == 67) && ('D' == 68) && ('E' == 69) \
    57     && ('F' == 70) && ('G' == 71) && ('H' == 72) && ('I' == 73) \
    58     && ('J' == 74) && ('K' == 75) && ('L' == 76) && ('M' == 77) \
    59     && ('N' == 78) && ('O' == 79) && ('P' == 80) && ('Q' == 81) \
    60     && ('R' == 82) && ('S' == 83) && ('T' == 84) && ('U' == 85) \
    61     && ('V' == 86) && ('W' == 87) && ('X' == 88) && ('Y' == 89) \
    62     && ('Z' == 90) && ('[' == 91) && ('\\' == 92) && (']' == 93) \
    63     && ('^' == 94) && ('_' == 95) && ('a' == 97) && ('b' == 98) \
    64     && ('c' == 99) && ('d' == 100) && ('e' == 101) && ('f' == 102) \
    65     && ('g' == 103) && ('h' == 104) && ('i' == 105) && ('j' == 106) \
    66     && ('k' == 107) && ('l' == 108) && ('m' == 109) && ('n' == 110) \
    67     && ('o' == 111) && ('p' == 112) && ('q' == 113) && ('r' == 114) \
    68     && ('s' == 115) && ('t' == 116) && ('u' == 117) && ('v' == 118) \
    69     && ('w' == 119) && ('x' == 120) && ('y' == 121) && ('z' == 122) \
    70     && ('{' == 123) && ('|' == 124) && ('}' == 125) && ('~' == 126)
    71 /* The character set is ASCII or one of its variants or extensions, not EBCDIC.
    72    Testing the value of '\n' and '\r' is not relevant.  */
    73 # define C_CTYPE_ASCII 1
    74 #elif ! (' ' == '\x40' && '0' == '\xf0'                     \
    75          && 'A' == '\xc1' && 'J' == '\xd1' && 'S' == '\xe2' \
    76          && 'a' == '\x81' && 'j' == '\x91' && 's' == '\xa2')
    77 # error "Only ASCII and EBCDIC are supported"
    78 #endif
    79 
    80 #if 'A' < 0
    81 # error "EBCDIC and char is signed -- not supported"
    82 #endif
    83 
    84 /* Cases for control characters.  */
    85 
    86 #define _C_CTYPE_CNTRL \
    87    case '\a': case '\b': case '\f': case '\n': \
    88    case '\r': case '\t': case '\v': \
    89    _C_CTYPE_OTHER_CNTRL
    90 
    91 /* ASCII control characters other than those with \-letter escapes.  */
    92 
    93 #if C_CTYPE_ASCII
    94 # define _C_CTYPE_OTHER_CNTRL \
    95     case '\x00': case '\x01': case '\x02': case '\x03': \
    96     case '\x04': case '\x05': case '\x06': case '\x0e': \
    97     case '\x0f': case '\x10': case '\x11': case '\x12': \
    98     case '\x13': case '\x14': case '\x15': case '\x16': \
    99     case '\x17': case '\x18': case '\x19': case '\x1a': \
   100     case '\x1b': case '\x1c': case '\x1d': case '\x1e': \
   101     case '\x1f': case '\x7f'
   102 #else
   103    /* Use EBCDIC code page 1047's assignments for ASCII control chars;
   104       assume all EBCDIC code pages agree about these assignments.  */
   105 # define _C_CTYPE_OTHER_CNTRL \
   106     case '\x00': case '\x01': case '\x02': case '\x03': \
   107     case '\x07': case '\x0e': case '\x0f': case '\x10': \
   108     case '\x11': case '\x12': case '\x13': case '\x18': \
   109     case '\x19': case '\x1c': case '\x1d': case '\x1e': \
   110     case '\x1f': case '\x26': case '\x27': case '\x2d': \
   111     case '\x2e': case '\x32': case '\x37': case '\x3c': \
   112     case '\x3d': case '\x3f'
   113 #endif
   114 
   115 /* Cases for lowercase hex letters, and lowercase letters, all offset by N.  */
   116 
   117 #define _C_CTYPE_LOWER_A_THRU_F_N(N) \
   118    case 'a' + (N): case 'b' + (N): case 'c' + (N): case 'd' + (N): \
   119    case 'e' + (N): case 'f' + (N)
   120 #define _C_CTYPE_LOWER_N(N) \
   121    _C_CTYPE_LOWER_A_THRU_F_N(N): \
   122    case 'g' + (N): case 'h' + (N): case 'i' + (N): case 'j' + (N): \
   123    case 'k' + (N): case 'l' + (N): case 'm' + (N): case 'n' + (N): \
   124    case 'o' + (N): case 'p' + (N): case 'q' + (N): case 'r' + (N): \
   125    case 's' + (N): case 't' + (N): case 'u' + (N): case 'v' + (N): \
   126    case 'w' + (N): case 'x' + (N): case 'y' + (N): case 'z' + (N)
   127 
   128 /* Cases for hex letters, digits, lower, punct, and upper.  */
   129 
   130 #define _C_CTYPE_A_THRU_F \
   131    _C_CTYPE_LOWER_A_THRU_F_N (0): \
   132    _C_CTYPE_LOWER_A_THRU_F_N ('A' - 'a')
   133 #define _C_CTYPE_DIGIT                     \
   134    case '0': case '1': case '2': case '3': \
   135    case '4': case '5': case '6': case '7': \
   136    case '8': case '9'
   137 #define _C_CTYPE_LOWER _C_CTYPE_LOWER_N (0)
   138 #define _C_CTYPE_PUNCT \
   139    case '!': case '"': case '#': case '$':  \
   140    case '%': case '&': case '\'': case '(': \
   141    case ')': case '*': case '+': case ',':  \
   142    case '-': case '.': case '/': case ':':  \
   143    case ';': case '<': case '=': case '>':  \
   144    case '?': case '@': case '[': case '\\': \
   145    case ']': case '^': case '_': case '`':  \
   146    case '{': case '|': case '}': case '~'
   147 #define _C_CTYPE_UPPER _C_CTYPE_LOWER_N ('A' - 'a')
   148 
   149 
   150 /* Function definitions.  */
   151 
   152 /* Unlike the functions in <ctype.h>, which require an argument in the range
   153    of the 'unsigned char' type, the functions here operate on values that are
   154    in the 'unsigned char' range or in the 'char' range.  In other words,
   155    when you have a 'char' value, you need to cast it before using it as
   156    argument to a <ctype.h> function:
   157 
   158          const char *s = ...;
   159          if (isalpha ((unsigned char) *s)) ...
   160 
   161    but you don't need to cast it for the functions defined in this file:
   162 
   163          const char *s = ...;
   164          if (c_isalpha (*s)) ...
   165  */
   166 
   167 C_CTYPE_INLINE bool
   168 c_isalnum (int c)
   169 {
   170   switch (c)
   171     {
   172     _C_CTYPE_DIGIT:
   173     _C_CTYPE_LOWER:
   174     _C_CTYPE_UPPER:
   175       return true;
   176     default:
   177       return false;
   178     }
   179 }
   180 
   181 C_CTYPE_INLINE bool
   182 c_isalpha (int c)
   183 {
   184   switch (c)
   185     {
   186     _C_CTYPE_LOWER:
   187     _C_CTYPE_UPPER:
   188       return true;
   189     default:
   190       return false;
   191     }
   192 }
   193 
   194 /* The function isascii is not locale dependent.
   195    Its use in EBCDIC is questionable. */
   196 C_CTYPE_INLINE bool
   197 c_isascii (int c)
   198 {
   199   switch (c)
   200     {
   201     case ' ':
   202     _C_CTYPE_CNTRL:
   203     _C_CTYPE_DIGIT:
   204     _C_CTYPE_LOWER:
   205     _C_CTYPE_PUNCT:
   206     _C_CTYPE_UPPER:
   207       return true;
   208     default:
   209       return false;
   210     }
   211 }
   212 
   213 C_CTYPE_INLINE bool
   214 c_isblank (int c)
   215 {
   216   return c == ' ' || c == '\t';
   217 }
   218 
   219 C_CTYPE_INLINE bool
   220 c_iscntrl (int c)
   221 {
   222   switch (c)
   223     {
   224     _C_CTYPE_CNTRL:
   225       return true;
   226     default:
   227       return false;
   228     }
   229 }
   230 
   231 C_CTYPE_INLINE bool
   232 c_isdigit (int c)
   233 {
   234   switch (c)
   235     {
   236     _C_CTYPE_DIGIT:
   237       return true;
   238     default:
   239       return false;
   240     }
   241 }
   242 
   243 C_CTYPE_INLINE bool
   244 c_isgraph (int c)
   245 {
   246   switch (c)
   247     {
   248     _C_CTYPE_DIGIT:
   249     _C_CTYPE_LOWER:
   250     _C_CTYPE_PUNCT:
   251     _C_CTYPE_UPPER:
   252       return true;
   253     default:
   254       return false;
   255     }
   256 }
   257 
   258 C_CTYPE_INLINE bool
   259 c_islower (int c)
   260 {
   261   switch (c)
   262     {
   263     _C_CTYPE_LOWER:
   264       return true;
   265     default:
   266       return false;
   267     }
   268 }
   269 
   270 C_CTYPE_INLINE bool
   271 c_isprint (int c)
   272 {
   273   switch (c)
   274     {
   275     case ' ':
   276     _C_CTYPE_DIGIT:
   277     _C_CTYPE_LOWER:
   278     _C_CTYPE_PUNCT:
   279     _C_CTYPE_UPPER:
   280       return true;
   281     default:
   282       return false;
   283     }
   284 }
   285 
   286 C_CTYPE_INLINE bool
   287 c_ispunct (int c)
   288 {
   289   switch (c)
   290     {
   291     _C_CTYPE_PUNCT:
   292       return true;
   293     default:
   294       return false;
   295     }
   296 }
   297 
   298 C_CTYPE_INLINE bool
   299 c_isspace (int c)
   300 {
   301   switch (c)
   302     {
   303     case ' ': case '\t': case '\n': case '\v': case '\f': case '\r':
   304       return true;
   305     default:
   306       return false;
   307     }
   308 }
   309 
   310 C_CTYPE_INLINE bool
   311 c_isupper (int c)
   312 {
   313   switch (c)
   314     {
   315     _C_CTYPE_UPPER:
   316       return true;
   317     default:
   318       return false;
   319     }
   320 }
   321 
   322 C_CTYPE_INLINE bool
   323 c_isxdigit (int c)
   324 {
   325   switch (c)
   326     {
   327     _C_CTYPE_DIGIT:
   328     _C_CTYPE_A_THRU_F:
   329       return true;
   330     default:
   331       return false;
   332     }
   333 }
   334 
   335 C_CTYPE_INLINE int
   336 c_tolower (int c)
   337 {
   338   switch (c)
   339     {
   340     _C_CTYPE_UPPER:
   341       return c - 'A' + 'a';
   342     default:
   343       return c;
   344     }
   345 }
   346 
   347 C_CTYPE_INLINE int
   348 c_toupper (int c)
   349 {
   350   switch (c)
   351     {
   352     _C_CTYPE_LOWER:
   353       return c - 'a' + 'A';
   354     default:
   355       return c;
   356     }
   357 }
   358 
   359 #ifdef __cplusplus
   360 }
   361 #endif
   362 
   363 _GL_INLINE_HEADER_END
   364 
   365 #endif /* C_CTYPE_H */

/* [<][>][^][v][top][bottom][index][help] */