wctype.c 24 KB

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