Some inline docs fixes.
[platform/upstream/glib.git] / glib / gutf8.c
1 /* gutf8.c - Operations on UTF-8 strings.
2  *
3  * Copyright (C) 1999 Tom Tromey
4  * Copyright (C) 2000 Red Hat, Inc.
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2 of the License, or (at your option) any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with this library; if not, write to the
18  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
19  * Boston, MA 02111-1307, USA.
20  */
21
22 #include <config.h>
23
24 #include <stdlib.h>
25 #ifdef HAVE_CODESET
26 #include <langinfo.h>
27 #endif
28 #include <string.h>
29
30 #include "glib.h"
31
32 #define UTF8_COMPUTE(Char, Mask, Len)                                         \
33   if (Char < 128)                                                             \
34     {                                                                         \
35       Len = 1;                                                                \
36       Mask = 0x7f;                                                            \
37     }                                                                         \
38   else if ((Char & 0xe0) == 0xc0)                                             \
39     {                                                                         \
40       Len = 2;                                                                \
41       Mask = 0x1f;                                                            \
42     }                                                                         \
43   else if ((Char & 0xf0) == 0xe0)                                             \
44     {                                                                         \
45       Len = 3;                                                                \
46       Mask = 0x0f;                                                            \
47     }                                                                         \
48   else if ((Char & 0xf8) == 0xf0)                                             \
49     {                                                                         \
50       Len = 4;                                                                \
51       Mask = 0x07;                                                            \
52     }                                                                         \
53   else if ((Char & 0xfc) == 0xf8)                                             \
54     {                                                                         \
55       Len = 5;                                                                \
56       Mask = 0x03;                                                            \
57     }                                                                         \
58   else if ((Char & 0xfe) == 0xfc)                                             \
59     {                                                                         \
60       Len = 6;                                                                \
61       Mask = 0x01;                                                            \
62     }                                                                         \
63   else                                                                        \
64     Len = -1;
65
66 #define UTF8_GET(Result, Chars, Count, Mask, Len)                             \
67   (Result) = (Chars)[0] & (Mask);                                             \
68   for ((Count) = 1; (Count) < (Len); ++(Count))                               \
69     {                                                                         \
70       if (((Chars)[(Count)] & 0xc0) != 0x80)                                  \
71         {                                                                     \
72           (Result) = -1;                                                      \
73           break;                                                              \
74         }                                                                     \
75       (Result) <<= 6;                                                         \
76       (Result) |= ((Chars)[(Count)] & 0x3f);                                  \
77     }
78 gchar g_utf8_skip[256] = {
79   1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,
80   1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,
81   1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,
82   1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,
83   1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,
84   1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,
85   2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,
86   3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,4,4,4,4,4,4,4,4,5,5,5,5,6,6,0,0
87 };
88
89 /**
90  * g_utf8_find_prev_char:
91  * @str: pointer to the beginning of a UTF-8 string
92  * @p: pointer to some position within @str
93  * 
94  * Given a position @p with a UTF-8 encoded string @str, find the start
95  * of the previous UTF-8 character starting before @p. Returns %NULL if no
96  * UTF-8 characters are present in @p before @str.
97  *
98  * @p does not have to be at the beginning of a UTF-8 chracter. No check
99  * is made to see if the character found is actually valid other than
100  * it starts with an appropriate byte.
101  *
102  * Return value: a pointer to the found character or %NULL.
103  **/
104 gchar *
105 g_utf8_find_prev_char (const char *str,
106                        const char *p)
107 {
108   for (--p; p > str; --p)
109     {
110       if ((*p & 0xc0) != 0x80)
111         return (gchar *)p;
112     }
113   return NULL;
114 }
115
116 /**
117  * g_utf8_find_next_char:
118  * @p: a pointer to a position within a UTF-8 encoded string
119  * @end: a pointer to the end of the string, or %NULL to indicate
120  *        that the string is NULL terminated, in which case
121  *        the returned value will be 
122  *
123  * Find the start of the next utf-8 character in the string after @p
124  *
125  * @p does not have to be at the beginning of a UTF-8 chracter. No check
126  * is made to see if the character found is actually valid other than
127  * it starts with an appropriate byte.
128  * 
129  * Return value: a pointer to the found character or %NULL
130  **/
131 gchar *
132 g_utf8_find_next_char (const gchar *p,
133                        const gchar *end)
134 {
135   if (*p)
136     {
137       if (end)
138         for (++p; p < end && (*p & 0xc0) == 0x80; ++p)
139           ;
140       else
141         for (++p; (*p & 0xc0) == 0x80; ++p)
142           ;
143     }
144   return (p == end) ? NULL : (gchar *)p;
145 }
146
147 /**
148  * g_utf8_prev_char:
149  * @p: a pointer to a position within a UTF-8 encoded string
150  *
151  * Find the previous UTF-8 character in the string before @p
152  *
153  * @p does not have to be at the beginning of a UTF-8 character. No check
154  * is made to see if the character found is actually valid other than
155  * it starts with an appropriate byte. If @p might be the first
156  * character of the string, you must use g_utf8_find_prev_char instead.
157  * 
158  * Return value: a pointer to the found character.
159  **/
160 gchar *
161 g_utf8_prev_char (const gchar *p)
162 {
163   while (TRUE)
164     {
165       p--;
166       if ((*p & 0xc0) != 0x80)
167         return (gchar *)p;
168     }
169 }
170
171 /**
172  * g_utf8_strlen:
173  * @p: pointer to the start of a UTF-8 string.
174  * @max: the maximum number of bytes to examine. If @max
175  *       is less than 0, then the string is assumed to be
176  *       nul-terminated.
177  * 
178  * Return value: the length of the string in characters
179  */
180 gint
181 g_utf8_strlen (const gchar *p, gint max)
182 {
183   int len = 0;
184   const gchar *start = p;
185   /* special case for the empty string */
186   if (!*p) 
187     return 0;
188   /* Note that the test here and the test in the loop differ subtly.
189      In the loop we want to see if we've passed the maximum limit --
190      for instance if the buffer ends mid-character.  Here at the top
191      of the loop we want to see if we've just reached the last byte.  */
192   while (max < 0 || p - start < max)
193     {
194       p = g_utf8_next_char (p);
195       ++len;
196       if (! *p || (max > 0 && p - start > max))
197         break;
198     }
199   return len;
200 }
201
202 /**
203  * g_utf8_get_char:
204  * @p: a pointer to unicode character encoded as UTF-8
205  * 
206  * Convert a sequence of bytes encoded as UTF-8 to a unicode character.
207  * 
208  * Return value: the resulting character or (gunichar)-1 if @p does
209  *               not point to a valid UTF-8 encoded unicode character
210  **/
211 gunichar
212 g_utf8_get_char (const gchar *p)
213 {
214   int i, mask = 0, len;
215   gunichar result;
216   unsigned char c = (unsigned char) *p;
217
218   UTF8_COMPUTE (c, mask, len);
219   if (len == -1)
220     return (gunichar)-1;
221   UTF8_GET (result, p, i, mask, len);
222
223   return result;
224 }
225
226 /**
227  * g_utf8_offset_to_pointer:
228  * @str: a UTF-8 encoded string
229  * @offset: a character offset within the string.
230  * 
231  * Converts from an integer character offset to a pointer to a position
232  * within the string.
233  * 
234  * Return value: the resulting pointer
235  **/
236 gchar *
237 g_utf8_offset_to_pointer  (const gchar *str,
238                            gint         offset)
239 {
240   const gchar *s = str;
241   while (offset--)
242     s = g_utf8_next_char (s);
243   
244   return (gchar *)s;
245 }
246
247 /**
248  * g_utf8_pointer_to_offset:
249  * @str: a UTF-8 encoded string
250  * @pos: a pointer to a position within @str
251  * 
252  * Converts from a pointer to position within a string to a integer
253  * character offset
254  * 
255  * Return value: the resulting character offset
256  **/
257 gint
258 g_utf8_pointer_to_offset (const gchar *str,
259                           const gchar *pos)
260 {
261   const gchar *s = str;
262   gint offset = 0;
263   
264   while (s < pos)
265     {
266       s = g_utf8_next_char (s);
267       offset++;
268     }
269
270   return offset;
271 }
272
273
274 gchar *
275 g_utf8_strncpy (gchar *dest, const gchar *src, size_t n)
276 {
277   const gchar *s = src;
278   while (n && *s)
279     {
280       s = g_utf8_next_char(s);
281       n--;
282     }
283   strncpy(dest, src, s - src);
284   dest[s - src] = 0;
285   return dest;
286 }
287
288 static gboolean
289 g_utf8_get_charset_internal (char **a)
290 {
291   char *charset = getenv("CHARSET");
292
293   if (charset && a && ! *a)
294     *a = charset;
295
296   if (charset && strstr (charset, "UTF-8"))
297       return TRUE;
298
299 #ifdef HAVE_CODESET
300   charset = nl_langinfo(CODESET);
301   if (charset)
302     {
303       if (a && ! *a)
304         *a = charset;
305       if (strcmp (charset, "UTF-8") == 0)
306         return TRUE;
307     }
308 #endif
309   
310 #if 0 /* #ifdef _NL_CTYPE_CODESET_NAME */
311   charset = nl_langinfo (_NL_CTYPE_CODESET_NAME);
312   if (charset)
313     {
314       if (a && ! *a)
315         *a = charset;
316       if (strcmp (charset, "UTF-8") == 0)
317         return TRUE;
318     }
319 #endif
320
321   if (a && ! *a) 
322     *a = "US-ASCII";
323   /* Assume this for compatibility at present.  */
324   return FALSE;
325 }
326
327 static int utf8_locale_cache = -1;
328 static char *utf8_charset_cache = NULL;
329
330 gboolean
331 g_get_charset (char **charset) 
332 {
333   if (utf8_locale_cache != -1)
334     {
335       if (charset)
336         *charset = utf8_charset_cache;
337       return utf8_locale_cache;
338     }
339   utf8_locale_cache = g_utf8_get_charset_internal (&utf8_charset_cache);
340   if (charset) 
341     *charset = utf8_charset_cache;
342   return utf8_locale_cache;
343 }
344
345 /* unicode_strchr */
346
347 /**
348  * g_unichar_to_utf8:
349  * @c: a ISO10646 character code
350  * @outbuf: output buffer, must have at least 6 bytes of space.
351  *       If %NULL, the length will be computed and returned
352  *       and nothing will be written to @out.
353  * 
354  * Convert a single character to utf8
355  * 
356  * Return value: number of bytes written
357  **/
358 int
359 g_unichar_to_utf8 (gunichar c, gchar *outbuf)
360 {
361   size_t len = 0;
362   int first;
363   int i;
364
365   if (c < 0x80)
366     {
367       first = 0;
368       len = 1;
369     }
370   else if (c < 0x800)
371     {
372       first = 0xc0;
373       len = 2;
374     }
375   else if (c < 0x10000)
376     {
377       first = 0xe0;
378       len = 3;
379     }
380    else if (c < 0x200000)
381     {
382       first = 0xf0;
383       len = 4;
384     }
385   else if (c < 0x4000000)
386     {
387       first = 0xf8;
388       len = 5;
389     }
390   else
391     {
392       first = 0xfc;
393       len = 6;
394     }
395
396   if (outbuf)
397     {
398       for (i = len - 1; i > 0; --i)
399         {
400           outbuf[i] = (c & 0x3f) | 0x80;
401           c >>= 6;
402         }
403       outbuf[0] = c | first;
404     }
405
406   return len;
407 }
408
409 /**
410  * g_utf8_strchr:
411  * @p: a nul-terminated utf-8 string
412  * @c: a iso-10646 character/
413  * 
414  * Find the leftmost occurence of the given iso-10646 character
415  * in a UTF-8 string.
416  * 
417  * Return value: NULL if the string does not contain the character, otherwise, a
418  *               a pointer to the start of the leftmost of the character in the string.
419  **/
420 gchar *
421 g_utf8_strchr (const char *p, gunichar c)
422 {
423   gchar ch[10];
424
425   gint len = g_unichar_to_utf8 (c, ch);
426   ch[len] = '\0';
427   
428   return strstr(p, ch);
429 }
430
431 #if 0
432 /**
433  * g_utf8_strrchr:
434  * @p: a nul-terminated utf-8 string
435  * @c: a iso-10646 character/
436  * 
437  * Find the rightmost occurence of the given iso-10646 character
438  * in a UTF-8 string.
439  * 
440  * Return value: NULL if the string does not contain the character, otherwise, a
441  *               a pointer to the start of the rightmost of the character in the string.
442  **/
443
444 /* This is ifdefed out atm as there is no strrstr function in libc.
445  */
446 gchar *
447 unicode_strrchr (const char *p, gunichar c)
448 {
449   gchar ch[10];
450
451   len = g_unichar_to_utf8 (c, ch);
452   ch[len] = '\0';
453   
454   return strrstr(p, ch);
455 }
456 #endif
457
458
459 /**
460  * g_utf8_to_ucs4:
461  * @str: a UTF-8 encoded strnig
462  * @len: the length of @
463  * 
464  * Convert a string from UTF-8 to a 32-bit fixed width
465  * representation as UCS-4.
466  * 
467  * Return value: a pointer to a newly allocated UCS-4 string.
468  *               This value must be freed with g_free()
469  **/
470 gunichar *
471 g_utf8_to_ucs4 (const char *str, int len)
472 {
473   gunichar *result;
474   gint n_chars, i;
475   const gchar *p;
476   
477   n_chars = g_utf8_strlen (str, len);
478   result = g_new (gunichar, n_chars);
479   
480   p = str;
481   for (i=0; i < n_chars; i++)
482     {
483       result[i] = g_utf8_get_char (p);
484       p = g_utf8_next_char (p);
485     }
486
487   return result;
488 }
489