wctype.c 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948
  1. /* Copyright (C) 2002, 2003 Manuel Novoa III
  2. *
  3. * This library is free software; you can redistribute it and/or
  4. * modify it under the terms of the GNU Library General Public
  5. * License as published by the Free Software Foundation; either
  6. * version 2 of the License, or (at your option) any later version.
  7. *
  8. * This library is distributed in the hope that it will be useful,
  9. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  11. * Library General Public License for more details.
  12. *
  13. * You should have received a copy of the GNU Library General Public
  14. * License along with this library; if not, write to the Free
  15. * Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  16. */
  17. /* ATTENTION! ATTENTION! ATTENTION! ATTENTION! ATTENTION!
  18. *
  19. * Besides uClibc, I'm using this code in my libc for elks, which is
  20. * a 16-bit environment with a fairly limited compiler. It would make
  21. * things much easier for me if this file isn't modified unnecessarily.
  22. * In particular, please put any new or replacement functions somewhere
  23. * else, and modify the makefile to use your version instead.
  24. * Thanks. Manuel
  25. *
  26. * ATTENTION! ATTENTION! ATTENTION! ATTENTION! ATTENTION! */
  27. #define _GNU_SOURCE
  28. #define __NO_CTYPE
  29. #include <wctype.h>
  30. #include <assert.h>
  31. #include <string.h>
  32. #include <errno.h>
  33. #include <locale.h>
  34. #include <ctype.h>
  35. #include <stdint.h>
  36. #include <bits/uClibc_uwchar.h>
  37. libc_hidden_proto(strcmp)
  38. libc_hidden_proto(__C_ctype_tolower)
  39. libc_hidden_proto(tolower)
  40. libc_hidden_proto(__C_ctype_toupper)
  41. libc_hidden_proto(toupper)
  42. libc_hidden_proto(towlower)
  43. libc_hidden_proto(towupper)
  44. libc_hidden_proto(towctrans)
  45. libc_hidden_proto(iswctype)
  46. #if defined(__LOCALE_C_ONLY) && defined(__UCLIBC_DO_XLOCALE)
  47. #error xlocale functionality is not supported in stub locale mode.
  48. #endif
  49. #ifdef __UCLIBC_HAS_XLOCALE__
  50. #include <xlocale.h>
  51. libc_hidden_proto(towlower_l)
  52. libc_hidden_proto(towupper_l)
  53. libc_hidden_proto(towctrans_l)
  54. libc_hidden_proto(iswctype_l)
  55. #else
  56. libc_hidden_proto(__ctype_b)
  57. #endif /* __UCLIBC_HAS_XLOCALE__ */
  58. #ifdef __UCLIBC_HAS_LOCALE__
  59. libc_hidden_proto(__global_locale)
  60. #endif
  61. /* We know wide char support is enabled. We wouldn't be here otherwise. */
  62. /* Define this if you want to unify the towupper and towlower code in the
  63. * towctrans function. */
  64. /* #define SMALL_UPLOW */
  65. /**********************************************************************/
  66. #ifdef __UCLIBC_MJN3_ONLY__
  67. #ifdef L_iswspace
  68. /* generates one warning */
  69. #warning TODO: Fix the __CTYPE_* codes!
  70. #endif
  71. #endif /* __UCLIBC_MJN3_ONLY__ */
  72. #if 1
  73. /* Taking advantage of the C99 mutual-exclusion guarantees for the various
  74. * (w)ctype classes, including the descriptions of printing and control
  75. * (w)chars, we can place each in one of the following mutually-exlusive
  76. * subsets. Since there are less than 16, we can store the data for
  77. * each (w)chars in a nibble. In contrast, glibc uses an unsigned int
  78. * per (w)char, with one bit flag for each is* type. While this allows
  79. * a simple '&' operation to determine the type vs. a range test and a
  80. * little special handling for the "blank" and "xdigit" types in my
  81. * approach, it also uses 8 times the space for the tables on the typical
  82. * 32-bit archs we supported.*/
  83. enum {
  84. __CTYPE_unclassified = 0,
  85. __CTYPE_alpha_nonupper_nonlower,
  86. __CTYPE_alpha_lower,
  87. __CTYPE_alpha_upper_lower,
  88. __CTYPE_alpha_upper,
  89. __CTYPE_digit,
  90. __CTYPE_punct,
  91. __CTYPE_graph,
  92. __CTYPE_print_space_nonblank,
  93. __CTYPE_print_space_blank,
  94. __CTYPE_space_nonblank_noncntrl,
  95. __CTYPE_space_blank_noncntrl,
  96. __CTYPE_cntrl_space_nonblank,
  97. __CTYPE_cntrl_space_blank,
  98. __CTYPE_cntrl_nonspace
  99. };
  100. #endif
  101. /* The following is used to implement wctype(), but it is defined
  102. * here because the ordering must agree with that of the enumeration
  103. * below (ignoring unclassified). */
  104. #define __CTYPE_TYPESTRING \
  105. "\6alnum\0\6alpha\0\6blank\0\6cntrl\0\6digit\0\6graph\0\6lower\0" \
  106. "\6print\0\6punct\0\6space\0\6upper\0\7xdigit\0\0"
  107. /* The values for wctype_t. */
  108. enum {
  109. _CTYPE_unclassified = 0,
  110. _CTYPE_isalnum,
  111. _CTYPE_isalpha,
  112. _CTYPE_isblank,
  113. _CTYPE_iscntrl,
  114. _CTYPE_isdigit,
  115. _CTYPE_isgraph,
  116. _CTYPE_islower,
  117. _CTYPE_isprint,
  118. _CTYPE_ispunct,
  119. _CTYPE_isspace,
  120. _CTYPE_isupper,
  121. _CTYPE_isxdigit /* _MUST_ be last of the standard classes! */
  122. };
  123. /* The following is used to implement wctrans(). */
  124. #define __CTYPE_TRANSTRING "\10tolower\0\10toupper\0\10totitle\0\0"
  125. enum {
  126. _CTYPE_tolower = 1,
  127. _CTYPE_toupper,
  128. _CTYPE_totitle
  129. };
  130. /*--------------------------------------------------------------------*/
  131. #define _CTYPE_iswxdigit (_CTYPE_isxdigit)
  132. /*--------------------------------------------------------------------*/
  133. #ifdef __UCLIBC_MJN3_ONLY__
  134. #ifdef L_iswspace
  135. /* generates one warning */
  136. #warning TODO: Fix WC* defines!
  137. #endif
  138. #endif /* __UCLIBC_MJN3_ONLY__ */
  139. #define ENCODING ((__UCLIBC_CURLOCALE_DATA).encoding)
  140. #define WCctype ((__UCLIBC_CURLOCALE_DATA).tblwctype)
  141. #define WCuplow ((__UCLIBC_CURLOCALE_DATA).tblwuplow)
  142. #define WCcmob ((__UCLIBC_CURLOCALE_DATA).tblwcomb)
  143. #define WCuplow_diff ((__UCLIBC_CURLOCALE_DATA).tblwuplow_diff)
  144. #define WC_TABLE_DOMAIN_MAX __LOCALE_DATA_WC_TABLE_DOMAIN_MAX
  145. #define WCctype_II_LEN __LOCALE_DATA_WCctype_II_LEN
  146. #define WCctype_TI_LEN __LOCALE_DATA_WCctype_TI_LEN
  147. #define WCctype_UT_LEN __LOCALE_DATA_WCctype_UT_LEN
  148. #define WCctype_II_SHIFT __LOCALE_DATA_WCctype_II_SHIFT
  149. #define WCctype_TI_SHIFT __LOCALE_DATA_WCctype_TI_SHIFT
  150. #define WCuplow_II_LEN __LOCALE_DATA_WCuplow_II_LEN
  151. #define WCuplow_TI_LEN __LOCALE_DATA_WCuplow_TI_LEN
  152. #define WCuplow_UT_LEN __LOCALE_DATA_WCuplow_UT_LEN
  153. #define WCuplow_II_SHIFT __LOCALE_DATA_WCuplow_II_SHIFT
  154. #define WCuplow_TI_SHIFT __LOCALE_DATA_WCuplow_TI_SHIFT
  155. #define WCctype_TI_MASK ((1 << (WCctype_TI_SHIFT)) - 1)
  156. #define WCctype_II_MASK ((1 << (WCctype_II_SHIFT)) - 1)
  157. /**********************************************************************/
  158. #undef __PASTE2
  159. #undef __PASTE3
  160. #define __PASTE2(X,Y) X ## Y
  161. #define __PASTE3(X,Y,Z) X ## Y ## Z
  162. #ifdef __UCLIBC_DO_XLOCALE
  163. #define ISW_FUNC_BODY(NAME) \
  164. libc_hidden_proto(__PASTE3(isw,NAME,_l)); \
  165. int __PASTE3(isw,NAME,_l) (wint_t wc, __locale_t l) \
  166. { \
  167. return iswctype_l(wc, __PASTE2(_CTYPE_is,NAME), l); \
  168. } \
  169. libc_hidden_def(__PASTE3(isw,NAME,_l))
  170. #else /* __UCLIBC_DO_XLOCALE */
  171. #define ISW_FUNC_BODY(NAME) \
  172. libc_hidden_proto(__PASTE2(isw,NAME)); \
  173. int __PASTE2(isw,NAME) (wint_t wc) \
  174. { \
  175. return iswctype(wc, __PASTE2(_CTYPE_is,NAME)); \
  176. } \
  177. libc_hidden_def(__PASTE2(isw,NAME))
  178. #endif /* __UCLIBC_DO_XLOCALE */
  179. /**********************************************************************/
  180. #if defined(L_iswalnum) || defined(L_iswalnum_l)
  181. ISW_FUNC_BODY(alnum);
  182. #endif
  183. /**********************************************************************/
  184. #if defined(L_iswalpha) || defined(L_iswalpha_l)
  185. ISW_FUNC_BODY(alpha);
  186. #endif
  187. /**********************************************************************/
  188. #if defined(L_iswblank) || defined(L_iswblank_l)
  189. ISW_FUNC_BODY(blank);
  190. #endif
  191. /**********************************************************************/
  192. #if defined(L_iswcntrl) || defined(L_iswcntrl_l)
  193. ISW_FUNC_BODY(cntrl);
  194. #endif
  195. /**********************************************************************/
  196. #if defined(L_iswdigit) || defined(L_iswdigit_l)
  197. ISW_FUNC_BODY(digit);
  198. #endif
  199. /**********************************************************************/
  200. #if defined(L_iswgraph) || defined(L_iswgraph_l)
  201. ISW_FUNC_BODY(graph);
  202. #endif
  203. /**********************************************************************/
  204. #if defined(L_iswlower) || defined(L_iswlower_l)
  205. ISW_FUNC_BODY(lower);
  206. #endif
  207. /**********************************************************************/
  208. #if defined(L_iswprint) || defined(L_iswprint_l)
  209. ISW_FUNC_BODY(print);
  210. #endif
  211. /**********************************************************************/
  212. #if defined(L_iswpunct) || defined(L_iswpunct_l)
  213. ISW_FUNC_BODY(punct);
  214. #endif
  215. /**********************************************************************/
  216. #if defined(L_iswspace) || defined(L_iswspace_l)
  217. ISW_FUNC_BODY(space);
  218. #endif
  219. /**********************************************************************/
  220. #if defined(L_iswupper) || defined(L_iswupper_l)
  221. ISW_FUNC_BODY(upper);
  222. #endif
  223. /**********************************************************************/
  224. #if defined(L_iswxdigit) || defined(L_iswxdigit_l)
  225. ISW_FUNC_BODY(xdigit);
  226. #endif
  227. /**********************************************************************/
  228. #if defined(L_towlower) || defined(L_towlower_l)
  229. #ifdef L_towlower
  230. #define TOWLOWER(w) towlower(w)
  231. #else /* L_towlower */
  232. #define TOWLOWER(w) towlower_l(w, __locale_t locale)
  233. #undef __UCLIBC_CURLOCALE_DATA
  234. #undef __UCLIBC_CURLOCALE
  235. #define __UCLIBC_CURLOCALE_DATA (*locale)
  236. #define __UCLIBC_CURLOCALE (locale)
  237. #endif /* L_towlower */
  238. #ifdef __UCLIBC_HAS_XLOCALE__
  239. #define TOWCTRANS(w,d) towctrans_l(w,d, __UCLIBC_CURLOCALE)
  240. #else /* __UCLIBC_HAS_XLOCALE__ */
  241. #define TOWCTRANS(w,d) towctrans(w,d)
  242. #endif /* __UCLIBC_HAS_XLOCALE__ */
  243. #define __C_towlower(wc) \
  244. ((((__uwchar_t)(wc)) <= 0x7f) ? (__C_ctype_tolower)[(wc)] : (wc))
  245. #ifdef __LOCALE_C_ONLY
  246. wint_t towlower(wint_t wc)
  247. {
  248. #ifdef __UCLIBC_HAS_CTYPE_TABLES__
  249. return __C_towlower(wc);
  250. #else
  251. return (wc == ((unsigned int)(wc)))
  252. ? __C_tolower(((unsigned int)(wc)))
  253. : 0;
  254. #endif
  255. }
  256. #else /* __LOCALE_C_ONLY */
  257. #ifdef SMALL_UPLOW
  258. #if defined(L_towlower) && defined(__UCLIBC_HAS_XLOCALE__)
  259. wint_t towlower(wint_t wc)
  260. {
  261. return towctrans_l(wc, _CTYPE_tolower, __UCLIBC_CURLOCALE);
  262. }
  263. #else /* defined(L_towlower) && defined(__UCLIBC_HAS_XLOCALE__) */
  264. wint_t TOWLOWER(wint_t wc)
  265. {
  266. return TOWCTRANS(wc, _CTYPE_tolower);
  267. }
  268. #endif /* defined(L_towlower) && defined(__UCLIBC_HAS_XLOCALE__) */
  269. #else /* SMALL_UPLOW */
  270. #if defined(L_towlower) && defined(__UCLIBC_HAS_XLOCALE__)
  271. wint_t towlower(wint_t wc)
  272. {
  273. return towlower_l(wc, __UCLIBC_CURLOCALE);
  274. }
  275. #else /* defined(L_towlower) && defined(__UCLIBC_HAS_XLOCALE__) */
  276. wint_t TOWLOWER(wint_t wc)
  277. {
  278. unsigned int sc, n, i;
  279. __uwchar_t u = wc;
  280. if (ENCODING == __ctype_encoding_7_bit) {
  281. /* We're in the C/POSIX locale, so ignore the tables. */
  282. return __C_towlower(wc);
  283. }
  284. if (u <= WC_TABLE_DOMAIN_MAX) {
  285. sc = u & ((1 << WCuplow_TI_SHIFT) - 1);
  286. u >>= WCuplow_TI_SHIFT;
  287. n = u & ((1 << WCuplow_II_SHIFT) - 1);
  288. u >>= WCuplow_II_SHIFT;
  289. i = ((unsigned int) WCuplow[u]) << WCuplow_II_SHIFT;
  290. i = ((unsigned int) WCuplow[WCuplow_II_LEN + i + n])
  291. << WCuplow_TI_SHIFT;
  292. i = ((unsigned int) WCuplow[WCuplow_II_LEN + WCuplow_TI_LEN
  293. + i + sc]) << 1;
  294. wc += WCuplow_diff[i + 1];
  295. }
  296. return wc;
  297. }
  298. #endif /* defined(L_towlower) && defined(__UCLIBC_HAS_XLOCALE__) */
  299. #endif /* SMALL_UPLOW */
  300. #ifdef L_towlower_l
  301. libc_hidden_def(towlower_l)
  302. #endif /* L_towlower_l */
  303. #endif /* __LOCALE_C_ONLY */
  304. #ifndef L_towlower_l
  305. libc_hidden_def(towlower)
  306. #endif
  307. #endif
  308. /**********************************************************************/
  309. #if defined(L_towupper) || defined(L_towupper_l)
  310. #ifdef L_towupper
  311. #define TOWUPPER(w) towupper(w)
  312. #else /* L_towupper */
  313. #define TOWUPPER(w) towupper_l(w, __locale_t locale)
  314. #undef __UCLIBC_CURLOCALE_DATA
  315. #undef __UCLIBC_CURLOCALE
  316. #define __UCLIBC_CURLOCALE_DATA (*locale)
  317. #define __UCLIBC_CURLOCALE (locale)
  318. #endif /* L_towupper */
  319. #ifdef __UCLIBC_HAS_XLOCALE__
  320. #define TOWCTRANS(w,d) towctrans_l(w,d, __UCLIBC_CURLOCALE)
  321. #else /* __UCLIBC_HAS_XLOCALE__ */
  322. #define TOWCTRANS(w,d) towctrans(w,d)
  323. #endif /* __UCLIBC_HAS_XLOCALE__ */
  324. #define __C_towupper(wc) \
  325. ((((__uwchar_t)(wc)) <= 0x7f) ? (__C_ctype_toupper)[(wc)] : (wc))
  326. #ifdef __LOCALE_C_ONLY
  327. wint_t towupper(wint_t wc)
  328. {
  329. #ifdef __UCLIBC_HAS_CTYPE_TABLES__
  330. return __C_towupper(wc);
  331. #else
  332. return (wc == ((unsigned int)(wc)))
  333. ? __C_toupper(((unsigned int)(wc)))
  334. : 0;
  335. #endif
  336. }
  337. #else /* __LOCALE_C_ONLY */
  338. #ifdef SMALL_UPLOW
  339. #if defined(L_towupper) && defined(__UCLIBC_HAS_XLOCALE__)
  340. wint_t towupper(wint_t wc)
  341. {
  342. return towctrans_l(wc, _CTYPE_toupper, __UCLIBC_CURLOCALE);
  343. }
  344. #else /* defined(L_towupper) && defined(__UCLIBC_HAS_XLOCALE__) */
  345. wint_t TOWUPPER(wint_t wc)
  346. {
  347. return TOWCTRANS(wc, _CTYPE_toupper);
  348. }
  349. #endif /* defined(L_towupper) && defined(__UCLIBC_HAS_XLOCALE__) */
  350. #else /* SMALL_UPLOW */
  351. #if defined(L_towupper) && defined(__UCLIBC_HAS_XLOCALE__)
  352. wint_t towupper(wint_t wc)
  353. {
  354. return towupper_l(wc, __UCLIBC_CURLOCALE);
  355. }
  356. #else /* defined(L_towupper) && defined(__UCLIBC_HAS_XLOCALE__) */
  357. wint_t TOWUPPER(wint_t wc)
  358. {
  359. unsigned int sc, n, i;
  360. __uwchar_t u = wc;
  361. if (ENCODING == __ctype_encoding_7_bit) {
  362. /* We're in the C/POSIX locale, so ignore the tables. */
  363. return __C_towupper(wc);
  364. }
  365. if (u <= WC_TABLE_DOMAIN_MAX) {
  366. sc = u & ((1 << WCuplow_TI_SHIFT) - 1);
  367. u >>= WCuplow_TI_SHIFT;
  368. n = u & ((1 << WCuplow_II_SHIFT) - 1);
  369. u >>= WCuplow_II_SHIFT;
  370. i = ((unsigned int) WCuplow[u]) << WCuplow_II_SHIFT;
  371. i = ((unsigned int) WCuplow[WCuplow_II_LEN + i + n])
  372. << WCuplow_TI_SHIFT;
  373. i = ((unsigned int) WCuplow[WCuplow_II_LEN + WCuplow_TI_LEN
  374. + i + sc]) << 1;
  375. wc += WCuplow_diff[i];
  376. }
  377. return wc;
  378. }
  379. #endif /* defined(L_towupper) && defined(__UCLIBC_HAS_XLOCALE__) */
  380. #endif /* SMALL_UPLOW */
  381. #ifdef L_towupper_l
  382. libc_hidden_def(towupper_l)
  383. #endif /* L_towupper_l */
  384. #endif /* __LOCALE_C_ONLY */
  385. #ifndef L_towupper_l
  386. libc_hidden_def(towupper)
  387. #endif
  388. #endif
  389. /**********************************************************************/
  390. #ifdef L_wctype
  391. static const unsigned char typestring[] = __CTYPE_TYPESTRING;
  392. /* extern const unsigned char typestring[]; */
  393. libc_hidden_proto(wctype)
  394. wctype_t wctype(const char *property)
  395. {
  396. const unsigned char *p;
  397. int i;
  398. p = typestring;
  399. i = 1;
  400. do {
  401. if (!strcmp(property, ++p)) {
  402. return i;
  403. }
  404. ++i;
  405. p += p[-1];
  406. } while (*p);
  407. /* TODO - Add locale-specific classifications. */
  408. return 0;
  409. }
  410. libc_hidden_def(wctype)
  411. #endif
  412. /**********************************************************************/
  413. #ifdef L_wctype_l
  414. #ifdef __UCLIBC_MJN3_ONLY__
  415. #warning REMINDER: Currently wctype_l simply calls wctype.
  416. #endif /* __UCLIBC_MJN3_ONLY__ */
  417. libc_hidden_proto(wctype)
  418. libc_hidden_proto(wctype_l)
  419. wctype_t wctype_l (const char *property, __locale_t locale)
  420. {
  421. return wctype(property);
  422. }
  423. libc_hidden_def(wctype_l)
  424. #endif
  425. /**********************************************************************/
  426. #if defined(L_iswctype) || defined(L_iswctype_l)
  427. #define __C_iswdigit(c) \
  428. ((sizeof(c) == sizeof(char)) \
  429. ? (((unsigned char)((c) - '0')) < 10) \
  430. : (((__uwchar_t)((c) - '0')) < 10))
  431. #define __C_iswxdigit(c) \
  432. (__C_iswdigit(c) \
  433. || ((sizeof(c) == sizeof(char)) \
  434. ? (((unsigned char)((((c)) | 0x20) - 'a')) < 6) \
  435. : (((__uwchar_t)((((c)) | 0x20) - 'a')) < 6)))
  436. #ifdef __UCLIBC_MJN3_ONLY__
  437. #ifdef L_iswctype
  438. #warning CONSIDER: Change to bit shift? would need to sync with wctype.h
  439. #endif
  440. #endif /* __UCLIBC_MJN3_ONLY__ */
  441. #ifdef __UCLIBC_HAS_CTYPE_TABLES__
  442. #if !defined(__UCLIBC_HAS_XLOCALE__) || defined(L_iswctype_l)
  443. static const unsigned short int desc2flag[] = {
  444. [_CTYPE_unclassified] = 0,
  445. [_CTYPE_isalnum] = (unsigned short int) _ISwalnum,
  446. [_CTYPE_isalpha] = (unsigned short int) _ISwalpha,
  447. [_CTYPE_isblank] = (unsigned short int) _ISwblank,
  448. [_CTYPE_iscntrl] = (unsigned short int) _ISwcntrl,
  449. [_CTYPE_isdigit] = (unsigned short int) _ISwdigit,
  450. [_CTYPE_isgraph] = (unsigned short int) _ISwgraph,
  451. [_CTYPE_islower] = (unsigned short int) _ISwlower,
  452. [_CTYPE_isprint] = (unsigned short int) _ISwprint,
  453. [_CTYPE_ispunct] = (unsigned short int) _ISwpunct,
  454. [_CTYPE_isspace] = (unsigned short int) _ISwspace,
  455. [_CTYPE_isupper] = (unsigned short int) _ISwupper,
  456. [_CTYPE_isxdigit] = (unsigned short int) _ISwxdigit,
  457. };
  458. #endif /* defined(L_iswctype_L) || defined(__LOCALE_C_ONLY) */
  459. #endif /* __UCLIBC_HAS_CTYPE_TABLES__ */
  460. #ifdef __LOCALE_C_ONLY
  461. #ifdef __UCLIBC_HAS_CTYPE_TABLES__
  462. int iswctype(wint_t wc, wctype_t desc)
  463. {
  464. /* Note... wctype_t is unsigned. */
  465. if ((((__uwchar_t) wc) <= 0x7f)
  466. && (desc < (sizeof(desc2flag)/sizeof(desc2flag[0])))
  467. ) {
  468. return __isctype(wc, desc2flag[desc]);
  469. }
  470. return 0;
  471. }
  472. #else /* __UCLIBC_HAS_CTYPE_TABLES__ */
  473. int iswctype(wint_t wc, wctype_t desc)
  474. {
  475. /* This is lame, but it is here just to get it working for now. */
  476. if (wc == ((unsigned int)(wc))) {
  477. switch(desc) {
  478. case _CTYPE_isupper:
  479. return __C_isupper((unsigned int)(wc));
  480. case _CTYPE_islower:
  481. return __C_islower((unsigned int)(wc));
  482. case _CTYPE_isalpha:
  483. return __C_isalpha((unsigned int)(wc));
  484. case _CTYPE_isdigit:
  485. return __C_isdigit((unsigned int)(wc));
  486. case _CTYPE_isxdigit:
  487. return __C_isxdigit((unsigned int)(wc));
  488. case _CTYPE_isspace:
  489. return __C_isspace((unsigned int)(wc));
  490. case _CTYPE_isprint:
  491. return __C_isprint((unsigned int)(wc));
  492. case _CTYPE_isgraph:
  493. return __C_isgraph((unsigned int)(wc));
  494. case _CTYPE_isblank:
  495. return __C_isblank((unsigned int)(wc));
  496. case _CTYPE_iscntrl:
  497. return __C_iscntrl((unsigned int)(wc));
  498. case _CTYPE_ispunct:
  499. return __C_ispunct((unsigned int)(wc));
  500. case _CTYPE_isalnum:
  501. return __C_isalnum((unsigned int)(wc));
  502. default:
  503. break;
  504. }
  505. }
  506. return 0;
  507. }
  508. #endif /* __UCLIBC_HAS_CTYPE_TABLES__ */
  509. #else /* __LOCALE_C_ONLY */
  510. #ifdef __UCLIBC_MJN3_ONLY__
  511. #ifdef L_iswctype
  512. #warning CONSIDER: Handle combining class?
  513. #endif
  514. #endif /* __UCLIBC_MJN3_ONLY__ */
  515. #ifdef L_iswctype
  516. #define ISWCTYPE(w,d) iswctype(w,d)
  517. #else /* L_iswctype */
  518. #define ISWCTYPE(w,d) iswctype_l(w,d, __locale_t locale)
  519. #undef __UCLIBC_CURLOCALE_DATA
  520. #undef __UCLIBC_CURLOCALE
  521. #define __UCLIBC_CURLOCALE_DATA (*locale)
  522. #define __UCLIBC_CURLOCALE (locale)
  523. #endif /* L_iswctype */
  524. #if defined(L_iswctype) && defined(__UCLIBC_HAS_XLOCALE__)
  525. int iswctype(wint_t wc, wctype_t desc)
  526. {
  527. return iswctype_l(wc, desc, __UCLIBC_CURLOCALE);
  528. }
  529. #else /* defined(L_iswctype) && defined(__UCLIBC_HAS_XLOCALE__) */
  530. int ISWCTYPE(wint_t wc, wctype_t desc)
  531. {
  532. unsigned int sc, n, i0, i1;
  533. unsigned char d = __CTYPE_unclassified;
  534. if ((ENCODING != __ctype_encoding_7_bit) || (((__uwchar_t) wc) <= 0x7f)){
  535. if (desc < _CTYPE_iswxdigit) {
  536. if (((__uwchar_t) wc) <= WC_TABLE_DOMAIN_MAX) {
  537. /* From here on, we know wc > 0. */
  538. sc = wc & WCctype_TI_MASK;
  539. wc >>= WCctype_TI_SHIFT;
  540. n = wc & WCctype_II_MASK;
  541. wc >>= WCctype_II_SHIFT;
  542. i0 = WCctype[wc];
  543. i0 <<= WCctype_II_SHIFT;
  544. i1 = WCctype[WCctype_II_LEN + i0 + n];
  545. i1 <<= (WCctype_TI_SHIFT-1);
  546. d = WCctype[WCctype_II_LEN + WCctype_TI_LEN + i1 + (sc >> 1)];
  547. d = (sc & 1) ? (d >> 4) : (d & 0xf);
  548. } else if ( ((((__uwchar_t)(wc - 0xe0020UL)) <= 0x5f)
  549. || (wc == 0xe0001UL))
  550. || ( (((__uwchar_t)(wc - 0xf0000UL)) < 0x20000UL)
  551. && ((wc & 0xffffU) <= 0xfffdU))
  552. ) {
  553. d = __CTYPE_punct;
  554. }
  555. #if 0
  556. return ( ((unsigned char)(d - ctype_range[2*desc]))
  557. <= ctype_range[2*desc + 1] )
  558. && ((desc != _CTYPE_iswblank) || (d & 1));
  559. #else
  560. return (__UCLIBC_CURLOCALE_DATA).code2flag[d] & desc2flag[desc];
  561. #endif
  562. }
  563. #ifdef __UCLIBC_MJN3_ONLY__
  564. #warning TODO: xdigit really needs to be handled better. Remember only for ascii!
  565. #endif /* __UCLIBC_MJN3_ONLY__ */
  566. /* TODO - Add locale-specific classifications. */
  567. return (desc == _CTYPE_iswxdigit) ? __C_iswxdigit(wc) : 0;
  568. }
  569. return 0;
  570. }
  571. #endif /* defined(L_iswctype) && defined(__UCLIBC_HAS_XLOCALE__) */
  572. #ifdef L_iswctype_l
  573. libc_hidden_def(iswctype_l)
  574. #endif /* L_iswctype_l */
  575. #endif /* __LOCALE_C_ONLY */
  576. #ifdef L_iswctype
  577. libc_hidden_def(iswctype)
  578. #endif /* L_iswctype */
  579. #endif
  580. /**********************************************************************/
  581. #if defined(L_towctrans) || defined(L_towctrans_l)
  582. #ifdef __LOCALE_C_ONLY
  583. /* Minimal support for C/POSIX locale. */
  584. #ifndef _tolower
  585. #warning _tolower is undefined!
  586. #define _tolower(c) tolower(c)
  587. #endif
  588. #ifndef _toupper
  589. #warning _toupper is undefined!
  590. #define _toupper(c) toupper(c)
  591. #endif
  592. wint_t towctrans(wint_t wc, wctrans_t desc)
  593. {
  594. if (((unsigned int)(desc - _CTYPE_tolower))
  595. <= (_CTYPE_toupper - _CTYPE_tolower)
  596. ) {
  597. /* Transliteration is either tolower or toupper. */
  598. if (((__uwchar_t) wc) <= 0x7f) {
  599. return (desc == _CTYPE_tolower) ? _tolower(wc) : _toupper(wc);
  600. }
  601. } else {
  602. __set_errno(EINVAL); /* Invalid transliteration. */
  603. }
  604. return wc;
  605. }
  606. #else /* __LOCALE_C_ONLY */
  607. #ifdef L_towctrans
  608. #define TOWCTRANS(w,d) towctrans(w,d)
  609. #else /* L_towctrans */
  610. #define TOWCTRANS(w,d) towctrans_l(w,d, __locale_t locale)
  611. #undef __UCLIBC_CURLOCALE_DATA
  612. #undef __UCLIBC_CURLOCALE
  613. #define __UCLIBC_CURLOCALE_DATA (*locale)
  614. #define __UCLIBC_CURLOCALE (locale)
  615. #endif /* L_towctrans */
  616. #ifdef __UCLIBC_HAS_XLOCALE__
  617. #define TOWLOWER(w,l) towlower_l(w,l)
  618. #define TOWUPPER(w,l) towupper_l(w,l)
  619. #else /* __UCLIBC_HAS_XLOCALE__ */
  620. #define TOWLOWER(w,l) towlower(w)
  621. #define TOWUPPER(w,l) towupper(w)
  622. #endif /* __UCLIBC_HAS_XLOCALE__ */
  623. #if defined(L_towctrans) && defined(__UCLIBC_HAS_XLOCALE__)
  624. wint_t towctrans(wint_t wc, wctrans_t desc)
  625. {
  626. return towctrans_l(wc, desc, __UCLIBC_CURLOCALE);
  627. }
  628. #else /* defined(L_towctrans) && defined(__UCLIBC_HAS_XLOCALE__) */
  629. #ifdef SMALL_UPLOW
  630. wint_t TOWCTRANS(wint_t wc, wctrans_t desc)
  631. {
  632. unsigned int sc, n, i;
  633. __uwchar_t u = wc;
  634. /* TODO - clean up */
  635. if (ENCODING == __ctype_encoding_7_bit) {
  636. if ((((__uwchar_t) wc) > 0x7f)
  637. || (((unsigned int)(desc - _CTYPE_tolower))
  638. > (_CTYPE_toupper - _CTYPE_tolower))
  639. ){
  640. /* We're in the C/POSIX locale, so ignore non-ASCII values
  641. * as well an any mappings other than toupper or tolower. */
  642. return wc;
  643. }
  644. }
  645. if (((unsigned int)(desc - _CTYPE_tolower))
  646. <= (_CTYPE_totitle - _CTYPE_tolower)
  647. ) {
  648. if (u <= WC_TABLE_DOMAIN_MAX) {
  649. sc = u & ((1 << WCuplow_TI_SHIFT) - 1);
  650. u >>= WCuplow_TI_SHIFT;
  651. n = u & ((1 << WCuplow_II_SHIFT) - 1);
  652. u >>= WCuplow_II_SHIFT;
  653. i = ((unsigned int) WCuplow[u]) << WCuplow_II_SHIFT;
  654. i = ((unsigned int) WCuplow[WCuplow_II_LEN + i + n])
  655. << WCuplow_TI_SHIFT;
  656. i = ((unsigned int) WCuplow[WCuplow_II_LEN + WCuplow_TI_LEN
  657. + i + sc]) << 1;
  658. if (desc == _CTYPE_tolower) {
  659. ++i;
  660. }
  661. wc += WCuplow_diff[i];
  662. if (desc == _CTYPE_totitle) {
  663. #ifdef __UCLIBC_MJN3_ONLY__
  664. #warning TODO: Verify totitle special cases!
  665. #endif /* __UCLIBC_MJN3_ONLY__ */
  666. /* WARNING! These special cases work for glibc 2.2.4. Changes
  667. * may be needed if the glibc locale tables are updated. */
  668. if ( (((__uwchar_t)(wc - 0x1c4)) <= (0x1cc - 0x1c4))
  669. || (wc == 0x1f1)
  670. ) {
  671. ++wc;
  672. }
  673. }
  674. }
  675. } else {
  676. /* TODO - Deal with other transliterations. */
  677. __set_errno(EINVAL);
  678. }
  679. return wc;
  680. }
  681. #else /* SMALL_UPLOW */
  682. wint_t TOWCTRANS(wint_t wc, wctrans_t desc)
  683. {
  684. if (ENCODING == __ctype_encoding_7_bit) {
  685. if ((((__uwchar_t) wc) > 0x7f)
  686. || (((unsigned int)(desc - _CTYPE_tolower))
  687. > (_CTYPE_toupper - _CTYPE_tolower))
  688. ){
  689. /* We're in the C/POSIX locale, so ignore non-ASCII values
  690. * as well an any mappings other than toupper or tolower. */
  691. return wc;
  692. }
  693. }
  694. if (desc == _CTYPE_tolower) {
  695. return TOWLOWER(wc, __UCLIBC_CURLOCALE);
  696. } else if (((unsigned int)(desc - _CTYPE_toupper))
  697. <= (_CTYPE_totitle - _CTYPE_toupper)
  698. ) {
  699. wc = TOWUPPER(wc, __UCLIBC_CURLOCALE);
  700. if (desc == _CTYPE_totitle) {
  701. #ifdef __UCLIBC_MJN3_ONLY__
  702. #warning TODO: Verify totitle special cases!
  703. #endif /* __UCLIBC_MJN3_ONLY__ */
  704. /* WARNING! These special cases work for glibc 2.2.4. Changes
  705. * may be needed if the glibc locale tables are updated. */
  706. if ( (((__uwchar_t)(wc - 0x1c4)) <= (0x1cc - 0x1c4))
  707. || (wc == 0x1f1)
  708. ) {
  709. ++wc;
  710. }
  711. }
  712. } else {
  713. /* TODO - Deal with other transliterations. */
  714. __set_errno(EINVAL);
  715. }
  716. return wc;
  717. }
  718. #endif /* SMALL_UPLOW */
  719. #endif /* defined(L_towctrans) && defined(__UCLIBC_HAS_XLOCALE__) */
  720. #ifdef L_towctrans_l
  721. libc_hidden_def(towctrans_l)
  722. #endif /* L_towctrans_l */
  723. #endif /* __LOCALE_C_ONLY */
  724. #ifndef L_towctrans_l
  725. libc_hidden_def(towctrans)
  726. #endif
  727. #endif
  728. /**********************************************************************/
  729. #ifdef L_wctrans
  730. static const char transstring[] = __CTYPE_TRANSTRING;
  731. libc_hidden_proto(wctrans)
  732. wctrans_t wctrans(const char *property)
  733. {
  734. const unsigned char *p;
  735. int i;
  736. p = transstring;
  737. i = 1;
  738. do {
  739. if (!strcmp(property, ++p)) {
  740. return i;
  741. }
  742. ++i;
  743. p += p[-1];
  744. } while (*p);
  745. /* TODO - Add locale-specific translations. */
  746. return 0;
  747. }
  748. libc_hidden_def(wctrans)
  749. #endif
  750. /**********************************************************************/
  751. #ifdef L_wctrans_l
  752. #ifdef __UCLIBC_MJN3_ONLY__
  753. #warning REMINDER: Currently wctrans_l simply calls wctrans.
  754. #endif /* __UCLIBC_MJN3_ONLY__ */
  755. libc_hidden_proto(wctrans)
  756. wctrans_t wctrans_l(const char *property, __locale_t locale)
  757. {
  758. return wctrans(property);
  759. }
  760. #endif
  761. /**********************************************************************/