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.
38 #define UTF8_COMPUTE(Char, Mask, Len) \
44 else if ((Char & 0xe0) == 0xc0) \
49 else if ((Char & 0xf0) == 0xe0) \
54 else if ((Char & 0xf8) == 0xf0) \
59 else if ((Char & 0xfc) == 0xf8) \
64 else if ((Char & 0xfe) == 0xfc) \
72 #define UTF8_LENGTH(Char) \
73 ((Char) < 0x80 ? 1 : \
74 ((Char) < 0x800 ? 2 : \
75 ((Char) < 0x10000 ? 3 : \
76 ((Char) < 0x200000 ? 4 : \
77 ((Char) < 0x4000000 ? 5 : 6)))))
80 #define UTF8_GET(Result, Chars, Count, Mask, Len) \
81 (Result) = (Chars)[0] & (Mask); \
82 for ((Count) = 1; (Count) < (Len); ++(Count)) \
84 if (((Chars)[(Count)] & 0xc0) != 0x80) \
90 (Result) |= ((Chars)[(Count)] & 0x3f); \
93 #define UNICODE_VALID(Char) \
94 ((Char) < 0x110000 && \
95 ((Char) < 0xD800 || (Char) >= 0xE000) && \
96 (Char) != 0xFFFE && (Char) != 0xFFFF)
99 gchar g_utf8_skip[256] = {
100 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,
101 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,
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 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,
107 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
111 * g_utf8_find_prev_char:
112 * @str: pointer to the beginning of a UTF-8 string
113 * @p: pointer to some position within @str
115 * Given a position @p with a UTF-8 encoded string @str, find the start
116 * of the previous UTF-8 character starting before @p. Returns %NULL if no
117 * UTF-8 characters are present in @p before @str.
119 * @p does not have to be at the beginning of a UTF-8 chracter. No check
120 * is made to see if the character found is actually valid other than
121 * it starts with an appropriate byte.
123 * Return value: a pointer to the found character or %NULL.
126 g_utf8_find_prev_char (const char *str,
129 for (--p; p > str; --p)
131 if ((*p & 0xc0) != 0x80)
138 * g_utf8_find_next_char:
139 * @p: a pointer to a position within a UTF-8 encoded string
140 * @end: a pointer to the end of the string, or %NULL to indicate
141 * that the string is NULL terminated, in which case
142 * the returned value will be
144 * Find the start of the next utf-8 character in the string after @p
146 * @p does not have to be at the beginning of a UTF-8 chracter. No check
147 * is made to see if the character found is actually valid other than
148 * it starts with an appropriate byte.
150 * Return value: a pointer to the found character or %NULL
153 g_utf8_find_next_char (const gchar *p,
159 for (++p; p < end && (*p & 0xc0) == 0x80; ++p)
162 for (++p; (*p & 0xc0) == 0x80; ++p)
165 return (p == end) ? NULL : (gchar *)p;
170 * @p: a pointer to a position within a UTF-8 encoded string
172 * Find the previous UTF-8 character in the string before @p
174 * @p does not have to be at the beginning of a UTF-8 character. No check
175 * is made to see if the character found is actually valid other than
176 * it starts with an appropriate byte. If @p might be the first
177 * character of the string, you must use g_utf8_find_prev_char instead.
179 * Return value: a pointer to the found character.
182 g_utf8_prev_char (const gchar *p)
187 if ((*p & 0xc0) != 0x80)
194 * @p: pointer to the start of a UTF-8 string.
195 * @max: the maximum number of bytes to examine. If @max
196 * is less than 0, then the string is assumed to be
199 * Return value: the length of the string in characters
202 g_utf8_strlen (const gchar *p, gint max)
205 const gchar *start = p;
206 /* special case for the empty string */
209 /* Note that the test here and the test in the loop differ subtly.
210 In the loop we want to see if we've passed the maximum limit --
211 for instance if the buffer ends mid-character. Here at the top
212 of the loop we want to see if we've just reached the last byte. */
213 while (max < 0 || p - start < max)
215 p = g_utf8_next_char (p);
217 if (! *p || (max > 0 && p - start > max))
225 * @p: a pointer to unicode character encoded as UTF-8
227 * Convert a sequence of bytes encoded as UTF-8 to a unicode character.
229 * Return value: the resulting character or (gunichar)-1 if @p does
230 * not point to a valid UTF-8 encoded unicode character
233 g_utf8_get_char (const gchar *p)
235 int i, mask = 0, len;
237 unsigned char c = (unsigned char) *p;
239 UTF8_COMPUTE (c, mask, len);
242 UTF8_GET (result, p, i, mask, len);
248 * g_utf8_offset_to_pointer:
249 * @str: a UTF-8 encoded string
250 * @offset: a character offset within the string.
252 * Converts from an integer character offset to a pointer to a position
255 * Return value: the resulting pointer
258 g_utf8_offset_to_pointer (const gchar *str,
261 const gchar *s = str;
263 s = g_utf8_next_char (s);
269 * g_utf8_pointer_to_offset:
270 * @str: a UTF-8 encoded string
271 * @pos: a pointer to a position within @str
273 * Converts from a pointer to position within a string to a integer
276 * Return value: the resulting character offset
279 g_utf8_pointer_to_offset (const gchar *str,
282 const gchar *s = str;
287 s = g_utf8_next_char (s);
296 g_utf8_strncpy (gchar *dest, const gchar *src, size_t n)
298 const gchar *s = src;
301 s = g_utf8_next_char(s);
304 strncpy(dest, src, s - src);
310 g_utf8_get_charset_internal (char **a)
312 char *charset = getenv("CHARSET");
314 if (charset && a && ! *a)
317 if (charset && strstr (charset, "UTF-8"))
321 charset = nl_langinfo(CODESET);
326 if (strcmp (charset, "UTF-8") == 0)
331 #if 0 /* #ifdef _NL_CTYPE_CODESET_NAME */
332 charset = nl_langinfo (_NL_CTYPE_CODESET_NAME);
337 if (strcmp (charset, "UTF-8") == 0)
345 static char codepage[10];
347 sprintf (codepage, "CP%d", GetACP ());
349 /* What about codepage 1200? Is that UTF-8? */
357 /* Assume this for compatibility at present. */
361 static int utf8_locale_cache = -1;
362 static char *utf8_charset_cache = NULL;
365 g_get_charset (char **charset)
367 if (utf8_locale_cache != -1)
370 *charset = utf8_charset_cache;
371 return utf8_locale_cache;
373 utf8_locale_cache = g_utf8_get_charset_internal (&utf8_charset_cache);
375 *charset = utf8_charset_cache;
376 return utf8_locale_cache;
383 * @c: a ISO10646 character code
384 * @outbuf: output buffer, must have at least 6 bytes of space.
385 * If %NULL, the length will be computed and returned
386 * and nothing will be written to @out.
388 * Convert a single character to utf8
390 * Return value: number of bytes written
393 g_unichar_to_utf8 (gunichar c, gchar *outbuf)
409 else if (c < 0x10000)
414 else if (c < 0x200000)
419 else if (c < 0x4000000)
432 for (i = len - 1; i > 0; --i)
434 outbuf[i] = (c & 0x3f) | 0x80;
437 outbuf[0] = c | first;
445 * @p: a nul-terminated utf-8 string
446 * @c: a iso-10646 character/
448 * Find the leftmost occurence of the given iso-10646 character
451 * Return value: NULL if the string does not contain the character, otherwise, a
452 * a pointer to the start of the leftmost of the character in the string.
455 g_utf8_strchr (const char *p, gunichar c)
459 gint len = g_unichar_to_utf8 (c, ch);
462 return strstr(p, ch);
468 * @p: a nul-terminated utf-8 string
469 * @c: a iso-10646 character/
471 * Find the rightmost occurence of the given iso-10646 character
474 * Return value: NULL if the string does not contain the character, otherwise, a
475 * a pointer to the start of the rightmost of the character in the string.
478 /* This is ifdefed out atm as there is no strrstr function in libc.
481 unicode_strrchr (const char *p, gunichar c)
485 len = g_unichar_to_utf8 (c, ch);
488 return strrstr(p, ch);
493 /* Like g_utf8_get_char, but take a maximum length
494 * and return (gunichar)-2 on incomplete trailing character
496 static inline gunichar
497 g_utf8_get_char_extended (const gchar *p, int max_len)
500 gunichar wc = (guchar) *p;
542 if (max_len >= 0 && len > max_len)
544 for (i = 1; i < max_len; i++)
546 if ((((guchar *)p)[i] & 0xc0) != 0x80)
552 for (i = 1; i < len; ++i)
554 gunichar ch = ((guchar *)p)[i];
556 if ((ch & 0xc0) != 0x80)
568 if (UTF8_LENGTH(wc) != len)
575 * g_utf8_to_ucs4_fast:
576 * @str: a UTF-8 encoded string
577 * @len: the maximum length of @str to use. If < 0, then
578 * the string is %NULL terminated.
579 * @items_written: location to store the number of characters in the
582 * Convert a string from UTF-8 to a 32-bit fixed width
583 * representation as UCS-4, assuming valid UTF-8 input.
584 * This function is roughly twice as fast as g_utf8_to_ucs4()
585 * but does no error checking on the input.
587 * Return value: a pointer to a newly allocated UCS-4 string.
588 * This value must be freed with g_free()
591 g_utf8_to_ucs4_fast (const gchar *str,
600 g_return_val_if_fail (str != NULL, NULL);
608 p = g_utf8_next_char (p);
614 while (*p && p < str + len)
616 p = g_utf8_next_char (p);
621 result = g_new (gunichar, n_chars + 1);
624 for (i=0; i < n_chars; i++)
626 gunichar wc = ((unsigned char *)p)[0];
661 for (j = 1; j < charlen; j++)
664 wc |= ((unsigned char *)p)[j] & 0x3f;
681 * @str: a UTF-8 encoded string
682 * @len: the maximum length of @str to use. If < 0, then
683 * the string is %NULL terminated.
684 * @items_read: location to store number of bytes read, or %NULL.
685 * If %NULL, then %G_CONVERT_ERROR_PARTIAL_INPUT will be
686 * returned in case @str contains a trailing partial
687 * character. If an error occurs then the index of the
688 * invalid input is stored here.
689 * @items_written: location to store number of characters written or %NULL.
690 * The value here stored does not include the trailing 0
692 * @error: location to store the error occuring, or %NULL to ignore
693 * errors. Any of the errors in #GConvertError other than
694 * %G_CONVERT_ERROR_NO_CONVERSION may occur.
696 * Convert a string from UTF-8 to a 32-bit fixed width
697 * representation as UCS-4. A trailing 0 will be added to the
698 * string after the converted text.
700 * Return value: a pointer to a newly allocated UCS-4 string.
701 * This value must be freed with g_free(). If an
702 * error occurs, %NULL will be returned and
706 g_utf8_to_ucs4 (const gchar *str,
712 gunichar *result = NULL;
718 while ((len < 0 || str + len - in > 0) && *in)
720 gunichar wc = g_utf8_get_char_extended (in, str + len - in);
723 if (wc == (gunichar)-2)
728 g_set_error (error, G_CONVERT_ERROR, G_CONVERT_ERROR_PARTIAL_INPUT,
729 _("Partial character sequence at end of input"));
732 g_set_error (error, G_CONVERT_ERROR, G_CONVERT_ERROR_ILLEGAL_SEQUENCE,
733 _("Invalid byte sequence in conversion input"));
740 in = g_utf8_next_char (in);
743 result = g_new (gunichar, n_chars + 1);
746 for (i=0; i < n_chars; i++)
748 result[i] = g_utf8_get_char (in);
749 in = g_utf8_next_char (in);
754 *items_written = n_chars;
758 *items_read = in - str;
765 * @str: a UCS-4 encoded string
766 * @len: the maximum length of @str to use. If < 0, then
767 * the string is %NULL terminated.
768 * @items_read: location to store number of characters read read, or %NULL.
769 * @items_written: location to store number of bytes written or %NULL.
770 * The value here stored does not include the trailing 0
772 * @error: location to store the error occuring, or %NULL to ignore
773 * errors. Any of the errors in #GConvertError other than
774 * %G_CONVERT_ERROR_NO_CONVERSION may occur.
776 * Convert a string from a 32-bit fixed width representation as UCS-4.
777 * to UTF-8. The result will be terminated with a 0 byte.
779 * Return value: a pointer to a newly allocated UTF-8 string.
780 * This value must be freed with g_free(). If an
781 * error occurs, %NULL will be returned and
785 g_ucs4_to_utf8 (const gunichar *str,
792 gchar *result = NULL;
797 for (i = 0; len < 0 || i < len ; i++)
802 if (str[i] >= 0x80000000)
807 g_set_error (error, G_CONVERT_ERROR, G_CONVERT_ERROR_ILLEGAL_SEQUENCE,
808 _("Character out of range for UTF-8"));
812 result_length += UTF8_LENGTH (str[i]);
815 result = g_malloc (result_length + 1);
819 while (p < result + result_length)
820 p += g_unichar_to_utf8 (str[i++], p);
825 *items_written = p - result;
834 #define SURROGATE_VALUE(h,l) (((h) - 0xd800) * 0x400 + (l) - 0xdc00 + 0x10000)
838 * @str: a UTF-16 encoded string
839 * @len: the maximum length of @str to use. If < 0, then
840 * the string is terminated with a 0 character.
841 * @items_read: location to store number of words read, or %NULL.
842 * If %NULL, then %G_CONVERT_ERROR_PARTIAL_INPUT will be
843 * returned in case @str contains a trailing partial
844 * character. If an error occurs then the index of the
845 * invalid input is stored here.
846 * @items_written: location to store number of bytes written, or %NULL.
847 * The value stored here does not include the trailing
849 * @error: location to store the error occuring, or %NULL to ignore
850 * errors. Any of the errors in #GConvertError other than
851 * %G_CONVERT_ERROR_NO_CONVERSION may occur.
853 * Convert a string from UTF-16 to UTF-8. The result will be
854 * terminated with a 0 byte.
856 * Return value: a pointer to a newly allocated UTF-8 string.
857 * This value must be freed with g_free(). If an
858 * error occurs, %NULL will be returned and
862 g_utf16_to_utf8 (const gunichar2 *str,
868 /* This function and g_utf16_to_ucs4 are almost exactly identical - The lines that differ
873 gchar *result = NULL;
875 gunichar high_surrogate;
877 g_return_val_if_fail (str != 0, NULL);
882 while ((len < 0 || in - str < len) && *in)
887 if (c >= 0xdc00 && c < 0xe000) /* low surrogate */
891 wc = SURROGATE_VALUE (high_surrogate, c);
896 g_set_error (error, G_CONVERT_ERROR, G_CONVERT_ERROR_ILLEGAL_SEQUENCE,
897 _("Invalid sequence in conversion input"));
905 g_set_error (error, G_CONVERT_ERROR, G_CONVERT_ERROR_ILLEGAL_SEQUENCE,
906 _("Invalid sequence in conversion input"));
910 if (c >= 0xd800 && c < 0xdc00) /* high surrogate */
919 /********** DIFFERENT for UTF8/UCS4 **********/
920 n_bytes += UTF8_LENGTH (wc);
926 if (high_surrogate && !items_read)
928 g_set_error (error, G_CONVERT_ERROR, G_CONVERT_ERROR_PARTIAL_INPUT,
929 _("Partial character sequence at end of input"));
933 /* At this point, everything is valid, and we just need to convert
935 /********** DIFFERENT for UTF8/UCS4 **********/
936 result = g_malloc (n_bytes + 1);
941 while (out < result + n_bytes)
946 if (c >= 0xdc00 && c < 0xe000) /* low surrogate */
948 wc = SURROGATE_VALUE (high_surrogate, c);
951 else if (c >= 0xd800 && c < 0xdc00) /* high surrogate */
959 /********** DIFFERENT for UTF8/UCS4 **********/
960 out += g_unichar_to_utf8 (wc, out);
966 /********** DIFFERENT for UTF8/UCS4 **********/
970 /********** DIFFERENT for UTF8/UCS4 **********/
971 *items_written = out - result;
975 *items_read = in - str;
982 * @str: a UTF-16 encoded string
983 * @len: the maximum length of @str to use. If < 0, then
984 * the string is terminated with a 0 character.
985 * @items_read: location to store number of words read, or %NULL.
986 * If %NULL, then %G_CONVERT_ERROR_PARTIAL_INPUT will be
987 * returned in case @str contains a trailing partial
988 * character. If an error occurs then the index of the
989 * invalid input is stored here.
990 * @items_written: location to store number of characters written, or %NULL.
991 * The value stored here does not include the trailing
993 * @error: location to store the error occuring, or %NULL to ignore
994 * errors. Any of the errors in #GConvertError other than
995 * %G_CONVERT_ERROR_NO_CONVERSION may occur.
997 * Convert a string from UTF-16 to UCS-4. The result will be
998 * terminated with a 0 character.
1000 * Return value: a pointer to a newly allocated UCS-4 string.
1001 * This value must be freed with g_free(). If an
1002 * error occurs, %NULL will be returned and
1006 g_utf16_to_ucs4 (const gunichar2 *str,
1009 gint *items_written,
1012 const gunichar2 *in;
1014 gchar *result = NULL;
1016 gunichar high_surrogate;
1018 g_return_val_if_fail (str != 0, NULL);
1023 while ((len < 0 || in - str < len) && *in)
1028 if (c >= 0xdc00 && c < 0xe000) /* low surrogate */
1032 wc = SURROGATE_VALUE (high_surrogate, c);
1037 g_set_error (error, G_CONVERT_ERROR, G_CONVERT_ERROR_ILLEGAL_SEQUENCE,
1038 _("Invalid sequence in conversion input"));
1046 g_set_error (error, G_CONVERT_ERROR, G_CONVERT_ERROR_ILLEGAL_SEQUENCE,
1047 _("Invalid sequence in conversion input"));
1051 if (c >= 0xd800 && c < 0xdc00) /* high surrogate */
1060 /********** DIFFERENT for UTF8/UCS4 **********/
1061 n_bytes += sizeof (gunichar);
1067 if (high_surrogate && !items_read)
1069 g_set_error (error, G_CONVERT_ERROR, G_CONVERT_ERROR_PARTIAL_INPUT,
1070 _("Partial character sequence at end of input"));
1074 /* At this point, everything is valid, and we just need to convert
1076 /********** DIFFERENT for UTF8/UCS4 **********/
1077 result = g_malloc (n_bytes + 4);
1082 while (out < result + n_bytes)
1087 if (c >= 0xdc00 && c < 0xe000) /* low surrogate */
1089 wc = SURROGATE_VALUE (high_surrogate, c);
1092 else if (c >= 0xd800 && c < 0xdc00) /* high surrogate */
1100 /********** DIFFERENT for UTF8/UCS4 **********/
1101 *(gunichar *)out = wc;
1102 out += sizeof (gunichar);
1108 /********** DIFFERENT for UTF8/UCS4 **********/
1109 *(gunichar *)out = 0;
1112 /********** DIFFERENT for UTF8/UCS4 **********/
1113 *items_written = (out - result) / sizeof (gunichar);
1117 *items_read = in - str;
1119 return (gunichar *)result;
1124 * @str: a UTF-8 encoded string
1125 * @len: the maximum length of @str to use. If < 0, then
1126 * the string is %NULL terminated.
1128 * @items_read: location to store number of bytes read, or %NULL.
1129 * If %NULL, then %G_CONVERT_ERROR_PARTIAL_INPUT will be
1130 * returned in case @str contains a trailing partial
1131 * character. If an error occurs then the index of the
1132 * invalid input is stored here.
1133 * @items_written: location to store number of words written, or %NULL.
1134 * The value stored here does not include the trailing
1136 * @error: location to store the error occuring, or %NULL to ignore
1137 * errors. Any of the errors in #GConvertError other than
1138 * %G_CONVERT_ERROR_NO_CONVERSION may occur.
1140 * Convert a string from UTF-8 to UTF-16. A 0 word will be
1141 * added to the result after the converted text.
1143 * Return value: a pointer to a newly allocated UTF-16 string.
1144 * This value must be freed with g_free(). If an
1145 * error occurs, %NULL will be returned and
1149 g_utf8_to_utf16 (const gchar *str,
1152 gint *items_written,
1155 gunichar2 *result = NULL;
1160 g_return_val_if_fail (str != NULL, NULL);
1164 while ((len < 0 || str + len - in > 0) && *in)
1166 gunichar wc = g_utf8_get_char_extended (in, str + len - in);
1167 if (wc & 0x80000000)
1169 if (wc == (gunichar)-2)
1174 g_set_error (error, G_CONVERT_ERROR, G_CONVERT_ERROR_PARTIAL_INPUT,
1175 _("Partial character sequence at end of input"));
1178 g_set_error (error, G_CONVERT_ERROR, G_CONVERT_ERROR_ILLEGAL_SEQUENCE,
1179 _("Invalid byte sequence in conversion input"));
1186 else if (wc < 0xe000)
1188 g_set_error (error, G_CONVERT_ERROR, G_CONVERT_ERROR_ILLEGAL_SEQUENCE,
1189 _("Invalid sequence in conversion input"));
1193 else if (wc < 0x10000)
1195 else if (wc < 0x110000)
1199 g_set_error (error, G_CONVERT_ERROR, G_CONVERT_ERROR_ILLEGAL_SEQUENCE,
1200 _("Character out of range for UTF-16"));
1205 in = g_utf8_next_char (in);
1208 result = g_new (gunichar2, n16 + 1);
1211 for (i = 0; i < n16;)
1213 gunichar wc = g_utf8_get_char (in);
1221 result[i++] = (wc - 0x10000) / 0x400 + 0xd800;
1222 result[i++] = (wc - 0x10000) % 0x400 + 0xdc00;
1225 in = g_utf8_next_char (in);
1231 *items_written = n16;
1235 *items_read = in - str;
1242 * @str: a UCS-4 encoded string
1243 * @len: the maximum length of @str to use. If < 0, then
1244 * the string is terminated with a zero character.
1245 * @items_read: location to store number of bytes read, or %NULL.
1246 * If an error occurs then the index of the invalid input
1248 * @items_written: location to store number of words written, or %NULL.
1249 * The value stored here does not include the trailing
1251 * @error: location to store the error occuring, or %NULL to ignore
1252 * errors. Any of the errors in #GConvertError other than
1253 * %G_CONVERT_ERROR_NO_CONVERSION may occur.
1255 * Convert a string from UCS-4 to UTF-16. A 0 word will be
1256 * added to the result after the converted text.
1258 * Return value: a pointer to a newly allocated UTF-16 string.
1259 * This value must be freed with g_free(). If an
1260 * error occurs, %NULL will be returned and
1264 g_ucs4_to_utf16 (const gunichar *str,
1267 gint *items_written,
1270 gunichar2 *result = NULL;
1276 while ((len < 0 || i < len) && str[i])
1278 gunichar wc = str[i];
1282 else if (wc < 0xe000)
1284 g_set_error (error, G_CONVERT_ERROR, G_CONVERT_ERROR_ILLEGAL_SEQUENCE,
1285 _("Invalid sequence in conversion input"));
1289 else if (wc < 0x10000)
1291 else if (wc < 0x110000)
1295 g_set_error (error, G_CONVERT_ERROR, G_CONVERT_ERROR_ILLEGAL_SEQUENCE,
1296 _("Character out of range for UTF-16"));
1304 result = g_new (gunichar2, n16 + 1);
1306 for (i = 0, j = 0; j < n16; i++)
1308 gunichar wc = str[i];
1316 result[j++] = (wc - 0x10000) / 0x400 + 0xd800;
1317 result[j++] = (wc - 0x10000) % 0x400 + 0xdc00;
1323 *items_written = n16;
1334 * @str: a pointer to character data
1335 * @max_len: max bytes to validate, or -1 to go until nul
1336 * @end: return location for end of valid data
1338 * Validates UTF-8 encoded text. @str is the text to validate;
1339 * if @str is nul-terminated, then @max_len can be -1, otherwise
1340 * @max_len should be the number of bytes to validate.
1341 * If @end is non-NULL, then the end of the valid range
1342 * will be stored there (i.e. the address of the first invalid byte
1343 * if some bytes were invalid, or the end of the text being validated
1346 * Returns TRUE if all of @str was valid. Many GLib and GTK+
1347 * routines <emphasis>require</emphasis> valid UTF8 as input;
1348 * so data read from a file or the network should be checked
1349 * with g_utf8_validate() before doing anything else with it.
1351 * Return value: TRUE if the text was valid UTF-8.
1354 g_utf8_validate (const gchar *str,
1361 g_return_val_if_fail (str != NULL, FALSE);
1368 while ((max_len < 0 || (p - str) < max_len) && *p)
1370 int i, mask = 0, len;
1372 unsigned char c = (unsigned char) *p;
1374 UTF8_COMPUTE (c, mask, len);
1379 /* check that the expected number of bytes exists in str */
1381 ((max_len - (p - str)) < len))
1384 UTF8_GET (result, p, i, mask, len);
1386 if (UTF8_LENGTH (result) != len) /* Check for overlong UTF-8 */
1389 if (result == (gunichar)-1)
1392 if (!UNICODE_VALID (result))
1401 /* See that we covered the entire length if a length was
1402 * passed in, or that we ended on a nul if not
1405 p != (str + max_len))
1407 else if (max_len < 0 &&