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 (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, gchar *outbuf)
460 else if (c < 0x10000)
465 else if (c < 0x200000)
470 else if (c < 0x4000000)
483 for (i = len - 1; i > 0; --i)
485 outbuf[i] = (c & 0x3f) | 0x80;
488 outbuf[0] = c | first;
496 * @p: a nul-terminated utf-8 string
497 * @c: a iso-10646 character
499 * Find the leftmost occurence of the given iso-10646 character
502 * Return value: NULL if the string does not contain the character, otherwise, a
503 * a pointer to the start of the leftmost of the character in the string.
506 g_utf8_strchr (const char *p, gunichar c)
510 gint len = g_unichar_to_utf8 (c, ch);
513 return strstr(p, ch);
519 * @p: a nul-terminated utf-8 string
520 * @c: a iso-10646 character/
522 * Find the rightmost occurence of the given iso-10646 character
525 * Return value: NULL if the string does not contain the character, otherwise, a
526 * a pointer to the start of the rightmost of the character in the string.
529 /* This is ifdefed out atm as there is no strrstr function in libc.
532 unicode_strrchr (const char *p, gunichar c)
536 len = g_unichar_to_utf8 (c, ch);
539 return strrstr(p, ch);
544 /* Like g_utf8_get_char, but take a maximum length
545 * and return (gunichar)-2 on incomplete trailing character
547 static inline gunichar
548 g_utf8_get_char_extended (const gchar *p, int max_len)
551 gunichar wc = (guchar) *p;
593 if (max_len >= 0 && len > max_len)
595 for (i = 1; i < max_len; i++)
597 if ((((guchar *)p)[i] & 0xc0) != 0x80)
603 for (i = 1; i < len; ++i)
605 gunichar ch = ((guchar *)p)[i];
607 if ((ch & 0xc0) != 0x80)
619 if (UTF8_LENGTH(wc) != len)
626 * g_utf8_to_ucs4_fast:
627 * @str: a UTF-8 encoded string
628 * @len: the maximum length of @str to use. If < 0, then
629 * the string is %NULL terminated.
630 * @items_written: location to store the number of characters in the
633 * Convert a string from UTF-8 to a 32-bit fixed width
634 * representation as UCS-4, assuming valid UTF-8 input.
635 * This function is roughly twice as fast as g_utf8_to_ucs4()
636 * but does no error checking on the input.
638 * Return value: a pointer to a newly allocated UCS-4 string.
639 * This value must be freed with g_free()
642 g_utf8_to_ucs4_fast (const gchar *str,
651 g_return_val_if_fail (str != NULL, NULL);
659 p = g_utf8_next_char (p);
665 while (p < str + len && *p)
667 p = g_utf8_next_char (p);
672 result = g_new (gunichar, n_chars + 1);
675 for (i=0; i < n_chars; i++)
677 gunichar wc = ((unsigned char *)p)[0];
712 for (j = 1; j < charlen; j++)
715 wc |= ((unsigned char *)p)[j] & 0x3f;
732 * @str: a UTF-8 encoded string
733 * @len: the maximum length of @str to use. If < 0, then
734 * the string is %NULL terminated.
735 * @items_read: location to store number of bytes read, or %NULL.
736 * If %NULL, then %G_CONVERT_ERROR_PARTIAL_INPUT will be
737 * returned in case @str contains a trailing partial
738 * character. If an error occurs then the index of the
739 * invalid input is stored here.
740 * @items_written: location to store number of characters written or %NULL.
741 * The value here stored does not include the trailing 0
743 * @error: location to store the error occuring, or %NULL to ignore
744 * errors. Any of the errors in #GConvertError other than
745 * %G_CONVERT_ERROR_NO_CONVERSION may occur.
747 * Convert a string from UTF-8 to a 32-bit fixed width
748 * representation as UCS-4. A trailing 0 will be added to the
749 * string after the converted text.
751 * Return value: a pointer to a newly allocated UCS-4 string.
752 * This value must be freed with g_free(). If an
753 * error occurs, %NULL will be returned and
757 g_utf8_to_ucs4 (const gchar *str,
763 gunichar *result = NULL;
769 while ((len < 0 || str + len - in > 0) && *in)
771 gunichar wc = g_utf8_get_char_extended (in, str + len - in);
774 if (wc == (gunichar)-2)
779 g_set_error (error, G_CONVERT_ERROR, G_CONVERT_ERROR_PARTIAL_INPUT,
780 _("Partial character sequence at end of input"));
783 g_set_error (error, G_CONVERT_ERROR, G_CONVERT_ERROR_ILLEGAL_SEQUENCE,
784 _("Invalid byte sequence in conversion input"));
791 in = g_utf8_next_char (in);
794 result = g_new (gunichar, n_chars + 1);
797 for (i=0; i < n_chars; i++)
799 result[i] = g_utf8_get_char (in);
800 in = g_utf8_next_char (in);
805 *items_written = n_chars;
809 *items_read = in - str;
816 * @str: a UCS-4 encoded string
817 * @len: the maximum length of @str to use. If < 0, then
818 * the string is %NULL terminated.
819 * @items_read: location to store number of characters read read, or %NULL.
820 * @items_written: location to store number of bytes written or %NULL.
821 * The value here stored does not include the trailing 0
823 * @error: location to store the error occuring, or %NULL to ignore
824 * errors. Any of the errors in #GConvertError other than
825 * %G_CONVERT_ERROR_NO_CONVERSION may occur.
827 * Convert a string from a 32-bit fixed width representation as UCS-4.
828 * to UTF-8. The result will be terminated with a 0 byte.
830 * Return value: a pointer to a newly allocated UTF-8 string.
831 * This value must be freed with g_free(). If an
832 * error occurs, %NULL will be returned and
836 g_ucs4_to_utf8 (const gunichar *str,
843 gchar *result = NULL;
848 for (i = 0; len < 0 || i < len ; i++)
853 if (str[i] >= 0x80000000)
858 g_set_error (error, G_CONVERT_ERROR, G_CONVERT_ERROR_ILLEGAL_SEQUENCE,
859 _("Character out of range for UTF-8"));
863 result_length += UTF8_LENGTH (str[i]);
866 result = g_malloc (result_length + 1);
870 while (p < result + result_length)
871 p += g_unichar_to_utf8 (str[i++], p);
876 *items_written = p - result;
885 #define SURROGATE_VALUE(h,l) (((h) - 0xd800) * 0x400 + (l) - 0xdc00 + 0x10000)
889 * @str: a UTF-16 encoded string
890 * @len: the maximum length of @str to use. If < 0, then
891 * the string is terminated with a 0 character.
892 * @items_read: location to store number of words read, or %NULL.
893 * If %NULL, then %G_CONVERT_ERROR_PARTIAL_INPUT will be
894 * returned in case @str contains a trailing partial
895 * character. If an error occurs then the index of the
896 * invalid input is stored here.
897 * @items_written: location to store number of bytes written, or %NULL.
898 * The value stored here does not include the trailing
900 * @error: location to store the error occuring, or %NULL to ignore
901 * errors. Any of the errors in #GConvertError other than
902 * %G_CONVERT_ERROR_NO_CONVERSION may occur.
904 * Convert a string from UTF-16 to UTF-8. The result will be
905 * terminated with a 0 byte.
907 * Return value: a pointer to a newly allocated UTF-8 string.
908 * This value must be freed with g_free(). If an
909 * error occurs, %NULL will be returned and
913 g_utf16_to_utf8 (const gunichar2 *str,
919 /* This function and g_utf16_to_ucs4 are almost exactly identical - The lines that differ
924 gchar *result = NULL;
926 gunichar high_surrogate;
928 g_return_val_if_fail (str != 0, NULL);
933 while ((len < 0 || in - str < len) && *in)
938 if (c >= 0xdc00 && c < 0xe000) /* low surrogate */
942 wc = SURROGATE_VALUE (high_surrogate, c);
947 g_set_error (error, G_CONVERT_ERROR, G_CONVERT_ERROR_ILLEGAL_SEQUENCE,
948 _("Invalid sequence in conversion input"));
956 g_set_error (error, G_CONVERT_ERROR, G_CONVERT_ERROR_ILLEGAL_SEQUENCE,
957 _("Invalid sequence in conversion input"));
961 if (c >= 0xd800 && c < 0xdc00) /* high surrogate */
970 /********** DIFFERENT for UTF8/UCS4 **********/
971 n_bytes += UTF8_LENGTH (wc);
977 if (high_surrogate && !items_read)
979 g_set_error (error, G_CONVERT_ERROR, G_CONVERT_ERROR_PARTIAL_INPUT,
980 _("Partial character sequence at end of input"));
984 /* At this point, everything is valid, and we just need to convert
986 /********** DIFFERENT for UTF8/UCS4 **********/
987 result = g_malloc (n_bytes + 1);
992 while (out < result + n_bytes)
997 if (c >= 0xdc00 && c < 0xe000) /* low surrogate */
999 wc = SURROGATE_VALUE (high_surrogate, c);
1002 else if (c >= 0xd800 && c < 0xdc00) /* high surrogate */
1010 /********** DIFFERENT for UTF8/UCS4 **********/
1011 out += g_unichar_to_utf8 (wc, out);
1017 /********** DIFFERENT for UTF8/UCS4 **********/
1021 /********** DIFFERENT for UTF8/UCS4 **********/
1022 *items_written = out - result;
1026 *items_read = in - str;
1033 * @str: a UTF-16 encoded string
1034 * @len: the maximum length of @str to use. If < 0, then
1035 * the string is terminated with a 0 character.
1036 * @items_read: location to store number of words read, or %NULL.
1037 * If %NULL, then %G_CONVERT_ERROR_PARTIAL_INPUT will be
1038 * returned in case @str contains a trailing partial
1039 * character. If an error occurs then the index of the
1040 * invalid input is stored here.
1041 * @items_written: location to store number of characters written, or %NULL.
1042 * The value stored here does not include the trailing
1044 * @error: location to store the error occuring, or %NULL to ignore
1045 * errors. Any of the errors in #GConvertError other than
1046 * %G_CONVERT_ERROR_NO_CONVERSION may occur.
1048 * Convert a string from UTF-16 to UCS-4. The result will be
1049 * terminated with a 0 character.
1051 * Return value: a pointer to a newly allocated UCS-4 string.
1052 * This value must be freed with g_free(). If an
1053 * error occurs, %NULL will be returned and
1057 g_utf16_to_ucs4 (const gunichar2 *str,
1060 gint *items_written,
1063 const gunichar2 *in;
1065 gchar *result = NULL;
1067 gunichar high_surrogate;
1069 g_return_val_if_fail (str != 0, NULL);
1074 while ((len < 0 || in - str < len) && *in)
1079 if (c >= 0xdc00 && c < 0xe000) /* low surrogate */
1083 wc = SURROGATE_VALUE (high_surrogate, c);
1088 g_set_error (error, G_CONVERT_ERROR, G_CONVERT_ERROR_ILLEGAL_SEQUENCE,
1089 _("Invalid sequence in conversion input"));
1097 g_set_error (error, G_CONVERT_ERROR, G_CONVERT_ERROR_ILLEGAL_SEQUENCE,
1098 _("Invalid sequence in conversion input"));
1102 if (c >= 0xd800 && c < 0xdc00) /* high surrogate */
1111 /********** DIFFERENT for UTF8/UCS4 **********/
1112 n_bytes += sizeof (gunichar);
1118 if (high_surrogate && !items_read)
1120 g_set_error (error, G_CONVERT_ERROR, G_CONVERT_ERROR_PARTIAL_INPUT,
1121 _("Partial character sequence at end of input"));
1125 /* At this point, everything is valid, and we just need to convert
1127 /********** DIFFERENT for UTF8/UCS4 **********/
1128 result = g_malloc (n_bytes + 4);
1133 while (out < result + n_bytes)
1138 if (c >= 0xdc00 && c < 0xe000) /* low surrogate */
1140 wc = SURROGATE_VALUE (high_surrogate, c);
1143 else if (c >= 0xd800 && c < 0xdc00) /* high surrogate */
1151 /********** DIFFERENT for UTF8/UCS4 **********/
1152 *(gunichar *)out = wc;
1153 out += sizeof (gunichar);
1159 /********** DIFFERENT for UTF8/UCS4 **********/
1160 *(gunichar *)out = 0;
1163 /********** DIFFERENT for UTF8/UCS4 **********/
1164 *items_written = (out - result) / sizeof (gunichar);
1168 *items_read = in - str;
1170 return (gunichar *)result;
1175 * @str: a UTF-8 encoded string
1176 * @len: the maximum length of @str to use. If < 0, then
1177 * the string is %NULL terminated.
1179 * @items_read: location to store number of bytes read, or %NULL.
1180 * If %NULL, then %G_CONVERT_ERROR_PARTIAL_INPUT will be
1181 * returned in case @str contains a trailing partial
1182 * character. If an error occurs then the index of the
1183 * invalid input is stored here.
1184 * @items_written: location to store number of words written, or %NULL.
1185 * The value stored here does not include the trailing
1187 * @error: location to store the error occuring, or %NULL to ignore
1188 * errors. Any of the errors in #GConvertError other than
1189 * %G_CONVERT_ERROR_NO_CONVERSION may occur.
1191 * Convert a string from UTF-8 to UTF-16. A 0 word will be
1192 * added to the result after the converted text.
1194 * Return value: a pointer to a newly allocated UTF-16 string.
1195 * This value must be freed with g_free(). If an
1196 * error occurs, %NULL will be returned and
1200 g_utf8_to_utf16 (const gchar *str,
1203 gint *items_written,
1206 gunichar2 *result = NULL;
1211 g_return_val_if_fail (str != NULL, NULL);
1215 while ((len < 0 || str + len - in > 0) && *in)
1217 gunichar wc = g_utf8_get_char_extended (in, str + len - in);
1218 if (wc & 0x80000000)
1220 if (wc == (gunichar)-2)
1225 g_set_error (error, G_CONVERT_ERROR, G_CONVERT_ERROR_PARTIAL_INPUT,
1226 _("Partial character sequence at end of input"));
1229 g_set_error (error, G_CONVERT_ERROR, G_CONVERT_ERROR_ILLEGAL_SEQUENCE,
1230 _("Invalid byte sequence in conversion input"));
1237 else if (wc < 0xe000)
1239 g_set_error (error, G_CONVERT_ERROR, G_CONVERT_ERROR_ILLEGAL_SEQUENCE,
1240 _("Invalid sequence in conversion input"));
1244 else if (wc < 0x10000)
1246 else if (wc < 0x110000)
1250 g_set_error (error, G_CONVERT_ERROR, G_CONVERT_ERROR_ILLEGAL_SEQUENCE,
1251 _("Character out of range for UTF-16"));
1256 in = g_utf8_next_char (in);
1259 result = g_new (gunichar2, n16 + 1);
1262 for (i = 0; i < n16;)
1264 gunichar wc = g_utf8_get_char (in);
1272 result[i++] = (wc - 0x10000) / 0x400 + 0xd800;
1273 result[i++] = (wc - 0x10000) % 0x400 + 0xdc00;
1276 in = g_utf8_next_char (in);
1282 *items_written = n16;
1286 *items_read = in - str;
1293 * @str: a UCS-4 encoded string
1294 * @len: the maximum length of @str to use. If < 0, then
1295 * the string is terminated with a zero character.
1296 * @items_read: location to store number of bytes read, or %NULL.
1297 * If an error occurs then the index of the invalid input
1299 * @items_written: location to store number of words written, or %NULL.
1300 * The value stored here does not include the trailing
1302 * @error: location to store the error occuring, or %NULL to ignore
1303 * errors. Any of the errors in #GConvertError other than
1304 * %G_CONVERT_ERROR_NO_CONVERSION may occur.
1306 * Convert a string from UCS-4 to UTF-16. A 0 word will be
1307 * added to the result after the converted text.
1309 * Return value: a pointer to a newly allocated UTF-16 string.
1310 * This value must be freed with g_free(). If an
1311 * error occurs, %NULL will be returned and
1315 g_ucs4_to_utf16 (const gunichar *str,
1318 gint *items_written,
1321 gunichar2 *result = NULL;
1327 while ((len < 0 || i < len) && str[i])
1329 gunichar wc = str[i];
1333 else if (wc < 0xe000)
1335 g_set_error (error, G_CONVERT_ERROR, G_CONVERT_ERROR_ILLEGAL_SEQUENCE,
1336 _("Invalid sequence in conversion input"));
1340 else if (wc < 0x10000)
1342 else if (wc < 0x110000)
1346 g_set_error (error, G_CONVERT_ERROR, G_CONVERT_ERROR_ILLEGAL_SEQUENCE,
1347 _("Character out of range for UTF-16"));
1355 result = g_new (gunichar2, n16 + 1);
1357 for (i = 0, j = 0; j < n16; i++)
1359 gunichar wc = str[i];
1367 result[j++] = (wc - 0x10000) / 0x400 + 0xd800;
1368 result[j++] = (wc - 0x10000) % 0x400 + 0xdc00;
1374 *items_written = n16;
1385 * @str: a pointer to character data
1386 * @max_len: max bytes to validate, or -1 to go until nul
1387 * @end: return location for end of valid data
1389 * Validates UTF-8 encoded text. @str is the text to validate;
1390 * if @str is nul-terminated, then @max_len can be -1, otherwise
1391 * @max_len should be the number of bytes to validate.
1392 * If @end is non-%NULL, then the end of the valid range
1393 * will be stored there (i.e. the address of the first invalid byte
1394 * if some bytes were invalid, or the end of the text being validated
1397 * Returns TRUE if all of @str was valid. Many GLib and GTK+
1398 * routines <emphasis>require</emphasis> valid UTF8 as input;
1399 * so data read from a file or the network should be checked
1400 * with g_utf8_validate() before doing anything else with it.
1402 * Return value: TRUE if the text was valid UTF-8.
1405 g_utf8_validate (const gchar *str,
1412 g_return_val_if_fail (str != NULL, FALSE);
1419 while ((max_len < 0 || (p - str) < max_len) && *p)
1421 int i, mask = 0, len;
1423 unsigned char c = (unsigned char) *p;
1425 UTF8_COMPUTE (c, mask, len);
1430 /* check that the expected number of bytes exists in str */
1432 ((max_len - (p - str)) < len))
1435 UTF8_GET (result, p, i, mask, len);
1437 if (UTF8_LENGTH (result) != len) /* Check for overlong UTF-8 */
1440 if (result == (gunichar)-1)
1443 if (!UNICODE_VALID (result))
1452 /* See that we covered the entire length if a length was
1453 * passed in, or that we ended on a nul if not
1456 p != (str + max_len))
1458 else if (max_len < 0 &&
1466 * g_unichar_validate:
1467 * @ch: a Unicode character
1469 * Checks whether @ch is a valid Unicode character. Some possible
1470 * integer values of @ch will not be valid. 0 is considered a valid
1471 * character, though it's normally a string terminator.
1473 * Return value: %TRUE if @ch is a valid Unicode character
1476 g_unichar_validate (gunichar ch)
1478 return UNICODE_VALID (ch);