1 /* gutf8.c - Operations on UTF-8 strings.
3 * Copyright (C) 1999 Tom Tromey
4 * Copyright (C) 2000 Red Hat, Inc.
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.
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.
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.
32 #define UTF8_COMPUTE(Char, Mask, Len) \
38 else if ((Char & 0xe0) == 0xc0) \
43 else if ((Char & 0xf0) == 0xe0) \
48 else if ((Char & 0xf8) == 0xf0) \
53 else if ((Char & 0xfc) == 0xf8) \
58 else if ((Char & 0xfe) == 0xfc) \
66 #define UTF8_GET(Result, Chars, Count, Mask, Len) \
67 (Result) = (Chars)[0] & (Mask); \
68 for ((Count) = 1; (Count) < (Len); ++(Count)) \
70 if (((Chars)[(Count)] & 0xc0) != 0x80) \
76 (Result) |= ((Chars)[(Count)] & 0x3f); \
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
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
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.
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.
102 * Return value: a pointer to the found character or %NULL.
105 g_utf8_find_prev_char (const char *str,
108 for (--p; p > str; --p)
110 if ((*p & 0xc0) != 0x80)
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
123 * Find the start of the next utf-8 character in the string after @p
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.
129 * Return value: a pointer to the found character or %NULL
132 g_utf8_find_next_char (const gchar *p,
138 for (++p; p < end && (*p & 0xc0) == 0x80; ++p)
141 for (++p; (*p & 0xc0) == 0x80; ++p)
144 return (p == end) ? NULL : (gchar *)p;
149 * @p: a pointer to a position within a UTF-8 encoded string
151 * Find the previous UTF-8 character in the string before @p
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.
158 * Return value: a pointer to the found character.
161 g_utf8_prev_char (const gchar *p)
166 if ((*p & 0xc0) != 0x80)
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
178 * Return value: the length of the string in characters
181 g_utf8_strlen (const gchar *p, gint max)
184 const gchar *start = p;
185 /* special case for the empty string */
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)
194 p = g_utf8_next_char (p);
196 if (! *p || (max > 0 && p - start > max))
204 * @p: a pointer to unicode character encoded as UTF-8
206 * Convert a sequence of bytes encoded as UTF-8 to a unicode character.
208 * Return value: the resulting character or (gunichar)-1 if @p does
209 * not point to a valid UTF-8 encoded unicode character
212 g_utf8_get_char (const gchar *p)
214 int i, mask = 0, len;
216 unsigned char c = (unsigned char) *p;
218 UTF8_COMPUTE (c, mask, len);
221 UTF8_GET (result, p, i, mask, len);
227 * g_utf8_offset_to_pointer:
228 * @str: a UTF-8 encoded string
229 * @offset: a character offset within the string.
231 * Converts from an integer character offset to a pointer to a position
234 * Return value: the resulting pointer
237 g_utf8_offset_to_pointer (const gchar *str,
240 const gchar *s = str;
242 s = g_utf8_next_char (s);
248 * g_utf8_pointer_to_offset:
249 * @str: a UTF-8 encoded string
250 * @pos: a pointer to a position within @str
252 * Converts from a pointer to position within a string to a integer
255 * Return value: the resulting character offset
258 g_utf8_pointer_to_offset (const gchar *str,
261 const gchar *s = str;
266 s = g_utf8_next_char (s);
275 g_utf8_strncpy (gchar *dest, const gchar *src, size_t n)
277 const gchar *s = src;
280 s = g_utf8_next_char(s);
283 strncpy(dest, src, s - src);
289 g_utf8_get_charset_internal (char **a)
291 char *charset = getenv("CHARSET");
293 if (charset && a && ! *a)
296 if (charset && strstr (charset, "UTF-8"))
300 charset = nl_langinfo(CODESET);
305 if (strcmp (charset, "UTF-8") == 0)
310 #if 0 /* #ifdef _NL_CTYPE_CODESET_NAME */
311 charset = nl_langinfo (_NL_CTYPE_CODESET_NAME);
316 if (strcmp (charset, "UTF-8") == 0)
323 /* Assume this for compatibility at present. */
327 static int utf8_locale_cache = -1;
328 static char *utf8_charset_cache = NULL;
331 g_get_charset (char **charset)
333 if (utf8_locale_cache != -1)
336 *charset = utf8_charset_cache;
337 return utf8_locale_cache;
339 utf8_locale_cache = g_utf8_get_charset_internal (&utf8_charset_cache);
341 *charset = utf8_charset_cache;
342 return utf8_locale_cache;
349 * @ch: a ISO10646 character code
350 * @out: 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.
354 * Convert a single character to utf8
356 * Return value: number of bytes written
359 g_unichar_to_utf8 (gunichar c, gchar *outbuf)
375 else if (c < 0x10000)
380 else if (c < 0x200000)
385 else if (c < 0x4000000)
398 for (i = len - 1; i > 0; --i)
400 outbuf[i] = (c & 0x3f) | 0x80;
403 outbuf[0] = c | first;
411 * @p: a nul-terminated utf-8 string
412 * @c: a iso-10646 character/
414 * Find the leftmost occurence of the given iso-10646 character
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.
421 g_utf8_strchr (const char *p, gunichar c)
425 gint len = g_unichar_to_utf8 (c, ch);
428 return strstr(p, ch);
434 * @p: a nul-terminated utf-8 string
435 * @c: a iso-10646 character/
437 * Find the rightmost occurence of the given iso-10646 character
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.
444 /* This is ifdefed out atm as there is no strrstr function in libc.
447 unicode_strrchr (const char *p, gunichar c)
451 len = g_unichar_to_utf8 (c, ch);
454 return strrstr(p, ch);
461 * @str: a UTF-8 encoded strnig
462 * @len: the length of @
464 * Convert a string from UTF-8 to a 32-bit fixed width
465 * representation as UCS-4.
467 * Return value: a pointer to a newly allocated UCS-4 string.
468 * This value must be freed with g_free()
471 g_utf8_to_ucs4 (const char *str, int len)
477 n_chars = g_utf8_strlen (str, len);
478 result = g_new (gunichar, n_chars);
481 for (i=0; i < n_chars; i++)
483 result[i] = g_utf8_get_char (p);
484 p = g_utf8_next_char (p);