#include "config.h"
#include <locale.h>
#include <string.h>
#ifdef __STDC_ISO_10646__
#include <wchar.h>
#endif
#include "glib.h"
#include "gunicodeprivate.h"
gint
g_utf8_collate (const gchar *str1,
const gchar *str2)
{
gint result;
#ifdef __STDC_ISO_10646__
gunichar *str1_norm;
gunichar *str2_norm;
g_return_val_if_fail (str1 != NULL, 0);
g_return_val_if_fail (str2 != NULL, 0);
str1_norm = _g_utf8_normalize_wc (str1, -1, G_NORMALIZE_ALL_COMPOSE);
str2_norm = _g_utf8_normalize_wc (str2, -1, G_NORMALIZE_ALL_COMPOSE);
result = wcscoll ((wchar_t *)str1_norm, (wchar_t *)str2_norm);
g_free (str1_norm);
g_free (str2_norm);
#else
const gchar *charset;
gchar *str1_norm;
gchar *str2_norm;
g_return_val_if_fail (str1 != NULL, 0);
g_return_val_if_fail (str2 != NULL, 0);
str1_norm = g_utf8_normalize (str1, -1, G_NORMALIZE_ALL_COMPOSE);
str2_norm = g_utf8_normalize (str2, -1, G_NORMALIZE_ALL_COMPOSE);
if (g_get_charset (&charset))
{
result = strcoll (str1_norm, str2_norm);
}
else
{
gchar *str1_locale = g_convert (str1_norm, -1, "UTF-8", charset, NULL, NULL, NULL);
gchar *str2_locale = g_convert (str2_norm, -1, "UTF-8", charset, NULL, NULL, NULL);
if (str1_locale && str2_locale)
result = strcoll (str1_locale, str2_locale);
else if (str1_locale)
result = -1;
else if (str2_locale)
result = 1;
else
result = strcmp (str1_norm, str2_norm);
g_free (str1_locale);
g_free (str2_locale);
}
g_free (str1_norm);
g_free (str2_norm);
#endif
return result;
}
#ifdef __STDC_ISO_10646__
static inline int
utf8_encode (char *buf, wchar_t val)
{
int retval;
if (val < 0x80)
{
if (buf)
*buf++ = (char) val;
retval = 1;
}
else
{
int step;
for (step = 2; step < 6; ++step)
if ((val & (~(guint32)0 << (5 * step + 1))) == 0)
break;
retval = step;
if (buf)
{
*buf = (unsigned char) (~0xff >> step);
--step;
do
{
buf[step] = 0x80 | (val & 0x3f);
val >>= 6;
}
while (--step > 0);
*buf |= val;
}
}
return retval;
}
#endif
gchar *
g_utf8_collate_key (const gchar *str,
gssize len)
{
gchar *result;
size_t xfrm_len;
#ifdef __STDC_ISO_10646__
gunichar *str_norm;
wchar_t *result_wc;
size_t i;
size_t result_len = 0;
g_return_val_if_fail (str != NULL, NULL);
str_norm = _g_utf8_normalize_wc (str, len, G_NORMALIZE_ALL_COMPOSE);
setlocale (LC_COLLATE, "");
xfrm_len = wcsxfrm (NULL, (wchar_t *)str_norm, 0);
result_wc = g_new (wchar_t, xfrm_len + 1);
wcsxfrm (result_wc, (wchar_t *)str_norm, xfrm_len + 1);
for (i=0; i < xfrm_len; i++)
result_len += utf8_encode (NULL, result_wc[i]);
result = g_malloc (result_len + 1);
result_len = 0;
for (i=0; i < xfrm_len; i++)
result_len += utf8_encode (result + result_len, result_wc[i]);
result[result_len] = '\0';
g_free (result_wc);
g_free (str_norm);
return result;
#else
const gchar *charset;
gchar *str_norm;
g_return_val_if_fail (str != NULL, NULL);
str_norm = g_utf8_normalize (str, len, G_NORMALIZE_ALL_COMPOSE);
if (g_get_charset (&charset))
{
xfrm_len = strxfrm (NULL, str_norm, 0);
result = g_malloc (xfrm_len + 1);
strxfrm (result, str_norm, xfrm_len + 1);
}
else
{
gchar *str_locale = g_convert (str_norm, -1, "UTF-8", charset, NULL, NULL, NULL);
if (str_locale)
{
xfrm_len = strxfrm (NULL, str_locale, 0);
result = g_malloc (xfrm_len + 2);
result[0] = 'A';
strxfrm (result + 1, str_locale, xfrm_len + 1);
g_free (str_locale);
}
else
{
xfrm_len = strlen (str_norm);
result = g_malloc (xfrm_len + 2);
result[0] = 'B';
memcpy (result + 1, str_norm, xfrm_len);
result[xfrm_len+1] = '\0';
}
}
g_free (str_norm);
#endif
return result;
}