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
250 * Return value: the resulting character
253 g_utf8_get_char (const gchar *p)
255 int i, mask = 0, len;
257 unsigned char c = (unsigned char) *p;
259 UTF8_COMPUTE (c, mask, len);
262 UTF8_GET (result, p, i, mask, len);
268 * g_utf8_offset_to_pointer:
269 * @str: a UTF-8 encoded string
270 * @offset: a character offset within the string.
272 * Converts from an integer character offset to a pointer to a position
275 * Return value: the resulting pointer
278 g_utf8_offset_to_pointer (const gchar *str,
281 const gchar *s = str;
283 s = g_utf8_next_char (s);
289 * g_utf8_pointer_to_offset:
290 * @str: a UTF-8 encoded string
291 * @pos: a pointer to a position within @str
293 * Converts from a pointer to position within a string to a integer
296 * Return value: the resulting character offset
299 g_utf8_pointer_to_offset (const gchar *str,
302 const gchar *s = str;
307 s = g_utf8_next_char (s);
317 * @dest: buffer to fill with characters from @src
319 * @n: character count
321 * Like the standard C strncpy() function, but copies a given number
322 * of characters instead of a given number of bytes. The @src string
323 * must be valid UTF-8 encoded text. (Use g_utf8_validate() on all
324 * text before trying to use UTF-8 utility functions with it.)
326 * Return value: @dest
329 g_utf8_strncpy (gchar *dest, const gchar *src, size_t n)
331 const gchar *s = src;
334 s = g_utf8_next_char(s);
337 strncpy(dest, src, s - src);
343 g_utf8_get_charset_internal (char **a)
345 char *charset = getenv("CHARSET");
347 if (charset && a && ! *a)
350 if (charset && strstr (charset, "UTF-8"))
354 charset = nl_langinfo(CODESET);
359 if (strcmp (charset, "UTF-8") == 0)
364 #if 0 /* #ifdef _NL_CTYPE_CODESET_NAME */
365 charset = nl_langinfo (_NL_CTYPE_CODESET_NAME);
370 if (strcmp (charset, "UTF-8") == 0)
375 #ifdef G_PLATFORM_WIN32
378 static char codepage[10];
380 sprintf (codepage, "CP%d", GetACP ());
382 /* What about codepage 1200? Is that UTF-8? */
390 /* Assume this for compatibility at present. */
394 static int utf8_locale_cache = -1;
395 static char *utf8_charset_cache = NULL;
399 * @charset: return location for character set name
401 * Obtains the character set for the current locale; you might use
402 * this character set as an argument to g_convert(), to convert from
403 * the current locale's encoding to some other encoding. (Frequently
404 * g_locale_to_utf8() and g_locale_from_utf8() are nice shortcuts,
407 * The return value is %TRUE if the locale's encoding is UTF-8, in that
408 * case you can perhaps avoid calling g_convert().
410 * The string returned in @charset is not allocated, and should not be
413 * Return value: %TRUE if the returned charset is UTF-8
416 g_get_charset (G_CONST_RETURN char **charset)
418 if (utf8_locale_cache != -1)
421 *charset = utf8_charset_cache;
422 return utf8_locale_cache;
424 utf8_locale_cache = g_utf8_get_charset_internal (&utf8_charset_cache);
426 *charset = utf8_charset_cache;
427 return utf8_locale_cache;
434 * @c: a ISO10646 character code
435 * @outbuf: output buffer, must have at least 6 bytes of space.
436 * If %NULL, the length will be computed and returned
437 * and nothing will be written to @out.
439 * Convert a single character to utf8
441 * Return value: number of bytes written
444 g_unichar_to_utf8 (gunichar c,
461 else if (c < 0x10000)
466 else if (c < 0x200000)
471 else if (c < 0x4000000)
484 for (i = len - 1; i > 0; --i)
486 outbuf[i] = (c & 0x3f) | 0x80;
489 outbuf[0] = c | first;
497 * @p: a nul-terminated utf-8 string
498 * @p_len: the maximum length of p
499 * @c: a iso-10646 character
501 * Find the leftmost occurence of the given iso-10646 character
502 * in a UTF-8 string, while limiting the search to p_len bytes.
503 * If len is -1, allow unbounded search.
505 * Return value: NULL if the string does not contain the character, otherwise, a
506 * a pointer to the start of the leftmost of the character in the string.
509 g_utf8_strchr (const char *p,
515 gint len = g_unichar_to_utf8 (c, ch);
518 return g_strstr_len (p, p_len, ch);
524 * @p: a nul-terminated utf-8 string
525 * @p_len: the maximum length of p
526 * @c: a iso-10646 character/
528 * Find the rightmost occurence of the given iso-10646 character
529 * in a UTF-8 string, while limiting the search to p_len bytes.
530 * If len is -1, allow unbounded search.
532 * Return value: NULL if the string does not contain the character, otherwise, a
533 * a pointer to the start of the rightmost of the character in the string.
536 g_utf8_strrchr (const char *p,
542 gint len = g_unichar_to_utf8 (c, ch);
545 return g_strrstr_len (p, p_len, ch);
549 /* Like g_utf8_get_char, but take a maximum length
550 * and return (gunichar)-2 on incomplete trailing character
552 static inline gunichar
553 g_utf8_get_char_extended (const gchar *p, gsize max_len)
556 gunichar wc = (guchar) *p;
596 if (max_len >= 0 && len > max_len)
598 for (i = 1; i < max_len; i++)
600 if ((((guchar *)p)[i] & 0xc0) != 0x80)
606 for (i = 1; i < len; ++i)
608 gunichar ch = ((guchar *)p)[i];
610 if ((ch & 0xc0) != 0x80)
622 if (UTF8_LENGTH(wc) != len)
629 * g_utf8_to_ucs4_fast:
630 * @str: a UTF-8 encoded string
631 * @len: the maximum length of @str to use. If < 0, then
632 * the string is %NULL terminated.
633 * @items_written: location to store the number of characters in the
636 * Convert a string from UTF-8 to a 32-bit fixed width
637 * representation as UCS-4, assuming valid UTF-8 input.
638 * This function is roughly twice as fast as g_utf8_to_ucs4()
639 * but does no error checking on the input.
641 * Return value: a pointer to a newly allocated UCS-4 string.
642 * This value must be freed with g_free()
645 g_utf8_to_ucs4_fast (const gchar *str,
647 glong *items_written)
654 g_return_val_if_fail (str != NULL, NULL);
662 p = g_utf8_next_char (p);
668 while (p < str + len && *p)
670 p = g_utf8_next_char (p);
675 result = g_new (gunichar, n_chars + 1);
678 for (i=0; i < n_chars; i++)
680 gunichar wc = ((unsigned char *)p)[0];
715 for (j = 1; j < charlen; j++)
718 wc |= ((unsigned char *)p)[j] & 0x3f;
735 * @str: a UTF-8 encoded string
736 * @len: the maximum length of @str to use. If < 0, then
737 * the string is %NULL terminated.
738 * @items_read: location to store number of bytes read, or %NULL.
739 * If %NULL, then %G_CONVERT_ERROR_PARTIAL_INPUT will be
740 * returned in case @str contains a trailing partial
741 * character. If an error occurs then the index of the
742 * invalid input is stored here.
743 * @items_written: location to store number of characters written or %NULL.
744 * The value here stored does not include the trailing 0
746 * @error: location to store the error occuring, or %NULL to ignore
747 * errors. Any of the errors in #GConvertError other than
748 * %G_CONVERT_ERROR_NO_CONVERSION may occur.
750 * Convert a string from UTF-8 to a 32-bit fixed width
751 * representation as UCS-4. A trailing 0 will be added to the
752 * string after the converted text.
754 * Return value: a pointer to a newly allocated UCS-4 string.
755 * This value must be freed with g_free(). If an
756 * error occurs, %NULL will be returned and
760 g_utf8_to_ucs4 (const gchar *str,
763 glong *items_written,
766 gunichar *result = NULL;
772 while ((len < 0 || str + len - in > 0) && *in)
774 gunichar wc = g_utf8_get_char_extended (in, str + len - in);
777 if (wc == (gunichar)-2)
782 g_set_error (error, G_CONVERT_ERROR, G_CONVERT_ERROR_PARTIAL_INPUT,
783 _("Partial character sequence at end of input"));
786 g_set_error (error, G_CONVERT_ERROR, G_CONVERT_ERROR_ILLEGAL_SEQUENCE,
787 _("Invalid byte sequence in conversion input"));
794 in = g_utf8_next_char (in);
797 result = g_new (gunichar, n_chars + 1);
800 for (i=0; i < n_chars; i++)
802 result[i] = g_utf8_get_char (in);
803 in = g_utf8_next_char (in);
808 *items_written = n_chars;
812 *items_read = in - str;
819 * @str: a UCS-4 encoded string
820 * @len: the maximum length of @str to use. If < 0, then
821 * the string is %NULL terminated.
822 * @items_read: location to store number of characters read read, or %NULL.
823 * @items_written: location to store number of bytes written or %NULL.
824 * The value here stored does not include the trailing 0
826 * @error: location to store the error occuring, or %NULL to ignore
827 * errors. Any of the errors in #GConvertError other than
828 * %G_CONVERT_ERROR_NO_CONVERSION may occur.
830 * Convert a string from a 32-bit fixed width representation as UCS-4.
831 * to UTF-8. The result will be terminated with a 0 byte.
833 * Return value: a pointer to a newly allocated UTF-8 string.
834 * This value must be freed with g_free(). If an
835 * error occurs, %NULL will be returned and
839 g_ucs4_to_utf8 (const gunichar *str,
842 glong *items_written,
846 gchar *result = NULL;
851 for (i = 0; len < 0 || i < len ; i++)
856 if (str[i] >= 0x80000000)
861 g_set_error (error, G_CONVERT_ERROR, G_CONVERT_ERROR_ILLEGAL_SEQUENCE,
862 _("Character out of range for UTF-8"));
866 result_length += UTF8_LENGTH (str[i]);
869 result = g_malloc (result_length + 1);
873 while (p < result + result_length)
874 p += g_unichar_to_utf8 (str[i++], p);
879 *items_written = p - result;
888 #define SURROGATE_VALUE(h,l) (((h) - 0xd800) * 0x400 + (l) - 0xdc00 + 0x10000)
892 * @str: a UTF-16 encoded string
893 * @len: the maximum length of @str to use. If < 0, then
894 * the string is terminated with a 0 character.
895 * @items_read: location to store number of words read, or %NULL.
896 * If %NULL, then %G_CONVERT_ERROR_PARTIAL_INPUT will be
897 * returned in case @str contains a trailing partial
898 * character. If an error occurs then the index of the
899 * invalid input is stored here.
900 * @items_written: location to store number of bytes written, or %NULL.
901 * The value stored here does not include the trailing
903 * @error: location to store the error occuring, or %NULL to ignore
904 * errors. Any of the errors in #GConvertError other than
905 * %G_CONVERT_ERROR_NO_CONVERSION may occur.
907 * Convert a string from UTF-16 to UTF-8. The result will be
908 * terminated with a 0 byte.
910 * Return value: a pointer to a newly allocated UTF-8 string.
911 * This value must be freed with g_free(). If an
912 * error occurs, %NULL will be returned and
916 g_utf16_to_utf8 (const gunichar2 *str,
919 glong *items_written,
922 /* This function and g_utf16_to_ucs4 are almost exactly identical - The lines that differ
927 gchar *result = NULL;
929 gunichar high_surrogate;
931 g_return_val_if_fail (str != 0, NULL);
936 while ((len < 0 || in - str < len) && *in)
941 if (c >= 0xdc00 && c < 0xe000) /* low surrogate */
945 wc = SURROGATE_VALUE (high_surrogate, c);
950 g_set_error (error, G_CONVERT_ERROR, G_CONVERT_ERROR_ILLEGAL_SEQUENCE,
951 _("Invalid sequence in conversion input"));
959 g_set_error (error, G_CONVERT_ERROR, G_CONVERT_ERROR_ILLEGAL_SEQUENCE,
960 _("Invalid sequence in conversion input"));
964 if (c >= 0xd800 && c < 0xdc00) /* high surrogate */
973 /********** DIFFERENT for UTF8/UCS4 **********/
974 n_bytes += UTF8_LENGTH (wc);
980 if (high_surrogate && !items_read)
982 g_set_error (error, G_CONVERT_ERROR, G_CONVERT_ERROR_PARTIAL_INPUT,
983 _("Partial character sequence at end of input"));
987 /* At this point, everything is valid, and we just need to convert
989 /********** DIFFERENT for UTF8/UCS4 **********/
990 result = g_malloc (n_bytes + 1);
995 while (out < result + n_bytes)
1000 if (c >= 0xdc00 && c < 0xe000) /* low surrogate */
1002 wc = SURROGATE_VALUE (high_surrogate, c);
1005 else if (c >= 0xd800 && c < 0xdc00) /* high surrogate */
1013 /********** DIFFERENT for UTF8/UCS4 **********/
1014 out += g_unichar_to_utf8 (wc, out);
1020 /********** DIFFERENT for UTF8/UCS4 **********/
1024 /********** DIFFERENT for UTF8/UCS4 **********/
1025 *items_written = out - result;
1029 *items_read = in - str;
1036 * @str: a UTF-16 encoded string
1037 * @len: the maximum length of @str to use. If < 0, then
1038 * the string is terminated with a 0 character.
1039 * @items_read: location to store number of words read, or %NULL.
1040 * If %NULL, then %G_CONVERT_ERROR_PARTIAL_INPUT will be
1041 * returned in case @str contains a trailing partial
1042 * character. If an error occurs then the index of the
1043 * invalid input is stored here.
1044 * @items_written: location to store number of characters written, or %NULL.
1045 * The value stored here does not include the trailing
1047 * @error: location to store the error occuring, or %NULL to ignore
1048 * errors. Any of the errors in #GConvertError other than
1049 * %G_CONVERT_ERROR_NO_CONVERSION may occur.
1051 * Convert a string from UTF-16 to UCS-4. The result will be
1052 * terminated with a 0 character.
1054 * Return value: a pointer to a newly allocated UCS-4 string.
1055 * This value must be freed with g_free(). If an
1056 * error occurs, %NULL will be returned and
1060 g_utf16_to_ucs4 (const gunichar2 *str,
1063 glong *items_written,
1066 const gunichar2 *in;
1068 gchar *result = NULL;
1070 gunichar high_surrogate;
1072 g_return_val_if_fail (str != 0, NULL);
1077 while ((len < 0 || in - str < len) && *in)
1082 if (c >= 0xdc00 && c < 0xe000) /* low surrogate */
1086 wc = SURROGATE_VALUE (high_surrogate, c);
1091 g_set_error (error, G_CONVERT_ERROR, G_CONVERT_ERROR_ILLEGAL_SEQUENCE,
1092 _("Invalid sequence in conversion input"));
1100 g_set_error (error, G_CONVERT_ERROR, G_CONVERT_ERROR_ILLEGAL_SEQUENCE,
1101 _("Invalid sequence in conversion input"));
1105 if (c >= 0xd800 && c < 0xdc00) /* high surrogate */
1114 /********** DIFFERENT for UTF8/UCS4 **********/
1115 n_bytes += sizeof (gunichar);
1121 if (high_surrogate && !items_read)
1123 g_set_error (error, G_CONVERT_ERROR, G_CONVERT_ERROR_PARTIAL_INPUT,
1124 _("Partial character sequence at end of input"));
1128 /* At this point, everything is valid, and we just need to convert
1130 /********** DIFFERENT for UTF8/UCS4 **********/
1131 result = g_malloc (n_bytes + 4);
1136 while (out < result + n_bytes)
1141 if (c >= 0xdc00 && c < 0xe000) /* low surrogate */
1143 wc = SURROGATE_VALUE (high_surrogate, c);
1146 else if (c >= 0xd800 && c < 0xdc00) /* high surrogate */
1154 /********** DIFFERENT for UTF8/UCS4 **********/
1155 *(gunichar *)out = wc;
1156 out += sizeof (gunichar);
1162 /********** DIFFERENT for UTF8/UCS4 **********/
1163 *(gunichar *)out = 0;
1166 /********** DIFFERENT for UTF8/UCS4 **********/
1167 *items_written = (out - result) / sizeof (gunichar);
1171 *items_read = in - str;
1173 return (gunichar *)result;
1178 * @str: a UTF-8 encoded string
1179 * @len: the maximum length of @str to use. If < 0, then
1180 * the string is %NULL terminated.
1182 * @items_read: location to store number of bytes read, or %NULL.
1183 * If %NULL, then %G_CONVERT_ERROR_PARTIAL_INPUT will be
1184 * returned in case @str contains a trailing partial
1185 * character. If an error occurs then the index of the
1186 * invalid input is stored here.
1187 * @items_written: location to store number of words written, or %NULL.
1188 * The value stored here does not include the trailing
1190 * @error: location to store the error occuring, or %NULL to ignore
1191 * errors. Any of the errors in #GConvertError other than
1192 * %G_CONVERT_ERROR_NO_CONVERSION may occur.
1194 * Convert a string from UTF-8 to UTF-16. A 0 word will be
1195 * added to the result after the converted text.
1197 * Return value: a pointer to a newly allocated UTF-16 string.
1198 * This value must be freed with g_free(). If an
1199 * error occurs, %NULL will be returned and
1203 g_utf8_to_utf16 (const gchar *str,
1206 glong *items_written,
1209 gunichar2 *result = NULL;
1214 g_return_val_if_fail (str != NULL, NULL);
1218 while ((len < 0 || str + len - in > 0) && *in)
1220 gunichar wc = g_utf8_get_char_extended (in, str + len - in);
1221 if (wc & 0x80000000)
1223 if (wc == (gunichar)-2)
1228 g_set_error (error, G_CONVERT_ERROR, G_CONVERT_ERROR_PARTIAL_INPUT,
1229 _("Partial character sequence at end of input"));
1232 g_set_error (error, G_CONVERT_ERROR, G_CONVERT_ERROR_ILLEGAL_SEQUENCE,
1233 _("Invalid byte sequence in conversion input"));
1240 else if (wc < 0xe000)
1242 g_set_error (error, G_CONVERT_ERROR, G_CONVERT_ERROR_ILLEGAL_SEQUENCE,
1243 _("Invalid sequence in conversion input"));
1247 else if (wc < 0x10000)
1249 else if (wc < 0x110000)
1253 g_set_error (error, G_CONVERT_ERROR, G_CONVERT_ERROR_ILLEGAL_SEQUENCE,
1254 _("Character out of range for UTF-16"));
1259 in = g_utf8_next_char (in);
1262 result = g_new (gunichar2, n16 + 1);
1265 for (i = 0; i < n16;)
1267 gunichar wc = g_utf8_get_char (in);
1275 result[i++] = (wc - 0x10000) / 0x400 + 0xd800;
1276 result[i++] = (wc - 0x10000) % 0x400 + 0xdc00;
1279 in = g_utf8_next_char (in);
1285 *items_written = n16;
1289 *items_read = in - str;
1296 * @str: a UCS-4 encoded string
1297 * @len: the maximum length of @str to use. If < 0, then
1298 * the string is terminated with a zero character.
1299 * @items_read: location to store number of bytes read, or %NULL.
1300 * If an error occurs then the index of the invalid input
1302 * @items_written: location to store number of words written, or %NULL.
1303 * The value stored here does not include the trailing
1305 * @error: location to store the error occuring, or %NULL to ignore
1306 * errors. Any of the errors in #GConvertError other than
1307 * %G_CONVERT_ERROR_NO_CONVERSION may occur.
1309 * Convert a string from UCS-4 to UTF-16. A 0 word will be
1310 * added to the result after the converted text.
1312 * Return value: a pointer to a newly allocated UTF-16 string.
1313 * This value must be freed with g_free(). If an
1314 * error occurs, %NULL will be returned and
1318 g_ucs4_to_utf16 (const gunichar *str,
1321 glong *items_written,
1324 gunichar2 *result = NULL;
1330 while ((len < 0 || i < len) && str[i])
1332 gunichar wc = str[i];
1336 else if (wc < 0xe000)
1338 g_set_error (error, G_CONVERT_ERROR, G_CONVERT_ERROR_ILLEGAL_SEQUENCE,
1339 _("Invalid sequence in conversion input"));
1343 else if (wc < 0x10000)
1345 else if (wc < 0x110000)
1349 g_set_error (error, G_CONVERT_ERROR, G_CONVERT_ERROR_ILLEGAL_SEQUENCE,
1350 _("Character out of range for UTF-16"));
1358 result = g_new (gunichar2, n16 + 1);
1360 for (i = 0, j = 0; j < n16; i++)
1362 gunichar wc = str[i];
1370 result[j++] = (wc - 0x10000) / 0x400 + 0xd800;
1371 result[j++] = (wc - 0x10000) % 0x400 + 0xdc00;
1377 *items_written = n16;
1388 * @str: a pointer to character data
1389 * @max_len: max bytes to validate, or -1 to go until nul
1390 * @end: return location for end of valid data
1392 * Validates UTF-8 encoded text. @str is the text to validate;
1393 * if @str is nul-terminated, then @max_len can be -1, otherwise
1394 * @max_len should be the number of bytes to validate.
1395 * If @end is non-%NULL, then the end of the valid range
1396 * will be stored there (i.e. the address of the first invalid byte
1397 * if some bytes were invalid, or the end of the text being validated
1400 * Returns TRUE if all of @str was valid. Many GLib and GTK+
1401 * routines <emphasis>require</emphasis> valid UTF8 as input;
1402 * so data read from a file or the network should be checked
1403 * with g_utf8_validate() before doing anything else with it.
1405 * Return value: TRUE if the text was valid UTF-8.
1408 g_utf8_validate (const gchar *str,
1415 g_return_val_if_fail (str != NULL, FALSE);
1422 while ((max_len < 0 || (p - str) < max_len) && *p)
1424 int i, mask = 0, len;
1426 unsigned char c = (unsigned char) *p;
1428 UTF8_COMPUTE (c, mask, len);
1433 /* check that the expected number of bytes exists in str */
1435 ((max_len - (p - str)) < len))
1438 UTF8_GET (result, p, i, mask, len);
1440 if (UTF8_LENGTH (result) != len) /* Check for overlong UTF-8 */
1443 if (result == (gunichar)-1)
1446 if (!UNICODE_VALID (result))
1455 /* See that we covered the entire length if a length was
1456 * passed in, or that we ended on a nul if not
1459 p != (str + max_len))
1461 else if (max_len < 0 &&
1469 * g_unichar_validate:
1470 * @ch: a Unicode character
1472 * Checks whether @ch is a valid Unicode character. Some possible
1473 * integer values of @ch will not be valid. 0 is considered a valid
1474 * character, though it's normally a string terminator.
1476 * Return value: %TRUE if @ch is a valid Unicode character
1479 g_unichar_validate (gunichar ch)
1481 return UNICODE_VALID (ch);