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.
36 #define UTF8_COMPUTE(Char, Mask, Len) \
42 else if ((Char & 0xe0) == 0xc0) \
47 else if ((Char & 0xf0) == 0xe0) \
52 else if ((Char & 0xf8) == 0xf0) \
57 else if ((Char & 0xfc) == 0xf8) \
62 else if ((Char & 0xfe) == 0xfc) \
70 #define UTF8_GET(Result, Chars, Count, Mask, Len) \
71 (Result) = (Chars)[0] & (Mask); \
72 for ((Count) = 1; (Count) < (Len); ++(Count)) \
74 if (((Chars)[(Count)] & 0xc0) != 0x80) \
80 (Result) |= ((Chars)[(Count)] & 0x3f); \
82 gchar g_utf8_skip[256] = {
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 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,
87 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,
88 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,
89 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,
90 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
94 * g_utf8_find_prev_char:
95 * @str: pointer to the beginning of a UTF-8 string
96 * @p: pointer to some position within @str
98 * Given a position @p with a UTF-8 encoded string @str, find the start
99 * of the previous UTF-8 character starting before @p. Returns %NULL if no
100 * UTF-8 characters are present in @p before @str.
102 * @p does not have to be at the beginning of a UTF-8 chracter. No check
103 * is made to see if the character found is actually valid other than
104 * it starts with an appropriate byte.
106 * Return value: a pointer to the found character or %NULL.
109 g_utf8_find_prev_char (const char *str,
112 for (--p; p > str; --p)
114 if ((*p & 0xc0) != 0x80)
121 * g_utf8_find_next_char:
122 * @p: a pointer to a position within a UTF-8 encoded string
123 * @end: a pointer to the end of the string, or %NULL to indicate
124 * that the string is NULL terminated, in which case
125 * the returned value will be
127 * Find the start of the next utf-8 character in the string after @p
129 * @p does not have to be at the beginning of a UTF-8 chracter. No check
130 * is made to see if the character found is actually valid other than
131 * it starts with an appropriate byte.
133 * Return value: a pointer to the found character or %NULL
136 g_utf8_find_next_char (const gchar *p,
142 for (++p; p < end && (*p & 0xc0) == 0x80; ++p)
145 for (++p; (*p & 0xc0) == 0x80; ++p)
148 return (p == end) ? NULL : (gchar *)p;
153 * @p: a pointer to a position within a UTF-8 encoded string
155 * Find the previous UTF-8 character in the string before @p
157 * @p does not have to be at the beginning of a UTF-8 character. No check
158 * is made to see if the character found is actually valid other than
159 * it starts with an appropriate byte. If @p might be the first
160 * character of the string, you must use g_utf8_find_prev_char instead.
162 * Return value: a pointer to the found character.
165 g_utf8_prev_char (const gchar *p)
170 if ((*p & 0xc0) != 0x80)
177 * @p: pointer to the start of a UTF-8 string.
178 * @max: the maximum number of bytes to examine. If @max
179 * is less than 0, then the string is assumed to be
182 * Return value: the length of the string in characters
185 g_utf8_strlen (const gchar *p, gint max)
188 const gchar *start = p;
189 /* special case for the empty string */
192 /* Note that the test here and the test in the loop differ subtly.
193 In the loop we want to see if we've passed the maximum limit --
194 for instance if the buffer ends mid-character. Here at the top
195 of the loop we want to see if we've just reached the last byte. */
196 while (max < 0 || p - start < max)
198 p = g_utf8_next_char (p);
200 if (! *p || (max > 0 && p - start > max))
208 * @p: a pointer to unicode character encoded as UTF-8
210 * Convert a sequence of bytes encoded as UTF-8 to a unicode character.
212 * Return value: the resulting character or (gunichar)-1 if @p does
213 * not point to a valid UTF-8 encoded unicode character
216 g_utf8_get_char (const gchar *p)
218 int i, mask = 0, len;
220 unsigned char c = (unsigned char) *p;
222 UTF8_COMPUTE (c, mask, len);
225 UTF8_GET (result, p, i, mask, len);
231 * g_utf8_offset_to_pointer:
232 * @str: a UTF-8 encoded string
233 * @offset: a character offset within the string.
235 * Converts from an integer character offset to a pointer to a position
238 * Return value: the resulting pointer
241 g_utf8_offset_to_pointer (const gchar *str,
244 const gchar *s = str;
246 s = g_utf8_next_char (s);
252 * g_utf8_pointer_to_offset:
253 * @str: a UTF-8 encoded string
254 * @pos: a pointer to a position within @str
256 * Converts from a pointer to position within a string to a integer
259 * Return value: the resulting character offset
262 g_utf8_pointer_to_offset (const gchar *str,
265 const gchar *s = str;
270 s = g_utf8_next_char (s);
279 g_utf8_strncpy (gchar *dest, const gchar *src, size_t n)
281 const gchar *s = src;
284 s = g_utf8_next_char(s);
287 strncpy(dest, src, s - src);
293 g_utf8_get_charset_internal (char **a)
295 char *charset = getenv("CHARSET");
297 if (charset && a && ! *a)
300 if (charset && strstr (charset, "UTF-8"))
304 charset = nl_langinfo(CODESET);
309 if (strcmp (charset, "UTF-8") == 0)
314 #if 0 /* #ifdef _NL_CTYPE_CODESET_NAME */
315 charset = nl_langinfo (_NL_CTYPE_CODESET_NAME);
320 if (strcmp (charset, "UTF-8") == 0)
328 static char codepage[10];
330 sprintf (codepage, "CP%d", GetACP ());
332 /* What about codepage 1200? Is that UTF-8? */
340 /* Assume this for compatibility at present. */
344 static int utf8_locale_cache = -1;
345 static char *utf8_charset_cache = NULL;
348 g_get_charset (char **charset)
350 if (utf8_locale_cache != -1)
353 *charset = utf8_charset_cache;
354 return utf8_locale_cache;
356 utf8_locale_cache = g_utf8_get_charset_internal (&utf8_charset_cache);
358 *charset = utf8_charset_cache;
359 return utf8_locale_cache;
366 * @c: a ISO10646 character code
367 * @outbuf: output buffer, must have at least 6 bytes of space.
368 * If %NULL, the length will be computed and returned
369 * and nothing will be written to @out.
371 * Convert a single character to utf8
373 * Return value: number of bytes written
376 g_unichar_to_utf8 (gunichar c, gchar *outbuf)
392 else if (c < 0x10000)
397 else if (c < 0x200000)
402 else if (c < 0x4000000)
415 for (i = len - 1; i > 0; --i)
417 outbuf[i] = (c & 0x3f) | 0x80;
420 outbuf[0] = c | first;
428 * @p: a nul-terminated utf-8 string
429 * @c: a iso-10646 character/
431 * Find the leftmost 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 leftmost of the character in the string.
438 g_utf8_strchr (const char *p, gunichar c)
442 gint len = g_unichar_to_utf8 (c, ch);
445 return strstr(p, ch);
451 * @p: a nul-terminated utf-8 string
452 * @c: a iso-10646 character/
454 * Find the rightmost occurence of the given iso-10646 character
457 * Return value: NULL if the string does not contain the character, otherwise, a
458 * a pointer to the start of the rightmost of the character in the string.
461 /* This is ifdefed out atm as there is no strrstr function in libc.
464 unicode_strrchr (const char *p, gunichar c)
468 len = g_unichar_to_utf8 (c, ch);
471 return strrstr(p, ch);
478 * @str: a UTF-8 encoded strnig
479 * @len: the length of @
481 * Convert a string from UTF-8 to a 32-bit fixed width
482 * representation as UCS-4.
484 * Return value: a pointer to a newly allocated UCS-4 string.
485 * This value must be freed with g_free()
488 g_utf8_to_ucs4 (const char *str, int len)
494 n_chars = g_utf8_strlen (str, len);
495 result = g_new (gunichar, n_chars);
498 for (i=0; i < n_chars; i++)
500 result[i] = g_utf8_get_char (p);
501 p = g_utf8_next_char (p);
509 * @str: a UCS-4 encoded string
510 * @len: the length of @
512 * Convert a string from a 32-bit fixed width representation as UCS-4.
515 * Return value: a pointer to a newly allocated UTF-8 string.
516 * This value must be freed with g_free()
519 g_ucs4_to_utf8 (const gunichar *str, int len)
526 for (i = 0; i < len ; i++)
527 result_length += g_unichar_to_utf8 (str[i], NULL);
531 result = g_malloc (result_length + 1);
534 for (i = 0; i < len ; i++)
535 p += g_unichar_to_utf8 (str[i], p);
544 * @str: a pointer to character data
545 * @max_len: max bytes to validate, or -1 to go until nul
546 * @end: return location for end of valid data
548 * Validates UTF-8 encoded text. @str is the text to validate;
549 * if @str is nul-terminated, then @max_len can be -1, otherwise
550 * @max_len should be the number of bytes to validate.
551 * If @end is non-NULL, then the end of the valid range
552 * will be stored there (i.e. the address of the first invalid byte
553 * if some bytes were invalid, or the end of the text being validated
556 * Returns TRUE if all of @str was valid. Many GLib and GTK+
557 * routines <emphasis>require</emphasis> valid UTF8 as input;
558 * so data read from a file or the network should be checked
559 * with g_utf8_validate() before doing anything else with it.
561 * Return value: TRUE if the text was valid UTF-8.
564 g_utf8_validate (const gchar *str,
576 while ((max_len < 0 || (p - str) < max_len) && *p)
578 int i, mask = 0, len;
580 unsigned char c = (unsigned char) *p;
582 UTF8_COMPUTE (c, mask, len);
587 /* check that the expected number of bytes exists in str */
589 ((max_len - (p - str)) < len))
592 UTF8_GET (result, p, i, mask, len);
594 if (result == (gunichar)-1)
603 /* See that we covered the entire length if a length was
604 * passed in, or that we ended on a nul if not
607 p != (str + max_len))
609 else if (max_len < 0 &&