wctype.c 24 KB

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