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,
335 const gchar *s = src;
338 s = g_utf8_next_char(s);
341 strncpy(dest, src, s - src);
347 g_utf8_get_charset_internal (char **a)
349 char *charset = getenv("CHARSET");
351 if (charset && a && ! *a)
354 if (charset && strstr (charset, "UTF-8"))
358 charset = nl_langinfo(CODESET);
363 if (strcmp (charset, "UTF-8") == 0)
368 #if 0 /* #ifdef _NL_CTYPE_CODESET_NAME */
369 charset = nl_langinfo (_NL_CTYPE_CODESET_NAME);
374 if (strcmp (charset, "UTF-8") == 0)
379 #ifdef G_PLATFORM_WIN32
382 static char codepage[10];
384 sprintf (codepage, "CP%d", GetACP ());
386 /* What about codepage 1200? Is that UTF-8? */
394 /* Assume this for compatibility at present. */
398 static int utf8_locale_cache = -1;
399 static char *utf8_charset_cache = NULL;
403 * @charset: return location for character set name
405 * Obtains the character set for the current locale; you might use
406 * this character set as an argument to g_convert(), to convert from
407 * the current locale's encoding to some other encoding. (Frequently
408 * g_locale_to_utf8() and g_locale_from_utf8() are nice shortcuts,
411 * The return value is %TRUE if the locale's encoding is UTF-8, in that
412 * case you can perhaps avoid calling g_convert().
414 * The string returned in @charset is not allocated, and should not be
417 * Return value: %TRUE if the returned charset is UTF-8
420 g_get_charset (G_CONST_RETURN char **charset)
422 if (utf8_locale_cache != -1)
425 *charset = utf8_charset_cache;
426 return utf8_locale_cache;
428 utf8_locale_cache = g_utf8_get_charset_internal (&utf8_charset_cache);
430 *charset = utf8_charset_cache;
431 return utf8_locale_cache;
438 * @c: a ISO10646 character code
439 * @outbuf: output buffer, must have at least 6 bytes of space.
440 * If %NULL, the length will be computed and returned
441 * and nothing will be written to @out.
443 * Convert a single character to utf8
445 * Return value: number of bytes written
448 g_unichar_to_utf8 (gunichar c,
465 else if (c < 0x10000)
470 else if (c < 0x200000)
475 else if (c < 0x4000000)
488 for (i = len - 1; i > 0; --i)
490 outbuf[i] = (c & 0x3f) | 0x80;
493 outbuf[0] = c | first;
501 * @p: a nul-terminated utf-8 string
502 * @p_len: the maximum length of p
503 * @c: a iso-10646 character
505 * Find the leftmost occurence of the given iso-10646 character
506 * in a UTF-8 string, while limiting the search to p_len bytes.
507 * If len is -1, allow unbounded search.
509 * Return value: NULL if the string does not contain the character, otherwise, a
510 * a pointer to the start of the leftmost of the character in the string.
513 g_utf8_strchr (const char *p,
519 gint len = g_unichar_to_utf8 (c, ch);
522 return g_strstr_len (p, p_len, ch);
528 * @p: a nul-terminated utf-8 string
529 * @p_len: the maximum length of p
530 * @c: a iso-10646 character/
532 * Find the rightmost occurence of the given iso-10646 character
533 * in a UTF-8 string, while limiting the search to p_len bytes.
534 * If len is -1, allow unbounded search.
536 * Return value: NULL if the string does not contain the character, otherwise, a
537 * a pointer to the start of the rightmost of the character in the string.
540 g_utf8_strrchr (const char *p,
546 gint len = g_unichar_to_utf8 (c, ch);
549 return g_strrstr_len (p, p_len, ch);
553 /* Like g_utf8_get_char, but take a maximum length
554 * and return (gunichar)-2 on incomplete trailing character
556 static inline gunichar
557 g_utf8_get_char_extended (const gchar *p,
561 gunichar wc = (guchar) *p;
601 if (max_len >= 0 && len > max_len)
603 for (i = 1; i < max_len; i++)
605 if ((((guchar *)p)[i] & 0xc0) != 0x80)
611 for (i = 1; i < len; ++i)
613 gunichar ch = ((guchar *)p)[i];
615 if ((ch & 0xc0) != 0x80)
627 if (UTF8_LENGTH(wc) != len)
634 * g_utf8_get_char_validated:
635 * @p: a pointer to unicode character encoded as UTF-8
636 * @max_len: the maximum number of bytes to read, or -1, for no maximum.
638 * Convert a sequence of bytes encoded as UTF-8 to a unicode character.
639 * This function checks for incomplete characters, for invalid characters
640 * such as characters that are out of the range of Unicode, and for
641 * overlong encodings of valid characters.
643 * Return value: the resulting character. If @p points to a partial
644 * sequence at the end of a string that could begin a valid character,
645 * returns (gunichar)-2; otherwise, if @p does not point to a valid
646 * UTF-8 encoded unicode character, returns (gunichar)-1.
649 g_utf8_get_char_validated (const gchar *p,
652 gunichar result = g_utf8_get_char_extended (p, max_len);
654 if (result & 0x80000000)
656 else if (!UNICODE_VALID (result))
663 * g_utf8_to_ucs4_fast:
664 * @str: a UTF-8 encoded string
665 * @len: the maximum length of @str to use. If < 0, then
666 * the string is %NULL terminated.
667 * @items_written: location to store the number of characters in the
670 * Convert a string from UTF-8 to a 32-bit fixed width
671 * representation as UCS-4, assuming valid UTF-8 input.
672 * This function is roughly twice as fast as g_utf8_to_ucs4()
673 * but does no error checking on the input.
675 * Return value: a pointer to a newly allocated UCS-4 string.
676 * This value must be freed with g_free()
679 g_utf8_to_ucs4_fast (const gchar *str,
681 glong *items_written)
688 g_return_val_if_fail (str != NULL, NULL);
696 p = g_utf8_next_char (p);
702 while (p < str + len && *p)
704 p = g_utf8_next_char (p);
709 result = g_new (gunichar, n_chars + 1);
712 for (i=0; i < n_chars; i++)
714 gunichar wc = ((unsigned char *)p)[0];
749 for (j = 1; j < charlen; j++)
752 wc |= ((unsigned char *)p)[j] & 0x3f;
769 * @str: a UTF-8 encoded string
770 * @len: the maximum length of @str to use. If < 0, then
771 * the string is %NULL terminated.
772 * @items_read: location to store number of bytes read, or %NULL.
773 * If %NULL, then %G_CONVERT_ERROR_PARTIAL_INPUT will be
774 * returned in case @str contains a trailing partial
775 * character. If an error occurs then the index of the
776 * invalid input is stored here.
777 * @items_written: location to store number of characters written or %NULL.
778 * The value here stored does not include the trailing 0
780 * @error: location to store the error occuring, or %NULL to ignore
781 * errors. Any of the errors in #GConvertError other than
782 * %G_CONVERT_ERROR_NO_CONVERSION may occur.
784 * Convert a string from UTF-8 to a 32-bit fixed width
785 * representation as UCS-4. A trailing 0 will be added to the
786 * string after the converted text.
788 * Return value: a pointer to a newly allocated UCS-4 string.
789 * This value must be freed with g_free(). If an
790 * error occurs, %NULL will be returned and
794 g_utf8_to_ucs4 (const gchar *str,
797 glong *items_written,
800 gunichar *result = NULL;
806 while ((len < 0 || str + len - in > 0) && *in)
808 gunichar wc = g_utf8_get_char_extended (in, str + len - in);
811 if (wc == (gunichar)-2)
816 g_set_error (error, G_CONVERT_ERROR, G_CONVERT_ERROR_PARTIAL_INPUT,
817 _("Partial character sequence at end of input"));
820 g_set_error (error, G_CONVERT_ERROR, G_CONVERT_ERROR_ILLEGAL_SEQUENCE,
821 _("Invalid byte sequence in conversion input"));
828 in = g_utf8_next_char (in);
831 result = g_new (gunichar, n_chars + 1);
834 for (i=0; i < n_chars; i++)
836 result[i] = g_utf8_get_char (in);
837 in = g_utf8_next_char (in);
842 *items_written = n_chars;
846 *items_read = in - str;
853 * @str: a UCS-4 encoded string
854 * @len: the maximum length of @str to use. If < 0, then
855 * the string is %NULL terminated.
856 * @items_read: location to store number of characters read read, or %NULL.
857 * @items_written: location to store number of bytes written or %NULL.
858 * The value here stored does not include the trailing 0
860 * @error: location to store the error occuring, or %NULL to ignore
861 * errors. Any of the errors in #GConvertError other than
862 * %G_CONVERT_ERROR_NO_CONVERSION may occur.
864 * Convert a string from a 32-bit fixed width representation as UCS-4.
865 * to UTF-8. The result will be terminated with a 0 byte.
867 * Return value: a pointer to a newly allocated UTF-8 string.
868 * This value must be freed with g_free(). If an
869 * error occurs, %NULL will be returned and
873 g_ucs4_to_utf8 (const gunichar *str,
876 glong *items_written,
880 gchar *result = NULL;
885 for (i = 0; len < 0 || i < len ; i++)
890 if (str[i] >= 0x80000000)
895 g_set_error (error, G_CONVERT_ERROR, G_CONVERT_ERROR_ILLEGAL_SEQUENCE,
896 _("Character out of range for UTF-8"));
900 result_length += UTF8_LENGTH (str[i]);
903 result = g_malloc (result_length + 1);
907 while (p < result + result_length)
908 p += g_unichar_to_utf8 (str[i++], p);
913 *items_written = p - result;
922 #define SURROGATE_VALUE(h,l) (((h) - 0xd800) * 0x400 + (l) - 0xdc00 + 0x10000)
926 * @str: a UTF-16 encoded string
927 * @len: the maximum length of @str to use. If < 0, then
928 * the string is terminated with a 0 character.
929 * @items_read: location to store number of words read, or %NULL.
930 * If %NULL, then %G_CONVERT_ERROR_PARTIAL_INPUT will be
931 * returned in case @str contains a trailing partial
932 * character. If an error occurs then the index of the
933 * invalid input is stored here.
934 * @items_written: location to store number of bytes written, or %NULL.
935 * The value stored here does not include the trailing
937 * @error: location to store the error occuring, or %NULL to ignore
938 * errors. Any of the errors in #GConvertError other than
939 * %G_CONVERT_ERROR_NO_CONVERSION may occur.
941 * Convert a string from UTF-16 to UTF-8. The result will be
942 * terminated with a 0 byte.
944 * Return value: a pointer to a newly allocated UTF-8 string.
945 * This value must be freed with g_free(). If an
946 * error occurs, %NULL will be returned and
950 g_utf16_to_utf8 (const gunichar2 *str,
953 glong *items_written,
956 /* This function and g_utf16_to_ucs4 are almost exactly identical - The lines that differ
961 gchar *result = NULL;
963 gunichar high_surrogate;
965 g_return_val_if_fail (str != 0, NULL);
970 while ((len < 0 || in - str < len) && *in)
975 if (c >= 0xdc00 && c < 0xe000) /* low surrogate */
979 wc = SURROGATE_VALUE (high_surrogate, c);
984 g_set_error (error, G_CONVERT_ERROR, G_CONVERT_ERROR_ILLEGAL_SEQUENCE,
985 _("Invalid sequence in conversion input"));
993 g_set_error (error, G_CONVERT_ERROR, G_CONVERT_ERROR_ILLEGAL_SEQUENCE,
994 _("Invalid sequence in conversion input"));
998 if (c >= 0xd800 && c < 0xdc00) /* high surrogate */
1007 /********** DIFFERENT for UTF8/UCS4 **********/
1008 n_bytes += UTF8_LENGTH (wc);
1014 if (high_surrogate && !items_read)
1016 g_set_error (error, G_CONVERT_ERROR, G_CONVERT_ERROR_PARTIAL_INPUT,
1017 _("Partial character sequence at end of input"));
1021 /* At this point, everything is valid, and we just need to convert
1023 /********** DIFFERENT for UTF8/UCS4 **********/
1024 result = g_malloc (n_bytes + 1);
1029 while (out < result + n_bytes)
1034 if (c >= 0xdc00 && c < 0xe000) /* low surrogate */
1036 wc = SURROGATE_VALUE (high_surrogate, c);
1039 else if (c >= 0xd800 && c < 0xdc00) /* high surrogate */
1047 /********** DIFFERENT for UTF8/UCS4 **********/
1048 out += g_unichar_to_utf8 (wc, out);
1054 /********** DIFFERENT for UTF8/UCS4 **********/
1058 /********** DIFFERENT for UTF8/UCS4 **********/
1059 *items_written = out - result;
1063 *items_read = in - str;
1070 * @str: a UTF-16 encoded string
1071 * @len: the maximum length of @str to use. If < 0, then
1072 * the string is terminated with a 0 character.
1073 * @items_read: location to store number of words read, or %NULL.
1074 * If %NULL, then %G_CONVERT_ERROR_PARTIAL_INPUT will be
1075 * returned in case @str contains a trailing partial
1076 * character. If an error occurs then the index of the
1077 * invalid input is stored here.
1078 * @items_written: location to store number of characters written, or %NULL.
1079 * The value stored here does not include the trailing
1081 * @error: location to store the error occuring, or %NULL to ignore
1082 * errors. Any of the errors in #GConvertError other than
1083 * %G_CONVERT_ERROR_NO_CONVERSION may occur.
1085 * Convert a string from UTF-16 to UCS-4. The result will be
1086 * terminated with a 0 character.
1088 * Return value: a pointer to a newly allocated UCS-4 string.
1089 * This value must be freed with g_free(). If an
1090 * error occurs, %NULL will be returned and
1094 g_utf16_to_ucs4 (const gunichar2 *str,
1097 glong *items_written,
1100 const gunichar2 *in;
1102 gchar *result = NULL;
1104 gunichar high_surrogate;
1106 g_return_val_if_fail (str != 0, NULL);
1111 while ((len < 0 || in - str < len) && *in)
1116 if (c >= 0xdc00 && c < 0xe000) /* low surrogate */
1120 wc = SURROGATE_VALUE (high_surrogate, c);
1125 g_set_error (error, G_CONVERT_ERROR, G_CONVERT_ERROR_ILLEGAL_SEQUENCE,
1126 _("Invalid sequence in conversion input"));
1134 g_set_error (error, G_CONVERT_ERROR, G_CONVERT_ERROR_ILLEGAL_SEQUENCE,
1135 _("Invalid sequence in conversion input"));
1139 if (c >= 0xd800 && c < 0xdc00) /* high surrogate */
1148 /********** DIFFERENT for UTF8/UCS4 **********/
1149 n_bytes += sizeof (gunichar);
1155 if (high_surrogate && !items_read)
1157 g_set_error (error, G_CONVERT_ERROR, G_CONVERT_ERROR_PARTIAL_INPUT,
1158 _("Partial character sequence at end of input"));
1162 /* At this point, everything is valid, and we just need to convert
1164 /********** DIFFERENT for UTF8/UCS4 **********/
1165 result = g_malloc (n_bytes + 4);
1170 while (out < result + n_bytes)
1175 if (c >= 0xdc00 && c < 0xe000) /* low surrogate */
1177 wc = SURROGATE_VALUE (high_surrogate, c);
1180 else if (c >= 0xd800 && c < 0xdc00) /* high surrogate */
1188 /********** DIFFERENT for UTF8/UCS4 **********/
1189 *(gunichar *)out = wc;
1190 out += sizeof (gunichar);
1196 /********** DIFFERENT for UTF8/UCS4 **********/
1197 *(gunichar *)out = 0;
1200 /********** DIFFERENT for UTF8/UCS4 **********/
1201 *items_written = (out - result) / sizeof (gunichar);
1205 *items_read = in - str;
1207 return (gunichar *)result;
1212 * @str: a UTF-8 encoded string
1213 * @len: the maximum length of @str to use. If < 0, then
1214 * the string is %NULL terminated.
1216 * @items_read: location to store number of bytes read, or %NULL.
1217 * If %NULL, then %G_CONVERT_ERROR_PARTIAL_INPUT will be
1218 * returned in case @str contains a trailing partial
1219 * character. If an error occurs then the index of the
1220 * invalid input is stored here.
1221 * @items_written: location to store number of words written, or %NULL.
1222 * The value stored here does not include the trailing
1224 * @error: location to store the error occuring, or %NULL to ignore
1225 * errors. Any of the errors in #GConvertError other than
1226 * %G_CONVERT_ERROR_NO_CONVERSION may occur.
1228 * Convert a string from UTF-8 to UTF-16. A 0 word will be
1229 * added to the result after the converted text.
1231 * Return value: a pointer to a newly allocated UTF-16 string.
1232 * This value must be freed with g_free(). If an
1233 * error occurs, %NULL will be returned and
1237 g_utf8_to_utf16 (const gchar *str,
1240 glong *items_written,
1243 gunichar2 *result = NULL;
1248 g_return_val_if_fail (str != NULL, NULL);
1252 while ((len < 0 || str + len - in > 0) && *in)
1254 gunichar wc = g_utf8_get_char_extended (in, str + len - in);
1255 if (wc & 0x80000000)
1257 if (wc == (gunichar)-2)
1262 g_set_error (error, G_CONVERT_ERROR, G_CONVERT_ERROR_PARTIAL_INPUT,
1263 _("Partial character sequence at end of input"));
1266 g_set_error (error, G_CONVERT_ERROR, G_CONVERT_ERROR_ILLEGAL_SEQUENCE,
1267 _("Invalid byte sequence in conversion input"));
1274 else if (wc < 0xe000)
1276 g_set_error (error, G_CONVERT_ERROR, G_CONVERT_ERROR_ILLEGAL_SEQUENCE,
1277 _("Invalid sequence in conversion input"));
1281 else if (wc < 0x10000)
1283 else if (wc < 0x110000)
1287 g_set_error (error, G_CONVERT_ERROR, G_CONVERT_ERROR_ILLEGAL_SEQUENCE,
1288 _("Character out of range for UTF-16"));
1293 in = g_utf8_next_char (in);
1296 result = g_new (gunichar2, n16 + 1);
1299 for (i = 0; i < n16;)
1301 gunichar wc = g_utf8_get_char (in);
1309 result[i++] = (wc - 0x10000) / 0x400 + 0xd800;
1310 result[i++] = (wc - 0x10000) % 0x400 + 0xdc00;
1313 in = g_utf8_next_char (in);
1319 *items_written = n16;
1323 *items_read = in - str;
1330 * @str: a UCS-4 encoded string
1331 * @len: the maximum length of @str to use. If < 0, then
1332 * the string is terminated with a zero character.
1333 * @items_read: location to store number of bytes read, or %NULL.
1334 * If an error occurs then the index of the invalid input
1336 * @items_written: location to store number of words written, or %NULL.
1337 * The value stored here does not include the trailing
1339 * @error: location to store the error occuring, or %NULL to ignore
1340 * errors. Any of the errors in #GConvertError other than
1341 * %G_CONVERT_ERROR_NO_CONVERSION may occur.
1343 * Convert a string from UCS-4 to UTF-16. A 0 word will be
1344 * added to the result after the converted text.
1346 * Return value: a pointer to a newly allocated UTF-16 string.
1347 * This value must be freed with g_free(). If an
1348 * error occurs, %NULL will be returned and
1352 g_ucs4_to_utf16 (const gunichar *str,
1355 glong *items_written,
1358 gunichar2 *result = NULL;
1364 while ((len < 0 || i < len) && str[i])
1366 gunichar wc = str[i];
1370 else if (wc < 0xe000)
1372 g_set_error (error, G_CONVERT_ERROR, G_CONVERT_ERROR_ILLEGAL_SEQUENCE,
1373 _("Invalid sequence in conversion input"));
1377 else if (wc < 0x10000)
1379 else if (wc < 0x110000)
1383 g_set_error (error, G_CONVERT_ERROR, G_CONVERT_ERROR_ILLEGAL_SEQUENCE,
1384 _("Character out of range for UTF-16"));
1392 result = g_new (gunichar2, n16 + 1);
1394 for (i = 0, j = 0; j < n16; i++)
1396 gunichar wc = str[i];
1404 result[j++] = (wc - 0x10000) / 0x400 + 0xd800;
1405 result[j++] = (wc - 0x10000) % 0x400 + 0xdc00;
1411 *items_written = n16;
1422 * @str: a pointer to character data
1423 * @max_len: max bytes to validate, or -1 to go until nul
1424 * @end: return location for end of valid data
1426 * Validates UTF-8 encoded text. @str is the text to validate;
1427 * if @str is nul-terminated, then @max_len can be -1, otherwise
1428 * @max_len should be the number of bytes to validate.
1429 * If @end is non-%NULL, then the end of the valid range
1430 * will be stored there (i.e. the address of the first invalid byte
1431 * if some bytes were invalid, or the end of the text being validated
1434 * Returns TRUE if all of @str was valid. Many GLib and GTK+
1435 * routines <emphasis>require</emphasis> valid UTF8 as input;
1436 * so data read from a file or the network should be checked
1437 * with g_utf8_validate() before doing anything else with it.
1439 * Return value: TRUE if the text was valid UTF-8.
1442 g_utf8_validate (const gchar *str,
1449 g_return_val_if_fail (str != NULL, FALSE);
1456 while ((max_len < 0 || (p - str) < max_len) && *p)
1458 int i, mask = 0, len;
1460 unsigned char c = (unsigned char) *p;
1462 UTF8_COMPUTE (c, mask, len);
1467 /* check that the expected number of bytes exists in str */
1469 ((max_len - (p - str)) < len))
1472 UTF8_GET (result, p, i, mask, len);
1474 if (UTF8_LENGTH (result) != len) /* Check for overlong UTF-8 */
1477 if (result == (gunichar)-1)
1480 if (!UNICODE_VALID (result))
1489 /* See that we covered the entire length if a length was
1490 * passed in, or that we ended on a nul if not
1493 p != (str + max_len))
1495 else if (max_len < 0 &&
1503 * g_unichar_validate:
1504 * @ch: a Unicode character
1506 * Checks whether @ch is a valid Unicode character. Some possible
1507 * integer values of @ch will not be valid. 0 is considered a valid
1508 * character, though it's normally a string terminator.
1510 * Return value: %TRUE if @ch is a valid Unicode character
1513 g_unichar_validate (gunichar ch)
1515 return UNICODE_VALID (ch);