_collate.c 17 KB

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