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