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, gint max)
208 const gchar *start = p;
214 p = g_utf8_next_char (p);
223 p = g_utf8_next_char (p);
225 while (p - start < max && *p)
228 p = g_utf8_next_char (p);
231 /* only do the last len increment if we got a complete
232 * char (don't count partial chars)
234 if (p - start == max)
243 * @p: a pointer to unicode character encoded as UTF-8
245 * Convert a sequence of bytes encoded as UTF-8 to a unicode character.
246 * If @p does not point to a valid UTF-8 encoded character, results are
249 * Return value: the resulting character
252 g_utf8_get_char (const gchar *p)
254 int i, mask = 0, len;
256 unsigned char c = (unsigned char) *p;
258 UTF8_COMPUTE (c, mask, len);
261 UTF8_GET (result, p, i, mask, len);
267 * g_utf8_offset_to_pointer:
268 * @str: a UTF-8 encoded string
269 * @offset: a character offset within the string.
271 * Converts from an integer character offset to a pointer to a position
274 * Return value: the resulting pointer
277 g_utf8_offset_to_pointer (const gchar *str,
280 const gchar *s = str;
282 s = g_utf8_next_char (s);
288 * g_utf8_pointer_to_offset:
289 * @str: a UTF-8 encoded string
290 * @pos: a pointer to a position within @str
292 * Converts from a pointer to position within a string to a integer
295 * Return value: the resulting character offset
298 g_utf8_pointer_to_offset (const gchar *str,
301 const gchar *s = str;
306 s = g_utf8_next_char (s);
316 * @dest: buffer to fill with characters from @src
318 * @n: character count
320 * Like the standard C strncpy() function, but copies a given number
321 * of characters instead of a given number of bytes. The @src string
322 * must be valid UTF-8 encoded text. (Use g_utf8_validate() on all
323 * text before trying to use UTF-8 utility functions with it.)
325 * Return value: @dest
328 g_utf8_strncpy (gchar *dest, const gchar *src, size_t n)
330 const gchar *s = src;
333 s = g_utf8_next_char(s);
336 strncpy(dest, src, s - src);
342 g_utf8_get_charset_internal (char **a)
344 char *charset = getenv("CHARSET");
346 if (charset && a && ! *a)
349 if (charset && strstr (charset, "UTF-8"))
353 charset = nl_langinfo(CODESET);
358 if (strcmp (charset, "UTF-8") == 0)
363 #if 0 /* #ifdef _NL_CTYPE_CODESET_NAME */
364 charset = nl_langinfo (_NL_CTYPE_CODESET_NAME);
369 if (strcmp (charset, "UTF-8") == 0)
374 #ifdef G_PLATFORM_WIN32
377 static char codepage[10];
379 sprintf (codepage, "CP%d", GetACP ());
381 /* What about codepage 1200? Is that UTF-8? */
389 /* Assume this for compatibility at present. */
393 static int utf8_locale_cache = -1;
394 static char *utf8_charset_cache = NULL;
398 * @charset: return location for character set name
400 * Obtains the character set for the current locale; you might use
401 * this character set as an argument to g_convert(), to convert from
402 * the current locale's encoding to some other encoding. (Frequently
403 * g_locale_to_utf8() and g_locale_from_utf8() are nice shortcuts,
406 * The return value is %TRUE if the locale's encoding is UTF-8, in that
407 * case you can perhaps avoid calling g_convert().
409 * The string returned in @charset is not allocated, and should not be
412 * Return value: %TRUE if the returned charset is UTF-8
415 g_get_charset (char **charset)
417 if (utf8_locale_cache != -1)
420 *charset = utf8_charset_cache;
421 return utf8_locale_cache;
423 utf8_locale_cache = g_utf8_get_charset_internal (&utf8_charset_cache);
425 *charset = utf8_charset_cache;
426 return utf8_locale_cache;
433 * @c: a ISO10646 character code
434 * @outbuf: output buffer, must have at least 6 bytes of space.
435 * If %NULL, the length will be computed and returned
436 * and nothing will be written to @out.
438 * Convert a single character to utf8
440 * Return value: number of bytes written
443 g_unichar_to_utf8 (gunichar c, gchar *outbuf)
459 else if (c < 0x10000)
464 else if (c < 0x200000)
469 else if (c < 0x4000000)
482 for (i = len - 1; i > 0; --i)
484 outbuf[i] = (c & 0x3f) | 0x80;
487 outbuf[0] = c | first;
495 * @p: a nul-terminated utf-8 string
496 * @c: a iso-10646 character
498 * Find the leftmost occurence of the given iso-10646 character
501 * Return value: NULL if the string does not contain the character, otherwise, a
502 * a pointer to the start of the leftmost of the character in the string.
505 g_utf8_strchr (const char *p, gunichar c)
509 gint len = g_unichar_to_utf8 (c, ch);
512 return strstr(p, ch);
518 * @p: a nul-terminated utf-8 string
519 * @c: a iso-10646 character/
521 * Find the rightmost occurence of the given iso-10646 character
524 * Return value: NULL if the string does not contain the character, otherwise, a
525 * a pointer to the start of the rightmost of the character in the string.
528 /* This is ifdefed out atm as there is no strrstr function in libc.
531 unicode_strrchr (const char *p, gunichar c)
535 len = g_unichar_to_utf8 (c, ch);
538 return strrstr(p, ch);
543 /* Like g_utf8_get_char, but take a maximum length
544 * and return (gunichar)-2 on incomplete trailing character
546 static inline gunichar
547 g_utf8_get_char_extended (const gchar *p, int max_len)
550 gunichar wc = (guchar) *p;
592 if (max_len >= 0 && len > max_len)
594 for (i = 1; i < max_len; i++)
596 if ((((guchar *)p)[i] & 0xc0) != 0x80)
602 for (i = 1; i < len; ++i)
604 gunichar ch = ((guchar *)p)[i];
606 if ((ch & 0xc0) != 0x80)
618 if (UTF8_LENGTH(wc) != len)
625 * g_utf8_to_ucs4_fast:
626 * @str: a UTF-8 encoded string
627 * @len: the maximum length of @str to use. If < 0, then
628 * the string is %NULL terminated.
629 * @items_written: location to store the number of characters in the
632 * Convert a string from UTF-8 to a 32-bit fixed width
633 * representation as UCS-4, assuming valid UTF-8 input.
634 * This function is roughly twice as fast as g_utf8_to_ucs4()
635 * but does no error checking on the input.
637 * Return value: a pointer to a newly allocated UCS-4 string.
638 * This value must be freed with g_free()
641 g_utf8_to_ucs4_fast (const gchar *str,
650 g_return_val_if_fail (str != NULL, NULL);
658 p = g_utf8_next_char (p);
664 while (p < str + len && *p)
666 p = g_utf8_next_char (p);
671 result = g_new (gunichar, n_chars + 1);
674 for (i=0; i < n_chars; i++)
676 gunichar wc = ((unsigned char *)p)[0];
711 for (j = 1; j < charlen; j++)
714 wc |= ((unsigned char *)p)[j] & 0x3f;
731 * @str: a UTF-8 encoded string
732 * @len: the maximum length of @str to use. If < 0, then
733 * the string is %NULL terminated.
734 * @items_read: location to store number of bytes read, or %NULL.
735 * If %NULL, then %G_CONVERT_ERROR_PARTIAL_INPUT will be
736 * returned in case @str contains a trailing partial
737 * character. If an error occurs then the index of the
738 * invalid input is stored here.
739 * @items_written: location to store number of characters written or %NULL.
740 * The value here stored does not include the trailing 0
742 * @error: location to store the error occuring, or %NULL to ignore
743 * errors. Any of the errors in #GConvertError other than
744 * %G_CONVERT_ERROR_NO_CONVERSION may occur.
746 * Convert a string from UTF-8 to a 32-bit fixed width
747 * representation as UCS-4. A trailing 0 will be added to the
748 * string after the converted text.
750 * Return value: a pointer to a newly allocated UCS-4 string.
751 * This value must be freed with g_free(). If an
752 * error occurs, %NULL will be returned and
756 g_utf8_to_ucs4 (const gchar *str,
762 gunichar *result = NULL;
768 while ((len < 0 || str + len - in > 0) && *in)
770 gunichar wc = g_utf8_get_char_extended (in, str + len - in);
773 if (wc == (gunichar)-2)
778 g_set_error (error, G_CONVERT_ERROR, G_CONVERT_ERROR_PARTIAL_INPUT,
779 _("Partial character sequence at end of input"));
782 g_set_error (error, G_CONVERT_ERROR, G_CONVERT_ERROR_ILLEGAL_SEQUENCE,
783 _("Invalid byte sequence in conversion input"));
790 in = g_utf8_next_char (in);
793 result = g_new (gunichar, n_chars + 1);
796 for (i=0; i < n_chars; i++)
798 result[i] = g_utf8_get_char (in);
799 in = g_utf8_next_char (in);
804 *items_written = n_chars;
808 *items_read = in - str;
815 * @str: a UCS-4 encoded string
816 * @len: the maximum length of @str to use. If < 0, then
817 * the string is %NULL terminated.
818 * @items_read: location to store number of characters read read, or %NULL.
819 * @items_written: location to store number of bytes written or %NULL.
820 * The value here stored does not include the trailing 0
822 * @error: location to store the error occuring, or %NULL to ignore
823 * errors. Any of the errors in #GConvertError other than
824 * %G_CONVERT_ERROR_NO_CONVERSION may occur.
826 * Convert a string from a 32-bit fixed width representation as UCS-4.
827 * to UTF-8. The result will be terminated with a 0 byte.
829 * Return value: a pointer to a newly allocated UTF-8 string.
830 * This value must be freed with g_free(). If an
831 * error occurs, %NULL will be returned and
835 g_ucs4_to_utf8 (const gunichar *str,
842 gchar *result = NULL;
847 for (i = 0; len < 0 || i < len ; i++)
852 if (str[i] >= 0x80000000)
857 g_set_error (error, G_CONVERT_ERROR, G_CONVERT_ERROR_ILLEGAL_SEQUENCE,
858 _("Character out of range for UTF-8"));
862 result_length += UTF8_LENGTH (str[i]);
865 result = g_malloc (result_length + 1);
869 while (p < result + result_length)
870 p += g_unichar_to_utf8 (str[i++], p);
875 *items_written = p - result;
884 #define SURROGATE_VALUE(h,l) (((h) - 0xd800) * 0x400 + (l) - 0xdc00 + 0x10000)
888 * @str: a UTF-16 encoded string
889 * @len: the maximum length of @str to use. If < 0, then
890 * the string is terminated with a 0 character.
891 * @items_read: location to store number of words read, or %NULL.
892 * If %NULL, then %G_CONVERT_ERROR_PARTIAL_INPUT will be
893 * returned in case @str contains a trailing partial
894 * character. If an error occurs then the index of the
895 * invalid input is stored here.
896 * @items_written: location to store number of bytes written, or %NULL.
897 * The value stored here does not include the trailing
899 * @error: location to store the error occuring, or %NULL to ignore
900 * errors. Any of the errors in #GConvertError other than
901 * %G_CONVERT_ERROR_NO_CONVERSION may occur.
903 * Convert a string from UTF-16 to UTF-8. The result will be
904 * terminated with a 0 byte.
906 * Return value: a pointer to a newly allocated UTF-8 string.
907 * This value must be freed with g_free(). If an
908 * error occurs, %NULL will be returned and
912 g_utf16_to_utf8 (const gunichar2 *str,
918 /* This function and g_utf16_to_ucs4 are almost exactly identical - The lines that differ
923 gchar *result = NULL;
925 gunichar high_surrogate;
927 g_return_val_if_fail (str != 0, NULL);
932 while ((len < 0 || in - str < len) && *in)
937 if (c >= 0xdc00 && c < 0xe000) /* low surrogate */
941 wc = SURROGATE_VALUE (high_surrogate, c);
946 g_set_error (error, G_CONVERT_ERROR, G_CONVERT_ERROR_ILLEGAL_SEQUENCE,
947 _("Invalid sequence in conversion input"));
955 g_set_error (error, G_CONVERT_ERROR, G_CONVERT_ERROR_ILLEGAL_SEQUENCE,
956 _("Invalid sequence in conversion input"));
960 if (c >= 0xd800 && c < 0xdc00) /* high surrogate */
969 /********** DIFFERENT for UTF8/UCS4 **********/
970 n_bytes += UTF8_LENGTH (wc);
976 if (high_surrogate && !items_read)
978 g_set_error (error, G_CONVERT_ERROR, G_CONVERT_ERROR_PARTIAL_INPUT,
979 _("Partial character sequence at end of input"));
983 /* At this point, everything is valid, and we just need to convert
985 /********** DIFFERENT for UTF8/UCS4 **********/
986 result = g_malloc (n_bytes + 1);
991 while (out < result + n_bytes)
996 if (c >= 0xdc00 && c < 0xe000) /* low surrogate */
998 wc = SURROGATE_VALUE (high_surrogate, c);
1001 else if (c >= 0xd800 && c < 0xdc00) /* high surrogate */
1009 /********** DIFFERENT for UTF8/UCS4 **********/
1010 out += g_unichar_to_utf8 (wc, out);
1016 /********** DIFFERENT for UTF8/UCS4 **********/
1020 /********** DIFFERENT for UTF8/UCS4 **********/
1021 *items_written = out - result;
1025 *items_read = in - str;
1032 * @str: a UTF-16 encoded string
1033 * @len: the maximum length of @str to use. If < 0, then
1034 * the string is terminated with a 0 character.
1035 * @items_read: location to store number of words read, or %NULL.
1036 * If %NULL, then %G_CONVERT_ERROR_PARTIAL_INPUT will be
1037 * returned in case @str contains a trailing partial
1038 * character. If an error occurs then the index of the
1039 * invalid input is stored here.
1040 * @items_written: location to store number of characters written, or %NULL.
1041 * The value stored here does not include the trailing
1043 * @error: location to store the error occuring, or %NULL to ignore
1044 * errors. Any of the errors in #GConvertError other than
1045 * %G_CONVERT_ERROR_NO_CONVERSION may occur.
1047 * Convert a string from UTF-16 to UCS-4. The result will be
1048 * terminated with a 0 character.
1050 * Return value: a pointer to a newly allocated UCS-4 string.
1051 * This value must be freed with g_free(). If an
1052 * error occurs, %NULL will be returned and
1056 g_utf16_to_ucs4 (const gunichar2 *str,
1059 gint *items_written,
1062 const gunichar2 *in;
1064 gchar *result = NULL;
1066 gunichar high_surrogate;
1068 g_return_val_if_fail (str != 0, NULL);
1073 while ((len < 0 || in - str < len) && *in)
1078 if (c >= 0xdc00 && c < 0xe000) /* low surrogate */
1082 wc = SURROGATE_VALUE (high_surrogate, c);
1087 g_set_error (error, G_CONVERT_ERROR, G_CONVERT_ERROR_ILLEGAL_SEQUENCE,
1088 _("Invalid sequence in conversion input"));
1096 g_set_error (error, G_CONVERT_ERROR, G_CONVERT_ERROR_ILLEGAL_SEQUENCE,
1097 _("Invalid sequence in conversion input"));
1101 if (c >= 0xd800 && c < 0xdc00) /* high surrogate */
1110 /********** DIFFERENT for UTF8/UCS4 **********/
1111 n_bytes += sizeof (gunichar);
1117 if (high_surrogate && !items_read)
1119 g_set_error (error, G_CONVERT_ERROR, G_CONVERT_ERROR_PARTIAL_INPUT,
1120 _("Partial character sequence at end of input"));
1124 /* At this point, everything is valid, and we just need to convert
1126 /********** DIFFERENT for UTF8/UCS4 **********/
1127 result = g_malloc (n_bytes + 4);
1132 while (out < result + n_bytes)
1137 if (c >= 0xdc00 && c < 0xe000) /* low surrogate */
1139 wc = SURROGATE_VALUE (high_surrogate, c);
1142 else if (c >= 0xd800 && c < 0xdc00) /* high surrogate */
1150 /********** DIFFERENT for UTF8/UCS4 **********/
1151 *(gunichar *)out = wc;
1152 out += sizeof (gunichar);
1158 /********** DIFFERENT for UTF8/UCS4 **********/
1159 *(gunichar *)out = 0;
1162 /********** DIFFERENT for UTF8/UCS4 **********/
1163 *items_written = (out - result) / sizeof (gunichar);
1167 *items_read = in - str;
1169 return (gunichar *)result;
1174 * @str: a UTF-8 encoded string
1175 * @len: the maximum length of @str to use. If < 0, then
1176 * the string is %NULL terminated.
1178 * @items_read: location to store number of bytes read, or %NULL.
1179 * If %NULL, then %G_CONVERT_ERROR_PARTIAL_INPUT will be
1180 * returned in case @str contains a trailing partial
1181 * character. If an error occurs then the index of the
1182 * invalid input is stored here.
1183 * @items_written: location to store number of words written, or %NULL.
1184 * The value stored here does not include the trailing
1186 * @error: location to store the error occuring, or %NULL to ignore
1187 * errors. Any of the errors in #GConvertError other than
1188 * %G_CONVERT_ERROR_NO_CONVERSION may occur.
1190 * Convert a string from UTF-8 to UTF-16. A 0 word will be
1191 * added to the result after the converted text.
1193 * Return value: a pointer to a newly allocated UTF-16 string.
1194 * This value must be freed with g_free(). If an
1195 * error occurs, %NULL will be returned and
1199 g_utf8_to_utf16 (const gchar *str,
1202 gint *items_written,
1205 gunichar2 *result = NULL;
1210 g_return_val_if_fail (str != NULL, NULL);
1214 while ((len < 0 || str + len - in > 0) && *in)
1216 gunichar wc = g_utf8_get_char_extended (in, str + len - in);
1217 if (wc & 0x80000000)
1219 if (wc == (gunichar)-2)
1224 g_set_error (error, G_CONVERT_ERROR, G_CONVERT_ERROR_PARTIAL_INPUT,
1225 _("Partial character sequence at end of input"));
1228 g_set_error (error, G_CONVERT_ERROR, G_CONVERT_ERROR_ILLEGAL_SEQUENCE,
1229 _("Invalid byte sequence in conversion input"));
1236 else if (wc < 0xe000)
1238 g_set_error (error, G_CONVERT_ERROR, G_CONVERT_ERROR_ILLEGAL_SEQUENCE,
1239 _("Invalid sequence in conversion input"));
1243 else if (wc < 0x10000)
1245 else if (wc < 0x110000)
1249 g_set_error (error, G_CONVERT_ERROR, G_CONVERT_ERROR_ILLEGAL_SEQUENCE,
1250 _("Character out of range for UTF-16"));
1255 in = g_utf8_next_char (in);
1258 result = g_new (gunichar2, n16 + 1);
1261 for (i = 0; i < n16;)
1263 gunichar wc = g_utf8_get_char (in);
1271 result[i++] = (wc - 0x10000) / 0x400 + 0xd800;
1272 result[i++] = (wc - 0x10000) % 0x400 + 0xdc00;
1275 in = g_utf8_next_char (in);
1281 *items_written = n16;
1285 *items_read = in - str;
1292 * @str: a UCS-4 encoded string
1293 * @len: the maximum length of @str to use. If < 0, then
1294 * the string is terminated with a zero character.
1295 * @items_read: location to store number of bytes read, or %NULL.
1296 * If an error occurs then the index of the invalid input
1298 * @items_written: location to store number of words written, or %NULL.
1299 * The value stored here does not include the trailing
1301 * @error: location to store the error occuring, or %NULL to ignore
1302 * errors. Any of the errors in #GConvertError other than
1303 * %G_CONVERT_ERROR_NO_CONVERSION may occur.
1305 * Convert a string from UCS-4 to UTF-16. A 0 word will be
1306 * added to the result after the converted text.
1308 * Return value: a pointer to a newly allocated UTF-16 string.
1309 * This value must be freed with g_free(). If an
1310 * error occurs, %NULL will be returned and
1314 g_ucs4_to_utf16 (const gunichar *str,
1317 gint *items_written,
1320 gunichar2 *result = NULL;
1326 while ((len < 0 || i < len) && str[i])
1328 gunichar wc = str[i];
1332 else if (wc < 0xe000)
1334 g_set_error (error, G_CONVERT_ERROR, G_CONVERT_ERROR_ILLEGAL_SEQUENCE,
1335 _("Invalid sequence in conversion input"));
1339 else if (wc < 0x10000)
1341 else if (wc < 0x110000)
1345 g_set_error (error, G_CONVERT_ERROR, G_CONVERT_ERROR_ILLEGAL_SEQUENCE,
1346 _("Character out of range for UTF-16"));
1354 result = g_new (gunichar2, n16 + 1);
1356 for (i = 0, j = 0; j < n16; i++)
1358 gunichar wc = str[i];
1366 result[j++] = (wc - 0x10000) / 0x400 + 0xd800;
1367 result[j++] = (wc - 0x10000) % 0x400 + 0xdc00;
1373 *items_written = n16;
1384 * @str: a pointer to character data
1385 * @max_len: max bytes to validate, or -1 to go until nul
1386 * @end: return location for end of valid data
1388 * Validates UTF-8 encoded text. @str is the text to validate;
1389 * if @str is nul-terminated, then @max_len can be -1, otherwise
1390 * @max_len should be the number of bytes to validate.
1391 * If @end is non-%NULL, then the end of the valid range
1392 * will be stored there (i.e. the address of the first invalid byte
1393 * if some bytes were invalid, or the end of the text being validated
1396 * Returns TRUE if all of @str was valid. Many GLib and GTK+
1397 * routines <emphasis>require</emphasis> valid UTF8 as input;
1398 * so data read from a file or the network should be checked
1399 * with g_utf8_validate() before doing anything else with it.
1401 * Return value: TRUE if the text was valid UTF-8.
1404 g_utf8_validate (const gchar *str,
1411 g_return_val_if_fail (str != NULL, FALSE);
1418 while ((max_len < 0 || (p - str) < max_len) && *p)
1420 int i, mask = 0, len;
1422 unsigned char c = (unsigned char) *p;
1424 UTF8_COMPUTE (c, mask, len);
1429 /* check that the expected number of bytes exists in str */
1431 ((max_len - (p - str)) < len))
1434 UTF8_GET (result, p, i, mask, len);
1436 if (UTF8_LENGTH (result) != len) /* Check for overlong UTF-8 */
1439 if (result == (gunichar)-1)
1442 if (!UNICODE_VALID (result))
1451 /* See that we covered the entire length if a length was
1452 * passed in, or that we ended on a nul if not
1455 p != (str + max_len))
1457 else if (max_len < 0 &&
1465 * g_unichar_validate:
1466 * @ch: a Unicode character
1468 * Checks whether @ch is a valid Unicode character. Some possible
1469 * integer values of @ch will not be valid. 0 is considered a valid
1470 * character, though it's normally a string terminator.
1472 * Return value: %TRUE if @ch is a valid Unicode character
1475 g_unichar_validate (gunichar ch)
1477 return UNICODE_VALID (ch);