wctype.c 25 KB

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