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 static const gchar utf8_skip_data[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
113 const gchar * const g_utf8_skip = utf8_skip_data;
116 * g_utf8_find_prev_char:
117 * @str: pointer to the beginning of a UTF-8 string
118 * @p: pointer to some position within @str
120 * Given a position @p with a UTF-8 encoded string @str, find the start
121 * of the previous UTF-8 character starting before @p. Returns %NULL if no
122 * UTF-8 characters are present in @p before @str.
124 * @p does not have to be at the beginning of a UTF-8 chracter. No check
125 * is made to see if the character found is actually valid other than
126 * it starts with an appropriate byte.
128 * Return value: a pointer to the found character or %NULL.
131 g_utf8_find_prev_char (const char *str,
134 for (--p; p >= str; --p)
136 if ((*p & 0xc0) != 0x80)
143 * g_utf8_find_next_char:
144 * @p: a pointer to a position within a UTF-8 encoded string
145 * @end: a pointer to the end of the string, or %NULL to indicate
146 * that the string is NULL terminated, in which case
147 * the returned value will be
149 * Find the start of the next utf-8 character in the string after @p
151 * @p does not have to be at the beginning of a UTF-8 chracter. No check
152 * is made to see if the character found is actually valid other than
153 * it starts with an appropriate byte.
155 * Return value: a pointer to the found character or %NULL
158 g_utf8_find_next_char (const gchar *p,
164 for (++p; p < end && (*p & 0xc0) == 0x80; ++p)
167 for (++p; (*p & 0xc0) == 0x80; ++p)
170 return (p == end) ? NULL : (gchar *)p;
175 * @p: a pointer to a position within a UTF-8 encoded string
177 * Find the previous UTF-8 character in the string before @p.
179 * @p does not have to be at the beginning of a UTF-8 character. No check
180 * is made to see if the character found is actually valid other than
181 * it starts with an appropriate byte. If @p might be the first
182 * character of the string, you must use g_utf8_find_prev_char instead.
184 * Return value: a pointer to the found character.
187 g_utf8_prev_char (const gchar *p)
192 if ((*p & 0xc0) != 0x80)
199 * @p: pointer to the start of a UTF-8 string.
200 * @max: the maximum number of bytes to examine. If @max
201 * is less than 0, then the string is assumed to be
204 * Return value: the length of the string in characters
207 g_utf8_strlen (const gchar *p,
211 const gchar *start = p;
217 p = g_utf8_next_char (p);
226 p = g_utf8_next_char (p);
228 while (p - start < max && *p)
231 p = g_utf8_next_char (p);
234 /* only do the last len increment if we got a complete
235 * char (don't count partial chars)
237 if (p - start == max)
246 * @p: a pointer to unicode character encoded as UTF-8
248 * Convert a sequence of bytes encoded as UTF-8 to a unicode character.
249 * If @p does not point to a valid UTF-8 encoded character, results are
250 * undefined. If you are not sure that the bytes are complete
251 * valid unicode characters, you should use g_utf8_get_char_validated()
254 * Return value: the resulting character
257 g_utf8_get_char (const gchar *p)
259 int i, mask = 0, len;
261 unsigned char c = (unsigned char) *p;
263 UTF8_COMPUTE (c, mask, len);
266 UTF8_GET (result, p, i, mask, len);
272 * g_utf8_offset_to_pointer:
273 * @str: a UTF-8 encoded string
274 * @offset: a character offset within the string.
276 * Converts from an integer character offset to a pointer to a position
279 * Return value: the resulting pointer
282 g_utf8_offset_to_pointer (const gchar *str,
285 const gchar *s = str;
287 s = g_utf8_next_char (s);
293 * g_utf8_pointer_to_offset:
294 * @str: a UTF-8 encoded string
295 * @pos: a pointer to a position within @str
297 * Converts from a pointer to position within a string to a integer
300 * Return value: the resulting character offset
303 g_utf8_pointer_to_offset (const gchar *str,
306 const gchar *s = str;
311 s = g_utf8_next_char (s);
321 * @dest: buffer to fill with characters from @src
323 * @n: character count
325 * Like the standard C strncpy() function, but copies a given number
326 * of characters instead of a given number of bytes. The @src string
327 * must be valid UTF-8 encoded text. (Use g_utf8_validate() on all
328 * text before trying to use UTF-8 utility functions with it.)
330 * Return value: @dest
333 g_utf8_strncpy (gchar *dest,
337 const gchar *s = src;
340 s = g_utf8_next_char(s);
343 strncpy(dest, src, s - src);
349 g_utf8_get_charset_internal (char **a)
351 char *charset = getenv("CHARSET");
353 if (charset && a && ! *a)
356 if (charset && strstr (charset, "UTF-8"))
360 charset = nl_langinfo(CODESET);
365 if (strcmp (charset, "UTF-8") == 0)
370 #if 0 /* #ifdef _NL_CTYPE_CODESET_NAME */
371 charset = nl_langinfo (_NL_CTYPE_CODESET_NAME);
376 if (strcmp (charset, "UTF-8") == 0)
381 #ifdef G_PLATFORM_WIN32
384 static char codepage[10];
386 sprintf (codepage, "CP%d", GetACP ());
388 /* What about codepage 1200? Is that UTF-8? */
396 /* Assume this for compatibility at present. */
400 static int utf8_locale_cache = -1;
401 static char *utf8_charset_cache = NULL;
405 * @charset: return location for character set name
407 * Obtains the character set for the current locale; you might use
408 * this character set as an argument to g_convert(), to convert from
409 * the current locale's encoding to some other encoding. (Frequently
410 * g_locale_to_utf8() and g_locale_from_utf8() are nice shortcuts,
413 * The return value is %TRUE if the locale's encoding is UTF-8, in that
414 * case you can perhaps avoid calling g_convert().
416 * The string returned in @charset is not allocated, and should not be
419 * Return value: %TRUE if the returned charset is UTF-8
422 g_get_charset (G_CONST_RETURN char **charset)
424 if (utf8_locale_cache != -1)
427 *charset = utf8_charset_cache;
428 return utf8_locale_cache;
430 utf8_locale_cache = g_utf8_get_charset_internal (&utf8_charset_cache);
432 *charset = utf8_charset_cache;
433 return utf8_locale_cache;
440 * @c: a ISO10646 character code
441 * @outbuf: output buffer, must have at least 6 bytes of space.
442 * If %NULL, the length will be computed and returned
443 * and nothing will be written to @out.
445 * Convert a single character to utf8
447 * Return value: number of bytes written
450 g_unichar_to_utf8 (gunichar c,
467 else if (c < 0x10000)
472 else if (c < 0x200000)
477 else if (c < 0x4000000)
490 for (i = len - 1; i > 0; --i)
492 outbuf[i] = (c & 0x3f) | 0x80;
495 outbuf[0] = c | first;
503 * @p: a nul-terminated utf-8 string
504 * @len: the maximum length of p
505 * @c: a iso-10646 character
507 * Find the leftmost occurence of the given iso-10646 character
508 * in a UTF-8 string, while limiting the search to p_len bytes.
509 * If len is -1, allow unbounded search.
511 * Return value: NULL if the string does not contain the character, otherwise, a
512 * a pointer to the start of the leftmost of the character in the string.
515 g_utf8_strchr (const char *p,
521 gint charlen = g_unichar_to_utf8 (c, ch);
524 return g_strstr_len (p, len, ch);
530 * @p: a nul-terminated utf-8 string
531 * @len: the maximum length of p
532 * @c: a iso-10646 character/
534 * Find the rightmost occurence of the given iso-10646 character
535 * in a UTF-8 string, while limiting the search to p_len bytes.
536 * If len is -1, allow unbounded search.
538 * Return value: NULL if the string does not contain the character, otherwise, a
539 * a pointer to the start of the rightmost of the character in the string.
542 g_utf8_strrchr (const char *p,
548 gint charlen = g_unichar_to_utf8 (c, ch);
551 return g_strrstr_len (p, len, ch);
555 /* Like g_utf8_get_char, but take a maximum length
556 * and return (gunichar)-2 on incomplete trailing character
558 static inline gunichar
559 g_utf8_get_char_extended (const gchar *p,
563 gunichar wc = (guchar) *p;
603 if (max_len >= 0 && len > max_len)
605 for (i = 1; i < max_len; i++)
607 if ((((guchar *)p)[i] & 0xc0) != 0x80)
613 for (i = 1; i < len; ++i)
615 gunichar ch = ((guchar *)p)[i];
617 if ((ch & 0xc0) != 0x80)
629 if (UTF8_LENGTH(wc) != len)
636 * g_utf8_get_char_validated:
637 * @p: a pointer to unicode character encoded as UTF-8
638 * @max_len: the maximum number of bytes to read, or -1, for no maximum.
640 * Convert a sequence of bytes encoded as UTF-8 to a unicode character.
641 * This function checks for incomplete characters, for invalid characters
642 * such as characters that are out of the range of Unicode, and for
643 * overlong encodings of valid characters.
645 * Return value: the resulting character. If @p points to a partial
646 * sequence at the end of a string that could begin a valid character,
647 * returns (gunichar)-2; otherwise, if @p does not point to a valid
648 * UTF-8 encoded unicode character, returns (gunichar)-1.
651 g_utf8_get_char_validated (const gchar *p,
654 gunichar result = g_utf8_get_char_extended (p, max_len);
656 if (result & 0x80000000)
658 else if (!UNICODE_VALID (result))
665 * g_utf8_to_ucs4_fast:
666 * @str: a UTF-8 encoded string
667 * @len: the maximum length of @str to use. If < 0, then
668 * the string is %NULL terminated.
669 * @items_written: location to store the number of characters in the
672 * Convert a string from UTF-8 to a 32-bit fixed width
673 * representation as UCS-4, assuming valid UTF-8 input.
674 * This function is roughly twice as fast as g_utf8_to_ucs4()
675 * but does no error checking on the input.
677 * Return value: a pointer to a newly allocated UCS-4 string.
678 * This value must be freed with g_free()
681 g_utf8_to_ucs4_fast (const gchar *str,
683 glong *items_written)
690 g_return_val_if_fail (str != NULL, NULL);
698 p = g_utf8_next_char (p);
704 while (p < str + len && *p)
706 p = g_utf8_next_char (p);
711 result = g_new (gunichar, n_chars + 1);
714 for (i=0; i < n_chars; i++)
716 gunichar wc = ((unsigned char *)p)[0];
751 for (j = 1; j < charlen; j++)
754 wc |= ((unsigned char *)p)[j] & 0x3f;
771 * @str: a UTF-8 encoded string
772 * @len: the maximum length of @str to use. If < 0, then
773 * the string is %NULL terminated.
774 * @items_read: location to store number of bytes read, or %NULL.
775 * If %NULL, then %G_CONVERT_ERROR_PARTIAL_INPUT will be
776 * returned in case @str contains a trailing partial
777 * character. If an error occurs then the index of the
778 * invalid input is stored here.
779 * @items_written: location to store number of characters written or %NULL.
780 * The value here stored does not include the trailing 0
782 * @error: location to store the error occuring, or %NULL to ignore
783 * errors. Any of the errors in #GConvertError other than
784 * %G_CONVERT_ERROR_NO_CONVERSION may occur.
786 * Convert a string from UTF-8 to a 32-bit fixed width
787 * representation as UCS-4. A trailing 0 will be added to the
788 * string after the converted text.
790 * Return value: a pointer to a newly allocated UCS-4 string.
791 * This value must be freed with g_free(). If an
792 * error occurs, %NULL will be returned and
796 g_utf8_to_ucs4 (const gchar *str,
799 glong *items_written,
802 gunichar *result = NULL;
808 while ((len < 0 || str + len - in > 0) && *in)
810 gunichar wc = g_utf8_get_char_extended (in, str + len - in);
813 if (wc == (gunichar)-2)
818 g_set_error (error, G_CONVERT_ERROR, G_CONVERT_ERROR_PARTIAL_INPUT,
819 _("Partial character sequence at end of input"));
822 g_set_error (error, G_CONVERT_ERROR, G_CONVERT_ERROR_ILLEGAL_SEQUENCE,
823 _("Invalid byte sequence in conversion input"));
830 in = g_utf8_next_char (in);
833 result = g_new (gunichar, n_chars + 1);
836 for (i=0; i < n_chars; i++)
838 result[i] = g_utf8_get_char (in);
839 in = g_utf8_next_char (in);
844 *items_written = n_chars;
848 *items_read = in - str;
855 * @str: a UCS-4 encoded string
856 * @len: the maximum length of @str to use. If < 0, then
857 * the string is %NULL terminated.
858 * @items_read: location to store number of characters read read, or %NULL.
859 * @items_written: location to store number of bytes written or %NULL.
860 * The value here stored does not include the trailing 0
862 * @error: location to store the error occuring, or %NULL to ignore
863 * errors. Any of the errors in #GConvertError other than
864 * %G_CONVERT_ERROR_NO_CONVERSION may occur.
866 * Convert a string from a 32-bit fixed width representation as UCS-4.
867 * to UTF-8. The result will be terminated with a 0 byte.
869 * Return value: a pointer to a newly allocated UTF-8 string.
870 * This value must be freed with g_free(). If an
871 * error occurs, %NULL will be returned and
875 g_ucs4_to_utf8 (const gunichar *str,
878 glong *items_written,
882 gchar *result = NULL;
887 for (i = 0; len < 0 || i < len ; i++)
892 if (str[i] >= 0x80000000)
897 g_set_error (error, G_CONVERT_ERROR, G_CONVERT_ERROR_ILLEGAL_SEQUENCE,
898 _("Character out of range for UTF-8"));
902 result_length += UTF8_LENGTH (str[i]);
905 result = g_malloc (result_length + 1);
909 while (p < result + result_length)
910 p += g_unichar_to_utf8 (str[i++], p);
915 *items_written = p - result;
924 #define SURROGATE_VALUE(h,l) (((h) - 0xd800) * 0x400 + (l) - 0xdc00 + 0x10000)
928 * @str: a UTF-16 encoded string
929 * @len: the maximum length of @str to use. If < 0, then
930 * the string is terminated with a 0 character.
931 * @items_read: location to store number of words read, or %NULL.
932 * If %NULL, then %G_CONVERT_ERROR_PARTIAL_INPUT will be
933 * returned in case @str contains a trailing partial
934 * character. If an error occurs then the index of the
935 * invalid input is stored here.
936 * @items_written: location to store number of bytes written, or %NULL.
937 * The value stored here does not include the trailing
939 * @error: location to store the error occuring, or %NULL to ignore
940 * errors. Any of the errors in #GConvertError other than
941 * %G_CONVERT_ERROR_NO_CONVERSION may occur.
943 * Convert a string from UTF-16 to UTF-8. The result will be
944 * terminated with a 0 byte.
946 * Return value: a pointer to a newly allocated UTF-8 string.
947 * This value must be freed with g_free(). If an
948 * error occurs, %NULL will be returned and
952 g_utf16_to_utf8 (const gunichar2 *str,
955 glong *items_written,
958 /* This function and g_utf16_to_ucs4 are almost exactly identical - The lines that differ
963 gchar *result = NULL;
965 gunichar high_surrogate;
967 g_return_val_if_fail (str != 0, NULL);
972 while ((len < 0 || in - str < len) && *in)
977 if (c >= 0xdc00 && c < 0xe000) /* low surrogate */
981 wc = SURROGATE_VALUE (high_surrogate, c);
986 g_set_error (error, G_CONVERT_ERROR, G_CONVERT_ERROR_ILLEGAL_SEQUENCE,
987 _("Invalid sequence in conversion input"));
995 g_set_error (error, G_CONVERT_ERROR, G_CONVERT_ERROR_ILLEGAL_SEQUENCE,
996 _("Invalid sequence in conversion input"));
1000 if (c >= 0xd800 && c < 0xdc00) /* high surrogate */
1009 /********** DIFFERENT for UTF8/UCS4 **********/
1010 n_bytes += UTF8_LENGTH (wc);
1016 if (high_surrogate && !items_read)
1018 g_set_error (error, G_CONVERT_ERROR, G_CONVERT_ERROR_PARTIAL_INPUT,
1019 _("Partial character sequence at end of input"));
1023 /* At this point, everything is valid, and we just need to convert
1025 /********** DIFFERENT for UTF8/UCS4 **********/
1026 result = g_malloc (n_bytes + 1);
1031 while (out < result + n_bytes)
1036 if (c >= 0xdc00 && c < 0xe000) /* low surrogate */
1038 wc = SURROGATE_VALUE (high_surrogate, c);
1041 else if (c >= 0xd800 && c < 0xdc00) /* high surrogate */
1049 /********** DIFFERENT for UTF8/UCS4 **********/
1050 out += g_unichar_to_utf8 (wc, out);
1056 /********** DIFFERENT for UTF8/UCS4 **********/
1060 /********** DIFFERENT for UTF8/UCS4 **********/
1061 *items_written = out - result;
1065 *items_read = in - str;
1072 * @str: a UTF-16 encoded string
1073 * @len: the maximum length of @str to use. If < 0, then
1074 * the string is terminated with a 0 character.
1075 * @items_read: location to store number of words read, or %NULL.
1076 * If %NULL, then %G_CONVERT_ERROR_PARTIAL_INPUT will be
1077 * returned in case @str contains a trailing partial
1078 * character. If an error occurs then the index of the
1079 * invalid input is stored here.
1080 * @items_written: location to store number of characters written, or %NULL.
1081 * The value stored here does not include the trailing
1083 * @error: location to store the error occuring, or %NULL to ignore
1084 * errors. Any of the errors in #GConvertError other than
1085 * %G_CONVERT_ERROR_NO_CONVERSION may occur.
1087 * Convert a string from UTF-16 to UCS-4. The result will be
1088 * terminated with a 0 character.
1090 * Return value: a pointer to a newly allocated UCS-4 string.
1091 * This value must be freed with g_free(). If an
1092 * error occurs, %NULL will be returned and
1096 g_utf16_to_ucs4 (const gunichar2 *str,
1099 glong *items_written,
1102 const gunichar2 *in;
1104 gchar *result = NULL;
1106 gunichar high_surrogate;
1108 g_return_val_if_fail (str != 0, NULL);
1113 while ((len < 0 || in - str < len) && *in)
1118 if (c >= 0xdc00 && c < 0xe000) /* low surrogate */
1122 wc = SURROGATE_VALUE (high_surrogate, c);
1127 g_set_error (error, G_CONVERT_ERROR, G_CONVERT_ERROR_ILLEGAL_SEQUENCE,
1128 _("Invalid sequence in conversion input"));
1136 g_set_error (error, G_CONVERT_ERROR, G_CONVERT_ERROR_ILLEGAL_SEQUENCE,
1137 _("Invalid sequence in conversion input"));
1141 if (c >= 0xd800 && c < 0xdc00) /* high surrogate */
1150 /********** DIFFERENT for UTF8/UCS4 **********/
1151 n_bytes += sizeof (gunichar);
1157 if (high_surrogate && !items_read)
1159 g_set_error (error, G_CONVERT_ERROR, G_CONVERT_ERROR_PARTIAL_INPUT,
1160 _("Partial character sequence at end of input"));
1164 /* At this point, everything is valid, and we just need to convert
1166 /********** DIFFERENT for UTF8/UCS4 **********/
1167 result = g_malloc (n_bytes + 4);
1172 while (out < result + n_bytes)
1177 if (c >= 0xdc00 && c < 0xe000) /* low surrogate */
1179 wc = SURROGATE_VALUE (high_surrogate, c);
1182 else if (c >= 0xd800 && c < 0xdc00) /* high surrogate */
1190 /********** DIFFERENT for UTF8/UCS4 **********/
1191 *(gunichar *)out = wc;
1192 out += sizeof (gunichar);
1198 /********** DIFFERENT for UTF8/UCS4 **********/
1199 *(gunichar *)out = 0;
1202 /********** DIFFERENT for UTF8/UCS4 **********/
1203 *items_written = (out - result) / sizeof (gunichar);
1207 *items_read = in - str;
1209 return (gunichar *)result;
1214 * @str: a UTF-8 encoded string
1215 * @len: the maximum length of @str to use. If < 0, then
1216 * the string is %NULL terminated.
1218 * @items_read: location to store number of bytes read, or %NULL.
1219 * If %NULL, then %G_CONVERT_ERROR_PARTIAL_INPUT will be
1220 * returned in case @str contains a trailing partial
1221 * character. If an error occurs then the index of the
1222 * invalid input is stored here.
1223 * @items_written: location to store number of words written, or %NULL.
1224 * The value stored here does not include the trailing
1226 * @error: location to store the error occuring, or %NULL to ignore
1227 * errors. Any of the errors in #GConvertError other than
1228 * %G_CONVERT_ERROR_NO_CONVERSION may occur.
1230 * Convert a string from UTF-8 to UTF-16. A 0 word will be
1231 * added to the result after the converted text.
1233 * Return value: a pointer to a newly allocated UTF-16 string.
1234 * This value must be freed with g_free(). If an
1235 * error occurs, %NULL will be returned and
1239 g_utf8_to_utf16 (const gchar *str,
1242 glong *items_written,
1245 gunichar2 *result = NULL;
1250 g_return_val_if_fail (str != NULL, NULL);
1254 while ((len < 0 || str + len - in > 0) && *in)
1256 gunichar wc = g_utf8_get_char_extended (in, str + len - in);
1257 if (wc & 0x80000000)
1259 if (wc == (gunichar)-2)
1264 g_set_error (error, G_CONVERT_ERROR, G_CONVERT_ERROR_PARTIAL_INPUT,
1265 _("Partial character sequence at end of input"));
1268 g_set_error (error, G_CONVERT_ERROR, G_CONVERT_ERROR_ILLEGAL_SEQUENCE,
1269 _("Invalid byte sequence in conversion input"));
1276 else if (wc < 0xe000)
1278 g_set_error (error, G_CONVERT_ERROR, G_CONVERT_ERROR_ILLEGAL_SEQUENCE,
1279 _("Invalid sequence in conversion input"));
1283 else if (wc < 0x10000)
1285 else if (wc < 0x110000)
1289 g_set_error (error, G_CONVERT_ERROR, G_CONVERT_ERROR_ILLEGAL_SEQUENCE,
1290 _("Character out of range for UTF-16"));
1295 in = g_utf8_next_char (in);
1298 result = g_new (gunichar2, n16 + 1);
1301 for (i = 0; i < n16;)
1303 gunichar wc = g_utf8_get_char (in);
1311 result[i++] = (wc - 0x10000) / 0x400 + 0xd800;
1312 result[i++] = (wc - 0x10000) % 0x400 + 0xdc00;
1315 in = g_utf8_next_char (in);
1321 *items_written = n16;
1325 *items_read = in - str;
1332 * @str: a UCS-4 encoded string
1333 * @len: the maximum length of @str to use. If < 0, then
1334 * the string is terminated with a zero character.
1335 * @items_read: location to store number of bytes read, or %NULL.
1336 * If an error occurs then the index of the invalid input
1338 * @items_written: location to store number of words written, or %NULL.
1339 * The value stored here does not include the trailing
1341 * @error: location to store the error occuring, or %NULL to ignore
1342 * errors. Any of the errors in #GConvertError other than
1343 * %G_CONVERT_ERROR_NO_CONVERSION may occur.
1345 * Convert a string from UCS-4 to UTF-16. A 0 word will be
1346 * added to the result after the converted text.
1348 * Return value: a pointer to a newly allocated UTF-16 string.
1349 * This value must be freed with g_free(). If an
1350 * error occurs, %NULL will be returned and
1354 g_ucs4_to_utf16 (const gunichar *str,
1357 glong *items_written,
1360 gunichar2 *result = NULL;
1366 while ((len < 0 || i < len) && str[i])
1368 gunichar wc = str[i];
1372 else if (wc < 0xe000)
1374 g_set_error (error, G_CONVERT_ERROR, G_CONVERT_ERROR_ILLEGAL_SEQUENCE,
1375 _("Invalid sequence in conversion input"));
1379 else if (wc < 0x10000)
1381 else if (wc < 0x110000)
1385 g_set_error (error, G_CONVERT_ERROR, G_CONVERT_ERROR_ILLEGAL_SEQUENCE,
1386 _("Character out of range for UTF-16"));
1394 result = g_new (gunichar2, n16 + 1);
1396 for (i = 0, j = 0; j < n16; i++)
1398 gunichar wc = str[i];
1406 result[j++] = (wc - 0x10000) / 0x400 + 0xd800;
1407 result[j++] = (wc - 0x10000) % 0x400 + 0xdc00;
1413 *items_written = n16;
1424 * @str: a pointer to character data
1425 * @max_len: max bytes to validate, or -1 to go until nul
1426 * @end: return location for end of valid data
1428 * Validates UTF-8 encoded text. @str is the text to validate;
1429 * if @str is nul-terminated, then @max_len can be -1, otherwise
1430 * @max_len should be the number of bytes to validate.
1431 * If @end is non-%NULL, then the end of the valid range
1432 * will be stored there (i.e. the address of the first invalid byte
1433 * if some bytes were invalid, or the end of the text being validated
1436 * Returns TRUE if all of @str was valid. Many GLib and GTK+
1437 * routines <emphasis>require</emphasis> valid UTF8 as input;
1438 * so data read from a file or the network should be checked
1439 * with g_utf8_validate() before doing anything else with it.
1441 * Return value: TRUE if the text was valid UTF-8.
1444 g_utf8_validate (const gchar *str,
1451 g_return_val_if_fail (str != NULL, FALSE);
1458 while ((max_len < 0 || (p - str) < max_len) && *p)
1460 int i, mask = 0, len;
1462 unsigned char c = (unsigned char) *p;
1464 UTF8_COMPUTE (c, mask, len);
1469 /* check that the expected number of bytes exists in str */
1471 ((max_len - (p - str)) < len))
1474 UTF8_GET (result, p, i, mask, len);
1476 if (UTF8_LENGTH (result) != len) /* Check for overlong UTF-8 */
1479 if (result == (gunichar)-1)
1482 if (!UNICODE_VALID (result))
1491 /* See that we covered the entire length if a length was
1492 * passed in, or that we ended on a nul if not
1495 p != (str + max_len))
1497 else if (max_len < 0 &&
1505 * g_unichar_validate:
1506 * @ch: a Unicode character
1508 * Checks whether @ch is a valid Unicode character. Some possible
1509 * integer values of @ch will not be valid. 0 is considered a valid
1510 * character, though it's normally a string terminator.
1512 * Return value: %TRUE if @ch is a valid Unicode character
1515 g_unichar_validate (gunichar ch)
1517 return UNICODE_VALID (ch);