Add some more cases to the app-id unit tests
[glib.git] / glib / gunicollate.c
blobf7257203d3f54e879368d2a090382985a86ada00
1 /* gunicollate.c - Collation
3 * Copyright 2001,2005 Red Hat, Inc.
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Lesser General Public
7 * License as published by the Free Software Foundation; either
8 * version 2 of the License, or (at your option) any later version.
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Lesser General Public License for more details.
15 * You should have received a copy of the GNU Lesser General Public License
16 * along with this library; if not, see <http://www.gnu.org/licenses/>.
19 #include "config.h"
21 #include <locale.h>
22 #include <string.h>
23 #ifdef __STDC_ISO_10646__
24 #include <wchar.h>
25 #endif
27 #ifdef HAVE_CARBON
28 #include <CoreServices/CoreServices.h>
29 #endif
31 #include "gmem.h"
32 #include "gunicode.h"
33 #include "gunicodeprivate.h"
34 #include "gstring.h"
35 #include "gstrfuncs.h"
36 #include "gtestutils.h"
37 #include "gcharset.h"
38 #ifndef __STDC_ISO_10646__
39 #include "gconvert.h"
40 #endif
43 #ifdef _MSC_VER
44 /* Workaround for bug in MSVCR80.DLL */
45 static gsize
46 msc_strxfrm_wrapper (char *string1,
47 const char *string2,
48 gsize count)
50 if (!string1 || count <= 0)
52 char tmp;
54 return strxfrm (&tmp, string2, 1);
56 return strxfrm (string1, string2, count);
58 #define strxfrm msc_strxfrm_wrapper
59 #endif
61 /**
62 * g_utf8_collate:
63 * @str1: a UTF-8 encoded string
64 * @str2: a UTF-8 encoded string
66 * Compares two strings for ordering using the linguistically
67 * correct rules for the [current locale][setlocale].
68 * When sorting a large number of strings, it will be significantly
69 * faster to obtain collation keys with g_utf8_collate_key() and
70 * compare the keys with strcmp() when sorting instead of sorting
71 * the original strings.
73 * Returns: < 0 if @str1 compares before @str2,
74 * 0 if they compare equal, > 0 if @str1 compares after @str2.
75 **/
76 gint
77 g_utf8_collate (const gchar *str1,
78 const gchar *str2)
80 gint result;
82 #ifdef HAVE_CARBON
84 UniChar *str1_utf16;
85 UniChar *str2_utf16;
86 glong len1;
87 glong len2;
88 SInt32 retval = 0;
90 g_return_val_if_fail (str1 != NULL, 0);
91 g_return_val_if_fail (str2 != NULL, 0);
93 str1_utf16 = g_utf8_to_utf16 (str1, -1, NULL, &len1, NULL);
94 str2_utf16 = g_utf8_to_utf16 (str2, -1, NULL, &len2, NULL);
96 UCCompareTextDefault (kUCCollateStandardOptions,
97 str1_utf16, len1, str2_utf16, len2,
98 NULL, &retval);
99 result = retval;
101 g_free (str2_utf16);
102 g_free (str1_utf16);
104 #elif defined(__STDC_ISO_10646__)
106 gunichar *str1_norm;
107 gunichar *str2_norm;
109 g_return_val_if_fail (str1 != NULL, 0);
110 g_return_val_if_fail (str2 != NULL, 0);
112 str1_norm = _g_utf8_normalize_wc (str1, -1, G_NORMALIZE_ALL_COMPOSE);
113 str2_norm = _g_utf8_normalize_wc (str2, -1, G_NORMALIZE_ALL_COMPOSE);
115 result = wcscoll ((wchar_t *)str1_norm, (wchar_t *)str2_norm);
117 g_free (str1_norm);
118 g_free (str2_norm);
120 #else /* !__STDC_ISO_10646__ */
122 const gchar *charset;
123 gchar *str1_norm;
124 gchar *str2_norm;
126 g_return_val_if_fail (str1 != NULL, 0);
127 g_return_val_if_fail (str2 != NULL, 0);
129 str1_norm = g_utf8_normalize (str1, -1, G_NORMALIZE_ALL_COMPOSE);
130 str2_norm = g_utf8_normalize (str2, -1, G_NORMALIZE_ALL_COMPOSE);
132 if (g_get_charset (&charset))
134 result = strcoll (str1_norm, str2_norm);
136 else
138 gchar *str1_locale = g_convert (str1_norm, -1, charset, "UTF-8", NULL, NULL, NULL);
139 gchar *str2_locale = g_convert (str2_norm, -1, charset, "UTF-8", NULL, NULL, NULL);
141 if (str1_locale && str2_locale)
142 result = strcoll (str1_locale, str2_locale);
143 else if (str1_locale)
144 result = -1;
145 else if (str2_locale)
146 result = 1;
147 else
148 result = strcmp (str1_norm, str2_norm);
150 g_free (str1_locale);
151 g_free (str2_locale);
154 g_free (str1_norm);
155 g_free (str2_norm);
157 #endif /* __STDC_ISO_10646__ */
159 return result;
162 #if defined(__STDC_ISO_10646__) || defined(HAVE_CARBON)
163 /* We need UTF-8 encoding of numbers to encode the weights if
164 * we are using wcsxfrm. However, we aren't encoding Unicode
165 * characters, so we can't simply use g_unichar_to_utf8.
167 * The following routine is taken (with modification) from GNU
168 * libc's strxfrm routine:
170 * Copyright (C) 1995-1999,2000,2001 Free Software Foundation, Inc.
171 * Written by Ulrich Drepper <drepper@cygnus.com>, 1995.
173 static inline int
174 utf8_encode (char *buf, wchar_t val)
176 int retval;
178 if (val < 0x80)
180 if (buf)
181 *buf++ = (char) val;
182 retval = 1;
184 else
186 int step;
188 for (step = 2; step < 6; ++step)
189 if ((val & (~(guint32)0 << (5 * step + 1))) == 0)
190 break;
191 retval = step;
193 if (buf)
195 *buf = (unsigned char) (~0xff >> step);
196 --step;
199 buf[step] = 0x80 | (val & 0x3f);
200 val >>= 6;
202 while (--step > 0);
203 *buf |= val;
207 return retval;
209 #endif /* __STDC_ISO_10646__ || HAVE_CARBON */
211 #ifdef HAVE_CARBON
213 static gchar *
214 collate_key_to_string (UCCollationValue *key,
215 gsize key_len)
217 gchar *result;
218 gsize result_len = 0;
219 const gsize start = 2 * sizeof (void *) / sizeof (UCCollationValue);
220 gsize i;
222 /* The first codes should be skipped: the same string on the same
223 * system can get different values at runtime in those positions,
224 * and they do not sort correctly. The exact size of the prefix
225 * depends on whether we are building 64 or 32 bit.
227 if (key_len <= start)
228 return g_strdup ("");
230 for (i = start; i < key_len; i++)
231 result_len += utf8_encode (NULL, g_htonl (key[i] + 1));
233 result = g_malloc (result_len + 1);
234 result_len = 0;
235 for (i = start; i < key_len; i++)
236 result_len += utf8_encode (result + result_len, g_htonl (key[i] + 1));
238 result[result_len] = '\0';
240 return result;
243 static gchar *
244 carbon_collate_key_with_collator (const gchar *str,
245 gssize len,
246 CollatorRef collator)
248 UniChar *str_utf16 = NULL;
249 glong len_utf16;
250 OSStatus ret;
251 UCCollationValue staticbuf[512];
252 UCCollationValue *freeme = NULL;
253 UCCollationValue *buf;
254 ItemCount buf_len;
255 ItemCount key_len;
256 ItemCount try_len;
257 gchar *result = NULL;
259 str_utf16 = g_utf8_to_utf16 (str, len, NULL, &len_utf16, NULL);
260 try_len = len_utf16 * 5 + 2;
262 if (try_len <= sizeof staticbuf)
264 buf = staticbuf;
265 buf_len = sizeof staticbuf;
267 else
269 freeme = g_new (UCCollationValue, try_len);
270 buf = freeme;
271 buf_len = try_len;
274 ret = UCGetCollationKey (collator, str_utf16, len_utf16,
275 buf_len, &key_len, buf);
277 if (ret == kCollateBufferTooSmall)
279 freeme = g_renew (UCCollationValue, freeme, try_len * 2);
280 buf = freeme;
281 buf_len = try_len * 2;
282 ret = UCGetCollationKey (collator, str_utf16, len_utf16,
283 buf_len, &key_len, buf);
286 if (ret == 0)
287 result = collate_key_to_string (buf, key_len);
288 else
289 result = g_strdup ("");
291 g_free (freeme);
292 g_free (str_utf16);
293 return result;
296 static gchar *
297 carbon_collate_key (const gchar *str,
298 gssize len)
300 static CollatorRef collator;
302 if (G_UNLIKELY (!collator))
304 UCCreateCollator (NULL, 0, kUCCollateStandardOptions, &collator);
306 if (!collator)
308 static gboolean been_here;
309 if (!been_here)
310 g_warning ("%s: UCCreateCollator failed", G_STRLOC);
311 been_here = TRUE;
312 return g_strdup ("");
316 return carbon_collate_key_with_collator (str, len, collator);
319 static gchar *
320 carbon_collate_key_for_filename (const gchar *str,
321 gssize len)
323 static CollatorRef collator;
325 if (G_UNLIKELY (!collator))
327 /* http://developer.apple.com/qa/qa2004/qa1159.html */
328 UCCreateCollator (NULL, 0,
329 kUCCollateComposeInsensitiveMask
330 | kUCCollateWidthInsensitiveMask
331 | kUCCollateCaseInsensitiveMask
332 | kUCCollateDigitsOverrideMask
333 | kUCCollateDigitsAsNumberMask
334 | kUCCollatePunctuationSignificantMask,
335 &collator);
337 if (!collator)
339 static gboolean been_here;
340 if (!been_here)
341 g_warning ("%s: UCCreateCollator failed", G_STRLOC);
342 been_here = TRUE;
343 return g_strdup ("");
347 return carbon_collate_key_with_collator (str, len, collator);
350 #endif /* HAVE_CARBON */
353 * g_utf8_collate_key:
354 * @str: a UTF-8 encoded string.
355 * @len: length of @str, in bytes, or -1 if @str is nul-terminated.
357 * Converts a string into a collation key that can be compared
358 * with other collation keys produced by the same function using
359 * strcmp().
361 * The results of comparing the collation keys of two strings
362 * with strcmp() will always be the same as comparing the two
363 * original keys with g_utf8_collate().
365 * Note that this function depends on the [current locale][setlocale].
367 * Returns: a newly allocated string. This string should
368 * be freed with g_free() when you are done with it.
370 gchar *
371 g_utf8_collate_key (const gchar *str,
372 gssize len)
374 gchar *result;
376 #ifdef HAVE_CARBON
378 g_return_val_if_fail (str != NULL, NULL);
379 result = carbon_collate_key (str, len);
381 #elif defined(__STDC_ISO_10646__)
383 gsize xfrm_len;
384 gunichar *str_norm;
385 wchar_t *result_wc;
386 gsize i;
387 gsize result_len = 0;
389 g_return_val_if_fail (str != NULL, NULL);
391 str_norm = _g_utf8_normalize_wc (str, len, G_NORMALIZE_ALL_COMPOSE);
393 xfrm_len = wcsxfrm (NULL, (wchar_t *)str_norm, 0);
394 result_wc = g_new (wchar_t, xfrm_len + 1);
395 wcsxfrm (result_wc, (wchar_t *)str_norm, xfrm_len + 1);
397 for (i = 0; i < xfrm_len; i++)
398 result_len += utf8_encode (NULL, result_wc[i]);
400 result = g_malloc (result_len + 1);
401 result_len = 0;
402 for (i = 0; i < xfrm_len; i++)
403 result_len += utf8_encode (result + result_len, result_wc[i]);
405 result[result_len] = '\0';
407 g_free (result_wc);
408 g_free (str_norm);
410 return result;
411 #else /* !__STDC_ISO_10646__ */
413 gsize xfrm_len;
414 const gchar *charset;
415 gchar *str_norm;
417 g_return_val_if_fail (str != NULL, NULL);
419 str_norm = g_utf8_normalize (str, len, G_NORMALIZE_ALL_COMPOSE);
421 result = NULL;
423 if (g_get_charset (&charset))
425 xfrm_len = strxfrm (NULL, str_norm, 0);
426 if (xfrm_len >= 0 && xfrm_len < G_MAXINT - 2)
428 result = g_malloc (xfrm_len + 1);
429 strxfrm (result, str_norm, xfrm_len + 1);
432 else
434 gchar *str_locale = g_convert (str_norm, -1, charset, "UTF-8", NULL, NULL, NULL);
436 if (str_locale)
438 xfrm_len = strxfrm (NULL, str_locale, 0);
439 if (xfrm_len < 0 || xfrm_len >= G_MAXINT - 2)
441 g_free (str_locale);
442 str_locale = NULL;
445 if (str_locale)
447 result = g_malloc (xfrm_len + 2);
448 result[0] = 'A';
449 strxfrm (result + 1, str_locale, xfrm_len + 1);
451 g_free (str_locale);
455 if (!result)
457 xfrm_len = strlen (str_norm);
458 result = g_malloc (xfrm_len + 2);
459 result[0] = 'B';
460 memcpy (result + 1, str_norm, xfrm_len);
461 result[xfrm_len+1] = '\0';
464 g_free (str_norm);
465 #endif /* __STDC_ISO_10646__ */
467 return result;
470 /* This is a collation key that is very very likely to sort before any
471 * collation key that libc strxfrm generates. We use this before any
472 * special case (dot or number) to make sure that its sorted before
473 * anything else.
475 #define COLLATION_SENTINEL "\1\1\1"
478 * g_utf8_collate_key_for_filename:
479 * @str: a UTF-8 encoded string.
480 * @len: length of @str, in bytes, or -1 if @str is nul-terminated.
482 * Converts a string into a collation key that can be compared
483 * with other collation keys produced by the same function using strcmp().
485 * In order to sort filenames correctly, this function treats the dot '.'
486 * as a special case. Most dictionary orderings seem to consider it
487 * insignificant, thus producing the ordering "event.c" "eventgenerator.c"
488 * "event.h" instead of "event.c" "event.h" "eventgenerator.c". Also, we
489 * would like to treat numbers intelligently so that "file1" "file10" "file5"
490 * is sorted as "file1" "file5" "file10".
492 * Note that this function depends on the [current locale][setlocale].
494 * Returns: a newly allocated string. This string should
495 * be freed with g_free() when you are done with it.
497 * Since: 2.8
499 gchar *
500 g_utf8_collate_key_for_filename (const gchar *str,
501 gssize len)
503 #ifndef HAVE_CARBON
504 GString *result;
505 GString *append;
506 const gchar *p;
507 const gchar *prev;
508 const gchar *end;
509 gchar *collate_key;
510 gint digits;
511 gint leading_zeros;
514 * How it works:
516 * Split the filename into collatable substrings which do
517 * not contain [.0-9] and special-cased substrings. The collatable
518 * substrings are run through the normal g_utf8_collate_key() and the
519 * resulting keys are concatenated with keys generated from the
520 * special-cased substrings.
522 * Special cases: Dots are handled by replacing them with '\1' which
523 * implies that short dot-delimited substrings are before long ones,
524 * e.g.
526 * a\1a (a.a)
527 * a-\1a (a-.a)
528 * aa\1a (aa.a)
530 * Numbers are handled by prepending to each number d-1 superdigits
531 * where d = number of digits in the number and SUPERDIGIT is a
532 * character with an integer value higher than any digit (for instance
533 * ':'). This ensures that single-digit numbers are sorted before
534 * double-digit numbers which in turn are sorted separately from
535 * triple-digit numbers, etc. To avoid strange side-effects when
536 * sorting strings that already contain SUPERDIGITs, a '\2'
537 * is also prepended, like this
539 * file\21 (file1)
540 * file\25 (file5)
541 * file\2:10 (file10)
542 * file\2:26 (file26)
543 * file\2::100 (file100)
544 * file:foo (file:foo)
546 * This has the side-effect of sorting numbers before everything else (except
547 * dots), but this is probably OK.
549 * Leading digits are ignored when doing the above. To discriminate
550 * numbers which differ only in the number of leading digits, we append
551 * the number of leading digits as a byte at the very end of the collation
552 * key.
554 * To try avoid conflict with any collation key sequence generated by libc we
555 * start each switch to a special cased part with a sentinel that hopefully
556 * will sort before anything libc will generate.
559 if (len < 0)
560 len = strlen (str);
562 result = g_string_sized_new (len * 2);
563 append = g_string_sized_new (0);
565 end = str + len;
567 /* No need to use utf8 functions, since we're only looking for ascii chars */
568 for (prev = p = str; p < end; p++)
570 switch (*p)
572 case '.':
573 if (prev != p)
575 collate_key = g_utf8_collate_key (prev, p - prev);
576 g_string_append (result, collate_key);
577 g_free (collate_key);
580 g_string_append (result, COLLATION_SENTINEL "\1");
582 /* skip the dot */
583 prev = p + 1;
584 break;
586 case '0':
587 case '1':
588 case '2':
589 case '3':
590 case '4':
591 case '5':
592 case '6':
593 case '7':
594 case '8':
595 case '9':
596 if (prev != p)
598 collate_key = g_utf8_collate_key (prev, p - prev);
599 g_string_append (result, collate_key);
600 g_free (collate_key);
603 g_string_append (result, COLLATION_SENTINEL "\2");
605 prev = p;
607 /* write d-1 colons */
608 if (*p == '0')
610 leading_zeros = 1;
611 digits = 0;
613 else
615 leading_zeros = 0;
616 digits = 1;
619 while (++p < end)
621 if (*p == '0' && !digits)
622 ++leading_zeros;
623 else if (g_ascii_isdigit(*p))
624 ++digits;
625 else
627 /* count an all-zero sequence as
628 * one digit plus leading zeros
630 if (!digits)
632 ++digits;
633 --leading_zeros;
635 break;
639 while (digits > 1)
641 g_string_append_c (result, ':');
642 --digits;
645 if (leading_zeros > 0)
647 g_string_append_c (append, (char)leading_zeros);
648 prev += leading_zeros;
651 /* write the number itself */
652 g_string_append_len (result, prev, p - prev);
654 prev = p;
655 --p; /* go one step back to avoid disturbing outer loop */
656 break;
658 default:
659 /* other characters just accumulate */
660 break;
664 if (prev != p)
666 collate_key = g_utf8_collate_key (prev, p - prev);
667 g_string_append (result, collate_key);
668 g_free (collate_key);
671 g_string_append (result, append->str);
672 g_string_free (append, TRUE);
674 return g_string_free (result, FALSE);
675 #else /* HAVE_CARBON */
676 return carbon_collate_key_for_filename (str, len);
677 #endif