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 #ifdef G_PLATFORM_WIN32
41 #define UTF8_COMPUTE(Char, Mask, Len) \
47 else if ((Char & 0xe0) == 0xc0) \
52 else if ((Char & 0xf0) == 0xe0) \
57 else if ((Char & 0xf8) == 0xf0) \
62 else if ((Char & 0xfc) == 0xf8) \
67 else if ((Char & 0xfe) == 0xfc) \
75 #define UTF8_LENGTH(Char) \
76 ((Char) < 0x80 ? 1 : \
77 ((Char) < 0x800 ? 2 : \
78 ((Char) < 0x10000 ? 3 : \
79 ((Char) < 0x200000 ? 4 : \
80 ((Char) < 0x4000000 ? 5 : 6)))))
83 #define UTF8_GET(Result, Chars, Count, Mask, Len) \
84 (Result) = (Chars)[0] & (Mask); \
85 for ((Count) = 1; (Count) < (Len); ++(Count)) \
87 if (((Chars)[(Count)] & 0xc0) != 0x80) \
93 (Result) |= ((Chars)[(Count)] & 0x3f); \
96 #define UNICODE_VALID(Char) \
97 ((Char) < 0x110000 && \
98 ((Char) < 0xD800 || (Char) >= 0xE000) && \
99 (Char) != 0xFFFE && (Char) != 0xFFFF)
102 gchar g_utf8_skip[256] = {
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 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,
109 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,
110 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
114 * g_utf8_find_prev_char:
115 * @str: pointer to the beginning of a UTF-8 string
116 * @p: pointer to some position within @str
118 * Given a position @p with a UTF-8 encoded string @str, find the start
119 * of the previous UTF-8 character starting before @p. Returns %NULL if no
120 * UTF-8 characters are present in @p before @str.
122 * @p does not have to be at the beginning of a UTF-8 chracter. No check
123 * is made to see if the character found is actually valid other than
124 * it starts with an appropriate byte.
126 * Return value: a pointer to the found character or %NULL.
129 g_utf8_find_prev_char (const char *str,
132 for (--p; p > str; --p)
134 if ((*p & 0xc0) != 0x80)
141 * g_utf8_find_next_char:
142 * @p: a pointer to a position within a UTF-8 encoded string
143 * @end: a pointer to the end of the string, or %NULL to indicate
144 * that the string is NULL terminated, in which case
145 * the returned value will be
147 * Find the start of the next utf-8 character in the string after @p
149 * @p does not have to be at the beginning of a UTF-8 chracter. No check
150 * is made to see if the character found is actually valid other than
151 * it starts with an appropriate byte.
153 * Return value: a pointer to the found character or %NULL
156 g_utf8_find_next_char (const gchar *p,
162 for (++p; p < end && (*p & 0xc0) == 0x80; ++p)
165 for (++p; (*p & 0xc0) == 0x80; ++p)
168 return (p == end) ? NULL : (gchar *)p;
173 * @p: a pointer to a position within a UTF-8 encoded string
175 * Find the previous UTF-8 character in the string before @p
177 * @p does not have to be at the beginning of a UTF-8 character. No check
178 * is made to see if the character found is actually valid other than
179 * it starts with an appropriate byte. If @p might be the first
180 * character of the string, you must use g_utf8_find_prev_char instead.
182 * Return value: a pointer to the found character.
185 g_utf8_prev_char (const gchar *p)
190 if ((*p & 0xc0) != 0x80)
197 * @p: pointer to the start of a UTF-8 string.
198 * @max: the maximum number of bytes to examine. If @max
199 * is less than 0, then the string is assumed to be
202 * Return value: the length of the string in characters
205 g_utf8_strlen (const gchar *p, gint max)
208 const gchar *start = p;
214 p = g_utf8_next_char (p);
223 p = g_utf8_next_char (p);
225 while (p - start < max && *p)
228 p = g_utf8_next_char (p);
231 /* only do the last len increment if we got a complete
232 * char (don't count partial chars)
234 if (p - start == max)
243 * @p: a pointer to unicode character encoded as UTF-8
245 * Convert a sequence of bytes encoded as UTF-8 to a unicode character.
247 * Return value: the resulting character or (gunichar)-1 if @p does
248 * not point to a valid UTF-8 encoded unicode character
251 g_utf8_get_char (const gchar *p)
253 int i, mask = 0, len;
255 unsigned char c = (unsigned char) *p;
257 UTF8_COMPUTE (c, mask, len);
260 UTF8_GET (result, p, i, mask, len);
266 * g_utf8_offset_to_pointer:
267 * @str: a UTF-8 encoded string
268 * @offset: a character offset within the string.
270 * Converts from an integer character offset to a pointer to a position
273 * Return value: the resulting pointer
276 g_utf8_offset_to_pointer (const gchar *str,
279 const gchar *s = str;
281 s = g_utf8_next_char (s);
287 * g_utf8_pointer_to_offset:
288 * @str: a UTF-8 encoded string
289 * @pos: a pointer to a position within @str
291 * Converts from a pointer to position within a string to a integer
294 * Return value: the resulting character offset
297 g_utf8_pointer_to_offset (const gchar *str,
300 const gchar *s = str;
305 s = g_utf8_next_char (s);
314 g_utf8_strncpy (gchar *dest, const gchar *src, size_t n)
316 const gchar *s = src;
319 s = g_utf8_next_char(s);
322 strncpy(dest, src, s - src);
328 g_utf8_get_charset_internal (char **a)
330 char *charset = getenv("CHARSET");
332 if (charset && a && ! *a)
335 if (charset && strstr (charset, "UTF-8"))
339 charset = nl_langinfo(CODESET);
344 if (strcmp (charset, "UTF-8") == 0)
349 #if 0 /* #ifdef _NL_CTYPE_CODESET_NAME */
350 charset = nl_langinfo (_NL_CTYPE_CODESET_NAME);
355 if (strcmp (charset, "UTF-8") == 0)
360 #ifdef G_PLATFORM_WIN32
363 static char codepage[10];
365 sprintf (codepage, "CP%d", GetACP ());
367 /* What about codepage 1200? Is that UTF-8? */
375 /* Assume this for compatibility at present. */
379 static int utf8_locale_cache = -1;
380 static char *utf8_charset_cache = NULL;
383 g_get_charset (char **charset)
385 if (utf8_locale_cache != -1)
388 *charset = utf8_charset_cache;
389 return utf8_locale_cache;
391 utf8_locale_cache = g_utf8_get_charset_internal (&utf8_charset_cache);
393 *charset = utf8_charset_cache;
394 return utf8_locale_cache;
401 * @c: a ISO10646 character code
402 * @outbuf: output buffer, must have at least 6 bytes of space.
403 * If %NULL, the length will be computed and returned
404 * and nothing will be written to @out.
406 * Convert a single character to utf8
408 * Return value: number of bytes written
411 g_unichar_to_utf8 (gunichar c, gchar *outbuf)
427 else if (c < 0x10000)
432 else if (c < 0x200000)
437 else if (c < 0x4000000)
450 for (i = len - 1; i > 0; --i)
452 outbuf[i] = (c & 0x3f) | 0x80;
455 outbuf[0] = c | first;
463 * @p: a nul-terminated utf-8 string
464 * @c: a iso-10646 character/
466 * Find the leftmost occurence of the given iso-10646 character
469 * Return value: NULL if the string does not contain the character, otherwise, a
470 * a pointer to the start of the leftmost of the character in the string.
473 g_utf8_strchr (const char *p, gunichar c)
477 gint len = g_unichar_to_utf8 (c, ch);
480 return strstr(p, ch);
486 * @p: a nul-terminated utf-8 string
487 * @c: a iso-10646 character/
489 * Find the rightmost occurence of the given iso-10646 character
492 * Return value: NULL if the string does not contain the character, otherwise, a
493 * a pointer to the start of the rightmost of the character in the string.
496 /* This is ifdefed out atm as there is no strrstr function in libc.
499 unicode_strrchr (const char *p, gunichar c)
503 len = g_unichar_to_utf8 (c, ch);
506 return strrstr(p, ch);
511 /* Like g_utf8_get_char, but take a maximum length
512 * and return (gunichar)-2 on incomplete trailing character
514 static inline gunichar
515 g_utf8_get_char_extended (const gchar *p, int max_len)
518 gunichar wc = (guchar) *p;
560 if (max_len >= 0 && len > max_len)
562 for (i = 1; i < max_len; i++)
564 if ((((guchar *)p)[i] & 0xc0) != 0x80)
570 for (i = 1; i < len; ++i)
572 gunichar ch = ((guchar *)p)[i];
574 if ((ch & 0xc0) != 0x80)
586 if (UTF8_LENGTH(wc) != len)
593 * g_utf8_to_ucs4_fast:
594 * @str: a UTF-8 encoded string
595 * @len: the maximum length of @str to use. If < 0, then
596 * the string is %NULL terminated.
597 * @items_written: location to store the number of characters in the
600 * Convert a string from UTF-8 to a 32-bit fixed width
601 * representation as UCS-4, assuming valid UTF-8 input.
602 * This function is roughly twice as fast as g_utf8_to_ucs4()
603 * but does no error checking on the input.
605 * Return value: a pointer to a newly allocated UCS-4 string.
606 * This value must be freed with g_free()
609 g_utf8_to_ucs4_fast (const gchar *str,
618 g_return_val_if_fail (str != NULL, NULL);
626 p = g_utf8_next_char (p);
632 while (*p && p < str + len)
634 p = g_utf8_next_char (p);
639 result = g_new (gunichar, n_chars + 1);
642 for (i=0; i < n_chars; i++)
644 gunichar wc = ((unsigned char *)p)[0];
679 for (j = 1; j < charlen; j++)
682 wc |= ((unsigned char *)p)[j] & 0x3f;
699 * @str: a UTF-8 encoded string
700 * @len: the maximum length of @str to use. If < 0, then
701 * the string is %NULL terminated.
702 * @items_read: location to store number of bytes read, or %NULL.
703 * If %NULL, then %G_CONVERT_ERROR_PARTIAL_INPUT will be
704 * returned in case @str contains a trailing partial
705 * character. If an error occurs then the index of the
706 * invalid input is stored here.
707 * @items_written: location to store number of characters written or %NULL.
708 * The value here stored does not include the trailing 0
710 * @error: location to store the error occuring, or %NULL to ignore
711 * errors. Any of the errors in #GConvertError other than
712 * %G_CONVERT_ERROR_NO_CONVERSION may occur.
714 * Convert a string from UTF-8 to a 32-bit fixed width
715 * representation as UCS-4. A trailing 0 will be added to the
716 * string after the converted text.
718 * Return value: a pointer to a newly allocated UCS-4 string.
719 * This value must be freed with g_free(). If an
720 * error occurs, %NULL will be returned and
724 g_utf8_to_ucs4 (const gchar *str,
730 gunichar *result = NULL;
736 while ((len < 0 || str + len - in > 0) && *in)
738 gunichar wc = g_utf8_get_char_extended (in, str + len - in);
741 if (wc == (gunichar)-2)
746 g_set_error (error, G_CONVERT_ERROR, G_CONVERT_ERROR_PARTIAL_INPUT,
747 _("Partial character sequence at end of input"));
750 g_set_error (error, G_CONVERT_ERROR, G_CONVERT_ERROR_ILLEGAL_SEQUENCE,
751 _("Invalid byte sequence in conversion input"));
758 in = g_utf8_next_char (in);
761 result = g_new (gunichar, n_chars + 1);
764 for (i=0; i < n_chars; i++)
766 result[i] = g_utf8_get_char (in);
767 in = g_utf8_next_char (in);
772 *items_written = n_chars;
776 *items_read = in - str;
783 * @str: a UCS-4 encoded string
784 * @len: the maximum length of @str to use. If < 0, then
785 * the string is %NULL terminated.
786 * @items_read: location to store number of characters read read, or %NULL.
787 * @items_written: location to store number of bytes written or %NULL.
788 * The value here stored does not include the trailing 0
790 * @error: location to store the error occuring, or %NULL to ignore
791 * errors. Any of the errors in #GConvertError other than
792 * %G_CONVERT_ERROR_NO_CONVERSION may occur.
794 * Convert a string from a 32-bit fixed width representation as UCS-4.
795 * to UTF-8. The result will be terminated with a 0 byte.
797 * Return value: a pointer to a newly allocated UTF-8 string.
798 * This value must be freed with g_free(). If an
799 * error occurs, %NULL will be returned and
803 g_ucs4_to_utf8 (const gunichar *str,
810 gchar *result = NULL;
815 for (i = 0; len < 0 || i < len ; i++)
820 if (str[i] >= 0x80000000)
825 g_set_error (error, G_CONVERT_ERROR, G_CONVERT_ERROR_ILLEGAL_SEQUENCE,
826 _("Character out of range for UTF-8"));
830 result_length += UTF8_LENGTH (str[i]);
833 result = g_malloc (result_length + 1);
837 while (p < result + result_length)
838 p += g_unichar_to_utf8 (str[i++], p);
843 *items_written = p - result;
852 #define SURROGATE_VALUE(h,l) (((h) - 0xd800) * 0x400 + (l) - 0xdc00 + 0x10000)
856 * @str: a UTF-16 encoded string
857 * @len: the maximum length of @str to use. If < 0, then
858 * the string is terminated with a 0 character.
859 * @items_read: location to store number of words read, or %NULL.
860 * If %NULL, then %G_CONVERT_ERROR_PARTIAL_INPUT will be
861 * returned in case @str contains a trailing partial
862 * character. If an error occurs then the index of the
863 * invalid input is stored here.
864 * @items_written: location to store number of bytes written, or %NULL.
865 * The value stored here does not include the trailing
867 * @error: location to store the error occuring, or %NULL to ignore
868 * errors. Any of the errors in #GConvertError other than
869 * %G_CONVERT_ERROR_NO_CONVERSION may occur.
871 * Convert a string from UTF-16 to UTF-8. The result will be
872 * terminated with a 0 byte.
874 * Return value: a pointer to a newly allocated UTF-8 string.
875 * This value must be freed with g_free(). If an
876 * error occurs, %NULL will be returned and
880 g_utf16_to_utf8 (const gunichar2 *str,
886 /* This function and g_utf16_to_ucs4 are almost exactly identical - The lines that differ
891 gchar *result = NULL;
893 gunichar high_surrogate;
895 g_return_val_if_fail (str != 0, NULL);
900 while ((len < 0 || in - str < len) && *in)
905 if (c >= 0xdc00 && c < 0xe000) /* low surrogate */
909 wc = SURROGATE_VALUE (high_surrogate, c);
914 g_set_error (error, G_CONVERT_ERROR, G_CONVERT_ERROR_ILLEGAL_SEQUENCE,
915 _("Invalid sequence in conversion input"));
923 g_set_error (error, G_CONVERT_ERROR, G_CONVERT_ERROR_ILLEGAL_SEQUENCE,
924 _("Invalid sequence in conversion input"));
928 if (c >= 0xd800 && c < 0xdc00) /* high surrogate */
937 /********** DIFFERENT for UTF8/UCS4 **********/
938 n_bytes += UTF8_LENGTH (wc);
944 if (high_surrogate && !items_read)
946 g_set_error (error, G_CONVERT_ERROR, G_CONVERT_ERROR_PARTIAL_INPUT,
947 _("Partial character sequence at end of input"));
951 /* At this point, everything is valid, and we just need to convert
953 /********** DIFFERENT for UTF8/UCS4 **********/
954 result = g_malloc (n_bytes + 1);
959 while (out < result + n_bytes)
964 if (c >= 0xdc00 && c < 0xe000) /* low surrogate */
966 wc = SURROGATE_VALUE (high_surrogate, c);
969 else if (c >= 0xd800 && c < 0xdc00) /* high surrogate */
977 /********** DIFFERENT for UTF8/UCS4 **********/
978 out += g_unichar_to_utf8 (wc, out);
984 /********** DIFFERENT for UTF8/UCS4 **********/
988 /********** DIFFERENT for UTF8/UCS4 **********/
989 *items_written = out - result;
993 *items_read = in - str;
1000 * @str: a UTF-16 encoded string
1001 * @len: the maximum length of @str to use. If < 0, then
1002 * the string is terminated with a 0 character.
1003 * @items_read: location to store number of words read, or %NULL.
1004 * If %NULL, then %G_CONVERT_ERROR_PARTIAL_INPUT will be
1005 * returned in case @str contains a trailing partial
1006 * character. If an error occurs then the index of the
1007 * invalid input is stored here.
1008 * @items_written: location to store number of characters written, or %NULL.
1009 * The value stored here does not include the trailing
1011 * @error: location to store the error occuring, or %NULL to ignore
1012 * errors. Any of the errors in #GConvertError other than
1013 * %G_CONVERT_ERROR_NO_CONVERSION may occur.
1015 * Convert a string from UTF-16 to UCS-4. The result will be
1016 * terminated with a 0 character.
1018 * Return value: a pointer to a newly allocated UCS-4 string.
1019 * This value must be freed with g_free(). If an
1020 * error occurs, %NULL will be returned and
1024 g_utf16_to_ucs4 (const gunichar2 *str,
1027 gint *items_written,
1030 const gunichar2 *in;
1032 gchar *result = NULL;
1034 gunichar high_surrogate;
1036 g_return_val_if_fail (str != 0, NULL);
1041 while ((len < 0 || in - str < len) && *in)
1046 if (c >= 0xdc00 && c < 0xe000) /* low surrogate */
1050 wc = SURROGATE_VALUE (high_surrogate, c);
1055 g_set_error (error, G_CONVERT_ERROR, G_CONVERT_ERROR_ILLEGAL_SEQUENCE,
1056 _("Invalid sequence in conversion input"));
1064 g_set_error (error, G_CONVERT_ERROR, G_CONVERT_ERROR_ILLEGAL_SEQUENCE,
1065 _("Invalid sequence in conversion input"));
1069 if (c >= 0xd800 && c < 0xdc00) /* high surrogate */
1078 /********** DIFFERENT for UTF8/UCS4 **********/
1079 n_bytes += sizeof (gunichar);
1085 if (high_surrogate && !items_read)
1087 g_set_error (error, G_CONVERT_ERROR, G_CONVERT_ERROR_PARTIAL_INPUT,
1088 _("Partial character sequence at end of input"));
1092 /* At this point, everything is valid, and we just need to convert
1094 /********** DIFFERENT for UTF8/UCS4 **********/
1095 result = g_malloc (n_bytes + 4);
1100 while (out < result + n_bytes)
1105 if (c >= 0xdc00 && c < 0xe000) /* low surrogate */
1107 wc = SURROGATE_VALUE (high_surrogate, c);
1110 else if (c >= 0xd800 && c < 0xdc00) /* high surrogate */
1118 /********** DIFFERENT for UTF8/UCS4 **********/
1119 *(gunichar *)out = wc;
1120 out += sizeof (gunichar);
1126 /********** DIFFERENT for UTF8/UCS4 **********/
1127 *(gunichar *)out = 0;
1130 /********** DIFFERENT for UTF8/UCS4 **********/
1131 *items_written = (out - result) / sizeof (gunichar);
1135 *items_read = in - str;
1137 return (gunichar *)result;
1142 * @str: a UTF-8 encoded string
1143 * @len: the maximum length of @str to use. If < 0, then
1144 * the string is %NULL terminated.
1146 * @items_read: location to store number of bytes read, or %NULL.
1147 * If %NULL, then %G_CONVERT_ERROR_PARTIAL_INPUT will be
1148 * returned in case @str contains a trailing partial
1149 * character. If an error occurs then the index of the
1150 * invalid input is stored here.
1151 * @items_written: location to store number of words written, or %NULL.
1152 * The value stored here does not include the trailing
1154 * @error: location to store the error occuring, or %NULL to ignore
1155 * errors. Any of the errors in #GConvertError other than
1156 * %G_CONVERT_ERROR_NO_CONVERSION may occur.
1158 * Convert a string from UTF-8 to UTF-16. A 0 word will be
1159 * added to the result after the converted text.
1161 * Return value: a pointer to a newly allocated UTF-16 string.
1162 * This value must be freed with g_free(). If an
1163 * error occurs, %NULL will be returned and
1167 g_utf8_to_utf16 (const gchar *str,
1170 gint *items_written,
1173 gunichar2 *result = NULL;
1178 g_return_val_if_fail (str != NULL, NULL);
1182 while ((len < 0 || str + len - in > 0) && *in)
1184 gunichar wc = g_utf8_get_char_extended (in, str + len - in);
1185 if (wc & 0x80000000)
1187 if (wc == (gunichar)-2)
1192 g_set_error (error, G_CONVERT_ERROR, G_CONVERT_ERROR_PARTIAL_INPUT,
1193 _("Partial character sequence at end of input"));
1196 g_set_error (error, G_CONVERT_ERROR, G_CONVERT_ERROR_ILLEGAL_SEQUENCE,
1197 _("Invalid byte sequence in conversion input"));
1204 else if (wc < 0xe000)
1206 g_set_error (error, G_CONVERT_ERROR, G_CONVERT_ERROR_ILLEGAL_SEQUENCE,
1207 _("Invalid sequence in conversion input"));
1211 else if (wc < 0x10000)
1213 else if (wc < 0x110000)
1217 g_set_error (error, G_CONVERT_ERROR, G_CONVERT_ERROR_ILLEGAL_SEQUENCE,
1218 _("Character out of range for UTF-16"));
1223 in = g_utf8_next_char (in);
1226 result = g_new (gunichar2, n16 + 1);
1229 for (i = 0; i < n16;)
1231 gunichar wc = g_utf8_get_char (in);
1239 result[i++] = (wc - 0x10000) / 0x400 + 0xd800;
1240 result[i++] = (wc - 0x10000) % 0x400 + 0xdc00;
1243 in = g_utf8_next_char (in);
1249 *items_written = n16;
1253 *items_read = in - str;
1260 * @str: a UCS-4 encoded string
1261 * @len: the maximum length of @str to use. If < 0, then
1262 * the string is terminated with a zero character.
1263 * @items_read: location to store number of bytes read, or %NULL.
1264 * If an error occurs then the index of the invalid input
1266 * @items_written: location to store number of words written, or %NULL.
1267 * The value stored here does not include the trailing
1269 * @error: location to store the error occuring, or %NULL to ignore
1270 * errors. Any of the errors in #GConvertError other than
1271 * %G_CONVERT_ERROR_NO_CONVERSION may occur.
1273 * Convert a string from UCS-4 to UTF-16. A 0 word will be
1274 * added to the result after the converted text.
1276 * Return value: a pointer to a newly allocated UTF-16 string.
1277 * This value must be freed with g_free(). If an
1278 * error occurs, %NULL will be returned and
1282 g_ucs4_to_utf16 (const gunichar *str,
1285 gint *items_written,
1288 gunichar2 *result = NULL;
1294 while ((len < 0 || i < len) && str[i])
1296 gunichar wc = str[i];
1300 else if (wc < 0xe000)
1302 g_set_error (error, G_CONVERT_ERROR, G_CONVERT_ERROR_ILLEGAL_SEQUENCE,
1303 _("Invalid sequence in conversion input"));
1307 else if (wc < 0x10000)
1309 else if (wc < 0x110000)
1313 g_set_error (error, G_CONVERT_ERROR, G_CONVERT_ERROR_ILLEGAL_SEQUENCE,
1314 _("Character out of range for UTF-16"));
1322 result = g_new (gunichar2, n16 + 1);
1324 for (i = 0, j = 0; j < n16; i++)
1326 gunichar wc = str[i];
1334 result[j++] = (wc - 0x10000) / 0x400 + 0xd800;
1335 result[j++] = (wc - 0x10000) % 0x400 + 0xdc00;
1341 *items_written = n16;
1352 * @str: a pointer to character data
1353 * @max_len: max bytes to validate, or -1 to go until nul
1354 * @end: return location for end of valid data
1356 * Validates UTF-8 encoded text. @str is the text to validate;
1357 * if @str is nul-terminated, then @max_len can be -1, otherwise
1358 * @max_len should be the number of bytes to validate.
1359 * If @end is non-NULL, then the end of the valid range
1360 * will be stored there (i.e. the address of the first invalid byte
1361 * if some bytes were invalid, or the end of the text being validated
1364 * Returns TRUE if all of @str was valid. Many GLib and GTK+
1365 * routines <emphasis>require</emphasis> valid UTF8 as input;
1366 * so data read from a file or the network should be checked
1367 * with g_utf8_validate() before doing anything else with it.
1369 * Return value: TRUE if the text was valid UTF-8.
1372 g_utf8_validate (const gchar *str,
1379 g_return_val_if_fail (str != NULL, FALSE);
1386 while ((max_len < 0 || (p - str) < max_len) && *p)
1388 int i, mask = 0, len;
1390 unsigned char c = (unsigned char) *p;
1392 UTF8_COMPUTE (c, mask, len);
1397 /* check that the expected number of bytes exists in str */
1399 ((max_len - (p - str)) < len))
1402 UTF8_GET (result, p, i, mask, len);
1404 if (UTF8_LENGTH (result) != len) /* Check for overlong UTF-8 */
1407 if (result == (gunichar)-1)
1410 if (!UNICODE_VALID (result))
1419 /* See that we covered the entire length if a length was
1420 * passed in, or that we ended on a nul if not
1423 p != (str + max_len))
1425 else if (max_len < 0 &&
1433 * g_unichar_validate:
1434 * @ch: a Unicode character
1436 * Checks whether @ch is a valid Unicode character. Some possible
1437 * integer values of @ch will not be valid. 0 is considered a valid
1438 * character, though it's normally a string terminator.
1440 * Return value: %TRUE if @ch is a valid Unicode character
1443 g_unichar_validate (gunichar ch)
1445 return UNICODE_VALID (ch);