wctype.c 24 KB

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