ctype.h 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. /*
  2. * Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #pragma once
  7. #include <sys/cdefs.h>
  8. __BEGIN_DECLS
  9. #ifndef EOF
  10. # define EOF (-1)
  11. #endif
  12. /* Do what newlib does to appease GCC's --with-newlib option. */
  13. #define _U 01
  14. #define _L 02
  15. #define _N 04
  16. #define _S 010
  17. #define _P 020
  18. #define _C 040
  19. #define _X 0100
  20. #define _B 0200
  21. /**
  22. * newlib has a 257 byte _ctype_ array to enable compiler tricks to catch
  23. * people passing char instead of int. We don't engage in those tricks,
  24. * but still claim to be newlib to the toolchains
  25. */
  26. extern char const _ctype_[1 + 256] __attribute__((visibility("default")));
  27. static inline int __inline_isalnum(int c)
  28. {
  29. return _ctype_[(unsigned char)(c) + 1] & (_U | _L | _N);
  30. }
  31. static inline int __inline_isalpha(int c)
  32. {
  33. return _ctype_[(unsigned char)(c) + 1] & (_U | _L);
  34. }
  35. static inline int __inline_isascii(int c)
  36. {
  37. return (unsigned)c <= 127;
  38. }
  39. static inline int __inline_iscntrl(int c)
  40. {
  41. return _ctype_[(unsigned char)(c) + 1] & (_C);
  42. }
  43. static inline int __inline_isdigit(int c)
  44. {
  45. return _ctype_[(unsigned char)(c) + 1] & (_N);
  46. }
  47. static inline int __inline_isxdigit(int c)
  48. {
  49. return _ctype_[(unsigned char)(c) + 1] & (_N | _X);
  50. }
  51. static inline int __inline_isspace(int c)
  52. {
  53. return _ctype_[(unsigned char)(c) + 1] & (_S);
  54. }
  55. static inline int __inline_ispunct(int c)
  56. {
  57. return _ctype_[(unsigned char)(c) + 1] & (_P);
  58. }
  59. static inline int __inline_isprint(int c)
  60. {
  61. return _ctype_[(unsigned char)(c) + 1] & (_P | _U | _L | _N | _B);
  62. }
  63. static inline int __inline_isgraph(int c)
  64. {
  65. return _ctype_[(unsigned char)(c) + 1] & (_P | _U | _L | _N);
  66. }
  67. static inline int __inline_islower(int c)
  68. {
  69. return _ctype_[(unsigned char)(c) + 1] & (_L);
  70. }
  71. static inline int __inline_isupper(int c)
  72. {
  73. return _ctype_[(unsigned char)(c) + 1] & (_U);
  74. }
  75. static inline int __inline_isblank(int c)
  76. {
  77. return _ctype_[(unsigned char)(c) + 1] & (_B) || (c == '\t');
  78. }
  79. static inline int __inline_toascii(int c)
  80. {
  81. return c & 127;
  82. }
  83. static inline int __inline_tolower(int c)
  84. {
  85. if (c >= 'A' && c <= 'Z')
  86. return c | 0x20;
  87. return c;
  88. }
  89. static inline int __inline_toupper(int c)
  90. {
  91. if (c >= 'a' && c <= 'z')
  92. return c & ~0x20;
  93. return c;
  94. }
  95. #ifdef __cplusplus
  96. extern "C" {
  97. #endif
  98. int isalnum(int c);
  99. int isalpha(int c);
  100. int isascii(int c);
  101. int iscntrl(int c);
  102. int isdigit(int c);
  103. int isxdigit(int c);
  104. int isspace(int c);
  105. int ispunct(int c);
  106. int isprint(int c);
  107. int isgraph(int c);
  108. int islower(int c);
  109. int isupper(int c);
  110. int isblank(int c);
  111. int toascii(int c);
  112. int tolower(int c);
  113. int toupper(int c);
  114. #ifdef __cplusplus
  115. }
  116. #endif
  117. #define isalnum __inline_isalnum
  118. #define isalpha __inline_isalpha
  119. #define isascii __inline_isascii
  120. #define iscntrl __inline_iscntrl
  121. #define isdigit __inline_isdigit
  122. #define isxdigit __inline_isxdigit
  123. #define isspace __inline_isspace
  124. #define ispunct __inline_ispunct
  125. #define isprint __inline_isprint
  126. #define isgraph __inline_isgraph
  127. #define islower __inline_islower
  128. #define isupper __inline_isupper
  129. #define isblank __inline_isblank
  130. #define toascii __inline_toascii
  131. #define tolower __inline_tolower
  132. #define toupper __inline_toupper
  133. __END_DECLS