ctype.c 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301
  1. /* ctype.c
  2. * Character classification and conversion
  3. * Copyright (C) 2000 Lineo, Inc.
  4. * Written by Erik Andersen
  5. * This file is part of the uClibc C library and is distributed
  6. * under the GNU Library General Public License.
  7. *
  8. * not C-locale only code
  9. * written by Vladimir Oleynik (c) vodz@usa.net
  10. * and Manuel Novoa III <mnovoa3@bellsouth.net>
  11. * used ideas is part of the GNU C Library.
  12. */
  13. #include <ctype.h>
  14. #ifdef L_isascii
  15. int
  16. isascii( int c )
  17. {
  18. return (c > 0 && c <= 0x7f);
  19. }
  20. #endif
  21. #ifdef L_isdigit
  22. int
  23. isdigit( int c )
  24. {
  25. return (c >= '0' && c <= '9');
  26. }
  27. #endif
  28. #ifdef L_toascii
  29. int
  30. toascii( int c )
  31. {
  32. return (c & 0x7f);
  33. }
  34. #endif
  35. /* locale depended */
  36. #ifndef __UCLIBC_HAS_LOCALE__
  37. #ifdef L_isalpha
  38. int
  39. isalpha( int c )
  40. {
  41. return (isupper(c) || islower(c));
  42. }
  43. #endif
  44. #ifdef L_isalnum
  45. int
  46. isalnum( int c )
  47. {
  48. return (isalpha(c) || isdigit(c));
  49. }
  50. #endif
  51. #ifdef L_iscntrl
  52. int
  53. iscntrl( int c )
  54. {
  55. return ((c >= 0) && ((c <= 0x1f) || (c == 0x7f)));
  56. }
  57. #endif
  58. #ifdef L_isgraph
  59. int
  60. isgraph( int c )
  61. {
  62. return (c > ' ' && isprint(c));
  63. }
  64. #endif
  65. #ifdef L_islower
  66. int
  67. islower( int c )
  68. {
  69. return (c >= 'a' && c <= 'z');
  70. }
  71. #endif
  72. #ifdef L_isprint
  73. int
  74. isprint( int c )
  75. {
  76. return (c >= ' ' && c <= '~');
  77. }
  78. #endif
  79. #ifdef L_ispunct
  80. int
  81. ispunct( int c )
  82. {
  83. return ((c > ' ' && c <= '~') && !isalnum(c));
  84. }
  85. #endif
  86. #ifdef L_isspace
  87. int
  88. isspace( int c )
  89. {
  90. return (c == ' ' || c == '\f' || c == '\n' || c == '\r' ||
  91. c == '\t' || c == '\v');
  92. }
  93. #endif
  94. #ifdef L_isupper
  95. int
  96. isupper( int c )
  97. {
  98. return (c >= 'A' && c <= 'Z');
  99. }
  100. #endif
  101. #ifdef L_isxdigit
  102. int
  103. isxdigit( int c )
  104. {
  105. return (isdigit(c) || (c >= 'a' && c <= 'f') || (c >= 'A' && c <= 'F'));
  106. }
  107. #endif
  108. #ifdef L_isxlower
  109. int
  110. isxlower( int c )
  111. {
  112. return (isdigit(c) || (c >= 'a' && c <= 'f'));
  113. }
  114. #endif
  115. #ifdef L_isxupper
  116. int
  117. isxupper( int c )
  118. {
  119. return (isdigit(c) || (c >= 'A' && c <= 'F'));
  120. }
  121. #endif
  122. #ifdef L_tolower
  123. int
  124. tolower( int c )
  125. {
  126. return (isupper(c) ? (c - 'A' + 'a') : (c));
  127. }
  128. #endif
  129. #ifdef L_toupper
  130. int
  131. toupper( int c )
  132. {
  133. return (islower(c) ? (c - 'a' + 'A') : (c));
  134. }
  135. #endif
  136. #else /* __UCLIBC_HAS_LOCALE__ */
  137. #include <limits.h>
  138. #include "./ctype.h"
  139. #define _UC_ISCTYPE(c, type) \
  140. ((c != -1) && ((_uc_ctype_b[(int)((unsigned char)c)] & type) != 0))
  141. #define _UC_ISCTYPE2(c, type, type2) \
  142. ((c != -1) && ((_uc_ctype_b[(int)((unsigned char)c)] & type) == type2))
  143. #ifdef L_ctype_C
  144. /* startup setlocale(LC_TYPE, "C"); */
  145. #include "ctype_C.c"
  146. const unsigned char *_uc_ctype_b = _uc_ctype_b_C;
  147. const unsigned char *_uc_ctype_trans = _uc_ctype_b_C+LOCALE_BUF_SIZE/2;
  148. #endif /* L_ctype_C */
  149. #ifdef L_isalpha
  150. int
  151. isalpha( int c )
  152. {
  153. return _UC_ISCTYPE(c, ISalpha);
  154. }
  155. #endif
  156. #ifdef L_isalnum
  157. int
  158. isalnum( int c )
  159. {
  160. return _UC_ISCTYPE(c, (ISalpha|ISxdigit));
  161. }
  162. #endif
  163. #ifdef L_iscntrl
  164. int
  165. iscntrl( int c )
  166. {
  167. return _UC_ISCTYPE(c, IScntrl);
  168. }
  169. #endif
  170. #ifdef L_isgraph
  171. int
  172. isgraph( int c )
  173. {
  174. return _UC_ISCTYPE2(c, (ISprint|ISspace), ISprint);
  175. }
  176. #endif
  177. #ifdef L_islower
  178. int
  179. islower( int c )
  180. {
  181. return _UC_ISCTYPE(c, ISlower);
  182. }
  183. #endif
  184. #ifdef L_isprint
  185. int
  186. isprint( int c )
  187. {
  188. return _UC_ISCTYPE(c, ISprint);
  189. }
  190. #endif
  191. #ifdef L_ispunct
  192. int
  193. ispunct( int c )
  194. {
  195. return _UC_ISCTYPE(c, ISpunct);
  196. }
  197. #endif
  198. #ifdef L_isspace
  199. int
  200. isspace( int c )
  201. {
  202. return _UC_ISCTYPE(c, ISspace);
  203. }
  204. #endif
  205. #ifdef L_isupper
  206. int
  207. isupper( int c )
  208. {
  209. return _UC_ISCTYPE(c, ISupper);
  210. }
  211. #endif
  212. #ifdef L_isxdigit
  213. int
  214. isxdigit( int c )
  215. {
  216. return _UC_ISCTYPE(c, ISxdigit);
  217. }
  218. #endif
  219. #ifdef L_isxlower
  220. int
  221. isxlower( int c )
  222. {
  223. return _UC_ISCTYPE2(c, (ISxdigit|ISupper), ISxdigit);
  224. }
  225. #endif
  226. #ifdef L_isxupper
  227. int
  228. isxupper( int c )
  229. {
  230. return _UC_ISCTYPE2(c, (ISxdigit|ISlower), ISxdigit);
  231. }
  232. #endif
  233. #ifdef L_tolower
  234. int
  235. tolower( int c )
  236. {
  237. if((c < CHAR_MIN) || (c > UCHAR_MAX))
  238. return c;
  239. if(isupper(c))
  240. return _uc_ctype_trans[(int)((unsigned char)c)];
  241. else
  242. return c;
  243. }
  244. #endif
  245. #ifdef L_toupper
  246. int
  247. toupper( int c )
  248. {
  249. if((c < CHAR_MIN) || (c > UCHAR_MAX))
  250. return c;
  251. if(islower(c))
  252. return _uc_ctype_trans[(int)((unsigned char)c)];
  253. else
  254. return c;
  255. }
  256. #endif
  257. #endif