_wctype.c 21 KB

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