_collate.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688
  1. /*
  2. * Copyright (C) 2002 Manuel Novoa III
  3. * Copyright (C) 2000-2005 Erik Andersen <andersen@uclibc.org>
  4. *
  5. * Licensed under the LGPL v2.1, see the file COPYING.LIB in this tarball.
  6. */
  7. /* Dec 20, 2002
  8. * Initial test implementation of strcoll, strxfrm, wcscoll, and wcsxfrm.
  9. * The code needs to be cleaned up a good bit, but I'd like to see people
  10. * test it out.
  11. *
  12. */
  13. #include "_string.h"
  14. #include <ctype.h>
  15. #include <locale.h>
  16. #include <stdlib.h>
  17. #include <errno.h>
  18. #include <assert.h>
  19. libc_hidden_proto(memset)
  20. libc_hidden_proto(memcpy)
  21. libc_hidden_proto(strlcpy)
  22. libc_hidden_proto(strcmp)
  23. #ifdef WANT_WIDE
  24. libc_hidden_proto(wcsxfrm)
  25. libc_hidden_proto(wcscmp)
  26. #endif
  27. #ifdef __UCLIBC_HAS_LOCALE__
  28. #if defined(L_strxfrm) || defined(L_strxfrm_l) || defined(L_wcsxfrm) || defined(L_wcsxfrm_l)
  29. #ifdef L_strxfrm
  30. #ifndef WANT_WIDE
  31. #error WANT_WIDE should be defined for L_strxfrm
  32. #endif
  33. #ifdef L_wcsxfrm
  34. #error L_wcsxfrm already defined for L_strxfrm
  35. #endif
  36. #endif /* L_strxfrm */
  37. #if defined(L_strxfrm) || defined(L_strxfrm_l)
  38. #define wcscoll strcoll
  39. #define wcscoll_l strcoll_l
  40. #define wcsxfrm strxfrm
  41. #define wcsxfrm_l strxfrm_l
  42. #undef WANT_WIDE
  43. #undef Wvoid
  44. #undef Wchar
  45. #undef Wuchar
  46. #undef Wint
  47. #define Wchar char
  48. #endif /* defined(L_strxfrm) || defined(L_strxfrm_l) */
  49. libc_hidden_proto(__global_locale)
  50. #if defined(__UCLIBC_HAS_XLOCALE__) && !defined(__UCLIBC_DO_XLOCALE)
  51. libc_hidden_proto(wcscoll_l)
  52. libc_hidden_proto(wcscoll)
  53. int wcscoll (const Wchar *s0, const Wchar *s1)
  54. {
  55. return wcscoll_l(s0, s1, __UCLIBC_CURLOCALE );
  56. }
  57. libc_hidden_def(wcscoll)
  58. libc_hidden_proto(wcsxfrm_l)
  59. libc_hidden_proto(wcsxfrm)
  60. size_t wcsxfrm(Wchar *__restrict ws1, const Wchar *__restrict ws2, size_t n)
  61. {
  62. return wcsxfrm_l(ws1, ws2, n, __UCLIBC_CURLOCALE );
  63. }
  64. libc_hidden_def(wcsxfrm)
  65. #else /* defined(__UCLIBC_HAS_XLOCALE__) && !defined(__UCLIBC_DO_XLOCALE) */
  66. #if 0
  67. #define CUR_COLLATE (&__UCLIBC_CURLOCALE_DATA.collate)
  68. #else
  69. #define CUR_COLLATE (& __LOCALE_PTR->collate)
  70. #endif
  71. #define MAX_PENDING 8
  72. typedef struct {
  73. const Wchar *s;
  74. const Wchar *eob; /* end of backward */
  75. __uwchar_t weight;
  76. __uwchar_t ui_weight; /* undefined or invalid */
  77. int colitem;
  78. int weightidx;
  79. int rule;
  80. size_t position;
  81. /* should be wchar_t. if wchar < 0 do EILSEQ? */
  82. __uwchar_t *cip;
  83. __uwchar_t ci_pending[MAX_PENDING]; /* nul-terminated */
  84. char *back_buf;
  85. char *bbe; /* end of back_buf (actual last... not 1 past end) */
  86. char *bp; /* ptr into backbuf, NULL if not in backward mode */
  87. char ibb[128];
  88. size_t bb_size;
  89. int ru_pushed;
  90. } col_state_t;
  91. #define WEIGHT_MASK 0x3fffU
  92. #define RULE_MASK 0xc000U
  93. #define RULE_FORWARD (1 << 14)
  94. #define RULE_POSITION (1 << 15)
  95. #define UI_IDX (WEIGHT_MASK-6)
  96. #define POSIT_IDX (WEIGHT_MASK-5)
  97. #define RANGE_IDX (WEIGHT_MASK-4)
  98. #define UNDEF_IDX (WEIGHT_MASK-3)
  99. #define INVAL_IDX (WEIGHT_MASK-2)
  100. #define DITTO_IDX (WEIGHT_MASK-1)
  101. #undef TRACE
  102. #if 0
  103. #define TRACE(X) printf X
  104. #else
  105. #define TRACE(X) ((void)0)
  106. #endif
  107. static int lookup(wchar_t wc __LOCALE_PARAM )
  108. {
  109. unsigned int sc, n, i0, i1;
  110. if (((__uwchar_t) wc) > 0xffffU) {
  111. return 0;
  112. }
  113. sc = wc & CUR_COLLATE->ti_mask;
  114. wc >>= CUR_COLLATE->ti_shift;
  115. n = wc & CUR_COLLATE->ii_mask;
  116. wc >>= CUR_COLLATE->ii_shift;
  117. i0 = CUR_COLLATE->wcs2colidt_tbl[wc];
  118. i0 <<= CUR_COLLATE->ii_shift;
  119. i1 = CUR_COLLATE->wcs2colidt_tbl[CUR_COLLATE->ii_len + i0 + n];
  120. i1 <<= CUR_COLLATE->ti_shift;
  121. return CUR_COLLATE->wcs2colidt_tbl[CUR_COLLATE->ii_len + CUR_COLLATE->ti_len + i1 + sc];
  122. }
  123. static void init_col_state(col_state_t *cs, const Wchar *wcs)
  124. {
  125. memset(cs, 0, sizeof(col_state_t));
  126. cs->s = wcs;
  127. cs->bp = cs->back_buf = cs->ibb;
  128. cs->bb_size = 128;
  129. cs->bbe = cs->back_buf + (cs->bb_size -1);
  130. }
  131. static void next_weight(col_state_t *cs, int pass __LOCALE_PARAM )
  132. {
  133. int r, w, ru, ri, popping_backup_stack;
  134. ssize_t n;
  135. const uint16_t *p;
  136. #ifdef WANT_WIDE
  137. #define WC (*cs->s)
  138. #define N (1)
  139. #else /* WANT_WIDE */
  140. wchar_t WC;
  141. size_t n0, nx;
  142. #define N n0
  143. #endif /* WANT_WIDE */
  144. do {
  145. if (cs->ru_pushed) {
  146. ru = cs->ru_pushed;
  147. TRACE(("ru_pushed = %d\n", ru));
  148. cs->ru_pushed = 0;
  149. goto POSITION_SKIP;
  150. }
  151. #ifdef __UCLIBC_MJN3_ONLY__
  152. #warning should we walk pendings backwards?
  153. #endif
  154. if (cs->cip) { /* possible pending weight */
  155. if ((r = *(cs->cip++)) == 0) {
  156. cs->cip = NULL;
  157. continue;
  158. }
  159. cs->weightidx = r & WEIGHT_MASK;
  160. assert(cs->weightidx);
  161. /* assert(cs->weightidx != WEIGHT_MASK); */
  162. } else { /* get the next collation item from the string */
  163. TRACE(("clearing popping flag\n"));
  164. popping_backup_stack = 0;
  165. IGNORE_LOOP:
  166. /* keep first pos as 0 for a sentinal */
  167. if (*cs->bp) { /* pending backward chars */
  168. POP_BACKUP:
  169. popping_backup_stack = 1;
  170. TRACE(("setting popping flag\n"));
  171. n = 0;
  172. if (*cs->bp > 0) { /* singles pending */
  173. cs->s -= 1;
  174. if ((*cs->bp -= 1) == 0) {
  175. cs->bp -= 1;
  176. }
  177. } else { /* last was a multi */
  178. cs->s += *cs->bp;
  179. cs->bp -= 1;
  180. }
  181. } else if (!*cs->s) { /* not in backward mode and end of string */
  182. cs->weight = 0;
  183. return;
  184. } else {
  185. cs->position += 1;
  186. }
  187. BACK_LOOP:
  188. #ifdef WANT_WIDE
  189. n = 1;
  190. cs->colitem = r = lookup(*cs->s __LOCALE_ARG );
  191. #else /* WANT_WIDE */
  192. n = n0 = __locale_mbrtowc_l(&WC, cs->s, __LOCALE_PTR);
  193. if (n < 0) {
  194. __set_errno(EILSEQ);
  195. cs->weight = 0;
  196. return;
  197. }
  198. cs->colitem = r = lookup(WC __LOCALE_ARG );
  199. #endif /* WANT_WIDE */
  200. TRACE((" r=%d WC=%#lx\n", r, (unsigned long)(WC)));
  201. if (r > CUR_COLLATE->max_col_index) { /* starting char for one or more sequences */
  202. p = CUR_COLLATE->multistart_tbl;
  203. p += p[r-CUR_COLLATE->max_col_index -1];
  204. do {
  205. n = N;
  206. r = *p++;
  207. do {
  208. if (!*p) { /* found it */
  209. cs->colitem = r;
  210. TRACE((" found multi %d\n", n));
  211. goto FOUND;
  212. }
  213. #ifdef WANT_WIDE
  214. /* the lookup check here is safe since we're assured that *p is a valid colidx */
  215. if (!cs->s[n] || (lookup(cs->s[n] __LOCALE_ARG ) != *p)) {
  216. do {} while (*p++);
  217. break;
  218. }
  219. ++p;
  220. ++n;
  221. #else /* WANT_WIDE */
  222. if (cs->s[n]) {
  223. nx = __locale_mbrtowc_l(&WC, cs->s + n, __LOCALE_PTR);
  224. if (nx < 0) {
  225. __set_errno(EILSEQ);
  226. cs->weight = 0;
  227. return;
  228. }
  229. }
  230. if (!cs->s[n] || (lookup(WC __LOCALE_ARG ) != *p)) {
  231. do {} while (*p++);
  232. break;
  233. }
  234. ++p;
  235. n += nx; /* Only gets here if cs->s[n] != 0, so nx is set. */
  236. #endif /* WANT_WIDE */
  237. } while (1);
  238. } while (1);
  239. } else if (r == 0) { /* illegal, undefined, or part of a range */
  240. if ((CUR_COLLATE->range_count)
  241. #ifdef __UCLIBC_MJN3_ONLY__
  242. #warning .. need to introduce range as a collating item?
  243. #endif
  244. && (((__uwchar_t)(WC - CUR_COLLATE->range_low)) <= CUR_COLLATE->range_count)
  245. ) { /* part of a range */
  246. /* Note: cs->colitem = 0 already. */
  247. TRACE((" found range\n"));
  248. ru = CUR_COLLATE->ruletable[CUR_COLLATE->range_rule_offset*CUR_COLLATE->MAX_WEIGHTS + pass];
  249. assert((ru & WEIGHT_MASK) != DITTO_IDX);
  250. if ((ru & WEIGHT_MASK) == WEIGHT_MASK) {
  251. ru = (ru & RULE_MASK) | RANGE_IDX;
  252. cs->weight = CUR_COLLATE->range_base_weight + (WC - CUR_COLLATE->range_low);
  253. }
  254. goto RANGE_SKIP_TO;
  255. } else if (((__uwchar_t)(WC)) <= 0x7fffffffUL) { /* legal but undefined */
  256. UNDEFINED:
  257. /* Note: cs->colitem = 0 already. */
  258. ri = CUR_COLLATE->undefined_idx;
  259. assert(ri != 0); /* implicit undefined isn't supported */
  260. TRACE((" found explicit UNDEFINED\n"));
  261. #ifdef __UCLIBC_MJN3_ONLY__
  262. #warning right now single weight locales do not support ..
  263. #endif
  264. if (CUR_COLLATE->num_weights == 1) {
  265. TRACE((" single weight UNDEFINED\n"));
  266. cs->weightidx = RANGE_IDX;
  267. cs->weight = ri;
  268. cs->s += n;
  269. goto PROCESS_WEIGHT;
  270. }
  271. ri = CUR_COLLATE->index2ruleidx[ri - 1];
  272. ru = CUR_COLLATE->ruletable[ri * CUR_COLLATE->MAX_WEIGHTS + pass];
  273. assert((ru & WEIGHT_MASK) != WEIGHT_MASK); /* TODO: handle ".." */
  274. if ((ru & WEIGHT_MASK) == DITTO_IDX) {
  275. cs->colitem = CUR_COLLATE->undefined_idx;
  276. }
  277. goto RANGE_SKIP_TO;
  278. } else { /* illegal */
  279. TRACE((" found illegal\n"));
  280. __set_errno(EINVAL);
  281. /* We put all illegals in the same equiv class with maximal weight,
  282. * and ignore them after the first pass. */
  283. if (pass > 0) {
  284. cs->s += n;
  285. goto IGNORE_LOOP;
  286. }
  287. ru = (RULE_FORWARD | RANGE_IDX);
  288. cs->weight = 0xffffU;
  289. goto RANGE_SKIP_TO;
  290. }
  291. } else if (CUR_COLLATE->num_weights == 1) {
  292. TRACE((" single weight\n"));
  293. cs->weightidx = RANGE_IDX;
  294. cs->weight = cs->colitem;
  295. cs->s += n;
  296. goto PROCESS_WEIGHT;
  297. } else {
  298. TRACE((" normal\n"));
  299. }
  300. /* if we get here, it is a normal char either singlely weighted, undefined, or in a range */
  301. FOUND:
  302. ri = CUR_COLLATE->index2ruleidx[cs->colitem - 1];
  303. TRACE((" ri=%d ", ri));
  304. #ifdef __UCLIBC_MJN3_ONLY__
  305. #warning make sure this is correct
  306. #endif
  307. if (!ri) {
  308. TRACE(("NOT IN THIS LOCALE\n"));
  309. goto UNDEFINED;
  310. }
  311. ru = CUR_COLLATE->ruletable[ri * CUR_COLLATE->MAX_WEIGHTS + pass];
  312. RANGE_SKIP_TO:
  313. #ifdef __UCLIBC_MJN3_ONLY__
  314. #warning ignoreables probably should not interrupt backwards processing, but this is wrong
  315. #endif
  316. /* if (!(ru & WEIGHT_MASK)) { */
  317. /* TRACE(("IGNORE\n")); */
  318. /* cs->s += n; */
  319. /* continue; */
  320. /* } */
  321. TRACE((" rule = %#x weight = %#x popping = %d s = %p eob = %p\n",
  322. ru & RULE_MASK, ru & WEIGHT_MASK, popping_backup_stack,
  323. cs->s, cs->eob));
  324. /* now we need to check if we're going backwards... */
  325. if (!popping_backup_stack) {
  326. if (!(ru & RULE_MASK)) { /* backward */
  327. TRACE(("backwards\n"));
  328. assert(cs->bp <= cs->bbe);
  329. if (cs->bp == cs->bbe) {
  330. if (cs->back_buf == cs->ibb) { /* was using internal buffer */
  331. cs->bp = malloc(cs->bb_size + 128);
  332. if (!cs->bp) {
  333. __set_errno(ENOMEM);
  334. #ifdef __UCLIBC_MJN3_ONLY__
  335. #warning what to do here?
  336. #endif
  337. cs->weight = 0;
  338. return;
  339. }
  340. memcpy(cs->bp, cs->back_buf, cs->bb_size);
  341. } else {
  342. cs->bp = realloc(cs->back_buf, cs->bb_size + 128);
  343. if (!cs->bp) {
  344. __set_errno(ENOMEM);
  345. #ifdef __UCLIBC_MJN3_ONLY__
  346. #warning what to do here?
  347. #endif
  348. cs->weight = 0;
  349. return;
  350. }
  351. }
  352. cs->bb_size += 128;
  353. cs->bbe = cs->bp + (cs->bbe - cs->back_buf);
  354. cs->back_buf = cs->bp;
  355. cs->bp = cs->bbe;
  356. }
  357. if (n==1) { /* single char */
  358. if (*cs->bp && (((unsigned char)(*cs->bp)) < CHAR_MAX)) {
  359. *cs->bp += 1; /* increment last single's count */
  360. } else { /* last was a multi, or just starting */
  361. if (!cs->bp) {
  362. cs->bp = cs->back_buf;
  363. } else {
  364. assert(cs->bp < cs->bbe);
  365. ++cs->bp;
  366. }
  367. *cs->bp = 1;
  368. }
  369. } else { /* multichar */
  370. assert(n>1);
  371. assert(cs->bp < cs->bbe);
  372. *++cs->bp = -n;
  373. }
  374. cs->s += n;
  375. if (*cs->s) {
  376. goto BACK_LOOP;
  377. }
  378. /* end-of-string so start popping */
  379. cs->eob = cs->s;
  380. TRACE(("popping\n"));
  381. goto POP_BACKUP;
  382. } else if (*cs->bp) { /* was going backward but this element isn't */
  383. /* discard current and use previous backward element */
  384. assert(!cs->cip);
  385. cs->eob = cs->s;
  386. TRACE(("popping\n"));
  387. goto POP_BACKUP;
  388. } else { /* was and still going forward */
  389. TRACE(("forwards\n"));
  390. if ((ru & (RULE_POSITION|WEIGHT_MASK)) > RULE_POSITION) {
  391. assert(ru & WEIGHT_MASK);
  392. cs->ru_pushed = ru;
  393. cs->weight = cs->position;
  394. #ifdef __UCLIBC_MJN3_ONLY__
  395. #warning devel code
  396. #endif
  397. cs->position = 0; /* reset to reduce size for strcoll? */
  398. cs->s += n;
  399. cs->weightidx = RANGE_IDX;
  400. goto PROCESS_WEIGHT;
  401. }
  402. }
  403. } else { /* popping backwards stack */
  404. TRACE(("popping (continued)\n"));
  405. if (!*cs->bp) {
  406. cs->s = cs->eob;
  407. }
  408. cs->s -= n;
  409. }
  410. cs->s += n;
  411. POSITION_SKIP:
  412. cs->weightidx = ru & WEIGHT_MASK;
  413. cs->rule = ru & RULE_MASK;
  414. }
  415. #ifdef __UCLIBC_MJN3_ONLY__
  416. #warning for pending we only want the weight... _not_ the rule
  417. #endif
  418. if (!cs->weightidx) { /* ignore */
  419. continue;
  420. }
  421. PROCESS_WEIGHT:
  422. assert(cs->weightidx);
  423. if (((unsigned int)(cs->weightidx - UI_IDX)) <= (INVAL_IDX-UI_IDX)) {
  424. if (cs->weightidx == UI_IDX) {
  425. cs->weight = cs->ui_weight;
  426. }
  427. return;
  428. }
  429. assert(cs->weightidx != WEIGHT_MASK);
  430. if (cs->weightidx == DITTO_IDX) { /* want the weight of the current collating item */
  431. TRACE(("doing ditto\n"));
  432. w = CUR_COLLATE->index2weight[cs->colitem -1];
  433. } else if (cs->weightidx <= CUR_COLLATE->max_col_index) { /* normal */
  434. TRACE(("doing normal\n"));
  435. w = CUR_COLLATE->index2weight[cs->weightidx -1];
  436. } else { /* a string */
  437. TRACE(("doing string\n"));
  438. assert(!(cs->weightidx & RULE_MASK));
  439. /* note: iso14561 allows null string here */
  440. p = CUR_COLLATE->weightstr + (cs->weightidx - (CUR_COLLATE->max_col_index + 2));
  441. if (*p & WEIGHT_MASK) {
  442. r = 0;
  443. do {
  444. assert(r < MAX_PENDING);
  445. cs->ci_pending[r++] = *p++;
  446. } while (*p & WEIGHT_MASK);
  447. cs->cip = cs->ci_pending;
  448. }
  449. continue;
  450. }
  451. cs->weight = w;
  452. return;
  453. } while (1);
  454. }
  455. libc_hidden_proto(__XL_NPP(wcscoll))
  456. int __XL_NPP(wcscoll) (const Wchar *s0, const Wchar *s1 __LOCALE_PARAM )
  457. {
  458. col_state_t ws[2];
  459. int pass;
  460. if (!CUR_COLLATE->num_weights) { /* C locale */
  461. #ifdef WANT_WIDE
  462. return wcscmp(s0, s1);
  463. #else /* WANT_WIDE */
  464. return strcmp(s0, s1);
  465. #endif /* WANT_WIDE */
  466. }
  467. pass = 0;
  468. do { /* loop through the weights levels */
  469. init_col_state(ws, s0);
  470. init_col_state(ws+1, s1);
  471. do { /* loop through the strings */
  472. /* for each string, get the next weight */
  473. next_weight(ws, pass __LOCALE_ARG );
  474. next_weight(ws+1, pass __LOCALE_ARG );
  475. TRACE(("w0=%lu w1=%lu\n",
  476. (unsigned long) ws[0].weight,
  477. (unsigned long) ws[1].weight));
  478. if (ws[0].weight != ws[1].weight) {
  479. return ws[0].weight - ws[1].weight;
  480. }
  481. } while (ws[0].weight);
  482. } while (++pass < CUR_COLLATE->num_weights);
  483. return 0;
  484. }
  485. libc_hidden_def(__XL_NPP(wcscoll))
  486. #ifdef WANT_WIDE
  487. extern size_t __wcslcpy(wchar_t *__restrict dst,
  488. const wchar_t *__restrict src, size_t n);
  489. libc_hidden_proto(__XL_NPP(wcsxfrm))
  490. size_t __XL_NPP(wcsxfrm)(wchar_t *__restrict ws1, const wchar_t *__restrict ws2,
  491. size_t n __LOCALE_PARAM )
  492. {
  493. col_state_t cs;
  494. size_t count;
  495. int pass;
  496. if (!CUR_COLLATE->num_weights) { /* C locale */
  497. return __wcslcpy(ws1, ws2, n);
  498. }
  499. #ifdef __UCLIBC_MJN3_ONLY__
  500. #warning handle empty string as a special case
  501. #endif
  502. count = pass = 0;
  503. do { /* loop through the weights levels */
  504. init_col_state(&cs, ws2);
  505. do { /* loop through the string */
  506. next_weight(&cs, pass __LOCALE_ARG );
  507. TRACE(("weight=%lu (%#lx)\n", (unsigned long) cs.weight, (unsigned long) cs.weight));
  508. if (count < n) {
  509. ws1[count] = cs.weight +1;
  510. }
  511. ++count;
  512. TRACE(("--------------------------------------------\n"));
  513. } while (cs.weight);
  514. if (count <= n) { /* overwrite the trailing 0 end-of-pass marker */
  515. ws1[count-1] = 1;
  516. }
  517. TRACE(("-------------------- pass %d --------------------\n", pass));
  518. } while (++pass < CUR_COLLATE->num_weights);
  519. if (count <= n) { /* oops... change it back */
  520. ws1[count-1] = 0;
  521. }
  522. return count-1;
  523. }
  524. libc_hidden_def(__XL_NPP(wcsxfrm))
  525. #else /* WANT_WIDE */
  526. static const unsigned long bound[] = {
  527. 1UL << 7,
  528. 1UL << 11,
  529. 1UL << 16,
  530. 1UL << 21,
  531. 1UL << 26,
  532. };
  533. static unsigned char first[] = {
  534. 0x0, 0xc0, 0xe0, 0xf0, 0xf8, 0xfc
  535. };
  536. /* Use an extension of UTF-8 to store a 32 bit val in max 6 bytes. */
  537. static size_t store(unsigned char *s, size_t count, size_t n, __uwchar_t weight)
  538. {
  539. int i, r;
  540. i = 0;
  541. do {
  542. if (weight < bound[i]) {
  543. break;
  544. }
  545. } while (++i < sizeof(bound)/sizeof(bound[0]));
  546. r = i+1;
  547. if (i + count < n) {
  548. s += count;
  549. s[0] = first[i];
  550. while (i) {
  551. s[i] = 0x80 | (weight & 0x3f);
  552. weight >>= 6;
  553. --i;
  554. }
  555. s[0] |= weight;
  556. }
  557. return r;
  558. }
  559. libc_hidden_proto(__XL_NPP(strxfrm))
  560. size_t __XL_NPP(strxfrm)(char *__restrict ws1, const char *__restrict ws2, size_t n
  561. __LOCALE_PARAM )
  562. {
  563. col_state_t cs;
  564. size_t count, inc;
  565. int pass;
  566. if (!CUR_COLLATE->num_weights) { /* C locale */
  567. return strlcpy(ws1, ws2, n);
  568. }
  569. #ifdef __UCLIBC_MJN3_ONLY__
  570. #warning handle empty string as a special case
  571. #endif
  572. inc = count = pass = 0;
  573. do { /* loop through the weights levels */
  574. init_col_state(&cs, ws2);
  575. do { /* loop through the string */
  576. next_weight(&cs, pass __LOCALE_ARG );
  577. TRACE(("weight=%lu (%#lx)\n", (unsigned long) cs.weight, (unsigned long) cs.weight));
  578. inc = store((unsigned char *)ws1, count, n, cs.weight + 1);
  579. count += inc;
  580. TRACE(("--------------------------------------------\n"));
  581. } while (cs.weight);
  582. /* overwrite the trailing 0 end-of-pass marker */
  583. assert(inc == 1);
  584. if (count <= n) {
  585. ws1[count-1] = 1;
  586. }
  587. TRACE(("-------------------- pass %d --------------------\n", pass));
  588. } while (++pass < CUR_COLLATE->num_weights);
  589. if (count <= n) { /* oops... change it back */
  590. ws1[count-1] = 0;
  591. }
  592. return count-1;
  593. }
  594. libc_hidden_def(__XL_NPP(strxfrm))
  595. #endif /* WANT_WIDE */
  596. #endif /* defined(__UCLIBC_HAS_XLOCALE__) && !defined(__UCLIBC_DO_XLOCALE) */
  597. #endif /* defined(L_strxfrm) || defined(L_strxfrm_l) || defined(L_wcsxfrm) || defined(L_wcsxfrm_l) */
  598. #endif /* __UCLIBC_HAS_LOCALE__ */
  599. /**********************************************************************/