Skip to content

Instantly share code, notes, and snippets.

@camillol
Created March 29, 2012 09:00
Show Gist options
  • Save camillol/2235195 to your computer and use it in GitHub Desktop.
Save camillol/2235195 to your computer and use it in GitHub Desktop.
patch to fix glib collation on recent OS X
diff --git a/glib/gunicollate.c b/glib/gunicollate.c
index 3649786..bd66c22 100644
--- a/glib/gunicollate.c
+++ b/glib/gunicollate.c
@@ -217,29 +217,25 @@ collate_key_to_string (UCCollationValue *key,
gsize key_len)
{
gchar *result;
- gsize result_len;
- gsize i;
-
- /* Pretty smart algorithm here: ignore first eight bytes of the
- * collation key. It doesn't produce results equivalent to
- * UCCompareCollationKeys's, but the difference seems to be only
- * that UCCompareCollationKeys in some cases produces 0 where our
- * comparison gets -1 or 1. */
+ gsize result_len = 0;
+ gsize i, start;
- if (key_len * sizeof (UCCollationValue) <= 8)
+ /* 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. */
+ start = 2 * sizeof(void *) / sizeof(UCCollationValue);
+ if (key_len <= start)
return g_strdup ("");
- result_len = 0;
- for (i = 8; i < key_len * sizeof (UCCollationValue); i++)
- /* there may be nul bytes, encode byteval+1 */
- result_len += utf8_encode (NULL, *((guchar*)key + i) + 1);
+ for (i = start; i < key_len; i++)
+ result_len += utf8_encode (NULL, htonl(key[i]+1));
result = g_malloc (result_len + 1);
result_len = 0;
- for (i = 8; i < key_len * sizeof (UCCollationValue); i++)
- result_len += utf8_encode (result + result_len, *((guchar*)key + i) + 1);
+ for (i = start; i < key_len; i++)
+ result_len += utf8_encode (result + result_len, htonl(key[i]+1));
- result[result_len] = 0;
+ result[result_len] = '\0';
return result;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment