wctype.c 24 KB

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