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