_wctype.c 21 KB

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