_wctype.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879
  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 /* __UCLIBC_MJN3_ONLY__ */
  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 /* L_towlower */
  179. #define TOWLOWER(w) towlower_l(w, __locale_t locale)
  180. #undef __UCLIBC_CURLOCALE
  181. #define __UCLIBC_CURLOCALE (locale)
  182. #endif /* L_towlower */
  183. #ifdef __UCLIBC_HAS_XLOCALE__
  184. #define TOWCTRANS(w,d) towctrans_l(w,d, __UCLIBC_CURLOCALE)
  185. #else /* __UCLIBC_HAS_XLOCALE__ */
  186. #define TOWCTRANS(w,d) towctrans(w,d)
  187. #endif /* __UCLIBC_HAS_XLOCALE__ */
  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 int)(wc)))
  197. ? __C_tolower(((unsigned int)(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 int 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 int) WCuplow[u]) << WCuplow_II_SHIFT;
  235. i = ((unsigned int) WCuplow[WCuplow_II_LEN + i + n])
  236. << WCuplow_TI_SHIFT;
  237. i = ((unsigned int) WCuplow[WCuplow_II_LEN + WCuplow_TI_LEN
  238. + i + sc]) << 1;
  239. wc += WCuplow_diff[i + 1];
  240. }
  241. return wc;
  242. }
  243. #endif /* defined(L_towlower) && defined(__UCLIBC_HAS_XLOCALE__) */
  244. #endif /* SMALL_UPLOW */
  245. #ifdef L_towlower_l
  246. libc_hidden_def(towlower_l)
  247. #endif /* L_towlower_l */
  248. #endif /* __LOCALE_C_ONLY */
  249. #ifndef L_towlower_l
  250. libc_hidden_def(towlower)
  251. #endif
  252. #endif
  253. /**********************************************************************/
  254. #if defined(L_towupper) || defined(L_towupper_l)
  255. #ifdef L_towupper
  256. #define TOWUPPER(w) towupper(w)
  257. #else /* L_towupper */
  258. #define TOWUPPER(w) towupper_l(w, __locale_t locale)
  259. #undef __UCLIBC_CURLOCALE
  260. #define __UCLIBC_CURLOCALE (locale)
  261. #endif /* L_towupper */
  262. #ifdef __UCLIBC_HAS_XLOCALE__
  263. #define TOWCTRANS(w,d) towctrans_l(w,d, __UCLIBC_CURLOCALE)
  264. #else /* __UCLIBC_HAS_XLOCALE__ */
  265. #define TOWCTRANS(w,d) towctrans(w,d)
  266. #endif /* __UCLIBC_HAS_XLOCALE__ */
  267. #define __C_towupper(wc) \
  268. ((((__uwchar_t)(wc)) <= 0x7f) ? (__C_ctype_toupper)[(wc)] : (wc))
  269. #ifdef __LOCALE_C_ONLY
  270. wint_t towupper(wint_t wc)
  271. {
  272. #ifdef __UCLIBC_HAS_CTYPE_TABLES__
  273. return __C_towupper(wc);
  274. #else
  275. return (wc == ((unsigned int)(wc)))
  276. ? __C_toupper(((unsigned int)(wc)))
  277. : 0;
  278. #endif
  279. }
  280. #else /* __LOCALE_C_ONLY */
  281. #ifdef SMALL_UPLOW
  282. #if defined(L_towupper) && defined(__UCLIBC_HAS_XLOCALE__)
  283. wint_t towupper(wint_t wc)
  284. {
  285. return towctrans_l(wc, _CTYPE_toupper, __UCLIBC_CURLOCALE);
  286. }
  287. #else /* defined(L_towupper) && defined(__UCLIBC_HAS_XLOCALE__) */
  288. wint_t TOWUPPER(wint_t wc)
  289. {
  290. return TOWCTRANS(wc, _CTYPE_toupper);
  291. }
  292. #endif /* defined(L_towupper) && defined(__UCLIBC_HAS_XLOCALE__) */
  293. #else /* SMALL_UPLOW */
  294. #if defined(L_towupper) && defined(__UCLIBC_HAS_XLOCALE__)
  295. wint_t towupper(wint_t wc)
  296. {
  297. return towupper_l(wc, __UCLIBC_CURLOCALE);
  298. }
  299. #else /* defined(L_towupper) && defined(__UCLIBC_HAS_XLOCALE__) */
  300. wint_t TOWUPPER(wint_t wc)
  301. {
  302. unsigned int sc, n, i;
  303. __uwchar_t u = wc;
  304. if (ENCODING == __ctype_encoding_7_bit) {
  305. /* We're in the C/POSIX locale, so ignore the tables. */
  306. return __C_towupper(wc);
  307. }
  308. if (u <= WC_TABLE_DOMAIN_MAX) {
  309. sc = u & ((1 << WCuplow_TI_SHIFT) - 1);
  310. u >>= WCuplow_TI_SHIFT;
  311. n = u & ((1 << WCuplow_II_SHIFT) - 1);
  312. u >>= WCuplow_II_SHIFT;
  313. i = ((unsigned int) WCuplow[u]) << WCuplow_II_SHIFT;
  314. i = ((unsigned int) WCuplow[WCuplow_II_LEN + i + n])
  315. << WCuplow_TI_SHIFT;
  316. i = ((unsigned int) WCuplow[WCuplow_II_LEN + WCuplow_TI_LEN
  317. + i + sc]) << 1;
  318. wc += WCuplow_diff[i];
  319. }
  320. return wc;
  321. }
  322. #endif /* defined(L_towupper) && defined(__UCLIBC_HAS_XLOCALE__) */
  323. #endif /* SMALL_UPLOW */
  324. #ifdef L_towupper_l
  325. libc_hidden_def(towupper_l)
  326. #endif /* L_towupper_l */
  327. #endif /* __LOCALE_C_ONLY */
  328. #ifndef L_towupper_l
  329. libc_hidden_def(towupper)
  330. #endif
  331. #endif
  332. /**********************************************************************/
  333. #ifdef L_wctype
  334. static const unsigned char typestring[] = __CTYPE_TYPESTRING;
  335. wctype_t wctype(const char *property)
  336. {
  337. const unsigned char *p;
  338. int i;
  339. p = typestring;
  340. i = 1;
  341. do {
  342. if (!strcmp(property, (const char *) ++p)) {
  343. return i;
  344. }
  345. ++i;
  346. p += p[-1];
  347. } while (*p);
  348. /* TODO - Add locale-specific classifications. */
  349. return 0;
  350. }
  351. libc_hidden_def(wctype)
  352. #endif
  353. /**********************************************************************/
  354. #ifdef L_wctype_l
  355. #ifdef __UCLIBC_MJN3_ONLY__
  356. #warning REMINDER: Currently wctype_l simply calls wctype.
  357. #endif /* __UCLIBC_MJN3_ONLY__ */
  358. wctype_t wctype_l (const char *property, __locale_t locale)
  359. {
  360. return wctype(property);
  361. }
  362. libc_hidden_def(wctype_l)
  363. #endif
  364. /**********************************************************************/
  365. #if defined(L_iswctype) || defined(L_iswctype_l)
  366. #define __C_iswdigit(c) \
  367. ((sizeof(c) == sizeof(char)) \
  368. ? (((unsigned char)((c) - '0')) < 10) \
  369. : (((__uwchar_t)((c) - '0')) < 10))
  370. #define __C_iswxdigit(c) \
  371. (__C_iswdigit(c) \
  372. || ((sizeof(c) == sizeof(char)) \
  373. ? (((unsigned char)((((c)) | 0x20) - 'a')) < 6) \
  374. : (((__uwchar_t)((((c)) | 0x20) - 'a')) < 6)))
  375. #ifdef __UCLIBC_MJN3_ONLY__
  376. #ifdef L_iswctype
  377. #warning CONSIDER: Change to bit shift? would need to sync with wctype.h
  378. #endif
  379. #endif /* __UCLIBC_MJN3_ONLY__ */
  380. #ifdef __UCLIBC_HAS_CTYPE_TABLES__
  381. #if !defined(__UCLIBC_HAS_XLOCALE__) || defined(L_iswctype_l)
  382. static const unsigned short int desc2flag[] = {
  383. [_CTYPE_unclassified] = 0,
  384. [_CTYPE_isalnum] = (unsigned short int) _ISwalnum,
  385. [_CTYPE_isalpha] = (unsigned short int) _ISwalpha,
  386. [_CTYPE_isblank] = (unsigned short int) _ISwblank,
  387. [_CTYPE_iscntrl] = (unsigned short int) _ISwcntrl,
  388. [_CTYPE_isdigit] = (unsigned short int) _ISwdigit,
  389. [_CTYPE_isgraph] = (unsigned short int) _ISwgraph,
  390. [_CTYPE_islower] = (unsigned short int) _ISwlower,
  391. [_CTYPE_isprint] = (unsigned short int) _ISwprint,
  392. [_CTYPE_ispunct] = (unsigned short int) _ISwpunct,
  393. [_CTYPE_isspace] = (unsigned short int) _ISwspace,
  394. [_CTYPE_isupper] = (unsigned short int) _ISwupper,
  395. [_CTYPE_isxdigit] = (unsigned short int) _ISwxdigit,
  396. };
  397. #endif /* defined(L_iswctype_L) || defined(__LOCALE_C_ONLY) */
  398. #endif /* __UCLIBC_HAS_CTYPE_TABLES__ */
  399. #ifdef __LOCALE_C_ONLY
  400. #ifdef __UCLIBC_HAS_CTYPE_TABLES__
  401. int iswctype(wint_t wc, wctype_t desc)
  402. {
  403. /* Note... wctype_t is unsigned. */
  404. if ((((__uwchar_t) wc) <= 0x7f)
  405. && (desc < (sizeof(desc2flag)/sizeof(desc2flag[0])))
  406. ) {
  407. return __isctype(wc, desc2flag[desc]);
  408. }
  409. return 0;
  410. }
  411. #else /* __UCLIBC_HAS_CTYPE_TABLES__ */
  412. int iswctype(wint_t wc, wctype_t desc)
  413. {
  414. /* This is lame, but it is here just to get it working for now. */
  415. if (wc == ((unsigned int)(wc))) {
  416. switch(desc) {
  417. case _CTYPE_isupper:
  418. return __C_isupper((unsigned int)(wc));
  419. case _CTYPE_islower:
  420. return __C_islower((unsigned int)(wc));
  421. case _CTYPE_isalpha:
  422. return __C_isalpha((unsigned int)(wc));
  423. case _CTYPE_isdigit:
  424. return __C_isdigit((unsigned int)(wc));
  425. case _CTYPE_isxdigit:
  426. return __C_isxdigit((unsigned int)(wc));
  427. case _CTYPE_isspace:
  428. return __C_isspace((unsigned int)(wc));
  429. case _CTYPE_isprint:
  430. return __C_isprint((unsigned int)(wc));
  431. case _CTYPE_isgraph:
  432. return __C_isgraph((unsigned int)(wc));
  433. case _CTYPE_isblank:
  434. return __C_isblank((unsigned int)(wc));
  435. case _CTYPE_iscntrl:
  436. return __C_iscntrl((unsigned int)(wc));
  437. case _CTYPE_ispunct:
  438. return __C_ispunct((unsigned int)(wc));
  439. case _CTYPE_isalnum:
  440. return __C_isalnum((unsigned int)(wc));
  441. default:
  442. break;
  443. }
  444. }
  445. return 0;
  446. }
  447. #endif /* __UCLIBC_HAS_CTYPE_TABLES__ */
  448. #else /* __LOCALE_C_ONLY */
  449. #ifdef __UCLIBC_MJN3_ONLY__
  450. #ifdef L_iswctype
  451. #warning CONSIDER: Handle combining class?
  452. #endif
  453. #endif /* __UCLIBC_MJN3_ONLY__ */
  454. #ifdef L_iswctype
  455. #define ISWCTYPE(w,d) iswctype(w,d)
  456. #else /* L_iswctype */
  457. #define ISWCTYPE(w,d) iswctype_l(w,d, __locale_t locale)
  458. #undef __UCLIBC_CURLOCALE
  459. #define __UCLIBC_CURLOCALE (locale)
  460. #endif /* L_iswctype */
  461. #if defined(L_iswctype) && defined(__UCLIBC_HAS_XLOCALE__)
  462. int iswctype(wint_t wc, wctype_t desc)
  463. {
  464. return iswctype_l(wc, desc, __UCLIBC_CURLOCALE);
  465. }
  466. #else /* defined(L_iswctype) && defined(__UCLIBC_HAS_XLOCALE__) */
  467. int ISWCTYPE(wint_t wc, wctype_t desc)
  468. {
  469. unsigned int sc, n, i0, i1;
  470. unsigned char d = __CTYPE_unclassified;
  471. if ((ENCODING != __ctype_encoding_7_bit) || (((__uwchar_t) wc) <= 0x7f)){
  472. if (desc < _CTYPE_iswxdigit) {
  473. if (((__uwchar_t) wc) <= WC_TABLE_DOMAIN_MAX) {
  474. /* From here on, we know wc > 0. */
  475. sc = wc & WCctype_TI_MASK;
  476. wc >>= WCctype_TI_SHIFT;
  477. n = wc & WCctype_II_MASK;
  478. wc >>= WCctype_II_SHIFT;
  479. i0 = WCctype[wc];
  480. i0 <<= WCctype_II_SHIFT;
  481. i1 = WCctype[WCctype_II_LEN + i0 + n];
  482. i1 <<= (WCctype_TI_SHIFT-1);
  483. d = WCctype[WCctype_II_LEN + WCctype_TI_LEN + i1 + (sc >> 1)];
  484. d = (sc & 1) ? (d >> 4) : (d & 0xf);
  485. } else if ( ((((__uwchar_t)(wc - 0xe0020UL)) <= 0x5f)
  486. || (wc == 0xe0001UL))
  487. || ( (((__uwchar_t)(wc - 0xf0000UL)) < 0x20000UL)
  488. && ((wc & 0xffffU) <= 0xfffdU))
  489. ) {
  490. d = __CTYPE_punct;
  491. }
  492. #if 0
  493. return ( ((unsigned char)(d - ctype_range[2*desc]))
  494. <= ctype_range[2*desc + 1] )
  495. && ((desc != _CTYPE_iswblank) || (d & 1));
  496. #else
  497. return __UCLIBC_CURLOCALE->code2flag[d] & desc2flag[desc];
  498. #endif
  499. }
  500. #ifdef __UCLIBC_MJN3_ONLY__
  501. #warning TODO: xdigit really needs to be handled better. Remember only for ascii!
  502. #endif /* __UCLIBC_MJN3_ONLY__ */
  503. /* TODO - Add locale-specific classifications. */
  504. return (desc == _CTYPE_iswxdigit) ? __C_iswxdigit(wc) : 0;
  505. }
  506. return 0;
  507. }
  508. #endif /* defined(L_iswctype) && defined(__UCLIBC_HAS_XLOCALE__) */
  509. #ifdef L_iswctype_l
  510. libc_hidden_def(iswctype_l)
  511. #endif /* L_iswctype_l */
  512. #endif /* __LOCALE_C_ONLY */
  513. #ifdef L_iswctype
  514. libc_hidden_def(iswctype)
  515. #endif /* L_iswctype */
  516. #endif
  517. /**********************************************************************/
  518. #if defined(L_towctrans) || defined(L_towctrans_l)
  519. #ifdef __LOCALE_C_ONLY
  520. /* Minimal support for C/POSIX locale. */
  521. wint_t towctrans(wint_t wc, wctrans_t desc)
  522. {
  523. if ((unsigned int)(desc - _CTYPE_tolower) <= (_CTYPE_toupper - _CTYPE_tolower)) {
  524. /* Transliteration is either tolower or toupper. */
  525. /* I think it's wrong: _toupper(c) assumes that c is a *lowercase* *letter* -
  526. * it is defined as ((c) ^ 0x20)!
  527. * if ((__uwchar_t) wc <= 0x7f) {
  528. * return (desc == _CTYPE_tolower) ? _tolower(wc) : _toupper(wc);
  529. * }
  530. */
  531. __uwchar_t c = wc | 0x20; /* lowercase if it's a letter */
  532. if (c >= 'a' && c <= 'z') {
  533. if (desc == _CTYPE_toupper)
  534. c &= ~0x20; /* uppercase */
  535. return c;
  536. }
  537. } else {
  538. __set_errno(EINVAL); /* Invalid transliteration. */
  539. }
  540. return wc;
  541. }
  542. #else /* __LOCALE_C_ONLY */
  543. #ifdef L_towctrans
  544. #define TOWCTRANS(w,d) towctrans(w,d)
  545. #else /* L_towctrans */
  546. #define TOWCTRANS(w,d) towctrans_l(w,d, __locale_t locale)
  547. #undef __UCLIBC_CURLOCALE
  548. #define __UCLIBC_CURLOCALE (locale)
  549. #endif /* L_towctrans */
  550. #ifdef __UCLIBC_HAS_XLOCALE__
  551. #define TOWLOWER(w,l) towlower_l(w,l)
  552. #define TOWUPPER(w,l) towupper_l(w,l)
  553. #else /* __UCLIBC_HAS_XLOCALE__ */
  554. #define TOWLOWER(w,l) towlower(w)
  555. #define TOWUPPER(w,l) towupper(w)
  556. #endif /* __UCLIBC_HAS_XLOCALE__ */
  557. #if defined(L_towctrans) && defined(__UCLIBC_HAS_XLOCALE__)
  558. wint_t towctrans(wint_t wc, wctrans_t desc)
  559. {
  560. return towctrans_l(wc, desc, __UCLIBC_CURLOCALE);
  561. }
  562. #else /* defined(L_towctrans) && defined(__UCLIBC_HAS_XLOCALE__) */
  563. #ifdef SMALL_UPLOW
  564. wint_t TOWCTRANS(wint_t wc, wctrans_t desc)
  565. {
  566. unsigned int sc, n, i;
  567. __uwchar_t u = wc;
  568. /* TODO - clean up */
  569. if (ENCODING == __ctype_encoding_7_bit) {
  570. if ((((__uwchar_t) wc) > 0x7f)
  571. || (((unsigned int)(desc - _CTYPE_tolower))
  572. > (_CTYPE_toupper - _CTYPE_tolower))
  573. ){
  574. /* We're in the C/POSIX locale, so ignore non-ASCII values
  575. * as well an any mappings other than toupper or tolower. */
  576. return wc;
  577. }
  578. }
  579. if (((unsigned int)(desc - _CTYPE_tolower))
  580. <= (_CTYPE_totitle - _CTYPE_tolower)
  581. ) {
  582. if (u <= WC_TABLE_DOMAIN_MAX) {
  583. sc = u & ((1 << WCuplow_TI_SHIFT) - 1);
  584. u >>= WCuplow_TI_SHIFT;
  585. n = u & ((1 << WCuplow_II_SHIFT) - 1);
  586. u >>= WCuplow_II_SHIFT;
  587. i = ((unsigned int) WCuplow[u]) << WCuplow_II_SHIFT;
  588. i = ((unsigned int) WCuplow[WCuplow_II_LEN + i + n])
  589. << WCuplow_TI_SHIFT;
  590. i = ((unsigned int) WCuplow[WCuplow_II_LEN + WCuplow_TI_LEN
  591. + i + sc]) << 1;
  592. if (desc == _CTYPE_tolower) {
  593. ++i;
  594. }
  595. wc += WCuplow_diff[i];
  596. if (desc == _CTYPE_totitle) {
  597. #ifdef __UCLIBC_MJN3_ONLY__
  598. #warning TODO: Verify totitle special cases!
  599. #endif /* __UCLIBC_MJN3_ONLY__ */
  600. /* WARNING! These special cases work for glibc 2.2.4. Changes
  601. * may be needed if the glibc locale tables are updated. */
  602. if ( (((__uwchar_t)(wc - 0x1c4)) <= (0x1cc - 0x1c4))
  603. || (wc == 0x1f1)
  604. ) {
  605. ++wc;
  606. }
  607. }
  608. }
  609. } else {
  610. /* TODO - Deal with other transliterations. */
  611. __set_errno(EINVAL);
  612. }
  613. return wc;
  614. }
  615. #else /* SMALL_UPLOW */
  616. wint_t TOWCTRANS(wint_t wc, wctrans_t desc)
  617. {
  618. if (ENCODING == __ctype_encoding_7_bit) {
  619. if ((((__uwchar_t) wc) > 0x7f)
  620. || (((unsigned int)(desc - _CTYPE_tolower))
  621. > (_CTYPE_toupper - _CTYPE_tolower))
  622. ){
  623. /* We're in the C/POSIX locale, so ignore non-ASCII values
  624. * as well an any mappings other than toupper or tolower. */
  625. return wc;
  626. }
  627. }
  628. if (desc == _CTYPE_tolower) {
  629. return TOWLOWER(wc, __UCLIBC_CURLOCALE);
  630. } else if (((unsigned int)(desc - _CTYPE_toupper))
  631. <= (_CTYPE_totitle - _CTYPE_toupper)
  632. ) {
  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 /* __UCLIBC_MJN3_ONLY__ */
  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 /* L_towctrans_l */
  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 /* __UCLIBC_MJN3_ONLY__ */
  688. wctrans_t wctrans_l(const char *property, __locale_t locale)
  689. {
  690. return wctrans(property);
  691. }
  692. #endif
  693. /**********************************************************************/