_wctype.c 23 KB

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