_collate.c 17 KB

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