wctype.c 24 KB

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