_collate.c 17 KB

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