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 Library 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 * Library General Public License for more details.
16 * You should have received a copy of the GNU Library 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.
25 #ifdef HAVE_LANGINFO_H
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); \
79 gchar g_utf8_skip[256] = {
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 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,
86 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,
87 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
91 * g_utf8_find_prev_char:
92 * @str: pointer to the beginning of a UTF-8 string
93 * @p: pointer to some position within @str
95 * Given a position @p with a UTF-8 encoded string @str, find the start
96 * of the previous UTF-8 character starting before @p. Returns %NULL if no
97 * UTF-8 characters are present in @p before @str.
99 * @p does not have to be at the beginning of a UTF-8 chracter. No check
100 * is made to see if the character found is actually valid other than
101 * it starts with an appropriate byte.
103 * Return value: a pointer to the found character or %NULL.
106 g_utf8_find_prev_char (const char *str,
109 for (--p; p > str; --p)
111 if ((*p & 0xc0) != 0x80)
118 * g_utf8_find_next_char:
119 * @p: a pointer to a position within a UTF-8 encoded string
120 * @end: a pointer to the end of the string, or %NULL to indicate
121 * that the string is NULL terminated, in which case
122 * the returned value will be
124 * Find the start of the next utf-8 character in the string after @p
126 * @p does not have to be at the beginning of a UTF-8 chracter. No check
127 * is made to see if the character found is actually valid other than
128 * it starts with an appropriate byte.
130 * Return value: a pointer to the found character or %NULL
133 g_utf8_find_next_char (const gchar *p,
139 for (++p; p < end && (*p & 0xc0) == 0x80; ++p)
142 for (++p; (*p & 0xc0) == 0x80; ++p)
145 return (p == end) ? NULL : (gchar *)p;
150 * @p: a pointer to a position within a UTF-8 encoded string
152 * Find the previous UTF-8 character in the string before @p
154 * @p does not have to be at the beginning of a UTF-8 character. No check
155 * is made to see if the character found is actually valid other than
156 * it starts with an appropriate byte. If @p might be the first
157 * character of the string, you must use g_utf8_find_prev_char instead.
159 * Return value: a pointer to the found character.
162 g_utf8_prev_char (const gchar *p)
167 if ((*p & 0xc0) != 0x80)
174 * @p: pointer to the start of a UTF-8 string.
175 * @max: the maximum number of bytes to examine. If @max
176 * is less than 0, then the string is assumed to be
179 * Return value: the length of the string in characters
182 g_utf8_strlen (const gchar *p, gint max)
185 const gchar *start = p;
186 /* special case for the empty string */
189 /* Note that the test here and the test in the loop differ subtly.
190 In the loop we want to see if we've passed the maximum limit --
191 for instance if the buffer ends mid-character. Here at the top
192 of the loop we want to see if we've just reached the last byte. */
193 while (max < 0 || p - start < max)
195 p = g_utf8_next_char (p);
197 if (! *p || (max > 0 && p - start > max))
205 * @p: a pointer to unicode character encoded as UTF-8
207 * Convert a sequence of bytes encoded as UTF-8 to a unicode character.
209 * Return value: the resulting character or (gunichar)-1 if @p does
210 * not point to a valid UTF-8 encoded unicode character
213 g_utf8_get_char (const gchar *p)
215 int i, mask = 0, len;
217 unsigned char c = (unsigned char) *p;
219 UTF8_COMPUTE (c, mask, len);
222 UTF8_GET (result, p, i, mask, len);
228 * g_utf8_offset_to_pointer:
229 * @str: a UTF-8 encoded string
230 * @offset: a character offset within the string.
232 * Converts from an integer character offset to a pointer to a position
235 * Return value: the resulting pointer
238 g_utf8_offset_to_pointer (const gchar *str,
241 const gchar *s = str;
243 s = g_utf8_next_char (s);
249 * g_utf8_pointer_to_offset:
250 * @str: a UTF-8 encoded string
251 * @pos: a pointer to a position within @str
253 * Converts from a pointer to position within a string to a integer
256 * Return value: the resulting character offset
259 g_utf8_pointer_to_offset (const gchar *str,
262 const gchar *s = str;
267 s = g_utf8_next_char (s);
276 g_utf8_strncpy (gchar *dest, const gchar *src, size_t n)
278 const gchar *s = src;
281 s = g_utf8_next_char(s);
284 strncpy(dest, src, s - src);
290 g_utf8_get_charset_internal (char **a)
292 char *charset = getenv("CHARSET");
294 if (charset && a && ! *a)
297 if (charset && strstr (charset, "UTF-8"))
300 #ifdef _NL_CTYPE_CODESET_NAME
301 charset = nl_langinfo (_NL_CTYPE_CODESET_NAME);
306 if (strcmp (charset, "UTF-8") == 0)
310 charset = nl_langinfo(CODESET);
315 if (strcmp (charset, "UTF-8") == 0)
322 /* Assume this for compatibility at present. */
326 static int utf8_locale_cache = -1;
327 static char *utf8_charset_cache = NULL;
330 g_get_charset (char **charset)
332 if (utf8_locale_cache != -1)
335 *charset = utf8_charset_cache;
336 return utf8_locale_cache;
338 utf8_locale_cache = g_utf8_get_charset_internal (&utf8_charset_cache);
340 *charset = utf8_charset_cache;
341 return utf8_locale_cache;
348 * @ch: a ISO10646 character code
349 * @out: output buffer, must have at least 6 bytes of space.
351 * Convert a single character to utf8
353 * Return value: number of bytes written
356 g_unichar_to_utf8 (gunichar c, gchar *outbuf)
372 else if (c < 0x10000)
377 else if (c < 0x200000)
382 else if (c < 0x4000000)
393 for (i = len - 1; i > 0; --i)
395 outbuf[i] = (c & 0x3f) | 0x80;
398 outbuf[0] = c | first;
405 * @p: a nul-terminated utf-8 string
406 * @c: a iso-10646 character/
408 * Find the leftmost occurence of the given iso-10646 character
411 * Return value: NULL if the string does not contain the character, otherwise, a
412 * a pointer to the start of the leftmost of the character in the string.
415 g_utf8_strchr (const char *p, gunichar c)
419 gint len = g_unichar_to_utf8 (c, ch);
422 return strstr(p, ch);
428 * @p: a nul-terminated utf-8 string
429 * @c: a iso-10646 character/
431 * Find the rightmost occurence of the given iso-10646 character
434 * Return value: NULL if the string does not contain the character, otherwise, a
435 * a pointer to the start of the rightmost of the character in the string.
438 /* This is ifdefed out atm as there is no strrstr function in libc.
441 unicode_strrchr (const char *p, gunichar c)
445 len = g_unichar_to_utf8 (c, ch);
448 return strrstr(p, ch);
455 * @str: a UTF-8 encoded strnig
456 * @len: the length of @
458 * Convert a string from UTF-8 to a 32-bit fixed width
459 * representation as UCS-4.
461 * Return value: a pointer to a newly allocated UCS-4 string.
462 * This value must be freed with g_free()
465 g_utf8_to_ucs4 (const char *str, int len)
471 n_chars = g_utf8_strlen (str, len);
472 result = g_new (gunichar, n_chars);
475 for (i=0; i < n_chars; i++)
477 result[i] = g_utf8_get_char (p);
478 p = g_utf8_next_char (p);