ctype.c 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333
  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. #define __USE_CTYPE_MACROS
  14. #include <ctype.h>
  15. #ifdef L_isascii
  16. #undef isascii
  17. int
  18. isascii( int c )
  19. {
  20. return (c > 0 && c <= 0x7f);
  21. }
  22. #endif
  23. #ifdef L_isdigit
  24. #undef isdigit
  25. int
  26. isdigit( int c )
  27. {
  28. return (c >= '0' && c <= '9');
  29. }
  30. #endif
  31. #ifdef L_toascii
  32. #undef toascii
  33. int
  34. toascii( int c )
  35. {
  36. return (c & 0x7f);
  37. }
  38. #endif
  39. /* locale depended */
  40. #ifndef __UCLIBC_HAS_LOCALE__
  41. #ifdef L_isalpha
  42. #undef isalpha
  43. int
  44. isalpha( int c )
  45. {
  46. return (isupper(c) || islower(c));
  47. }
  48. #endif
  49. #ifdef L_isalnum
  50. #undef isalnum
  51. int
  52. isalnum( int c )
  53. {
  54. return (isalpha(c) || isdigit(c));
  55. }
  56. #endif
  57. #ifdef L_iscntrl
  58. #undef iscntrl
  59. int
  60. iscntrl( int c )
  61. {
  62. return ((c >= 0) && ((c <= 0x1f) || (c == 0x7f)));
  63. }
  64. #endif
  65. #ifdef L_isgraph
  66. #undef isgraph
  67. int
  68. isgraph( int c )
  69. {
  70. return (c > ' ' && isprint(c));
  71. }
  72. #endif
  73. #ifdef L_islower
  74. #undef islower
  75. int
  76. islower( int c )
  77. {
  78. return (c >= 'a' && c <= 'z');
  79. }
  80. #endif
  81. #ifdef L_isprint
  82. #undef isprint
  83. int
  84. isprint( int c )
  85. {
  86. return (c >= ' ' && c <= '~');
  87. }
  88. #endif
  89. #ifdef L_ispunct
  90. #undef ispunct
  91. int
  92. ispunct( int c )
  93. {
  94. return ((c > ' ' && c <= '~') && !isalnum(c));
  95. }
  96. #endif
  97. #ifdef L_isspace
  98. #undef isspace
  99. int
  100. isspace( int c )
  101. {
  102. return (c == ' ' || c == '\f' || c == '\n' || c == '\r' ||
  103. c == '\t' || c == '\v');
  104. }
  105. #endif
  106. #ifdef L_isupper
  107. #undef isupper
  108. int
  109. isupper( int c )
  110. {
  111. return (c >= 'A' && c <= 'Z');
  112. }
  113. #endif
  114. #ifdef L_isxdigit
  115. #undef isxdigit
  116. int
  117. isxdigit( int c )
  118. {
  119. return (isdigit(c) || (c >= 'a' && c <= 'f') || (c >= 'A' && c <= 'F'));
  120. }
  121. #endif
  122. #ifdef L_isxlower
  123. #undef isxlower
  124. int
  125. isxlower( int c )
  126. {
  127. return (isdigit(c) || (c >= 'a' && c <= 'f'));
  128. }
  129. #endif
  130. #ifdef L_isxupper
  131. #undef isxupper
  132. int
  133. isxupper( int c )
  134. {
  135. return (isdigit(c) || (c >= 'A' && c <= 'F'));
  136. }
  137. #endif
  138. #ifdef L_tolower
  139. #undef tolower
  140. int
  141. tolower( int c )
  142. {
  143. return (isupper(c) ? (c - 'A' + 'a') : (c));
  144. }
  145. #endif
  146. #ifdef L_toupper
  147. #undef toupper
  148. int
  149. toupper( int c )
  150. {
  151. return (islower(c) ? (c - 'a' + 'A') : (c));
  152. }
  153. #endif
  154. #else /* __UCLIBC_HAS_LOCALE__ */
  155. #include <limits.h>
  156. #include "../locale/_locale.h"
  157. #define _UC_ISCTYPE(c, type) \
  158. ((c != -1) && ((_uc_ctype_b[(int)((unsigned char)c)] & type) != 0))
  159. #define _UC_ISCTYPE2(c, type, type2) \
  160. ((c != -1) && ((_uc_ctype_b[(int)((unsigned char)c)] & type) == type2))
  161. #ifdef L_ctype_C
  162. /* startup setlocale(LC_TYPE, "C"); */
  163. #include "ctype_C.c"
  164. const unsigned char *_uc_ctype_b = _uc_ctype_b_C;
  165. const unsigned char *_uc_ctype_trans = _uc_ctype_b_C+LOCALE_BUF_SIZE/2;
  166. #endif /* L_ctype_C */
  167. #ifdef L_isalpha
  168. #undef isalpha
  169. int
  170. isalpha( int c )
  171. {
  172. return _UC_ISCTYPE(c, ISalpha);
  173. }
  174. #endif
  175. #ifdef L_isalnum
  176. #undef isalnum
  177. int
  178. isalnum( int c )
  179. {
  180. return _UC_ISCTYPE(c, (ISalpha|ISxdigit));
  181. }
  182. #endif
  183. #ifdef L_iscntrl
  184. #undef iscntrl
  185. int
  186. iscntrl( int c )
  187. {
  188. return _UC_ISCTYPE(c, IScntrl);
  189. }
  190. #endif
  191. #ifdef L_isgraph
  192. #undef isgraph
  193. int
  194. isgraph( int c )
  195. {
  196. return _UC_ISCTYPE2(c, (ISprint|ISspace), ISprint);
  197. }
  198. #endif
  199. #ifdef L_islower
  200. #undef islower
  201. int
  202. islower( int c )
  203. {
  204. return _UC_ISCTYPE(c, ISlower);
  205. }
  206. #endif
  207. #ifdef L_isprint
  208. #undef isprint
  209. int
  210. isprint( int c )
  211. {
  212. return _UC_ISCTYPE(c, ISprint);
  213. }
  214. #endif
  215. #ifdef L_ispunct
  216. #undef ispunct
  217. int
  218. ispunct( int c )
  219. {
  220. return _UC_ISCTYPE(c, ISpunct);
  221. }
  222. #endif
  223. #ifdef L_isspace
  224. #undef isspace
  225. int
  226. isspace( int c )
  227. {
  228. return _UC_ISCTYPE(c, ISspace);
  229. }
  230. #endif
  231. #ifdef L_isupper
  232. #undef isupper
  233. int
  234. isupper( int c )
  235. {
  236. return _UC_ISCTYPE(c, ISupper);
  237. }
  238. #endif
  239. #ifdef L_isxdigit
  240. #undef isxdigit
  241. int
  242. isxdigit( int c )
  243. {
  244. return _UC_ISCTYPE(c, ISxdigit);
  245. }
  246. #endif
  247. #ifdef L_isxlower
  248. #undef isxlower
  249. int
  250. isxlower( int c )
  251. {
  252. return _UC_ISCTYPE2(c, (ISxdigit|ISupper), ISxdigit);
  253. }
  254. #endif
  255. #ifdef L_isxupper
  256. #undef isxupper
  257. int
  258. isxupper( int c )
  259. {
  260. return _UC_ISCTYPE2(c, (ISxdigit|ISlower), ISxdigit);
  261. }
  262. #endif
  263. #ifdef L_tolower
  264. #undef tolower
  265. int
  266. tolower( int c )
  267. {
  268. if((c < CHAR_MIN) || (c > UCHAR_MAX))
  269. return c;
  270. if(isupper(c))
  271. return _uc_ctype_trans[(int)((unsigned char)c)];
  272. else
  273. return c;
  274. }
  275. #endif
  276. #ifdef L_toupper
  277. #undef toupper
  278. int
  279. toupper( int c )
  280. {
  281. if((c < CHAR_MIN) || (c > UCHAR_MAX))
  282. return c;
  283. if(islower(c))
  284. return _uc_ctype_trans[(int)((unsigned char)c)];
  285. else
  286. return c;
  287. }
  288. #endif
  289. #endif