ctype.c 4.7 KB

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