wctype.c 22 KB

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