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.
40 #define UTF8_COMPUTE(Char, Mask, Len) \
46 else if ((Char & 0xe0) == 0xc0) \
51 else if ((Char & 0xf0) == 0xe0) \
56 else if ((Char & 0xf8) == 0xf0) \
61 else if ((Char & 0xfc) == 0xf8) \
66 else if ((Char & 0xfe) == 0xfc) \
74 #define UTF8_LENGTH(Char) \
75 ((Char) < 0x80 ? 1 : \
76 ((Char) < 0x800 ? 2 : \
77 ((Char) < 0x10000 ? 3 : \
78 ((Char) < 0x200000 ? 4 : \
79 ((Char) < 0x4000000 ? 5 : 6)))))
82 #define UTF8_GET(Result, Chars, Count, Mask, Len) \
83 (Result) = (Chars)[0] & (Mask); \
84 for ((Count) = 1; (Count) < (Len); ++(Count)) \
86 if (((Chars)[(Count)] & 0xc0) != 0x80) \
92 (Result) |= ((Chars)[(Count)] & 0x3f); \
95 #define UNICODE_VALID(Char) \
96 ((Char) < 0x110000 && \
97 ((Char) < 0xD800 || (Char) >= 0xE000) && \
98 (Char) != 0xFFFE && (Char) != 0xFFFF)
101 gchar g_utf8_skip[256] = {
102 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,
103 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,
104 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,
105 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,
106 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,
107 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,
108 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,
109 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
113 * g_utf8_find_prev_char:
114 * @str: pointer to the beginning of a UTF-8 string
115 * @p: pointer to some position within @str
117 * Given a position @p with a UTF-8 encoded string @str, find the start
118 * of the previous UTF-8 character starting before @p. Returns %NULL if no
119 * UTF-8 characters are present in @p before @str.
121 * @p does not have to be at the beginning of a UTF-8 chracter. No check
122 * is made to see if the character found is actually valid other than
123 * it starts with an appropriate byte.
125 * Return value: a pointer to the found character or %NULL.
128 g_utf8_find_prev_char (const char *str,
131 for (--p; p > str; --p)
133 if ((*p & 0xc0) != 0x80)
140 * g_utf8_find_next_char:
141 * @p: a pointer to a position within a UTF-8 encoded string
142 * @end: a pointer to the end of the string, or %NULL to indicate
143 * that the string is NULL terminated, in which case
144 * the returned value will be
146 * Find the start of the next utf-8 character in the string after @p
148 * @p does not have to be at the beginning of a UTF-8 chracter. No check
149 * is made to see if the character found is actually valid other than
150 * it starts with an appropriate byte.
152 * Return value: a pointer to the found character or %NULL
155 g_utf8_find_next_char (const gchar *p,
161 for (++p; p < end && (*p & 0xc0) == 0x80; ++p)
164 for (++p; (*p & 0xc0) == 0x80; ++p)
167 return (p == end) ? NULL : (gchar *)p;
172 * @p: a pointer to a position within a UTF-8 encoded string
174 * Find the previous UTF-8 character in the string before @p
176 * @p does not have to be at the beginning of a UTF-8 character. No check
177 * is made to see if the character found is actually valid other than
178 * it starts with an appropriate byte. If @p might be the first
179 * character of the string, you must use g_utf8_find_prev_char instead.
181 * Return value: a pointer to the found character.
184 g_utf8_prev_char (const gchar *p)
189 if ((*p & 0xc0) != 0x80)
196 * @p: pointer to the start of a UTF-8 string.
197 * @max: the maximum number of bytes to examine. If @max
198 * is less than 0, then the string is assumed to be
201 * Return value: the length of the string in characters
204 g_utf8_strlen (const gchar *p, gint max)
207 const gchar *start = p;
208 /* special case for the empty string */
211 /* Note that the test here and the test in the loop differ subtly.
212 In the loop we want to see if we've passed the maximum limit --
213 for instance if the buffer ends mid-character. Here at the top
214 of the loop we want to see if we've just reached the last byte. */
215 while (max < 0 || p - start < max)
217 p = g_utf8_next_char (p);
219 if (! *p || (max > 0 && p - start > max))
227 * @p: a pointer to unicode character encoded as UTF-8
229 * Convert a sequence of bytes encoded as UTF-8 to a unicode character.
231 * Return value: the resulting character or (gunichar)-1 if @p does
232 * not point to a valid UTF-8 encoded unicode character
235 g_utf8_get_char (const gchar *p)
237 int i, mask = 0, len;
239 unsigned char c = (unsigned char) *p;
241 UTF8_COMPUTE (c, mask, len);
244 UTF8_GET (result, p, i, mask, len);
250 * g_utf8_offset_to_pointer:
251 * @str: a UTF-8 encoded string
252 * @offset: a character offset within the string.
254 * Converts from an integer character offset to a pointer to a position
257 * Return value: the resulting pointer
260 g_utf8_offset_to_pointer (const gchar *str,
263 const gchar *s = str;
265 s = g_utf8_next_char (s);
271 * g_utf8_pointer_to_offset:
272 * @str: a UTF-8 encoded string
273 * @pos: a pointer to a position within @str
275 * Converts from a pointer to position within a string to a integer
278 * Return value: the resulting character offset
281 g_utf8_pointer_to_offset (const gchar *str,
284 const gchar *s = str;
289 s = g_utf8_next_char (s);
298 g_utf8_strncpy (gchar *dest, const gchar *src, size_t n)
300 const gchar *s = src;
303 s = g_utf8_next_char(s);
306 strncpy(dest, src, s - src);
312 g_utf8_get_charset_internal (char **a)
314 char *charset = getenv("CHARSET");
316 if (charset && a && ! *a)
319 if (charset && strstr (charset, "UTF-8"))
323 charset = nl_langinfo(CODESET);
328 if (strcmp (charset, "UTF-8") == 0)
333 #if 0 /* #ifdef _NL_CTYPE_CODESET_NAME */
334 charset = nl_langinfo (_NL_CTYPE_CODESET_NAME);
339 if (strcmp (charset, "UTF-8") == 0)
347 static char codepage[10];
349 sprintf (codepage, "CP%d", GetACP ());
351 /* What about codepage 1200? Is that UTF-8? */
359 /* Assume this for compatibility at present. */
363 static int utf8_locale_cache = -1;
364 static char *utf8_charset_cache = NULL;
367 g_get_charset (char **charset)
369 if (utf8_locale_cache != -1)
372 *charset = utf8_charset_cache;
373 return utf8_locale_cache;
375 utf8_locale_cache = g_utf8_get_charset_internal (&utf8_charset_cache);
377 *charset = utf8_charset_cache;
378 return utf8_locale_cache;
385 * @c: a ISO10646 character code
386 * @outbuf: output buffer, must have at least 6 bytes of space.
387 * If %NULL, the length will be computed and returned
388 * and nothing will be written to @out.
390 * Convert a single character to utf8
392 * Return value: number of bytes written
395 g_unichar_to_utf8 (gunichar c, gchar *outbuf)
411 else if (c < 0x10000)
416 else if (c < 0x200000)
421 else if (c < 0x4000000)
434 for (i = len - 1; i > 0; --i)
436 outbuf[i] = (c & 0x3f) | 0x80;
439 outbuf[0] = c | first;
447 * @p: a nul-terminated utf-8 string
448 * @c: a iso-10646 character/
450 * Find the leftmost occurence of the given iso-10646 character
453 * Return value: NULL if the string does not contain the character, otherwise, a
454 * a pointer to the start of the leftmost of the character in the string.
457 g_utf8_strchr (const char *p, gunichar c)
461 gint len = g_unichar_to_utf8 (c, ch);
464 return strstr(p, ch);
470 * @p: a nul-terminated utf-8 string
471 * @c: a iso-10646 character/
473 * Find the rightmost occurence of the given iso-10646 character
476 * Return value: NULL if the string does not contain the character, otherwise, a
477 * a pointer to the start of the rightmost of the character in the string.
480 /* This is ifdefed out atm as there is no strrstr function in libc.
483 unicode_strrchr (const char *p, gunichar c)
487 len = g_unichar_to_utf8 (c, ch);
490 return strrstr(p, ch);
495 /* Like g_utf8_get_char, but take a maximum length
496 * and return (gunichar)-2 on incomplete trailing character
498 static inline gunichar
499 g_utf8_get_char_extended (const gchar *p, int max_len)
502 gunichar wc = (guchar) *p;
544 if (max_len >= 0 && len > max_len)
546 for (i = 1; i < max_len; i++)
548 if ((((guchar *)p)[i] & 0xc0) != 0x80)
554 for (i = 1; i < len; ++i)
556 gunichar ch = ((guchar *)p)[i];
558 if ((ch & 0xc0) != 0x80)
570 if (UTF8_LENGTH(wc) != len)
577 * g_utf8_to_ucs4_fast:
578 * @str: a UTF-8 encoded string
579 * @len: the maximum length of @str to use. If < 0, then
580 * the string is %NULL terminated.
581 * @items_written: location to store the number of characters in the
584 * Convert a string from UTF-8 to a 32-bit fixed width
585 * representation as UCS-4, assuming valid UTF-8 input.
586 * This function is roughly twice as fast as g_utf8_to_ucs4()
587 * but does no error checking on the input.
589 * Return value: a pointer to a newly allocated UCS-4 string.
590 * This value must be freed with g_free()
593 g_utf8_to_ucs4_fast (const gchar *str,
602 g_return_val_if_fail (str != NULL, NULL);
610 p = g_utf8_next_char (p);
616 while (*p && p < str + len)
618 p = g_utf8_next_char (p);
623 result = g_new (gunichar, n_chars + 1);
626 for (i=0; i < n_chars; i++)
628 gunichar wc = ((unsigned char *)p)[0];
663 for (j = 1; j < charlen; j++)
666 wc |= ((unsigned char *)p)[j] & 0x3f;
683 * @str: a UTF-8 encoded string
684 * @len: the maximum length of @str to use. If < 0, then
685 * the string is %NULL terminated.
686 * @items_read: location to store number of bytes read, or %NULL.
687 * If %NULL, then %G_CONVERT_ERROR_PARTIAL_INPUT will be
688 * returned in case @str contains a trailing partial
689 * character. If an error occurs then the index of the
690 * invalid input is stored here.
691 * @items_written: location to store number of characters written or %NULL.
692 * The value here stored does not include the trailing 0
694 * @error: location to store the error occuring, or %NULL to ignore
695 * errors. Any of the errors in #GConvertError other than
696 * %G_CONVERT_ERROR_NO_CONVERSION may occur.
698 * Convert a string from UTF-8 to a 32-bit fixed width
699 * representation as UCS-4. A trailing 0 will be added to the
700 * string after the converted text.
702 * Return value: a pointer to a newly allocated UCS-4 string.
703 * This value must be freed with g_free(). If an
704 * error occurs, %NULL will be returned and
708 g_utf8_to_ucs4 (const gchar *str,
714 gunichar *result = NULL;
720 while ((len < 0 || str + len - in > 0) && *in)
722 gunichar wc = g_utf8_get_char_extended (in, str + len - in);
725 if (wc == (gunichar)-2)
730 g_set_error (error, G_CONVERT_ERROR, G_CONVERT_ERROR_PARTIAL_INPUT,
731 _("Partial character sequence at end of input"));
734 g_set_error (error, G_CONVERT_ERROR, G_CONVERT_ERROR_ILLEGAL_SEQUENCE,
735 _("Invalid byte sequence in conversion input"));
742 in = g_utf8_next_char (in);
745 result = g_new (gunichar, n_chars + 1);
748 for (i=0; i < n_chars; i++)
750 result[i] = g_utf8_get_char (in);
751 in = g_utf8_next_char (in);
756 *items_written = n_chars;
760 *items_read = in - str;
767 * @str: a UCS-4 encoded string
768 * @len: the maximum length of @str to use. If < 0, then
769 * the string is %NULL terminated.
770 * @items_read: location to store number of characters read read, or %NULL.
771 * @items_written: location to store number of bytes written or %NULL.
772 * The value here stored does not include the trailing 0
774 * @error: location to store the error occuring, or %NULL to ignore
775 * errors. Any of the errors in #GConvertError other than
776 * %G_CONVERT_ERROR_NO_CONVERSION may occur.
778 * Convert a string from a 32-bit fixed width representation as UCS-4.
779 * to UTF-8. The result will be terminated with a 0 byte.
781 * Return value: a pointer to a newly allocated UTF-8 string.
782 * This value must be freed with g_free(). If an
783 * error occurs, %NULL will be returned and
787 g_ucs4_to_utf8 (const gunichar *str,
794 gchar *result = NULL;
799 for (i = 0; len < 0 || i < len ; i++)
804 if (str[i] >= 0x80000000)
809 g_set_error (error, G_CONVERT_ERROR, G_CONVERT_ERROR_ILLEGAL_SEQUENCE,
810 _("Character out of range for UTF-8"));
814 result_length += UTF8_LENGTH (str[i]);
817 result = g_malloc (result_length + 1);
821 while (p < result + result_length)
822 p += g_unichar_to_utf8 (str[i++], p);
827 *items_written = p - result;
836 #define SURROGATE_VALUE(h,l) (((h) - 0xd800) * 0x400 + (l) - 0xdc00 + 0x10000)
840 * @str: a UTF-16 encoded string
841 * @len: the maximum length of @str to use. If < 0, then
842 * the string is terminated with a 0 character.
843 * @items_read: location to store number of words read, or %NULL.
844 * If %NULL, then %G_CONVERT_ERROR_PARTIAL_INPUT will be
845 * returned in case @str contains a trailing partial
846 * character. If an error occurs then the index of the
847 * invalid input is stored here.
848 * @items_written: location to store number of bytes written, or %NULL.
849 * The value stored here does not include the trailing
851 * @error: location to store the error occuring, or %NULL to ignore
852 * errors. Any of the errors in #GConvertError other than
853 * %G_CONVERT_ERROR_NO_CONVERSION may occur.
855 * Convert a string from UTF-16 to UTF-8. The result will be
856 * terminated with a 0 byte.
858 * Return value: a pointer to a newly allocated UTF-8 string.
859 * This value must be freed with g_free(). If an
860 * error occurs, %NULL will be returned and
864 g_utf16_to_utf8 (const gunichar2 *str,
870 /* This function and g_utf16_to_ucs4 are almost exactly identical - The lines that differ
875 gchar *result = NULL;
877 gunichar high_surrogate;
879 g_return_val_if_fail (str != 0, NULL);
884 while ((len < 0 || in - str < len) && *in)
889 if (c >= 0xdc00 && c < 0xe000) /* low surrogate */
893 wc = SURROGATE_VALUE (high_surrogate, c);
898 g_set_error (error, G_CONVERT_ERROR, G_CONVERT_ERROR_ILLEGAL_SEQUENCE,
899 _("Invalid sequence in conversion input"));
907 g_set_error (error, G_CONVERT_ERROR, G_CONVERT_ERROR_ILLEGAL_SEQUENCE,
908 _("Invalid sequence in conversion input"));
912 if (c >= 0xd800 && c < 0xdc00) /* high surrogate */
921 /********** DIFFERENT for UTF8/UCS4 **********/
922 n_bytes += UTF8_LENGTH (wc);
928 if (high_surrogate && !items_read)
930 g_set_error (error, G_CONVERT_ERROR, G_CONVERT_ERROR_PARTIAL_INPUT,
931 _("Partial character sequence at end of input"));
935 /* At this point, everything is valid, and we just need to convert
937 /********** DIFFERENT for UTF8/UCS4 **********/
938 result = g_malloc (n_bytes + 1);
943 while (out < result + n_bytes)
948 if (c >= 0xdc00 && c < 0xe000) /* low surrogate */
950 wc = SURROGATE_VALUE (high_surrogate, c);
953 else if (c >= 0xd800 && c < 0xdc00) /* high surrogate */
961 /********** DIFFERENT for UTF8/UCS4 **********/
962 out += g_unichar_to_utf8 (wc, out);
968 /********** DIFFERENT for UTF8/UCS4 **********/
972 /********** DIFFERENT for UTF8/UCS4 **********/
973 *items_written = out - result;
977 *items_read = in - str;
984 * @str: a UTF-16 encoded string
985 * @len: the maximum length of @str to use. If < 0, then
986 * the string is terminated with a 0 character.
987 * @items_read: location to store number of words read, or %NULL.
988 * If %NULL, then %G_CONVERT_ERROR_PARTIAL_INPUT will be
989 * returned in case @str contains a trailing partial
990 * character. If an error occurs then the index of the
991 * invalid input is stored here.
992 * @items_written: location to store number of characters written, or %NULL.
993 * The value stored here does not include the trailing
995 * @error: location to store the error occuring, or %NULL to ignore
996 * errors. Any of the errors in #GConvertError other than
997 * %G_CONVERT_ERROR_NO_CONVERSION may occur.
999 * Convert a string from UTF-16 to UCS-4. The result will be
1000 * terminated with a 0 character.
1002 * Return value: a pointer to a newly allocated UCS-4 string.
1003 * This value must be freed with g_free(). If an
1004 * error occurs, %NULL will be returned and
1008 g_utf16_to_ucs4 (const gunichar2 *str,
1011 gint *items_written,
1014 const gunichar2 *in;
1016 gchar *result = NULL;
1018 gunichar high_surrogate;
1020 g_return_val_if_fail (str != 0, NULL);
1025 while ((len < 0 || in - str < len) && *in)
1030 if (c >= 0xdc00 && c < 0xe000) /* low surrogate */
1034 wc = SURROGATE_VALUE (high_surrogate, c);
1039 g_set_error (error, G_CONVERT_ERROR, G_CONVERT_ERROR_ILLEGAL_SEQUENCE,
1040 _("Invalid sequence in conversion input"));
1048 g_set_error (error, G_CONVERT_ERROR, G_CONVERT_ERROR_ILLEGAL_SEQUENCE,
1049 _("Invalid sequence in conversion input"));
1053 if (c >= 0xd800 && c < 0xdc00) /* high surrogate */
1062 /********** DIFFERENT for UTF8/UCS4 **********/
1063 n_bytes += sizeof (gunichar);
1069 if (high_surrogate && !items_read)
1071 g_set_error (error, G_CONVERT_ERROR, G_CONVERT_ERROR_PARTIAL_INPUT,
1072 _("Partial character sequence at end of input"));
1076 /* At this point, everything is valid, and we just need to convert
1078 /********** DIFFERENT for UTF8/UCS4 **********/
1079 result = g_malloc (n_bytes + 4);
1084 while (out < result + n_bytes)
1089 if (c >= 0xdc00 && c < 0xe000) /* low surrogate */
1091 wc = SURROGATE_VALUE (high_surrogate, c);
1094 else if (c >= 0xd800 && c < 0xdc00) /* high surrogate */
1102 /********** DIFFERENT for UTF8/UCS4 **********/
1103 *(gunichar *)out = wc;
1104 out += sizeof (gunichar);
1110 /********** DIFFERENT for UTF8/UCS4 **********/
1111 *(gunichar *)out = 0;
1114 /********** DIFFERENT for UTF8/UCS4 **********/
1115 *items_written = (out - result) / sizeof (gunichar);
1119 *items_read = in - str;
1121 return (gunichar *)result;
1126 * @str: a UTF-8 encoded string
1127 * @len: the maximum length of @str to use. If < 0, then
1128 * the string is %NULL terminated.
1130 * @items_read: location to store number of bytes read, or %NULL.
1131 * If %NULL, then %G_CONVERT_ERROR_PARTIAL_INPUT will be
1132 * returned in case @str contains a trailing partial
1133 * character. If an error occurs then the index of the
1134 * invalid input is stored here.
1135 * @items_written: location to store number of words written, or %NULL.
1136 * The value stored here does not include the trailing
1138 * @error: location to store the error occuring, or %NULL to ignore
1139 * errors. Any of the errors in #GConvertError other than
1140 * %G_CONVERT_ERROR_NO_CONVERSION may occur.
1142 * Convert a string from UTF-8 to UTF-16. A 0 word will be
1143 * added to the result after the converted text.
1145 * Return value: a pointer to a newly allocated UTF-16 string.
1146 * This value must be freed with g_free(). If an
1147 * error occurs, %NULL will be returned and
1151 g_utf8_to_utf16 (const gchar *str,
1154 gint *items_written,
1157 gunichar2 *result = NULL;
1162 g_return_val_if_fail (str != NULL, NULL);
1166 while ((len < 0 || str + len - in > 0) && *in)
1168 gunichar wc = g_utf8_get_char_extended (in, str + len - in);
1169 if (wc & 0x80000000)
1171 if (wc == (gunichar)-2)
1176 g_set_error (error, G_CONVERT_ERROR, G_CONVERT_ERROR_PARTIAL_INPUT,
1177 _("Partial character sequence at end of input"));
1180 g_set_error (error, G_CONVERT_ERROR, G_CONVERT_ERROR_ILLEGAL_SEQUENCE,
1181 _("Invalid byte sequence in conversion input"));
1188 else if (wc < 0xe000)
1190 g_set_error (error, G_CONVERT_ERROR, G_CONVERT_ERROR_ILLEGAL_SEQUENCE,
1191 _("Invalid sequence in conversion input"));
1195 else if (wc < 0x10000)
1197 else if (wc < 0x110000)
1201 g_set_error (error, G_CONVERT_ERROR, G_CONVERT_ERROR_ILLEGAL_SEQUENCE,
1202 _("Character out of range for UTF-16"));
1207 in = g_utf8_next_char (in);
1210 result = g_new (gunichar2, n16 + 1);
1213 for (i = 0; i < n16;)
1215 gunichar wc = g_utf8_get_char (in);
1223 result[i++] = (wc - 0x10000) / 0x400 + 0xd800;
1224 result[i++] = (wc - 0x10000) % 0x400 + 0xdc00;
1227 in = g_utf8_next_char (in);
1233 *items_written = n16;
1237 *items_read = in - str;
1244 * @str: a UCS-4 encoded string
1245 * @len: the maximum length of @str to use. If < 0, then
1246 * the string is terminated with a zero character.
1247 * @items_read: location to store number of bytes read, or %NULL.
1248 * If an error occurs then the index of the invalid input
1250 * @items_written: location to store number of words written, or %NULL.
1251 * The value stored here does not include the trailing
1253 * @error: location to store the error occuring, or %NULL to ignore
1254 * errors. Any of the errors in #GConvertError other than
1255 * %G_CONVERT_ERROR_NO_CONVERSION may occur.
1257 * Convert a string from UCS-4 to UTF-16. A 0 word will be
1258 * added to the result after the converted text.
1260 * Return value: a pointer to a newly allocated UTF-16 string.
1261 * This value must be freed with g_free(). If an
1262 * error occurs, %NULL will be returned and
1266 g_ucs4_to_utf16 (const gunichar *str,
1269 gint *items_written,
1272 gunichar2 *result = NULL;
1278 while ((len < 0 || i < len) && str[i])
1280 gunichar wc = str[i];
1284 else if (wc < 0xe000)
1286 g_set_error (error, G_CONVERT_ERROR, G_CONVERT_ERROR_ILLEGAL_SEQUENCE,
1287 _("Invalid sequence in conversion input"));
1291 else if (wc < 0x10000)
1293 else if (wc < 0x110000)
1297 g_set_error (error, G_CONVERT_ERROR, G_CONVERT_ERROR_ILLEGAL_SEQUENCE,
1298 _("Character out of range for UTF-16"));
1306 result = g_new (gunichar2, n16 + 1);
1308 for (i = 0, j = 0; j < n16; i++)
1310 gunichar wc = str[i];
1318 result[j++] = (wc - 0x10000) / 0x400 + 0xd800;
1319 result[j++] = (wc - 0x10000) % 0x400 + 0xdc00;
1325 *items_written = n16;
1336 * @str: a pointer to character data
1337 * @max_len: max bytes to validate, or -1 to go until nul
1338 * @end: return location for end of valid data
1340 * Validates UTF-8 encoded text. @str is the text to validate;
1341 * if @str is nul-terminated, then @max_len can be -1, otherwise
1342 * @max_len should be the number of bytes to validate.
1343 * If @end is non-NULL, then the end of the valid range
1344 * will be stored there (i.e. the address of the first invalid byte
1345 * if some bytes were invalid, or the end of the text being validated
1348 * Returns TRUE if all of @str was valid. Many GLib and GTK+
1349 * routines <emphasis>require</emphasis> valid UTF8 as input;
1350 * so data read from a file or the network should be checked
1351 * with g_utf8_validate() before doing anything else with it.
1353 * Return value: TRUE if the text was valid UTF-8.
1356 g_utf8_validate (const gchar *str,
1363 g_return_val_if_fail (str != NULL, FALSE);
1370 while ((max_len < 0 || (p - str) < max_len) && *p)
1372 int i, mask = 0, len;
1374 unsigned char c = (unsigned char) *p;
1376 UTF8_COMPUTE (c, mask, len);
1381 /* check that the expected number of bytes exists in str */
1383 ((max_len - (p - str)) < len))
1386 UTF8_GET (result, p, i, mask, len);
1388 if (UTF8_LENGTH (result) != len) /* Check for overlong UTF-8 */
1391 if (result == (gunichar)-1)
1394 if (!UNICODE_VALID (result))
1403 /* See that we covered the entire length if a length was
1404 * passed in, or that we ended on a nul if not
1407 p != (str + max_len))
1409 else if (max_len < 0 &&