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