wctype.c 23 KB

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