_wctype.c 21 KB

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