_wctype.c 24 KB

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