_wctype.c 20 KB

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