safe-ctype.h 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. /* <ctype.h> replacement macros.
  2. Copyright (C) 2000, 2001 Free Software Foundation, Inc.
  3. Contributed by Zack Weinberg <zackw@stanford.edu>.
  4. This file is part of the libiberty library.
  5. Libiberty is free software; you can redistribute it and/or
  6. modify it under the terms of the GNU Library General Public
  7. License as published by the Free Software Foundation; either
  8. version 2 of the License, or (at your option) any later version.
  9. Libiberty is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  12. Library General Public License for more details.
  13. You should have received a copy of the GNU Library General Public
  14. License along with libiberty; see the file COPYING.LIB. If
  15. not, write to the Free Software Foundation, Inc., 51 Franklin Street - Fifth Floor,
  16. Boston, MA 02110-1301, USA. */
  17. /* This is a compatible replacement of the standard C library's <ctype.h>
  18. with the following properties:
  19. - Implements all isxxx() macros required by C99.
  20. - Also implements some character classes useful when
  21. parsing C-like languages.
  22. - Does not change behavior depending on the current locale.
  23. - Behaves properly for all values in the range of a signed or
  24. unsigned char.
  25. To avoid conflicts, this header defines the isxxx functions in upper
  26. case, e.g. ISALPHA not isalpha. */
  27. #ifndef SAFE_CTYPE_H
  28. #define SAFE_CTYPE_H
  29. /* Determine host character set. */
  30. #define HOST_CHARSET_UNKNOWN 0
  31. #define HOST_CHARSET_ASCII 1
  32. #define HOST_CHARSET_EBCDIC 2
  33. #if '\n' == 0x0A && ' ' == 0x20 && '0' == 0x30 \
  34. && 'A' == 0x41 && 'a' == 0x61 && '!' == 0x21
  35. # define HOST_CHARSET HOST_CHARSET_ASCII
  36. #else
  37. # if '\n' == 0x15 && ' ' == 0x40 && '0' == 0xF0 \
  38. && 'A' == 0xC1 && 'a' == 0x81 && '!' == 0x5A
  39. # define HOST_CHARSET HOST_CHARSET_EBCDIC
  40. # else
  41. # define HOST_CHARSET HOST_CHARSET_UNKNOWN
  42. # endif
  43. #endif
  44. /* Categories. */
  45. enum {
  46. /* In C99 */
  47. _sch_isblank = 0x0001, /* space \t */
  48. _sch_iscntrl = 0x0002, /* nonprinting characters */
  49. _sch_isdigit = 0x0004, /* 0-9 */
  50. _sch_islower = 0x0008, /* a-z */
  51. _sch_isprint = 0x0010, /* any printing character including ' ' */
  52. _sch_ispunct = 0x0020, /* all punctuation */
  53. _sch_isspace = 0x0040, /* space \t \n \r \f \v */
  54. _sch_isupper = 0x0080, /* A-Z */
  55. _sch_isxdigit = 0x0100, /* 0-9A-Fa-f */
  56. /* Extra categories useful to cpplib. */
  57. _sch_isidst = 0x0200, /* A-Za-z_ */
  58. _sch_isvsp = 0x0400, /* \n \r */
  59. _sch_isnvsp = 0x0800, /* space \t \f \v \0 */
  60. /* Combinations of the above. */
  61. _sch_isalpha = _sch_isupper|_sch_islower, /* A-Za-z */
  62. _sch_isalnum = _sch_isalpha|_sch_isdigit, /* A-Za-z0-9 */
  63. _sch_isidnum = _sch_isidst|_sch_isdigit, /* A-Za-z0-9_ */
  64. _sch_isgraph = _sch_isalnum|_sch_ispunct, /* isprint and not space */
  65. _sch_iscppsp = _sch_isvsp|_sch_isnvsp, /* isspace + \0 */
  66. _sch_isbasic = _sch_isprint|_sch_iscppsp /* basic charset of ISO C
  67. (plus ` and @) */
  68. };
  69. /* Character classification. */
  70. extern const unsigned short _sch_istable[256];
  71. #define _sch_test(c, bit) (_sch_istable[(c) & 0xff] & (unsigned short)(bit))
  72. #define ISALPHA(c) _sch_test(c, _sch_isalpha)
  73. #define ISALNUM(c) _sch_test(c, _sch_isalnum)
  74. #define ISBLANK(c) _sch_test(c, _sch_isblank)
  75. #define ISCNTRL(c) _sch_test(c, _sch_iscntrl)
  76. #define ISDIGIT(c) _sch_test(c, _sch_isdigit)
  77. #define ISGRAPH(c) _sch_test(c, _sch_isgraph)
  78. #define ISLOWER(c) _sch_test(c, _sch_islower)
  79. #define ISPRINT(c) _sch_test(c, _sch_isprint)
  80. #define ISPUNCT(c) _sch_test(c, _sch_ispunct)
  81. #define ISSPACE(c) _sch_test(c, _sch_isspace)
  82. #define ISUPPER(c) _sch_test(c, _sch_isupper)
  83. #define ISXDIGIT(c) _sch_test(c, _sch_isxdigit)
  84. #define ISIDNUM(c) _sch_test(c, _sch_isidnum)
  85. #define ISIDST(c) _sch_test(c, _sch_isidst)
  86. #define IS_ISOBASIC(c) _sch_test(c, _sch_isbasic)
  87. #define IS_VSPACE(c) _sch_test(c, _sch_isvsp)
  88. #define IS_NVSPACE(c) _sch_test(c, _sch_isnvsp)
  89. #define IS_SPACE_OR_NUL(c) _sch_test(c, _sch_iscppsp)
  90. /* Character transformation. */
  91. extern const unsigned char _sch_toupper[256];
  92. extern const unsigned char _sch_tolower[256];
  93. #define TOUPPER(c) _sch_toupper[(c) & 0xff]
  94. #define TOLOWER(c) _sch_tolower[(c) & 0xff]
  95. /* Prevent the users of safe-ctype.h from accidently using the routines
  96. from ctype.h. Initially, the approach was to produce an error when
  97. detecting that ctype.h has been included. But this was causing
  98. trouble as ctype.h might get indirectly included as a result of
  99. including another system header (for instance gnulib's stdint.h).
  100. So we include ctype.h here and then immediately redefine its macros. */
  101. #include <ctype.h>
  102. #undef isalpha
  103. #define isalpha(c) do_not_use_isalpha_with_safe_ctype
  104. #undef isalnum
  105. #define isalnum(c) do_not_use_isalnum_with_safe_ctype
  106. #undef iscntrl
  107. #define iscntrl(c) do_not_use_iscntrl_with_safe_ctype
  108. #undef isdigit
  109. #define isdigit(c) do_not_use_isdigit_with_safe_ctype
  110. #undef isgraph
  111. #define isgraph(c) do_not_use_isgraph_with_safe_ctype
  112. #undef islower
  113. #define islower(c) do_not_use_islower_with_safe_ctype
  114. #undef isprint
  115. #define isprint(c) do_not_use_isprint_with_safe_ctype
  116. #undef ispunct
  117. #define ispunct(c) do_not_use_ispunct_with_safe_ctype
  118. #undef isspace
  119. #define isspace(c) do_not_use_isspace_with_safe_ctype
  120. #undef isupper
  121. #define isupper(c) do_not_use_isupper_with_safe_ctype
  122. #undef isxdigit
  123. #define isxdigit(c) do_not_use_isxdigit_with_safe_ctype
  124. #undef toupper
  125. #define toupper(c) do_not_use_toupper_with_safe_ctype
  126. #undef tolower
  127. #define tolower(c) do_not_use_tolower_with_safe_ctype
  128. #endif /* SAFE_CTYPE_H */