Adjust to changed parameter names.
[platform/upstream/glib.git] / glib / gunicollate.c
1 /* gunicollate.c - Collation
2  *
3  *  Copyright 2001 Red Hat, Inc.
4  *
5  * The Gnome Library is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Lesser General Public License as
7  * published by the Free Software Foundation; either version 2 of the
8  * License, or (at your option) any later version.
9  *
10  * The Gnome 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.
14  *
15  * You should have received a copy of the GNU Lesser General Public
16  * License along with the Gnome Library; see the file COPYING.LIB.  If not,
17  * write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18  *   Boston, MA 02111-1307, USA.
19  */
20
21 #include <locale.h>
22 #include <string.h>
23 #ifdef __STDC_ISO_10646__
24 #include <wchar.h>
25 #endif
26
27 #include "glib.h"
28
29 extern gunichar *_g_utf8_normalize_wc (const gchar    *str,
30                                        gssize          max_len,
31                                        GNormalizeMode  mode);
32
33 /**
34  * g_utf8_collate:
35  * @str1: a UTF-8 encoded string
36  * @str2: a UTF-8 encoded string
37  * 
38  * Compares two strings for ordering using the linguistically
39  * correct rules for the current locale. When sorting a large
40  * number of strings, it will be significantly faster to
41  * obtain collation keys with g_utf8_collate_key() and 
42  * compare the keys with <function>strcmp()</function> when 
43  * sorting instead of sorting the original strings.
44  * 
45  * Return value: -1 if @str1 compares before @str2, 0 if they
46  *   compare equal, 1 if @str1 compares after @str2.
47  **/
48 gint
49 g_utf8_collate (const gchar *str1,
50                 const gchar *str2)
51 {
52   gint result;
53   
54 #ifdef __STDC_ISO_10646__
55
56   gunichar *str1_norm = _g_utf8_normalize_wc (str1, -1, G_NORMALIZE_ALL_COMPOSE);
57   gunichar *str2_norm = _g_utf8_normalize_wc (str2, -1, G_NORMALIZE_ALL_COMPOSE);
58
59   result = wcscoll ((wchar_t *)str1_norm, (wchar_t *)str2_norm);
60
61   g_free (str1_norm);
62   g_free (str2_norm);
63
64 #else /* !__STDC_ISO_10646__ */
65
66   const gchar *charset;
67   gchar *str1_norm = g_utf8_normalize (str1, -1, G_NORMALIZE_ALL_COMPOSE);
68   gchar *str2_norm = g_utf8_normalize (str2, -1, G_NORMALIZE_ALL_COMPOSE);
69
70   if (g_get_charset (&charset))
71     {
72       result = strcoll (str1_norm, str2_norm);
73     }
74   else
75     {
76       gchar *str1_locale = g_convert (str1_norm, -1, "UTF-8", charset, NULL, NULL, NULL);
77       gchar *str2_locale = g_convert (str2_norm, -1, "UTF-8", charset, NULL, NULL, NULL);
78
79       if (str1_locale && str2_locale)
80         result =  strcoll (str1_locale, str2_locale);
81       else if (str1_locale)
82         result = -1;
83       else if (str2_locale)
84         result = 1;
85       else
86         result = strcmp (str1_norm, str2_norm);
87
88       g_free (str1_locale);
89       g_free (str2_locale);
90     }
91
92   g_free (str1_norm);
93   g_free (str2_norm);
94
95 #endif /* __STDC_ISO_10646__ */
96
97   return result;
98 }
99
100 #ifdef __STDC_ISO_10646__
101 /* We need UTF-8 encoding of numbers to encode the weights if
102  * we are using wcsxfrm. However, we aren't encoding Unicode
103  * characters, so we can't simply use g_unichar_to_utf8.
104  *
105  * The following routine is taken (with modification) from GNU
106  * libc's strxfrm routine:
107  *
108  * Copyright (C) 1995-1999,2000,2001 Free Software Foundation, Inc.
109  * Written by Ulrich Drepper <drepper@cygnus.com>, 1995.
110  */
111 static inline int
112 utf8_encode (char *buf, wchar_t val)
113 {
114   int retval;
115
116   if (val < 0x80)
117     {
118       if (buf)
119         *buf++ = (char) val;
120       retval = 1;
121     }
122   else
123     {
124       int step;
125
126       for (step = 2; step < 6; ++step)
127         if ((val & (~(guint32)0 << (5 * step + 1))) == 0)
128           break;
129       retval = step;
130
131       if (buf)
132         {
133           *buf = (unsigned char) (~0xff >> step);
134           --step;
135           do
136             {
137               buf[step] = 0x80 | (val & 0x3f);
138               val >>= 6;
139             }
140           while (--step > 0);
141           *buf |= val;
142         }
143     }
144
145   return retval;
146 }
147 #endif /* __STDC_ISO_10646__ */
148
149 /**
150  * g_utf8_collate_key:
151  * @str: a UTF-8 encoded string.
152  * @len: length of @str, in bytes, or -1 if @str is nul-terminated.
153  *
154  * Converts a string into a collation key that can be compared
155  * with other collation keys using <function>strcmp()</function>. 
156  * The results of comparing the collation keys of two strings 
157  * with <function>strcmp()</function> will always be the same as 
158  * comparing the two original keys with g_utf8_collate().
159  * 
160  * Return value: a newly allocated string. This string should
161  *   be freed with g_free() when you are done with it.
162  **/
163 gchar *
164 g_utf8_collate_key (const gchar *str,
165                     gssize       len)
166 {
167   gchar *result;
168   size_t xfrm_len;
169   
170 #ifdef __STDC_ISO_10646__
171
172   gunichar *str_norm = _g_utf8_normalize_wc (str, len, G_NORMALIZE_ALL_COMPOSE);
173   wchar_t *result_wc;
174   size_t i;
175   size_t result_len = 0;
176
177   setlocale (LC_COLLATE, "");
178
179   xfrm_len = wcsxfrm (NULL, (wchar_t *)str_norm, 0);
180   result_wc = g_new (wchar_t, xfrm_len + 1);
181   wcsxfrm (result_wc, (wchar_t *)str_norm, xfrm_len + 1);
182
183   for (i=0; i < xfrm_len; i++)
184     result_len += utf8_encode (NULL, result_wc[i]);
185
186   result = g_malloc (result_len + 1);
187   result_len = 0;
188   for (i=0; i < xfrm_len; i++)
189     result_len += utf8_encode (result + result_len, result_wc[i]);
190
191   result[result_len] = '\0';
192
193   g_free (result_wc);
194   g_free (str_norm);
195
196   return result;
197 #else /* !__STDC_ISO_10646__ */
198
199   const gchar *charset;
200   gchar *str_norm = g_utf8_normalize (str, len, G_NORMALIZE_ALL_COMPOSE);
201
202   if (g_get_charset (&charset))
203     {
204       xfrm_len = strxfrm (NULL, str_norm, 0);
205       result = g_malloc (xfrm_len + 1);
206       strxfrm (result, str_norm, xfrm_len + 1);
207     }
208   else
209     {
210       gchar *str_locale = g_convert (str_norm, -1, "UTF-8", charset, NULL, NULL, NULL);
211
212       if (str_locale)
213         {
214           xfrm_len = strxfrm (NULL, str_locale, 0);
215           result = g_malloc (xfrm_len + 2);
216           result[0] = 'A';
217           strxfrm (result + 1, str_locale, xfrm_len + 1);
218           
219           g_free (str_locale);
220         }
221       else
222         {
223           xfrm_len = strlen (str_norm);
224           result = g_malloc (xfrm_len + 2);
225           result[0] = 'B';
226           memcpy (result + 1, str_norm, xfrm_len);
227           result[xfrm_len+1] = '\0';
228         }
229     }
230
231   g_free (str_norm);
232 #endif /* __STDC_ISO_10646__ */
233
234   return result;
235 }