wctype.c 24 KB

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