+#endif /* __STDC_ISO_10646__ || HAVE_CARBON */
+
+#ifdef HAVE_CARBON
+
+static gchar *
+collate_key_to_string (UCCollationValue *key,
+ gsize key_len)
+{
+ gchar *result;
+ gsize result_len = 0;
+ const gsize start = 2 * sizeof (void *) / sizeof (UCCollationValue);
+ gsize i;
+
+ /* The first codes should be skipped: the same string on the same
+ * system can get different values at runtime in those positions,
+ * and they do not sort correctly. The exact size of the prefix
+ * depends on whether we are building 64 or 32 bit.
+ */
+ if (key_len <= start)
+ return g_strdup ("");
+
+ for (i = start; i < key_len; i++)
+ result_len += utf8_encode (NULL, g_htonl (key[i] + 1));
+
+ result = g_malloc (result_len + 1);
+ result_len = 0;
+ for (i = start; i < key_len; i++)
+ result_len += utf8_encode (result + result_len, g_htonl (key[i] + 1));
+
+ result[result_len] = '\0';
+
+ return result;
+}
+
+static gchar *
+carbon_collate_key_with_collator (const gchar *str,
+ gssize len,
+ CollatorRef collator)
+{
+ UniChar *str_utf16 = NULL;
+ glong len_utf16;
+ OSStatus ret;
+ UCCollationValue staticbuf[512];
+ UCCollationValue *freeme = NULL;
+ UCCollationValue *buf;
+ ItemCount buf_len;
+ ItemCount key_len;
+ ItemCount try_len;
+ gchar *result = NULL;
+
+ str_utf16 = g_utf8_to_utf16 (str, len, NULL, &len_utf16, NULL);
+ try_len = len_utf16 * 5 + 2;
+
+ if (try_len <= sizeof staticbuf)
+ {
+ buf = staticbuf;
+ buf_len = sizeof staticbuf;
+ }
+ else
+ {
+ freeme = g_new (UCCollationValue, try_len);
+ buf = freeme;
+ buf_len = try_len;
+ }
+
+ ret = UCGetCollationKey (collator, str_utf16, len_utf16,
+ buf_len, &key_len, buf);
+
+ if (ret == kCollateBufferTooSmall)
+ {
+ freeme = g_renew (UCCollationValue, freeme, try_len * 2);
+ buf = freeme;
+ buf_len = try_len * 2;
+ ret = UCGetCollationKey (collator, str_utf16, len_utf16,
+ buf_len, &key_len, buf);
+ }
+
+ if (ret == 0)
+ result = collate_key_to_string (buf, key_len);
+ else
+ result = g_strdup ("");
+
+ g_free (freeme);
+ g_free (str_utf16);
+ return result;
+}
+
+static gchar *
+carbon_collate_key (const gchar *str,
+ gssize len)
+{
+ static CollatorRef collator;
+
+ if (G_UNLIKELY (!collator))
+ {
+ UCCreateCollator (NULL, 0, kUCCollateStandardOptions, &collator);
+
+ if (!collator)
+ {
+ static gboolean been_here;
+ if (!been_here)
+ g_warning ("%s: UCCreateCollator failed", G_STRLOC);
+ been_here = TRUE;
+ return g_strdup ("");
+ }
+ }
+
+ return carbon_collate_key_with_collator (str, len, collator);
+}
+
+static gchar *
+carbon_collate_key_for_filename (const gchar *str,
+ gssize len)
+{
+ static CollatorRef collator;
+
+ if (G_UNLIKELY (!collator))
+ {
+ /* http://developer.apple.com/qa/qa2004/qa1159.html */
+ UCCreateCollator (NULL, 0,
+ kUCCollateComposeInsensitiveMask
+ | kUCCollateWidthInsensitiveMask
+ | kUCCollateCaseInsensitiveMask
+ | kUCCollateDigitsOverrideMask
+ | kUCCollateDigitsAsNumberMask
+ | kUCCollatePunctuationSignificantMask,
+ &collator);
+
+ if (!collator)
+ {
+ static gboolean been_here;
+ if (!been_here)
+ g_warning ("%s: UCCreateCollator failed", G_STRLOC);
+ been_here = TRUE;
+ return g_strdup ("");
+ }
+ }
+
+ return carbon_collate_key_with_collator (str, len, collator);
+}
+
+#endif /* HAVE_CARBON */