1 --- collate.c.bsdnew 2009-11-09 15:05:25.000000000 -0800
2 +++ collate.c 2009-11-09 15:20:20.000000000 -0800
5 __FBSDID("$FreeBSD: src/lib/libc/locale/collate.c,v 1.35 2005/02/27 20:31:13 ru Exp $");
7 +#include "xlocale_private.h"
8 +/* assumes the locale_t variable is named loc */
9 +#define __collate_chain_equiv_table (loc->__lc_collate->__chain_equiv_table)
10 +#define __collate_chain_pri_table (loc->__lc_collate->__chain_pri_table)
11 +#define __collate_char_pri_table (loc->__lc_collate->__char_pri_table)
12 +#define __collate_info (&loc->__lc_collate->__info)
13 +#define __collate_large_char_pri_table (loc->__lc_collate->__large_char_pri_table)
14 +#define __collate_substitute_table (loc->__lc_collate->__substitute_table)
16 #include "namespace.h"
17 #include <arpa/inet.h>
27 #include "un-namespace.h"
30 @@ -44,36 +56,50 @@ __FBSDID("$FreeBSD: src/lib/libc/locale/
32 #include "libc_private.h"
34 -int __collate_load_error = 1;
35 -int __collate_substitute_nontrivial;
37 -u_char __collate_substitute_table[UCHAR_MAX + 1][STR_LEN];
38 -struct __collate_st_char_pri __collate_char_pri_table[UCHAR_MAX + 1];
39 -struct __collate_st_chain_pri *__collate_chain_pri_table;
41 +#if __DARWIN_BYTE_ORDER == __DARWIN_LITTLE_ENDIAN
42 +static void wntohl(wchar_t *, int);
43 +#endif /* __DARWIN_BYTE_ORDER == __DARWIN_LITTLE_ENDIAN */
44 void __collate_err(int ex, const char *f) __dead2;
47 -__collate_load_tables(const char *encoding)
49 + * Normally, the __collate_* routines should all be __private_extern__,
50 + * but grep is using them (3715846). Until we can provide an alternative,
51 + * we leave them public, and provide a read-only __collate_load_error variable
53 +#undef __collate_load_error
54 +int __collate_load_error = 1;
56 +__private_extern__ int
57 +__collate_load_tables(const char *encoding, locale_t loc)
60 - int i, saverr, chains;
62 + int i, saverr, chains, z;
63 char strbuf[STR_LEN], buf[PATH_MAX];
64 - void *TMP_substitute_table, *TMP_char_pri_table, *TMP_chain_pri_table;
65 - static char collate_encoding[ENCODING_LEN + 1];
66 + struct __xlocale_st_collate *TMP;
67 + static struct __xlocale_st_collate *cache = NULL;
68 + struct __collate_st_info info;
71 /* 'encoding' must be already checked. */
72 if (strcmp(encoding, "C") == 0 || strcmp(encoding, "POSIX") == 0) {
73 - __collate_load_error = 1;
74 + loc->__collate_load_error = 1;
75 + if (loc == &__global_locale)
76 + __collate_load_error = 1;
77 + XL_RELEASE(loc->__lc_collate);
78 + loc->__lc_collate = NULL;
83 * If the locale name is the same as our cache, use the cache.
85 - if (strcmp(encoding, collate_encoding) == 0) {
86 - __collate_load_error = 0;
87 + if (cache && strcmp(encoding, cache->__encoding) == 0) {
88 + loc->__collate_load_error = 0;
89 + if (loc == &__global_locale)
90 + __collate_load_error = 0;
91 + XL_RELEASE(loc->__lc_collate);
92 + loc->__lc_collate = cache;
93 + XL_RETAIN(loc->__lc_collate);
97 @@ -97,9 +123,7 @@ __collate_load_tables(const char *encodi
101 - if (strcmp(strbuf, COLLATE_VERSION) == 0)
103 - else if (strcmp(strbuf, COLLATE_VERSION1_2) == 0)
104 + if (strcmp(strbuf, COLLATE_VERSION1_1A) == 0)
108 @@ -107,13 +131,21 @@ __collate_load_tables(const char *encodi
112 - if (fread(&u32, sizeof(u32), 1, fp) != 1) {
113 + if (fread(&info, sizeof(info), 1, fp) != 1) {
119 - if ((chains = (int)ntohl(u32)) < 1) {
120 +#if __DARWIN_BYTE_ORDER == __DARWIN_LITTLE_ENDIAN
121 + for(z = 0; z < info.directive_count; z++) {
122 + info.undef_pri[z] = ntohl(info.undef_pri[z]);
123 + info.subst_count[z] = ntohl(info.subst_count[z]);
125 + info.chain_count = ntohl(info.chain_count);
126 + info.large_pri_count = ntohl(info.large_pri_count);
127 +#endif /* __DARWIN_BYTE_ORDER == __DARWIN_LITTLE_ENDIAN */
128 + if ((chains = info.chain_count) < 0) {
132 @@ -121,143 +153,446 @@ __collate_load_tables(const char *encodi
136 - if ((TMP_substitute_table =
137 - malloc(sizeof(__collate_substitute_table))) == NULL) {
138 + i = sizeof(struct __xlocale_st_collate)
139 + + sizeof(struct __collate_st_chain_pri) * chains
140 + + sizeof(struct __collate_st_large_char_pri) * info.large_pri_count;
141 + for(z = 0; z < info.directive_count; z++)
142 + i += sizeof(struct __collate_st_subst) * info.subst_count[z];
143 + if ((TMP = (struct __xlocale_st_collate *)malloc(i)) == NULL) {
149 - if ((TMP_char_pri_table =
150 - malloc(sizeof(__collate_char_pri_table))) == NULL) {
152 - free(TMP_substitute_table);
155 - return (_LDP_ERROR);
157 - if ((TMP_chain_pri_table =
158 - malloc(sizeof(*__collate_chain_pri_table) * chains)) == NULL) {
160 - free(TMP_substitute_table);
161 - free(TMP_char_pri_table);
164 - return (_LDP_ERROR);
166 + TMP->__refcount = 2; /* one for the locale, one for the cache */
167 + TMP->__free_extra = NULL;
169 #define FREAD(a, b, c, d) \
171 if (fread(a, b, c, d) != c) { \
173 - free(TMP_substitute_table); \
174 - free(TMP_char_pri_table); \
175 - free(TMP_chain_pri_table); \
179 return (_LDP_ERROR); \
183 - FREAD(TMP_substitute_table, sizeof(__collate_substitute_table), 1, fp);
184 - FREAD(TMP_char_pri_table, sizeof(__collate_char_pri_table), 1, fp);
185 - FREAD(TMP_chain_pri_table,
186 - sizeof(*__collate_chain_pri_table), chains, fp);
187 + /* adjust size to read the remaining in one chunk */
188 + i -= offsetof(struct __xlocale_st_collate, __char_pri_table);
189 + FREAD(TMP->__char_pri_table, i, 1, fp);
192 - (void)strcpy(collate_encoding, encoding);
193 - if (__collate_substitute_table_ptr != NULL)
194 - free(__collate_substitute_table_ptr);
195 - __collate_substitute_table_ptr = TMP_substitute_table;
196 - if (__collate_char_pri_table_ptr != NULL)
197 - free(__collate_char_pri_table_ptr);
198 - __collate_char_pri_table_ptr = TMP_char_pri_table;
199 - for (i = 0; i < UCHAR_MAX + 1; i++) {
200 - __collate_char_pri_table[i].prim =
201 - ntohl(__collate_char_pri_table[i].prim);
202 - __collate_char_pri_table[i].sec =
203 - ntohl(__collate_char_pri_table[i].sec);
205 - if (__collate_chain_pri_table != NULL)
206 - free(__collate_chain_pri_table);
207 - __collate_chain_pri_table = TMP_chain_pri_table;
208 - for (i = 0; i < chains; i++) {
209 - __collate_chain_pri_table[i].prim =
210 - ntohl(__collate_chain_pri_table[i].prim);
211 - __collate_chain_pri_table[i].sec =
212 - ntohl(__collate_chain_pri_table[i].sec);
214 - __collate_substitute_nontrivial = 0;
215 - for (i = 0; i < UCHAR_MAX + 1; i++) {
216 - if (__collate_substitute_table[i][0] != i ||
217 - __collate_substitute_table[i][1] != 0) {
218 - __collate_substitute_nontrivial = 1;
220 + vp = (void *)(TMP + 1);
222 + /* the COLLATE_SUBST_DUP optimization relies on COLL_WEIGHTS_MAX == 2 */
223 + if (info.subst_count[0] > 0) {
224 + TMP->__substitute_table[0] = (struct __collate_st_subst *)vp;
225 + vp += info.subst_count[0] * sizeof(struct __collate_st_subst);
227 + TMP->__substitute_table[0] = NULL;
228 + if (info.flags & COLLATE_SUBST_DUP)
229 + TMP->__substitute_table[1] = TMP->__substitute_table[0];
230 + else if (info.subst_count[1] > 0) {
231 + TMP->__substitute_table[1] = (struct __collate_st_subst *)vp;
232 + vp += info.subst_count[1] * sizeof(struct __collate_st_subst);
234 + TMP->__substitute_table[1] = NULL;
237 + TMP->__chain_pri_table = (struct __collate_st_chain_pri *)vp;
238 + vp += chains * sizeof(struct __collate_st_chain_pri);
240 + TMP->__chain_pri_table = NULL;
241 + if (info.large_pri_count > 0)
242 + TMP->__large_char_pri_table = (struct __collate_st_large_char_pri *)vp;
244 + TMP->__large_char_pri_table = NULL;
246 +#if __DARWIN_BYTE_ORDER == __DARWIN_LITTLE_ENDIAN
248 + struct __collate_st_char_pri *p = TMP->__char_pri_table;
249 + for(i = UCHAR_MAX + 1; i-- > 0; p++) {
250 + for(z = 0; z < info.directive_count; z++)
251 + p->pri[z] = ntohl(p->pri[z]);
254 - __collate_load_error = 0;
255 + for(z = 0; z < info.directive_count; z++)
256 + if (info.subst_count[z] > 0) {
257 + struct __collate_st_subst *p = TMP->__substitute_table[z];
258 + for(i = info.subst_count[z]; i-- > 0; p++) {
259 + p->val = ntohl(p->val);
260 + wntohl(p->str, STR_LEN);
264 + struct __collate_st_chain_pri *p = TMP->__chain_pri_table;
265 + for(i = chains; i-- > 0; p++) {
266 + wntohl(p->str, STR_LEN);
267 + for(z = 0; z < info.directive_count; z++)
268 + p->pri[z] = ntohl(p->pri[z]);
271 + if (info.large_pri_count > 0) {
272 + struct __collate_st_large_char_pri *p = TMP->__large_char_pri_table;
273 + for(i = info.large_pri_count; i-- > 0; p++) {
274 + p->val = ntohl(p->val);
275 + for(z = 0; z < info.directive_count; z++)
276 + p->pri.pri[z] = ntohl(p->pri.pri[z]);
279 +#endif /* __DARWIN_BYTE_ORDER == __DARWIN_LITTLE_ENDIAN */
280 + (void)strcpy(TMP->__encoding, encoding);
281 + (void)memcpy(&TMP->__info, &info, sizeof(info));
284 + XL_RELEASE(loc->__lc_collate);
285 + loc->__lc_collate = cache;
286 + /* no need to retain, since we set __refcount to 2 above */
288 + loc->__collate_substitute_nontrivial = (info.subst_count[0] > 0 || info.subst_count[1] > 0);
289 + loc->__collate_load_error = 0;
290 + if (loc == &__global_locale)
291 + __collate_load_error = 0;
293 return (_LDP_LOADED);
297 -__collate_substitute(const u_char *s)
299 +__collate_wcsnlen(const wchar_t *s, int len)
302 + while (*s && n < len) {
309 +static struct __collate_st_subst *
310 +substsearch(const wchar_t key, struct __collate_st_subst *tab, int n)
315 + struct __collate_st_subst *p;
317 + while (low <= high) {
318 + next = (low + high) / 2;
320 + compar = key - p->val;
331 +__private_extern__ wchar_t *
332 +__collate_substitute(const wchar_t *s, int which, locale_t loc)
334 int dest_len, len, nlen;
335 - int delta = strlen(s);
336 - u_char *dest_str = NULL;
337 + int n, delta, nsubst;
338 + wchar_t *dest_str = NULL;
340 + struct __collate_st_subst *subst, *match;
342 if (s == NULL || *s == '\0')
343 - return (__collate_strdup(""));
344 - delta += delta / 8;
345 - dest_str = malloc(dest_len = delta);
346 + return (__collate_wcsdup(L""));
347 + dest_len = wcslen(s);
348 + nsubst = __collate_info->subst_count[which];
350 + return __collate_wcsdup(s);
351 + subst = __collate_substitute_table[which];
352 + delta = dest_len / 4;
355 + dest_str = (wchar_t *)malloc((dest_len += delta) * sizeof(wchar_t));
356 if (dest_str == NULL)
357 __collate_err(EX_OSERR, __func__);
360 - nlen = len + strlen(__collate_substitute_table[*s]);
361 + if ((match = substsearch(*s, subst, nsubst)) != NULL) {
363 + n = __collate_wcsnlen(fp, STR_LEN);
369 if (dest_len <= nlen) {
370 - dest_str = reallocf(dest_str, dest_len = nlen + delta);
371 + dest_str = reallocf(dest_str, (dest_len = nlen + delta) * sizeof(wchar_t));
372 if (dest_str == NULL)
373 __collate_err(EX_OSERR, __func__);
375 - (void)strcpy(dest_str + len, __collate_substitute_table[*s++]);
377 + wcsncpy(dest_str + len, fp, n);
386 -__collate_lookup(const u_char *t, int *len, int *prim, int *sec)
387 +static struct __collate_st_chain_pri *
388 +chainsearch(const wchar_t *key, int *len, locale_t loc)
391 + int high = __collate_info->chain_count - 1;
392 + int next, compar, l;
393 + struct __collate_st_chain_pri *p;
394 + struct __collate_st_chain_pri *tab = __collate_chain_pri_table;
396 + while (low <= high) {
397 + next = (low + high) / 2;
399 + compar = *key - *p->str;
401 + l = __collate_wcsnlen(p->str, STR_LEN);
402 + compar = wcsncmp(key, p->str, l);
416 +static struct __collate_st_large_char_pri *
417 +largesearch(const wchar_t key, locale_t loc)
420 + int high = __collate_info->large_pri_count - 1;
422 + struct __collate_st_large_char_pri *p;
423 + struct __collate_st_large_char_pri *tab = __collate_large_char_pri_table;
425 + while (low <= high) {
426 + next = (low + high) / 2;
428 + compar = key - p->val;
439 +__private_extern__ void
440 +__collate_lookup_l(const wchar_t *t, int *len, int *prim, int *sec, locale_t loc)
442 struct __collate_st_chain_pri *p2;
447 - for (p2 = __collate_chain_pri_table; p2->str[0] != '\0'; p2++) {
448 - if (*t == p2->str[0] &&
449 - strncmp(t, p2->str, strlen(p2->str)) == 0) {
450 - *len = strlen(p2->str);
453 + p2 = chainsearch(t, &l, loc);
454 + /* use the chain if prim >= 0 */
455 + if (p2 && p2->pri[0] >= 0) {
457 + *prim = p2->pri[0];
461 + if (*t <= UCHAR_MAX) {
462 + *prim = __collate_char_pri_table[*t].pri[0];
463 + *sec = __collate_char_pri_table[*t].pri[1];
466 + if (__collate_info->large_pri_count > 0) {
467 + struct __collate_st_large_char_pri *match;
468 + match = largesearch(*t, loc);
470 + *prim = match->pri.pri[0];
471 + *sec = match->pri.pri[1];
475 + *prim = (l = __collate_info->undef_pri[0]) >= 0 ? l : *t - l;
476 + *sec = (l = __collate_info->undef_pri[1]) >= 0 ? l : *t - l;
480 + * This is only provided for programs (like grep) that are calling this
481 + * private function. This will go away eventually.
484 +__collate_lookup(const unsigned char *t, int *len, int *prim, int *sec)
486 + locale_t loc = __current_locale();
487 + wchar_t *w = __collate_mbstowcs((const char *)t, loc);
490 + __collate_lookup_l(w, len, prim, sec, loc);
496 +__private_extern__ void
497 +__collate_lookup_which(const wchar_t *t, int *len, int *pri, int which, locale_t loc)
499 + struct __collate_st_chain_pri *p2;
504 + p2 = chainsearch(t, &l, loc);
506 + p = p2->pri[which];
507 + /* use the chain if pri >= 0 */
514 - *prim = __collate_char_pri_table[*t].prim;
515 - *sec = __collate_char_pri_table[*t].sec;
516 + if (*t <= UCHAR_MAX) {
517 + *pri = __collate_char_pri_table[*t].pri[which];
520 + if (__collate_info->large_pri_count > 0) {
521 + struct __collate_st_large_char_pri *match;
522 + match = largesearch(*t, loc);
524 + *pri = match->pri.pri[which];
528 + *pri = (l = __collate_info->undef_pri[which]) >= 0 ? l : *t - l;
531 +__private_extern__ wchar_t *
532 +__collate_mbstowcs(const char *s, locale_t loc)
534 + static const mbstate_t initial;
542 + if ((len = mbsrtowcs_l(NULL, &ss, 0, &st, loc)) == (size_t)-1)
544 + if ((wcs = (wchar_t *)malloc((len + 1) * sizeof(wchar_t))) == NULL)
545 + __collate_err(EX_OSERR, __func__);
547 + mbsrtowcs_l(wcs, &s, len, &st, loc);
554 -__collate_strdup(u_char *s)
555 +__private_extern__ wchar_t *
556 +__collate_wcsdup(const wchar_t *s)
558 - u_char *t = strdup(s);
559 + size_t len = wcslen(s) + 1;
563 + if ((wcs = (wchar_t *)malloc(len * sizeof(wchar_t))) == NULL)
564 __collate_err(EX_OSERR, __func__);
571 +__private_extern__ void
572 +__collate_xfrm(const wchar_t *src, wchar_t **xf, locale_t loc)
577 + wchar_t *tt = NULL, *tr = NULL;
580 + struct __collate_st_info *info = __collate_info;
583 + for(pass = 0; pass < COLL_WEIGHTS_MAX; pass++)
585 + for(pass = 0; pass < info->directive_count; pass++) {
586 + direc = info->directive[pass];
587 + if (pass == 0 || !(info->flags & COLLATE_SUBST_DUP)) {
591 + tt = __collate_substitute(src, pass, loc);
593 + if (direc & DIRECTIVE_BACKWARD) {
594 + wchar_t *bp, *fp, c;
598 + tr = __collate_wcsdup(tt ? tt : src);
600 + fp = tr + wcslen(tr) - 1;
606 + t = (const wchar_t *)tr;
608 + t = (const wchar_t *)tt;
610 + t = (const wchar_t *)src;
612 + if ((xf[pass] = (wchar_t *)malloc(sizeof(wchar_t) * (wcslen(t) + 1))) == NULL) {
619 + if (direc & DIRECTIVE_POSITION) {
621 + __collate_lookup_which(t, &len, &pri, pass, loc);
629 + pri = COLLATE_MAX_PRIORITY;
635 + __collate_lookup_which(t, &len, &pri, pass, loc);
657 +__private_extern__ void
658 __collate_err(int ex, const char *f)
661 @@ -275,24 +610,345 @@ __collate_err(int ex, const char *f)
666 + * __collate_collating_symbol takes the multibyte string specified by
667 + * src and slen, and using ps, converts that to a wide character. Then
668 + * it is checked to verify it is a collating symbol, and then copies
669 + * it to the wide character string specified by dst and dlen (the
670 + * results are not null terminated). The length of the wide characters
671 + * copied to dst is returned if successful. Zero is returned if no such
672 + * collating symbol exists. (size_t)-1 is returned if there are wide-character
673 + * conversion errors, if the length of the converted string is greater that
674 + * STR_LEN or if dlen is too small. It is up to the calling routine to
675 + * preserve the mbstate_t structure as needed.
677 +__private_extern__ size_t
678 +__collate_collating_symbol(wchar_t *dst, size_t dlen, const char *src, size_t slen, mbstate_t *ps, locale_t loc)
680 + wchar_t wname[STR_LEN];
685 + if (loc->__collate_load_error) {
688 + if (slen != 1 || !isascii(*src))
693 + for(wp = wname, len = 0; slen > 0; len++) {
694 + l = mbrtowc_l(&w, src, slen, ps, loc);
695 + if (l == (size_t)-1 || l == (size_t)-2)
699 + if (len >= STR_LEN)
703 + slen = (long)slen - (long)l;
705 + if (len == 0 || len > dlen)
708 + if (*wname <= UCHAR_MAX) {
709 + if (__collate_char_pri_table[*wname].pri[0] >= 0) {
715 + } else if (__collate_info->large_pri_count > 0) {
716 + struct __collate_st_large_char_pri *match;
717 + match = largesearch(*wname, loc);
718 + if (match && match->pri.pri[0] >= 0) {
727 + if (__collate_info->chain_count > 0) {
728 + struct __collate_st_chain_pri *match;
730 + match = chainsearch(wname, &ll, loc);
734 + wcsncpy(dst, wname, dlen);
742 + * __collate_equiv_class returns the equivalence class number for the symbol
743 + * specified by src and slen, using ps to convert from multi-byte to wide
744 + * character. Zero is returned if the symbol is not in an equivalence
745 + * class. -1 is returned if there are wide character conversion error,
746 + * if there are any greater-than-8-bit characters or if a multi-byte symbol
747 + * is greater or equal to STR_LEN in length. It is up to the calling
748 + * routine to preserve the mbstate_t structure as needed.
750 +__private_extern__ int
751 +__collate_equiv_class(const char *src, size_t slen, mbstate_t *ps, locale_t loc)
753 + wchar_t wname[STR_LEN];
759 + if (loc->__collate_load_error)
761 + for(wp = wname, len = 0; slen > 0; len++) {
762 + l = mbrtowc_l(&w, src, slen, ps, loc);
763 + if (l == (size_t)-1 || l == (size_t)-2)
767 + if (len >= STR_LEN)
771 + slen = (long)slen - (long)l;
777 + if (*wname <= UCHAR_MAX)
778 + e = __collate_char_pri_table[*wname].pri[0];
779 + else if (__collate_info->large_pri_count > 0) {
780 + struct __collate_st_large_char_pri *match;
781 + match = largesearch(*wname, loc);
783 + e = match->pri.pri[0];
786 + return IGNORE_EQUIV_CLASS;
787 + return e > 0 ? e : 0;
790 + if (__collate_info->chain_count > 0) {
791 + struct __collate_st_chain_pri *match;
793 + match = chainsearch(wname, &ll, loc);
797 + return IGNORE_EQUIV_CLASS;
798 + return e < 0 ? -e : e;
805 + * __collate_equiv_match tries to match any single or multi-character symbol
806 + * in equivalence class equiv_class in the multi-byte string specified by src
807 + * and slen. If start is non-zero, it is taken to be the first (pre-converted)
808 + * wide character. Subsequence wide characters, if needed, will use ps in
809 + * the conversion. On a successful match, the length of the matched string
810 + * is returned (including the start character). If dst is non-NULL, the
811 + * matched wide-character string is copied to dst, a wide character array of
812 + * length dlen (the results are not zero-terminated). If rlen is non-NULL,
813 + * the number of character in src actually used is returned. Zero is
814 + * returned by __collate_equiv_match if there is no match. (size_t)-1 is
815 + * returned on error: if there were conversion errors or if dlen is too small
816 + * to accept the results. On no match or error, ps is restored to its incoming
820 +__collate_equiv_match(int equiv_class, wchar_t *dst, size_t dlen, wchar_t start, const char *src, size_t slen, mbstate_t *ps, size_t *rlen, locale_t loc)
823 + size_t len, l, clen;
825 + wchar_t buf[STR_LEN], *wp;
827 + const char *s = src;
829 + struct __collate_st_chain_pri *ch = NULL;
832 + if (loc->__collate_load_error)
834 + if (equiv_class == IGNORE_EQUIV_CLASS)
844 + /* convert up to the max chain length */
845 + while(sl > 0 && len < __collate_info->chain_max_len) {
846 + l = mbrtowc_l(&w, s, sl, ps, loc);
847 + if (l == (size_t)-1 || l == (size_t)-2 || l == 0)
856 + if (len > 1 && (ch = chainsearch(buf, &i, loc)) != NULL) {
857 + int e = ch->pri[0];
860 + if (e == equiv_class)
863 + /* try single character */
865 + if (*buf <= UCHAR_MAX) {
866 + if (equiv_class == __collate_char_pri_table[*buf].pri[0])
868 + } else if (__collate_info->large_pri_count > 0) {
869 + struct __collate_st_large_char_pri *match;
870 + match = largesearch(*buf, loc);
871 + if (match && equiv_class == match->pri.pri[0])
879 + /* if we converted more than we used, restore to initial and reconvert
880 + * up to what did match */
889 + l = mbrtowc_l(&w, src, slen, ps, loc);
901 + for(wp = buf; len > 0; len--)
909 +#if __DARWIN_BYTE_ORDER == __DARWIN_LITTLE_ENDIAN
911 +wntohl(wchar_t *str, int len)
913 + for(; *str && len > 0; str++, len--)
914 + *str = ntohl(*str);
916 +#endif /* __DARWIN_BYTE_ORDER == __DARWIN_LITTLE_ENDIAN */
922 + static char buf[5];
924 + if (c >=32 && c <= 126)
925 + sprintf(buf, "'%c' ", c);
927 + sprintf(buf, "\\x{%02x}", c);
932 +showwcs(const wchar_t *t, int len)
934 + static char buf[64];
937 + for(; *t && len > 0; len--, t++) {
938 + if (*t >=32 && *t <= 126)
941 + sprintf(cp, "\\x{%02x}", *t);
950 __collate_print_tables()
953 - struct __collate_st_chain_pri *p2;
955 + locale_t loc = __current_locale();
957 - printf("Substitute table:\n");
958 - for (i = 0; i < UCHAR_MAX + 1; i++)
959 - if (i != *__collate_substitute_table[i])
960 - printf("\t'%c' --> \"%s\"\n", i,
961 - __collate_substitute_table[i]);
962 - printf("Chain priority table:\n");
963 - for (p2 = __collate_chain_pri_table; p2->str[0] != '\0'; p2++)
964 - printf("\t\"%s\" : %d %d\n", p2->str, p2->prim, p2->sec);
965 + printf("Info: p=%d s=%d f=0x%02x m=%d dc=%d up=%d us=%d pc=%d sc=%d cc=%d lc=%d\n",
966 + __collate_info->directive[0], __collate_info->directive[1],
967 + __collate_info->flags, __collate_info->chain_max_len,
968 + __collate_info->directive_count,
969 + __collate_info->undef_pri[0], __collate_info->undef_pri[1],
970 + __collate_info->subst_count[0], __collate_info->subst_count[1],
971 + __collate_info->chain_count, __collate_info->large_pri_count);
972 + for(z = 0; z < __collate_info->directive_count; z++) {
973 + if (__collate_info->subst_count[z] > 0) {
974 + struct __collate_st_subst *p2 = __collate_substitute_table[z];
975 + if (z == 0 && (__collate_info->flags & COLLATE_SUBST_DUP))
976 + printf("Both substitute tables:\n");
978 + printf("Substitute table %d:\n", z);
979 + for (i = __collate_info->subst_count[z]; i-- > 0; p2++)
980 + printf("\t%s --> \"%s\"\n",
982 + showwcs(p2->str, STR_LEN));
985 + if (__collate_info->chain_count > 0) {
986 + printf("Chain priority table:\n");
987 + struct __collate_st_chain_pri *p2 = __collate_chain_pri_table;
988 + for (i = __collate_info->chain_count; i-- > 0; p2++) {
989 + printf("\t\"%s\" :", showwcs(p2->str, STR_LEN));
990 + for(z = 0; z < __collate_info->directive_count; z++)
991 + printf(" %d", p2->pri[z]);
995 printf("Char priority table:\n");
996 - for (i = 0; i < UCHAR_MAX + 1; i++)
997 - printf("\t'%c' : %d %d\n", i, __collate_char_pri_table[i].prim,
998 - __collate_char_pri_table[i].sec);
1000 + struct __collate_st_char_pri *p2 = __collate_char_pri_table;
1001 + for (i = 0; i < UCHAR_MAX + 1; i++, p2++) {
1002 + printf("\t%s :", show(i));
1003 + for(z = 0; z < __collate_info->directive_count; z++)
1004 + printf(" %d", p2->pri[z]);
1008 + if (__collate_info->large_pri_count > 0) {
1009 + struct __collate_st_large_char_pri *p2 = __collate_large_char_pri_table;
1010 + printf("Large priority table:\n");
1011 + for (i = __collate_info->large_pri_count; i-- > 0; p2++) {
1012 + printf("\t%s :", show(p2->val));
1013 + for(z = 0; z < __collate_info->directive_count; z++)
1014 + printf(" %d", p2->pri.pri[z]);