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 encoded 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 character. 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 nul-terminated, in which case
147 * the returned value will be
149 * Finds 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 character. 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 * Finds 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 encoded 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 * Returns the length of the string in characters.
206 * Return value: the length of the string in characters
209 g_utf8_strlen (const gchar *p,
213 const gchar *start = p;
219 p = g_utf8_next_char (p);
228 p = g_utf8_next_char (p);
230 while (p - start < max && *p)
233 p = g_utf8_next_char (p);
236 /* only do the last len increment if we got a complete
237 * char (don't count partial chars)
239 if (p - start == max)
248 * @p: a pointer to Unicode character encoded as UTF-8
250 * Converts a sequence of bytes encoded as UTF-8 to a Unicode character.
251 * If @p does not point to a valid UTF-8 encoded character, results are
252 * undefined. If you are not sure that the bytes are complete
253 * valid Unicode characters, you should use g_utf8_get_char_validated()
256 * Return value: the resulting character
259 g_utf8_get_char (const gchar *p)
261 int i, mask = 0, len;
263 unsigned char c = (unsigned char) *p;
265 UTF8_COMPUTE (c, mask, len);
268 UTF8_GET (result, p, i, mask, len);
274 * g_utf8_offset_to_pointer:
275 * @str: a UTF-8 encoded string
276 * @offset: a character offset within @str
278 * Converts from an integer character offset to a pointer to a position
281 * Return value: the resulting pointer
284 g_utf8_offset_to_pointer (const gchar *str,
287 const gchar *s = str;
289 s = g_utf8_next_char (s);
295 * g_utf8_pointer_to_offset:
296 * @str: a UTF-8 encoded string
297 * @pos: a pointer to a position within @str
299 * Converts from a pointer to position within a string to a integer
302 * Return value: the resulting character offset
305 g_utf8_pointer_to_offset (const gchar *str,
308 const gchar *s = str;
313 s = g_utf8_next_char (s);
323 * @dest: buffer to fill with characters from @src
324 * @src: UTF-8 encoded string
325 * @n: character count
327 * Like the standard C <function>strncpy()</function> function, but
328 * copies a given number of characters instead of a given number of
329 * bytes. The @src string must be valid UTF-8 encoded text.
330 * (Use g_utf8_validate() on all text before trying to use UTF-8
331 * utility functions with it.)
333 * Return value: @dest
336 g_utf8_strncpy (gchar *dest,
340 const gchar *s = src;
343 s = g_utf8_next_char(s);
346 strncpy(dest, src, s - src);
352 g_utf8_get_charset_internal (char **a)
354 char *charset = getenv("CHARSET");
356 if (charset && a && ! *a)
359 if (charset && strstr (charset, "UTF-8"))
363 charset = nl_langinfo(CODESET);
368 if (strcmp (charset, "UTF-8") == 0)
373 #if 0 /* #ifdef _NL_CTYPE_CODESET_NAME */
374 charset = nl_langinfo (_NL_CTYPE_CODESET_NAME);
379 if (strcmp (charset, "UTF-8") == 0)
384 #ifdef G_PLATFORM_WIN32
387 static char codepage[10];
389 sprintf (codepage, "CP%d", GetACP ());
391 /* What about codepage 1200? Is that UTF-8? */
399 /* Assume this for compatibility at present. */
403 static int utf8_locale_cache = -1;
404 static char *utf8_charset_cache = NULL;
408 * @charset: return location for character set name
410 * Obtains the character set for the current locale; you might use
411 * this character set as an argument to g_convert(), to convert from
412 * the current locale's encoding to some other encoding. (Frequently
413 * g_locale_to_utf8() and g_locale_from_utf8() are nice shortcuts,
416 * The return value is %TRUE if the locale's encoding is UTF-8, in that
417 * case you can perhaps avoid calling g_convert().
419 * The string returned in @charset is not allocated, and should not be
422 * Return value: %TRUE if the returned charset is UTF-8
425 g_get_charset (G_CONST_RETURN char **charset)
427 if (utf8_locale_cache != -1)
430 *charset = utf8_charset_cache;
431 return utf8_locale_cache;
433 utf8_locale_cache = g_utf8_get_charset_internal (&utf8_charset_cache);
435 *charset = utf8_charset_cache;
436 return utf8_locale_cache;
443 * @c: a ISO10646 character code
444 * @outbuf: output buffer, must have at least 6 bytes of space.
445 * If %NULL, the length will be computed and returned
446 * and nothing will be written to @outbuf.
448 * Converts a single character to UTF-8.
450 * Return value: number of bytes written
453 g_unichar_to_utf8 (gunichar c,
470 else if (c < 0x10000)
475 else if (c < 0x200000)
480 else if (c < 0x4000000)
493 for (i = len - 1; i > 0; --i)
495 outbuf[i] = (c & 0x3f) | 0x80;
498 outbuf[0] = c | first;
506 * @p: a nul-terminated UTF-8 encoded string
507 * @len: the maximum length of @p
508 * @c: a ISO10646 character
510 * Finds the leftmost occurrence of the given ISO10646 character
511 * in a UTF-8 encoded string, while limiting the search to @len bytes.
512 * If @len is -1, allow unbounded search.
514 * Return value: %NULL if the string does not contain the character,
515 * otherwise, a pointer to the start of the leftmost occurrence of
516 * the character in the string.
519 g_utf8_strchr (const char *p,
525 gint charlen = g_unichar_to_utf8 (c, ch);
528 return g_strstr_len (p, len, ch);
534 * @p: a nul-terminated UTF-8 encoded string
535 * @len: the maximum length of @p
536 * @c: a ISO10646 character
538 * Find the rightmost occurrence of the given ISO10646 character
539 * in a UTF-8 encoded string, while limiting the search to @len bytes.
540 * If @len is -1, allow unbounded search.
542 * Return value: %NULL if the string does not contain the character,
543 * otherwise, a pointer to the start of the rightmost occurrence of the
544 * character in the string.
547 g_utf8_strrchr (const char *p,
553 gint charlen = g_unichar_to_utf8 (c, ch);
556 return g_strrstr_len (p, len, ch);
560 /* Like g_utf8_get_char, but take a maximum length
561 * and return (gunichar)-2 on incomplete trailing character
563 static inline gunichar
564 g_utf8_get_char_extended (const gchar *p,
568 gunichar wc = (guchar) *p;
608 if (max_len >= 0 && len > max_len)
610 for (i = 1; i < max_len; i++)
612 if ((((guchar *)p)[i] & 0xc0) != 0x80)
618 for (i = 1; i < len; ++i)
620 gunichar ch = ((guchar *)p)[i];
622 if ((ch & 0xc0) != 0x80)
634 if (UTF8_LENGTH(wc) != len)
641 * g_utf8_get_char_validated:
642 * @p: a pointer to Unicode character encoded as UTF-8
643 * @max_len: the maximum number of bytes to read, or -1, for no maximum.
645 * Convert a sequence of bytes encoded as UTF-8 to a Unicode character.
646 * This function checks for incomplete characters, for invalid characters
647 * such as characters that are out of the range of Unicode, and for
648 * overlong encodings of valid characters.
650 * Return value: the resulting character. If @p points to a partial
651 * sequence at the end of a string that could begin a valid character,
652 * returns (gunichar)-2; otherwise, if @p does not point to a valid
653 * UTF-8 encoded Unicode character, returns (gunichar)-1.
656 g_utf8_get_char_validated (const gchar *p,
659 gunichar result = g_utf8_get_char_extended (p, max_len);
661 if (result & 0x80000000)
663 else if (!UNICODE_VALID (result))
670 * g_utf8_to_ucs4_fast:
671 * @str: a UTF-8 encoded string
672 * @len: the maximum length of @str to use. If @len < 0, then
673 * the string is nul-terminated.
674 * @items_written: location to store the number of characters in the
677 * Convert a string from UTF-8 to a 32-bit fixed width
678 * representation as UCS-4, assuming valid UTF-8 input.
679 * This function is roughly twice as fast as g_utf8_to_ucs4()
680 * but does no error checking on the input.
682 * Return value: a pointer to a newly allocated UCS-4 string.
683 * This value must be freed with g_free().
686 g_utf8_to_ucs4_fast (const gchar *str,
688 glong *items_written)
695 g_return_val_if_fail (str != NULL, NULL);
703 p = g_utf8_next_char (p);
709 while (p < str + len && *p)
711 p = g_utf8_next_char (p);
716 result = g_new (gunichar, n_chars + 1);
719 for (i=0; i < n_chars; i++)
721 gunichar wc = ((unsigned char *)p)[0];
756 for (j = 1; j < charlen; j++)
759 wc |= ((unsigned char *)p)[j] & 0x3f;
776 * @str: a UTF-8 encoded string
777 * @len: the maximum length of @str to use. If @len < 0, then
778 * the string is nul-terminated.
779 * @items_read: location to store number of bytes read, or %NULL.
780 * If %NULL, then %G_CONVERT_ERROR_PARTIAL_INPUT will be
781 * returned in case @str contains a trailing partial
782 * character. If an error occurs then the index of the
783 * invalid input is stored here.
784 * @items_written: location to store number of characters written or %NULL.
785 * The value here stored does not include the trailing 0
787 * @error: location to store the error occuring, or %NULL to ignore
788 * errors. Any of the errors in #GConvertError other than
789 * %G_CONVERT_ERROR_NO_CONVERSION may occur.
791 * Convert a string from UTF-8 to a 32-bit fixed width
792 * representation as UCS-4. A trailing 0 will be added to the
793 * string after the converted text.
795 * Return value: a pointer to a newly allocated UCS-4 string.
796 * This value must be freed with g_free(). If an
797 * error occurs, %NULL will be returned and
801 g_utf8_to_ucs4 (const gchar *str,
804 glong *items_written,
807 gunichar *result = NULL;
813 while ((len < 0 || str + len - in > 0) && *in)
815 gunichar wc = g_utf8_get_char_extended (in, str + len - in);
818 if (wc == (gunichar)-2)
823 g_set_error (error, G_CONVERT_ERROR, G_CONVERT_ERROR_PARTIAL_INPUT,
824 _("Partial character sequence at end of input"));
827 g_set_error (error, G_CONVERT_ERROR, G_CONVERT_ERROR_ILLEGAL_SEQUENCE,
828 _("Invalid byte sequence in conversion input"));
835 in = g_utf8_next_char (in);
838 result = g_new (gunichar, n_chars + 1);
841 for (i=0; i < n_chars; i++)
843 result[i] = g_utf8_get_char (in);
844 in = g_utf8_next_char (in);
849 *items_written = n_chars;
853 *items_read = in - str;
860 * @str: a UCS-4 encoded string
861 * @len: the maximum length of @str to use. If @len < 0, then
862 * the string is terminated with a 0 character.
863 * @items_read: location to store number of characters read read, or %NULL.
864 * @items_written: location to store number of bytes written or %NULL.
865 * The value here stored does not include the trailing 0
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 a 32-bit fixed width representation as UCS-4.
872 * to UTF-8. The result will be 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_ucs4_to_utf8 (const gunichar *str,
883 glong *items_written,
887 gchar *result = NULL;
892 for (i = 0; len < 0 || i < len ; i++)
897 if (str[i] >= 0x80000000)
902 g_set_error (error, G_CONVERT_ERROR, G_CONVERT_ERROR_ILLEGAL_SEQUENCE,
903 _("Character out of range for UTF-8"));
907 result_length += UTF8_LENGTH (str[i]);
910 result = g_malloc (result_length + 1);
914 while (p < result + result_length)
915 p += g_unichar_to_utf8 (str[i++], p);
920 *items_written = p - result;
929 #define SURROGATE_VALUE(h,l) (((h) - 0xd800) * 0x400 + (l) - 0xdc00 + 0x10000)
933 * @str: a UTF-16 encoded string
934 * @len: the maximum length of @str to use. If @len < 0, then
935 * the string is terminated with a 0 character.
936 * @items_read: location to store number of words read, or %NULL.
937 * If %NULL, then %G_CONVERT_ERROR_PARTIAL_INPUT will be
938 * returned in case @str contains a trailing partial
939 * character. If an error occurs then the index of the
940 * invalid input is stored here.
941 * @items_written: location to store number of bytes written, or %NULL.
942 * The value stored here does not include the trailing
944 * @error: location to store the error occuring, or %NULL to ignore
945 * errors. Any of the errors in #GConvertError other than
946 * %G_CONVERT_ERROR_NO_CONVERSION may occur.
948 * Convert a string from UTF-16 to UTF-8. The result will be
949 * terminated with a 0 byte.
951 * Return value: a pointer to a newly allocated UTF-8 string.
952 * This value must be freed with g_free(). If an
953 * error occurs, %NULL will be returned and
957 g_utf16_to_utf8 (const gunichar2 *str,
960 glong *items_written,
963 /* This function and g_utf16_to_ucs4 are almost exactly identical - The lines that differ
968 gchar *result = NULL;
970 gunichar high_surrogate;
972 g_return_val_if_fail (str != 0, NULL);
977 while ((len < 0 || in - str < len) && *in)
982 if (c >= 0xdc00 && c < 0xe000) /* low surrogate */
986 wc = SURROGATE_VALUE (high_surrogate, c);
991 g_set_error (error, G_CONVERT_ERROR, G_CONVERT_ERROR_ILLEGAL_SEQUENCE,
992 _("Invalid sequence in conversion input"));
1000 g_set_error (error, G_CONVERT_ERROR, G_CONVERT_ERROR_ILLEGAL_SEQUENCE,
1001 _("Invalid sequence in conversion input"));
1005 if (c >= 0xd800 && c < 0xdc00) /* high surrogate */
1014 /********** DIFFERENT for UTF8/UCS4 **********/
1015 n_bytes += UTF8_LENGTH (wc);
1021 if (high_surrogate && !items_read)
1023 g_set_error (error, G_CONVERT_ERROR, G_CONVERT_ERROR_PARTIAL_INPUT,
1024 _("Partial character sequence at end of input"));
1028 /* At this point, everything is valid, and we just need to convert
1030 /********** DIFFERENT for UTF8/UCS4 **********/
1031 result = g_malloc (n_bytes + 1);
1036 while (out < result + n_bytes)
1041 if (c >= 0xdc00 && c < 0xe000) /* low surrogate */
1043 wc = SURROGATE_VALUE (high_surrogate, c);
1046 else if (c >= 0xd800 && c < 0xdc00) /* high surrogate */
1054 /********** DIFFERENT for UTF8/UCS4 **********/
1055 out += g_unichar_to_utf8 (wc, out);
1061 /********** DIFFERENT for UTF8/UCS4 **********/
1065 /********** DIFFERENT for UTF8/UCS4 **********/
1066 *items_written = out - result;
1070 *items_read = in - str;
1077 * @str: a UTF-16 encoded string
1078 * @len: the maximum length of @str to use. If @len < 0, then
1079 * the string is terminated with a 0 character.
1080 * @items_read: location to store number of words read, or %NULL.
1081 * If %NULL, then %G_CONVERT_ERROR_PARTIAL_INPUT will be
1082 * returned in case @str contains a trailing partial
1083 * character. If an error occurs then the index of the
1084 * invalid input is stored here.
1085 * @items_written: location to store number of characters written, or %NULL.
1086 * The value stored here does not include the trailing
1088 * @error: location to store the error occuring, or %NULL to ignore
1089 * errors. Any of the errors in #GConvertError other than
1090 * %G_CONVERT_ERROR_NO_CONVERSION may occur.
1092 * Convert a string from UTF-16 to UCS-4. The result will be
1093 * terminated with a 0 character.
1095 * Return value: a pointer to a newly allocated UCS-4 string.
1096 * This value must be freed with g_free(). If an
1097 * error occurs, %NULL will be returned and
1101 g_utf16_to_ucs4 (const gunichar2 *str,
1104 glong *items_written,
1107 const gunichar2 *in;
1109 gchar *result = NULL;
1111 gunichar high_surrogate;
1113 g_return_val_if_fail (str != 0, NULL);
1118 while ((len < 0 || in - str < len) && *in)
1123 if (c >= 0xdc00 && c < 0xe000) /* low surrogate */
1127 wc = SURROGATE_VALUE (high_surrogate, c);
1132 g_set_error (error, G_CONVERT_ERROR, G_CONVERT_ERROR_ILLEGAL_SEQUENCE,
1133 _("Invalid sequence in conversion input"));
1141 g_set_error (error, G_CONVERT_ERROR, G_CONVERT_ERROR_ILLEGAL_SEQUENCE,
1142 _("Invalid sequence in conversion input"));
1146 if (c >= 0xd800 && c < 0xdc00) /* high surrogate */
1155 /********** DIFFERENT for UTF8/UCS4 **********/
1156 n_bytes += sizeof (gunichar);
1162 if (high_surrogate && !items_read)
1164 g_set_error (error, G_CONVERT_ERROR, G_CONVERT_ERROR_PARTIAL_INPUT,
1165 _("Partial character sequence at end of input"));
1169 /* At this point, everything is valid, and we just need to convert
1171 /********** DIFFERENT for UTF8/UCS4 **********/
1172 result = g_malloc (n_bytes + 4);
1177 while (out < result + n_bytes)
1182 if (c >= 0xdc00 && c < 0xe000) /* low surrogate */
1184 wc = SURROGATE_VALUE (high_surrogate, c);
1187 else if (c >= 0xd800 && c < 0xdc00) /* high surrogate */
1195 /********** DIFFERENT for UTF8/UCS4 **********/
1196 *(gunichar *)out = wc;
1197 out += sizeof (gunichar);
1203 /********** DIFFERENT for UTF8/UCS4 **********/
1204 *(gunichar *)out = 0;
1207 /********** DIFFERENT for UTF8/UCS4 **********/
1208 *items_written = (out - result) / sizeof (gunichar);
1212 *items_read = in - str;
1214 return (gunichar *)result;
1219 * @str: a UTF-8 encoded string
1220 * @len: the maximum length of @str to use. If @len < 0, then
1221 * the string is nul-terminated.
1223 * @items_read: location to store number of bytes read, or %NULL.
1224 * If %NULL, then %G_CONVERT_ERROR_PARTIAL_INPUT will be
1225 * returned in case @str contains a trailing partial
1226 * character. If an error occurs then the index of the
1227 * invalid input is stored here.
1228 * @items_written: location to store number of words written, or %NULL.
1229 * The value stored here does not include the trailing
1231 * @error: location to store the error occuring, or %NULL to ignore
1232 * errors. Any of the errors in #GConvertError other than
1233 * %G_CONVERT_ERROR_NO_CONVERSION may occur.
1235 * Convert a string from UTF-8 to UTF-16. A 0 word will be
1236 * added to the result after the converted text.
1238 * Return value: a pointer to a newly allocated UTF-16 string.
1239 * This value must be freed with g_free(). If an
1240 * error occurs, %NULL will be returned and
1244 g_utf8_to_utf16 (const gchar *str,
1247 glong *items_written,
1250 gunichar2 *result = NULL;
1255 g_return_val_if_fail (str != NULL, NULL);
1259 while ((len < 0 || str + len - in > 0) && *in)
1261 gunichar wc = g_utf8_get_char_extended (in, str + len - in);
1262 if (wc & 0x80000000)
1264 if (wc == (gunichar)-2)
1269 g_set_error (error, G_CONVERT_ERROR, G_CONVERT_ERROR_PARTIAL_INPUT,
1270 _("Partial character sequence at end of input"));
1273 g_set_error (error, G_CONVERT_ERROR, G_CONVERT_ERROR_ILLEGAL_SEQUENCE,
1274 _("Invalid byte sequence in conversion input"));
1281 else if (wc < 0xe000)
1283 g_set_error (error, G_CONVERT_ERROR, G_CONVERT_ERROR_ILLEGAL_SEQUENCE,
1284 _("Invalid sequence in conversion input"));
1288 else if (wc < 0x10000)
1290 else if (wc < 0x110000)
1294 g_set_error (error, G_CONVERT_ERROR, G_CONVERT_ERROR_ILLEGAL_SEQUENCE,
1295 _("Character out of range for UTF-16"));
1300 in = g_utf8_next_char (in);
1303 result = g_new (gunichar2, n16 + 1);
1306 for (i = 0; i < n16;)
1308 gunichar wc = g_utf8_get_char (in);
1316 result[i++] = (wc - 0x10000) / 0x400 + 0xd800;
1317 result[i++] = (wc - 0x10000) % 0x400 + 0xdc00;
1320 in = g_utf8_next_char (in);
1326 *items_written = n16;
1330 *items_read = in - str;
1337 * @str: a UCS-4 encoded string
1338 * @len: the maximum length of @str to use. If @len < 0, then
1339 * the string is terminated with a 0 character.
1340 * @items_read: location to store number of bytes read, or %NULL.
1341 * If an error occurs then the index of the invalid input
1343 * @items_written: location to store number of words written, or %NULL.
1344 * The value stored here does not include the trailing
1346 * @error: location to store the error occuring, or %NULL to ignore
1347 * errors. Any of the errors in #GConvertError other than
1348 * %G_CONVERT_ERROR_NO_CONVERSION may occur.
1350 * Convert a string from UCS-4 to UTF-16. A 0 word will be
1351 * added to the result after the converted text.
1353 * Return value: a pointer to a newly allocated UTF-16 string.
1354 * This value must be freed with g_free(). If an
1355 * error occurs, %NULL will be returned and
1359 g_ucs4_to_utf16 (const gunichar *str,
1362 glong *items_written,
1365 gunichar2 *result = NULL;
1371 while ((len < 0 || i < len) && str[i])
1373 gunichar wc = str[i];
1377 else if (wc < 0xe000)
1379 g_set_error (error, G_CONVERT_ERROR, G_CONVERT_ERROR_ILLEGAL_SEQUENCE,
1380 _("Invalid sequence in conversion input"));
1384 else if (wc < 0x10000)
1386 else if (wc < 0x110000)
1390 g_set_error (error, G_CONVERT_ERROR, G_CONVERT_ERROR_ILLEGAL_SEQUENCE,
1391 _("Character out of range for UTF-16"));
1399 result = g_new (gunichar2, n16 + 1);
1401 for (i = 0, j = 0; j < n16; i++)
1403 gunichar wc = str[i];
1411 result[j++] = (wc - 0x10000) / 0x400 + 0xd800;
1412 result[j++] = (wc - 0x10000) % 0x400 + 0xdc00;
1418 *items_written = n16;
1429 * @str: a pointer to character data
1430 * @max_len: max bytes to validate, or -1 to go until nul
1431 * @end: return location for end of valid data
1433 * Validates UTF-8 encoded text. @str is the text to validate;
1434 * if @str is nul-terminated, then @max_len can be -1, otherwise
1435 * @max_len should be the number of bytes to validate.
1436 * If @end is non-%NULL, then the end of the valid range
1437 * will be stored there (i.e. the address of the first invalid byte
1438 * if some bytes were invalid, or the end of the text being validated
1441 * Returns %TRUE if all of @str was valid. Many GLib and GTK+
1442 * routines <emphasis>require</emphasis> valid UTF-8 as input;
1443 * so data read from a file or the network should be checked
1444 * with g_utf8_validate() before doing anything else with it.
1446 * Return value: %TRUE if the text was valid UTF-8
1449 g_utf8_validate (const gchar *str,
1456 g_return_val_if_fail (str != NULL, FALSE);
1463 while ((max_len < 0 || (p - str) < max_len) && *p)
1465 int i, mask = 0, len;
1467 unsigned char c = (unsigned char) *p;
1469 UTF8_COMPUTE (c, mask, len);
1474 /* check that the expected number of bytes exists in str */
1476 ((max_len - (p - str)) < len))
1479 UTF8_GET (result, p, i, mask, len);
1481 if (UTF8_LENGTH (result) != len) /* Check for overlong UTF-8 */
1484 if (result == (gunichar)-1)
1487 if (!UNICODE_VALID (result))
1496 /* See that we covered the entire length if a length was
1497 * passed in, or that we ended on a nul if not
1500 p != (str + max_len))
1502 else if (max_len < 0 &&
1510 * g_unichar_validate:
1511 * @ch: a Unicode character
1513 * Checks whether @ch is a valid Unicode character. Some possible
1514 * integer values of @ch will not be valid. 0 is considered a valid
1515 * character, though it's normally a string terminator.
1517 * Return value: %TRUE if @ch is a valid Unicode character
1520 g_unichar_validate (gunichar ch)
1522 return UNICODE_VALID (ch);