Make PLT-reduction work with gcc4, and don't include everything in
[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 "config.h"
22
23 #include <locale.h>
24 #include <string.h>
25 #ifdef __STDC_ISO_10646__
26 #include <wchar.h>
27 #endif
28
29 #include "glib.h"
30 #include "gunicodeprivate.h"
31 #include "galias.h"
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 strcmp() when 
43  * sorting instead of sorting the original strings.
44  * 
45  * Return value: &lt; 0 if @str1 compares before @str2, 
46  *   0 if they compare equal, &gt; 0 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;
57   gunichar *str2_norm;
58
59   g_return_val_if_fail (str1 != NULL, 0);
60   g_return_val_if_fail (str2 != NULL, 0);
61
62   str1_norm = _g_utf8_normalize_wc (str1, -1, G_NORMALIZE_ALL_COMPOSE);
63   str2_norm = _g_utf8_normalize_wc (str2, -1, G_NORMALIZE_ALL_COMPOSE);
64
65   result = wcscoll ((wchar_t *)str1_norm, (wchar_t *)str2_norm);
66
67   g_free (str1_norm);
68   g_free (str2_norm);
69
70 #else /* !__STDC_ISO_10646__ */
71
72   const gchar *charset;
73   gchar *str1_norm;
74   gchar *str2_norm;
75
76   g_return_val_if_fail (str1 != NULL, 0);
77   g_return_val_if_fail (str2 != NULL, 0);
78
79   str1_norm = g_utf8_normalize (str1, -1, G_NORMALIZE_ALL_COMPOSE);
80   str2_norm = g_utf8_normalize (str2, -1, G_NORMALIZE_ALL_COMPOSE);
81
82   if (g_get_charset (&charset))
83     {
84       result = strcoll (str1_norm, str2_norm);
85     }
86   else
87     {
88       gchar *str1_locale = g_convert (str1_norm, -1, charset, "UTF-8", NULL, NULL, NULL);
89       gchar *str2_locale = g_convert (str2_norm, -1, charset, "UTF-8", NULL, NULL, NULL);
90
91       if (str1_locale && str2_locale)
92         result =  strcoll (str1_locale, str2_locale);
93       else if (str1_locale)
94         result = -1;
95       else if (str2_locale)
96         result = 1;
97       else
98         result = strcmp (str1_norm, str2_norm);
99
100       g_free (str1_locale);
101       g_free (str2_locale);
102     }
103
104   g_free (str1_norm);
105   g_free (str2_norm);
106
107 #endif /* __STDC_ISO_10646__ */
108
109   return result;
110 }
111
112 #ifdef __STDC_ISO_10646__
113 /* We need UTF-8 encoding of numbers to encode the weights if
114  * we are using wcsxfrm. However, we aren't encoding Unicode
115  * characters, so we can't simply use g_unichar_to_utf8.
116  *
117  * The following routine is taken (with modification) from GNU
118  * libc's strxfrm routine:
119  *
120  * Copyright (C) 1995-1999,2000,2001 Free Software Foundation, Inc.
121  * Written by Ulrich Drepper <drepper@cygnus.com>, 1995.
122  */
123 static inline int
124 utf8_encode (char *buf, wchar_t val)
125 {
126   int retval;
127
128   if (val < 0x80)
129     {
130       if (buf)
131         *buf++ = (char) val;
132       retval = 1;
133     }
134   else
135     {
136       int step;
137
138       for (step = 2; step < 6; ++step)
139         if ((val & (~(guint32)0 << (5 * step + 1))) == 0)
140           break;
141       retval = step;
142
143       if (buf)
144         {
145           *buf = (unsigned char) (~0xff >> step);
146           --step;
147           do
148             {
149               buf[step] = 0x80 | (val & 0x3f);
150               val >>= 6;
151             }
152           while (--step > 0);
153           *buf |= val;
154         }
155     }
156
157   return retval;
158 }
159 #endif /* __STDC_ISO_10646__ */
160
161 /**
162  * g_utf8_collate_key:
163  * @str: a UTF-8 encoded string.
164  * @len: length of @str, in bytes, or -1 if @str is nul-terminated.
165  *
166  * Converts a string into a collation key that can be compared
167  * with other collation keys using strcmp(). 
168  * The results of comparing the collation keys of two strings 
169  * with strcmp() will always be the same as 
170  * comparing the two original keys with g_utf8_collate().
171  * 
172  * Return value: a newly allocated string. This string should
173  *   be freed with g_free() when you are done with it.
174  **/
175 gchar *
176 g_utf8_collate_key (const gchar *str,
177                     gssize       len)
178 {
179   gchar *result;
180   size_t xfrm_len;
181   
182 #ifdef __STDC_ISO_10646__
183
184   gunichar *str_norm;
185   wchar_t *result_wc;
186   size_t i;
187   size_t result_len = 0;
188
189   g_return_val_if_fail (str != NULL, NULL);
190
191   str_norm = _g_utf8_normalize_wc (str, len, G_NORMALIZE_ALL_COMPOSE);
192
193   setlocale (LC_COLLATE, "");
194
195   xfrm_len = wcsxfrm (NULL, (wchar_t *)str_norm, 0);
196   result_wc = g_new (wchar_t, xfrm_len + 1);
197   wcsxfrm (result_wc, (wchar_t *)str_norm, xfrm_len + 1);
198
199   for (i=0; i < xfrm_len; i++)
200     result_len += utf8_encode (NULL, result_wc[i]);
201
202   result = g_malloc (result_len + 1);
203   result_len = 0;
204   for (i=0; i < xfrm_len; i++)
205     result_len += utf8_encode (result + result_len, result_wc[i]);
206
207   result[result_len] = '\0';
208
209   g_free (result_wc);
210   g_free (str_norm);
211
212   return result;
213 #else /* !__STDC_ISO_10646__ */
214
215   const gchar *charset;
216   gchar *str_norm;
217
218   g_return_val_if_fail (str != NULL, NULL);
219
220   str_norm = g_utf8_normalize (str, len, G_NORMALIZE_ALL_COMPOSE);
221
222   if (g_get_charset (&charset))
223     {
224       xfrm_len = strxfrm (NULL, str_norm, 0);
225       result = g_malloc (xfrm_len + 1);
226       strxfrm (result, str_norm, xfrm_len + 1);
227     }
228   else
229     {
230       gchar *str_locale = g_convert (str_norm, -1, charset, "UTF-8", NULL, NULL, NULL);
231
232       if (str_locale)
233         {
234           xfrm_len = strxfrm (NULL, str_locale, 0);
235           if (xfrm_len < 0 || xfrm_len >= G_MAXINT - 2)
236             {
237               g_free (str_locale);
238               str_locale = NULL;
239             }
240         }
241       if (str_locale)
242         {
243           result = g_malloc (xfrm_len + 2);
244           result[0] = 'A';
245           strxfrm (result + 1, str_locale, xfrm_len + 1);
246           
247           g_free (str_locale);
248         }
249       else
250         {
251           xfrm_len = strlen (str_norm);
252           result = g_malloc (xfrm_len + 2);
253           result[0] = 'B';
254           memcpy (result + 1, str_norm, xfrm_len);
255           result[xfrm_len+1] = '\0';
256         }
257     }
258
259   g_free (str_norm);
260 #endif /* __STDC_ISO_10646__ */
261
262   return result;
263 }
264
265 #define __G_UNICOLLATE_C__
266 #include "galiasdef.c"