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,
209 const gchar *start = p;
215 p = g_utf8_next_char (p);
224 p = g_utf8_next_char (p);
226 while (p - start < max && *p)
229 p = g_utf8_next_char (p);
232 /* only do the last len increment if we got a complete
233 * char (don't count partial chars)
235 if (p - start == max)
244 * @p: a pointer to unicode character encoded as UTF-8
246 * Convert a sequence of bytes encoded as UTF-8 to a unicode character.
247 * If @p does not point to a valid UTF-8 encoded character, results are
248 * undefined. If you are not sure that the bytes are complete
249 * valid unicode characters, you should use g_utf8_get_char_validated()
252 * Return value: the resulting character
255 g_utf8_get_char (const gchar *p)
257 int i, mask = 0, len;
259 unsigned char c = (unsigned char) *p;
261 UTF8_COMPUTE (c, mask, len);
264 UTF8_GET (result, p, i, mask, len);
270 * g_utf8_offset_to_pointer:
271 * @str: a UTF-8 encoded string
272 * @offset: a character offset within the string.
274 * Converts from an integer character offset to a pointer to a position
277 * Return value: the resulting pointer
280 g_utf8_offset_to_pointer (const gchar *str,
283 const gchar *s = str;
285 s = g_utf8_next_char (s);
291 * g_utf8_pointer_to_offset:
292 * @str: a UTF-8 encoded string
293 * @pos: a pointer to a position within @str
295 * Converts from a pointer to position within a string to a integer
298 * Return value: the resulting character offset
301 g_utf8_pointer_to_offset (const gchar *str,
304 const gchar *s = str;
309 s = g_utf8_next_char (s);
319 * @dest: buffer to fill with characters from @src
321 * @n: character count
323 * Like the standard C strncpy() function, but copies a given number
324 * of characters instead of a given number of bytes. The @src string
325 * must be valid UTF-8 encoded text. (Use g_utf8_validate() on all
326 * text before trying to use UTF-8 utility functions with it.)
328 * Return value: @dest
331 g_utf8_strncpy (gchar *dest, const gchar *src, size_t n)
333 const gchar *s = src;
336 s = g_utf8_next_char(s);
339 strncpy(dest, src, s - src);
345 g_utf8_get_charset_internal (char **a)
347 char *charset = getenv("CHARSET");
349 if (charset && a && ! *a)
352 if (charset && strstr (charset, "UTF-8"))
356 charset = nl_langinfo(CODESET);
361 if (strcmp (charset, "UTF-8") == 0)
366 #if 0 /* #ifdef _NL_CTYPE_CODESET_NAME */
367 charset = nl_langinfo (_NL_CTYPE_CODESET_NAME);
372 if (strcmp (charset, "UTF-8") == 0)
377 #ifdef G_PLATFORM_WIN32
380 static char codepage[10];
382 sprintf (codepage, "CP%d", GetACP ());
384 /* What about codepage 1200? Is that UTF-8? */
392 /* Assume this for compatibility at present. */
396 static int utf8_locale_cache = -1;
397 static char *utf8_charset_cache = NULL;
401 * @charset: return location for character set name
403 * Obtains the character set for the current locale; you might use
404 * this character set as an argument to g_convert(), to convert from
405 * the current locale's encoding to some other encoding. (Frequently
406 * g_locale_to_utf8() and g_locale_from_utf8() are nice shortcuts,
409 * The return value is %TRUE if the locale's encoding is UTF-8, in that
410 * case you can perhaps avoid calling g_convert().
412 * The string returned in @charset is not allocated, and should not be
415 * Return value: %TRUE if the returned charset is UTF-8
418 g_get_charset (G_CONST_RETURN char **charset)
420 if (utf8_locale_cache != -1)
423 *charset = utf8_charset_cache;
424 return utf8_locale_cache;
426 utf8_locale_cache = g_utf8_get_charset_internal (&utf8_charset_cache);
428 *charset = utf8_charset_cache;
429 return utf8_locale_cache;
436 * @c: a ISO10646 character code
437 * @outbuf: output buffer, must have at least 6 bytes of space.
438 * If %NULL, the length will be computed and returned
439 * and nothing will be written to @out.
441 * Convert a single character to utf8
443 * Return value: number of bytes written
446 g_unichar_to_utf8 (gunichar c,
463 else if (c < 0x10000)
468 else if (c < 0x200000)
473 else if (c < 0x4000000)
486 for (i = len - 1; i > 0; --i)
488 outbuf[i] = (c & 0x3f) | 0x80;
491 outbuf[0] = c | first;
499 * @p: a nul-terminated utf-8 string
500 * @p_len: the maximum length of p
501 * @c: a iso-10646 character
503 * Find the leftmost occurence of the given iso-10646 character
504 * in a UTF-8 string, while limiting the search to p_len bytes.
505 * If len is -1, allow unbounded search.
507 * Return value: NULL if the string does not contain the character, otherwise, a
508 * a pointer to the start of the leftmost of the character in the string.
511 g_utf8_strchr (const char *p,
517 gint len = g_unichar_to_utf8 (c, ch);
520 return g_strstr_len (p, p_len, ch);
526 * @p: a nul-terminated utf-8 string
527 * @p_len: the maximum length of p
528 * @c: a iso-10646 character/
530 * Find the rightmost occurence of the given iso-10646 character
531 * in a UTF-8 string, while limiting the search to p_len bytes.
532 * If len is -1, allow unbounded search.
534 * Return value: NULL if the string does not contain the character, otherwise, a
535 * a pointer to the start of the rightmost of the character in the string.
538 g_utf8_strrchr (const char *p,
544 gint len = g_unichar_to_utf8 (c, ch);
547 return g_strrstr_len (p, p_len, ch);
551 /* Like g_utf8_get_char, but take a maximum length
552 * and return (gunichar)-2 on incomplete trailing character
554 static inline gunichar
555 g_utf8_get_char_extended (const gchar *p,
559 gunichar wc = (guchar) *p;
599 if (max_len >= 0 && len > max_len)
601 for (i = 1; i < max_len; i++)
603 if ((((guchar *)p)[i] & 0xc0) != 0x80)
609 for (i = 1; i < len; ++i)
611 gunichar ch = ((guchar *)p)[i];
613 if ((ch & 0xc0) != 0x80)
625 if (UTF8_LENGTH(wc) != len)
632 * g_utf8_get_char_validated:
633 * @p: a pointer to unicode character encoded as UTF-8
634 * @max_len: the maximum number of bytes to read, or -1, for no maximum.
636 * Convert a sequence of bytes encoded as UTF-8 to a unicode character.
637 * This function checks for incomplete characters, for invalid characters
638 * such as characters that are out of the range of Unicode, and for
639 * overlong encodings of valid characters.
641 * Return value: the resulting character. If @p points to a partial
642 * sequence at the end of a string that could begin a valid character,
643 * returns (gunichar)-2; otherwise, if @p does not point to a valid
644 * UTF-8 encoded unicode character, returns (gunichar)-1.
647 g_utf8_get_char_validated (const gchar *p,
650 gunichar result = g_utf8_get_char_extended (p, max_len);
652 if (result & 0x80000000)
654 else if (!UNICODE_VALID (result))
661 * g_utf8_to_ucs4_fast:
662 * @str: a UTF-8 encoded string
663 * @len: the maximum length of @str to use. If < 0, then
664 * the string is %NULL terminated.
665 * @items_written: location to store the number of characters in the
668 * Convert a string from UTF-8 to a 32-bit fixed width
669 * representation as UCS-4, assuming valid UTF-8 input.
670 * This function is roughly twice as fast as g_utf8_to_ucs4()
671 * but does no error checking on the input.
673 * Return value: a pointer to a newly allocated UCS-4 string.
674 * This value must be freed with g_free()
677 g_utf8_to_ucs4_fast (const gchar *str,
679 glong *items_written)
686 g_return_val_if_fail (str != NULL, NULL);
694 p = g_utf8_next_char (p);
700 while (p < str + len && *p)
702 p = g_utf8_next_char (p);
707 result = g_new (gunichar, n_chars + 1);
710 for (i=0; i < n_chars; i++)
712 gunichar wc = ((unsigned char *)p)[0];
747 for (j = 1; j < charlen; j++)
750 wc |= ((unsigned char *)p)[j] & 0x3f;
767 * @str: a UTF-8 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 bytes read, or %NULL.
771 * If %NULL, then %G_CONVERT_ERROR_PARTIAL_INPUT will be
772 * returned in case @str contains a trailing partial
773 * character. If an error occurs then the index of the
774 * invalid input is stored here.
775 * @items_written: location to store number of characters written or %NULL.
776 * The value here stored does not include the trailing 0
778 * @error: location to store the error occuring, or %NULL to ignore
779 * errors. Any of the errors in #GConvertError other than
780 * %G_CONVERT_ERROR_NO_CONVERSION may occur.
782 * Convert a string from UTF-8 to a 32-bit fixed width
783 * representation as UCS-4. A trailing 0 will be added to the
784 * string after the converted text.
786 * Return value: a pointer to a newly allocated UCS-4 string.
787 * This value must be freed with g_free(). If an
788 * error occurs, %NULL will be returned and
792 g_utf8_to_ucs4 (const gchar *str,
795 glong *items_written,
798 gunichar *result = NULL;
804 while ((len < 0 || str + len - in > 0) && *in)
806 gunichar wc = g_utf8_get_char_extended (in, str + len - in);
809 if (wc == (gunichar)-2)
814 g_set_error (error, G_CONVERT_ERROR, G_CONVERT_ERROR_PARTIAL_INPUT,
815 _("Partial character sequence at end of input"));
818 g_set_error (error, G_CONVERT_ERROR, G_CONVERT_ERROR_ILLEGAL_SEQUENCE,
819 _("Invalid byte sequence in conversion input"));
826 in = g_utf8_next_char (in);
829 result = g_new (gunichar, n_chars + 1);
832 for (i=0; i < n_chars; i++)
834 result[i] = g_utf8_get_char (in);
835 in = g_utf8_next_char (in);
840 *items_written = n_chars;
844 *items_read = in - str;
851 * @str: a UCS-4 encoded string
852 * @len: the maximum length of @str to use. If < 0, then
853 * the string is %NULL terminated.
854 * @items_read: location to store number of characters read read, or %NULL.
855 * @items_written: location to store number of bytes written or %NULL.
856 * The value here stored does not include the trailing 0
858 * @error: location to store the error occuring, or %NULL to ignore
859 * errors. Any of the errors in #GConvertError other than
860 * %G_CONVERT_ERROR_NO_CONVERSION may occur.
862 * Convert a string from a 32-bit fixed width representation as UCS-4.
863 * to UTF-8. The result will be terminated with a 0 byte.
865 * Return value: a pointer to a newly allocated UTF-8 string.
866 * This value must be freed with g_free(). If an
867 * error occurs, %NULL will be returned and
871 g_ucs4_to_utf8 (const gunichar *str,
874 glong *items_written,
878 gchar *result = NULL;
883 for (i = 0; len < 0 || i < len ; i++)
888 if (str[i] >= 0x80000000)
893 g_set_error (error, G_CONVERT_ERROR, G_CONVERT_ERROR_ILLEGAL_SEQUENCE,
894 _("Character out of range for UTF-8"));
898 result_length += UTF8_LENGTH (str[i]);
901 result = g_malloc (result_length + 1);
905 while (p < result + result_length)
906 p += g_unichar_to_utf8 (str[i++], p);
911 *items_written = p - result;
920 #define SURROGATE_VALUE(h,l) (((h) - 0xd800) * 0x400 + (l) - 0xdc00 + 0x10000)
924 * @str: a UTF-16 encoded string
925 * @len: the maximum length of @str to use. If < 0, then
926 * the string is terminated with a 0 character.
927 * @items_read: location to store number of words read, or %NULL.
928 * If %NULL, then %G_CONVERT_ERROR_PARTIAL_INPUT will be
929 * returned in case @str contains a trailing partial
930 * character. If an error occurs then the index of the
931 * invalid input is stored here.
932 * @items_written: location to store number of bytes written, or %NULL.
933 * The value stored here does not include the trailing
935 * @error: location to store the error occuring, or %NULL to ignore
936 * errors. Any of the errors in #GConvertError other than
937 * %G_CONVERT_ERROR_NO_CONVERSION may occur.
939 * Convert a string from UTF-16 to UTF-8. The result will be
940 * terminated with a 0 byte.
942 * Return value: a pointer to a newly allocated UTF-8 string.
943 * This value must be freed with g_free(). If an
944 * error occurs, %NULL will be returned and
948 g_utf16_to_utf8 (const gunichar2 *str,
951 glong *items_written,
954 /* This function and g_utf16_to_ucs4 are almost exactly identical - The lines that differ
959 gchar *result = NULL;
961 gunichar high_surrogate;
963 g_return_val_if_fail (str != 0, NULL);
968 while ((len < 0 || in - str < len) && *in)
973 if (c >= 0xdc00 && c < 0xe000) /* low surrogate */
977 wc = SURROGATE_VALUE (high_surrogate, c);
982 g_set_error (error, G_CONVERT_ERROR, G_CONVERT_ERROR_ILLEGAL_SEQUENCE,
983 _("Invalid sequence in conversion input"));
991 g_set_error (error, G_CONVERT_ERROR, G_CONVERT_ERROR_ILLEGAL_SEQUENCE,
992 _("Invalid sequence in conversion input"));
996 if (c >= 0xd800 && c < 0xdc00) /* high surrogate */
1005 /********** DIFFERENT for UTF8/UCS4 **********/
1006 n_bytes += UTF8_LENGTH (wc);
1012 if (high_surrogate && !items_read)
1014 g_set_error (error, G_CONVERT_ERROR, G_CONVERT_ERROR_PARTIAL_INPUT,
1015 _("Partial character sequence at end of input"));
1019 /* At this point, everything is valid, and we just need to convert
1021 /********** DIFFERENT for UTF8/UCS4 **********/
1022 result = g_malloc (n_bytes + 1);
1027 while (out < result + n_bytes)
1032 if (c >= 0xdc00 && c < 0xe000) /* low surrogate */
1034 wc = SURROGATE_VALUE (high_surrogate, c);
1037 else if (c >= 0xd800 && c < 0xdc00) /* high surrogate */
1045 /********** DIFFERENT for UTF8/UCS4 **********/
1046 out += g_unichar_to_utf8 (wc, out);
1052 /********** DIFFERENT for UTF8/UCS4 **********/
1056 /********** DIFFERENT for UTF8/UCS4 **********/
1057 *items_written = out - result;
1061 *items_read = in - str;
1068 * @str: a UTF-16 encoded string
1069 * @len: the maximum length of @str to use. If < 0, then
1070 * the string is terminated with a 0 character.
1071 * @items_read: location to store number of words read, or %NULL.
1072 * If %NULL, then %G_CONVERT_ERROR_PARTIAL_INPUT will be
1073 * returned in case @str contains a trailing partial
1074 * character. If an error occurs then the index of the
1075 * invalid input is stored here.
1076 * @items_written: location to store number of characters written, or %NULL.
1077 * The value stored here does not include the trailing
1079 * @error: location to store the error occuring, or %NULL to ignore
1080 * errors. Any of the errors in #GConvertError other than
1081 * %G_CONVERT_ERROR_NO_CONVERSION may occur.
1083 * Convert a string from UTF-16 to UCS-4. The result will be
1084 * terminated with a 0 character.
1086 * Return value: a pointer to a newly allocated UCS-4 string.
1087 * This value must be freed with g_free(). If an
1088 * error occurs, %NULL will be returned and
1092 g_utf16_to_ucs4 (const gunichar2 *str,
1095 glong *items_written,
1098 const gunichar2 *in;
1100 gchar *result = NULL;
1102 gunichar high_surrogate;
1104 g_return_val_if_fail (str != 0, NULL);
1109 while ((len < 0 || in - str < len) && *in)
1114 if (c >= 0xdc00 && c < 0xe000) /* low surrogate */
1118 wc = SURROGATE_VALUE (high_surrogate, c);
1123 g_set_error (error, G_CONVERT_ERROR, G_CONVERT_ERROR_ILLEGAL_SEQUENCE,
1124 _("Invalid sequence in conversion input"));
1132 g_set_error (error, G_CONVERT_ERROR, G_CONVERT_ERROR_ILLEGAL_SEQUENCE,
1133 _("Invalid sequence in conversion input"));
1137 if (c >= 0xd800 && c < 0xdc00) /* high surrogate */
1146 /********** DIFFERENT for UTF8/UCS4 **********/
1147 n_bytes += sizeof (gunichar);
1153 if (high_surrogate && !items_read)
1155 g_set_error (error, G_CONVERT_ERROR, G_CONVERT_ERROR_PARTIAL_INPUT,
1156 _("Partial character sequence at end of input"));
1160 /* At this point, everything is valid, and we just need to convert
1162 /********** DIFFERENT for UTF8/UCS4 **********/
1163 result = g_malloc (n_bytes + 4);
1168 while (out < result + n_bytes)
1173 if (c >= 0xdc00 && c < 0xe000) /* low surrogate */
1175 wc = SURROGATE_VALUE (high_surrogate, c);
1178 else if (c >= 0xd800 && c < 0xdc00) /* high surrogate */
1186 /********** DIFFERENT for UTF8/UCS4 **********/
1187 *(gunichar *)out = wc;
1188 out += sizeof (gunichar);
1194 /********** DIFFERENT for UTF8/UCS4 **********/
1195 *(gunichar *)out = 0;
1198 /********** DIFFERENT for UTF8/UCS4 **********/
1199 *items_written = (out - result) / sizeof (gunichar);
1203 *items_read = in - str;
1205 return (gunichar *)result;
1210 * @str: a UTF-8 encoded string
1211 * @len: the maximum length of @str to use. If < 0, then
1212 * the string is %NULL terminated.
1214 * @items_read: location to store number of bytes read, or %NULL.
1215 * If %NULL, then %G_CONVERT_ERROR_PARTIAL_INPUT will be
1216 * returned in case @str contains a trailing partial
1217 * character. If an error occurs then the index of the
1218 * invalid input is stored here.
1219 * @items_written: location to store number of words written, or %NULL.
1220 * The value stored here does not include the trailing
1222 * @error: location to store the error occuring, or %NULL to ignore
1223 * errors. Any of the errors in #GConvertError other than
1224 * %G_CONVERT_ERROR_NO_CONVERSION may occur.
1226 * Convert a string from UTF-8 to UTF-16. A 0 word will be
1227 * added to the result after the converted text.
1229 * Return value: a pointer to a newly allocated UTF-16 string.
1230 * This value must be freed with g_free(). If an
1231 * error occurs, %NULL will be returned and
1235 g_utf8_to_utf16 (const gchar *str,
1238 glong *items_written,
1241 gunichar2 *result = NULL;
1246 g_return_val_if_fail (str != NULL, NULL);
1250 while ((len < 0 || str + len - in > 0) && *in)
1252 gunichar wc = g_utf8_get_char_extended (in, str + len - in);
1253 if (wc & 0x80000000)
1255 if (wc == (gunichar)-2)
1260 g_set_error (error, G_CONVERT_ERROR, G_CONVERT_ERROR_PARTIAL_INPUT,
1261 _("Partial character sequence at end of input"));
1264 g_set_error (error, G_CONVERT_ERROR, G_CONVERT_ERROR_ILLEGAL_SEQUENCE,
1265 _("Invalid byte sequence in conversion input"));
1272 else if (wc < 0xe000)
1274 g_set_error (error, G_CONVERT_ERROR, G_CONVERT_ERROR_ILLEGAL_SEQUENCE,
1275 _("Invalid sequence in conversion input"));
1279 else if (wc < 0x10000)
1281 else if (wc < 0x110000)
1285 g_set_error (error, G_CONVERT_ERROR, G_CONVERT_ERROR_ILLEGAL_SEQUENCE,
1286 _("Character out of range for UTF-16"));
1291 in = g_utf8_next_char (in);
1294 result = g_new (gunichar2, n16 + 1);
1297 for (i = 0; i < n16;)
1299 gunichar wc = g_utf8_get_char (in);
1307 result[i++] = (wc - 0x10000) / 0x400 + 0xd800;
1308 result[i++] = (wc - 0x10000) % 0x400 + 0xdc00;
1311 in = g_utf8_next_char (in);
1317 *items_written = n16;
1321 *items_read = in - str;
1328 * @str: a UCS-4 encoded string
1329 * @len: the maximum length of @str to use. If < 0, then
1330 * the string is terminated with a zero character.
1331 * @items_read: location to store number of bytes read, or %NULL.
1332 * If an error occurs then the index of the invalid input
1334 * @items_written: location to store number of words written, or %NULL.
1335 * The value stored here does not include the trailing
1337 * @error: location to store the error occuring, or %NULL to ignore
1338 * errors. Any of the errors in #GConvertError other than
1339 * %G_CONVERT_ERROR_NO_CONVERSION may occur.
1341 * Convert a string from UCS-4 to UTF-16. A 0 word will be
1342 * added to the result after the converted text.
1344 * Return value: a pointer to a newly allocated UTF-16 string.
1345 * This value must be freed with g_free(). If an
1346 * error occurs, %NULL will be returned and
1350 g_ucs4_to_utf16 (const gunichar *str,
1353 glong *items_written,
1356 gunichar2 *result = NULL;
1362 while ((len < 0 || i < len) && str[i])
1364 gunichar wc = str[i];
1368 else if (wc < 0xe000)
1370 g_set_error (error, G_CONVERT_ERROR, G_CONVERT_ERROR_ILLEGAL_SEQUENCE,
1371 _("Invalid sequence in conversion input"));
1375 else if (wc < 0x10000)
1377 else if (wc < 0x110000)
1381 g_set_error (error, G_CONVERT_ERROR, G_CONVERT_ERROR_ILLEGAL_SEQUENCE,
1382 _("Character out of range for UTF-16"));
1390 result = g_new (gunichar2, n16 + 1);
1392 for (i = 0, j = 0; j < n16; i++)
1394 gunichar wc = str[i];
1402 result[j++] = (wc - 0x10000) / 0x400 + 0xd800;
1403 result[j++] = (wc - 0x10000) % 0x400 + 0xdc00;
1409 *items_written = n16;
1420 * @str: a pointer to character data
1421 * @max_len: max bytes to validate, or -1 to go until nul
1422 * @end: return location for end of valid data
1424 * Validates UTF-8 encoded text. @str is the text to validate;
1425 * if @str is nul-terminated, then @max_len can be -1, otherwise
1426 * @max_len should be the number of bytes to validate.
1427 * If @end is non-%NULL, then the end of the valid range
1428 * will be stored there (i.e. the address of the first invalid byte
1429 * if some bytes were invalid, or the end of the text being validated
1432 * Returns TRUE if all of @str was valid. Many GLib and GTK+
1433 * routines <emphasis>require</emphasis> valid UTF8 as input;
1434 * so data read from a file or the network should be checked
1435 * with g_utf8_validate() before doing anything else with it.
1437 * Return value: TRUE if the text was valid UTF-8.
1440 g_utf8_validate (const gchar *str,
1447 g_return_val_if_fail (str != NULL, FALSE);
1454 while ((max_len < 0 || (p - str) < max_len) && *p)
1456 int i, mask = 0, len;
1458 unsigned char c = (unsigned char) *p;
1460 UTF8_COMPUTE (c, mask, len);
1465 /* check that the expected number of bytes exists in str */
1467 ((max_len - (p - str)) < len))
1470 UTF8_GET (result, p, i, mask, len);
1472 if (UTF8_LENGTH (result) != len) /* Check for overlong UTF-8 */
1475 if (result == (gunichar)-1)
1478 if (!UNICODE_VALID (result))
1487 /* See that we covered the entire length if a length was
1488 * passed in, or that we ended on a nul if not
1491 p != (str + max_len))
1493 else if (max_len < 0 &&
1501 * g_unichar_validate:
1502 * @ch: a Unicode character
1504 * Checks whether @ch is a valid Unicode character. Some possible
1505 * integer values of @ch will not be valid. 0 is considered a valid
1506 * character, though it's normally a string terminator.
1508 * Return value: %TRUE if @ch is a valid Unicode character
1511 g_unichar_validate (gunichar ch)
1513 return UNICODE_VALID (ch);