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.
247 * Return value: the resulting character or (gunichar)-1 if @p does
248 * not point to a valid UTF-8 encoded unicode character
251 g_utf8_get_char (const gchar *p)
253 int i, mask = 0, len;
255 unsigned char c = (unsigned char) *p;
257 UTF8_COMPUTE (c, mask, len);
260 UTF8_GET (result, p, i, mask, len);
266 * g_utf8_offset_to_pointer:
267 * @str: a UTF-8 encoded string
268 * @offset: a character offset within the string.
270 * Converts from an integer character offset to a pointer to a position
273 * Return value: the resulting pointer
276 g_utf8_offset_to_pointer (const gchar *str,
279 const gchar *s = str;
281 s = g_utf8_next_char (s);
287 * g_utf8_pointer_to_offset:
288 * @str: a UTF-8 encoded string
289 * @pos: a pointer to a position within @str
291 * Converts from a pointer to position within a string to a integer
294 * Return value: the resulting character offset
297 g_utf8_pointer_to_offset (const gchar *str,
300 const gchar *s = str;
305 s = g_utf8_next_char (s);
315 * @dest: buffer to fill with characters from @src
317 * @n: character count
319 * Like the standard C strncpy() function, but copies a given number
320 * of characters instead of a given number of bytes. The @src string
321 * must be valid UTF-8 encoded text. (Use g_utf8_validate() on all
322 * text before trying to use UTF-8 utility functions with it.)
324 * Return value: @dest
327 g_utf8_strncpy (gchar *dest, const gchar *src, size_t n)
329 const gchar *s = src;
332 s = g_utf8_next_char(s);
335 strncpy(dest, src, s - src);
341 g_utf8_get_charset_internal (char **a)
343 char *charset = getenv("CHARSET");
345 if (charset && a && ! *a)
348 if (charset && strstr (charset, "UTF-8"))
352 charset = nl_langinfo(CODESET);
357 if (strcmp (charset, "UTF-8") == 0)
362 #if 0 /* #ifdef _NL_CTYPE_CODESET_NAME */
363 charset = nl_langinfo (_NL_CTYPE_CODESET_NAME);
368 if (strcmp (charset, "UTF-8") == 0)
373 #ifdef G_PLATFORM_WIN32
376 static char codepage[10];
378 sprintf (codepage, "CP%d", GetACP ());
380 /* What about codepage 1200? Is that UTF-8? */
388 /* Assume this for compatibility at present. */
392 static int utf8_locale_cache = -1;
393 static char *utf8_charset_cache = NULL;
397 * @charset: return location for character set name
399 * Obtains the character set for the current locale; you might use
400 * this character set as an argument to g_convert(), to convert from
401 * the current locale's encoding to some other encoding. (Frequently
402 * g_locale_to_utf8() and g_locale_from_utf8() are nice shortcuts,
405 * The return value is %TRUE if the locale's encoding is UTF-8, in that
406 * case you can perhaps avoid calling g_convert().
408 * The string returned in @charset is not allocated, and should not be
411 * Return value: %TRUE if the returned charset is UTF-8
414 g_get_charset (char **charset)
416 if (utf8_locale_cache != -1)
419 *charset = utf8_charset_cache;
420 return utf8_locale_cache;
422 utf8_locale_cache = g_utf8_get_charset_internal (&utf8_charset_cache);
424 *charset = utf8_charset_cache;
425 return utf8_locale_cache;
432 * @c: a ISO10646 character code
433 * @outbuf: output buffer, must have at least 6 bytes of space.
434 * If %NULL, the length will be computed and returned
435 * and nothing will be written to @out.
437 * Convert a single character to utf8
439 * Return value: number of bytes written
442 g_unichar_to_utf8 (gunichar c, gchar *outbuf)
458 else if (c < 0x10000)
463 else if (c < 0x200000)
468 else if (c < 0x4000000)
481 for (i = len - 1; i > 0; --i)
483 outbuf[i] = (c & 0x3f) | 0x80;
486 outbuf[0] = c | first;
494 * @p: a nul-terminated utf-8 string
495 * @c: a iso-10646 character/
497 * Find the leftmost occurence of the given iso-10646 character
500 * Return value: NULL if the string does not contain the character, otherwise, a
501 * a pointer to the start of the leftmost of the character in the string.
504 g_utf8_strchr (const char *p, gunichar c)
508 gint len = g_unichar_to_utf8 (c, ch);
511 return strstr(p, ch);
517 * @p: a nul-terminated utf-8 string
518 * @c: a iso-10646 character/
520 * Find the rightmost occurence of the given iso-10646 character
523 * Return value: NULL if the string does not contain the character, otherwise, a
524 * a pointer to the start of the rightmost of the character in the string.
527 /* This is ifdefed out atm as there is no strrstr function in libc.
530 unicode_strrchr (const char *p, gunichar c)
534 len = g_unichar_to_utf8 (c, ch);
537 return strrstr(p, ch);
542 /* Like g_utf8_get_char, but take a maximum length
543 * and return (gunichar)-2 on incomplete trailing character
545 static inline gunichar
546 g_utf8_get_char_extended (const gchar *p, int max_len)
549 gunichar wc = (guchar) *p;
591 if (max_len >= 0 && len > max_len)
593 for (i = 1; i < max_len; i++)
595 if ((((guchar *)p)[i] & 0xc0) != 0x80)
601 for (i = 1; i < len; ++i)
603 gunichar ch = ((guchar *)p)[i];
605 if ((ch & 0xc0) != 0x80)
617 if (UTF8_LENGTH(wc) != len)
624 * g_utf8_to_ucs4_fast:
625 * @str: a UTF-8 encoded string
626 * @len: the maximum length of @str to use. If < 0, then
627 * the string is %NULL terminated.
628 * @items_written: location to store the number of characters in the
631 * Convert a string from UTF-8 to a 32-bit fixed width
632 * representation as UCS-4, assuming valid UTF-8 input.
633 * This function is roughly twice as fast as g_utf8_to_ucs4()
634 * but does no error checking on the input.
636 * Return value: a pointer to a newly allocated UCS-4 string.
637 * This value must be freed with g_free()
640 g_utf8_to_ucs4_fast (const gchar *str,
649 g_return_val_if_fail (str != NULL, NULL);
657 p = g_utf8_next_char (p);
663 while (p < str + len && *p)
665 p = g_utf8_next_char (p);
670 result = g_new (gunichar, n_chars + 1);
673 for (i=0; i < n_chars; i++)
675 gunichar wc = ((unsigned char *)p)[0];
710 for (j = 1; j < charlen; j++)
713 wc |= ((unsigned char *)p)[j] & 0x3f;
730 * @str: a UTF-8 encoded string
731 * @len: the maximum length of @str to use. If < 0, then
732 * the string is %NULL terminated.
733 * @items_read: location to store number of bytes read, or %NULL.
734 * If %NULL, then %G_CONVERT_ERROR_PARTIAL_INPUT will be
735 * returned in case @str contains a trailing partial
736 * character. If an error occurs then the index of the
737 * invalid input is stored here.
738 * @items_written: location to store number of characters written or %NULL.
739 * The value here stored does not include the trailing 0
741 * @error: location to store the error occuring, or %NULL to ignore
742 * errors. Any of the errors in #GConvertError other than
743 * %G_CONVERT_ERROR_NO_CONVERSION may occur.
745 * Convert a string from UTF-8 to a 32-bit fixed width
746 * representation as UCS-4. A trailing 0 will be added to the
747 * string after the converted text.
749 * Return value: a pointer to a newly allocated UCS-4 string.
750 * This value must be freed with g_free(). If an
751 * error occurs, %NULL will be returned and
755 g_utf8_to_ucs4 (const gchar *str,
761 gunichar *result = NULL;
767 while ((len < 0 || str + len - in > 0) && *in)
769 gunichar wc = g_utf8_get_char_extended (in, str + len - in);
772 if (wc == (gunichar)-2)
777 g_set_error (error, G_CONVERT_ERROR, G_CONVERT_ERROR_PARTIAL_INPUT,
778 _("Partial character sequence at end of input"));
781 g_set_error (error, G_CONVERT_ERROR, G_CONVERT_ERROR_ILLEGAL_SEQUENCE,
782 _("Invalid byte sequence in conversion input"));
789 in = g_utf8_next_char (in);
792 result = g_new (gunichar, n_chars + 1);
795 for (i=0; i < n_chars; i++)
797 result[i] = g_utf8_get_char (in);
798 in = g_utf8_next_char (in);
803 *items_written = n_chars;
807 *items_read = in - str;
814 * @str: a UCS-4 encoded string
815 * @len: the maximum length of @str to use. If < 0, then
816 * the string is %NULL terminated.
817 * @items_read: location to store number of characters read read, or %NULL.
818 * @items_written: location to store number of bytes written or %NULL.
819 * The value here stored does not include the trailing 0
821 * @error: location to store the error occuring, or %NULL to ignore
822 * errors. Any of the errors in #GConvertError other than
823 * %G_CONVERT_ERROR_NO_CONVERSION may occur.
825 * Convert a string from a 32-bit fixed width representation as UCS-4.
826 * to UTF-8. The result will be terminated with a 0 byte.
828 * Return value: a pointer to a newly allocated UTF-8 string.
829 * This value must be freed with g_free(). If an
830 * error occurs, %NULL will be returned and
834 g_ucs4_to_utf8 (const gunichar *str,
841 gchar *result = NULL;
846 for (i = 0; len < 0 || i < len ; i++)
851 if (str[i] >= 0x80000000)
856 g_set_error (error, G_CONVERT_ERROR, G_CONVERT_ERROR_ILLEGAL_SEQUENCE,
857 _("Character out of range for UTF-8"));
861 result_length += UTF8_LENGTH (str[i]);
864 result = g_malloc (result_length + 1);
868 while (p < result + result_length)
869 p += g_unichar_to_utf8 (str[i++], p);
874 *items_written = p - result;
883 #define SURROGATE_VALUE(h,l) (((h) - 0xd800) * 0x400 + (l) - 0xdc00 + 0x10000)
887 * @str: a UTF-16 encoded string
888 * @len: the maximum length of @str to use. If < 0, then
889 * the string is terminated with a 0 character.
890 * @items_read: location to store number of words read, or %NULL.
891 * If %NULL, then %G_CONVERT_ERROR_PARTIAL_INPUT will be
892 * returned in case @str contains a trailing partial
893 * character. If an error occurs then the index of the
894 * invalid input is stored here.
895 * @items_written: location to store number of bytes written, or %NULL.
896 * The value stored here does not include the trailing
898 * @error: location to store the error occuring, or %NULL to ignore
899 * errors. Any of the errors in #GConvertError other than
900 * %G_CONVERT_ERROR_NO_CONVERSION may occur.
902 * Convert a string from UTF-16 to UTF-8. The result will be
903 * terminated with a 0 byte.
905 * Return value: a pointer to a newly allocated UTF-8 string.
906 * This value must be freed with g_free(). If an
907 * error occurs, %NULL will be returned and
911 g_utf16_to_utf8 (const gunichar2 *str,
917 /* This function and g_utf16_to_ucs4 are almost exactly identical - The lines that differ
922 gchar *result = NULL;
924 gunichar high_surrogate;
926 g_return_val_if_fail (str != 0, NULL);
931 while ((len < 0 || in - str < len) && *in)
936 if (c >= 0xdc00 && c < 0xe000) /* low surrogate */
940 wc = SURROGATE_VALUE (high_surrogate, c);
945 g_set_error (error, G_CONVERT_ERROR, G_CONVERT_ERROR_ILLEGAL_SEQUENCE,
946 _("Invalid sequence in conversion input"));
954 g_set_error (error, G_CONVERT_ERROR, G_CONVERT_ERROR_ILLEGAL_SEQUENCE,
955 _("Invalid sequence in conversion input"));
959 if (c >= 0xd800 && c < 0xdc00) /* high surrogate */
968 /********** DIFFERENT for UTF8/UCS4 **********/
969 n_bytes += UTF8_LENGTH (wc);
975 if (high_surrogate && !items_read)
977 g_set_error (error, G_CONVERT_ERROR, G_CONVERT_ERROR_PARTIAL_INPUT,
978 _("Partial character sequence at end of input"));
982 /* At this point, everything is valid, and we just need to convert
984 /********** DIFFERENT for UTF8/UCS4 **********/
985 result = g_malloc (n_bytes + 1);
990 while (out < result + n_bytes)
995 if (c >= 0xdc00 && c < 0xe000) /* low surrogate */
997 wc = SURROGATE_VALUE (high_surrogate, c);
1000 else if (c >= 0xd800 && c < 0xdc00) /* high surrogate */
1008 /********** DIFFERENT for UTF8/UCS4 **********/
1009 out += g_unichar_to_utf8 (wc, out);
1015 /********** DIFFERENT for UTF8/UCS4 **********/
1019 /********** DIFFERENT for UTF8/UCS4 **********/
1020 *items_written = out - result;
1024 *items_read = in - str;
1031 * @str: a UTF-16 encoded string
1032 * @len: the maximum length of @str to use. If < 0, then
1033 * the string is terminated with a 0 character.
1034 * @items_read: location to store number of words read, or %NULL.
1035 * If %NULL, then %G_CONVERT_ERROR_PARTIAL_INPUT will be
1036 * returned in case @str contains a trailing partial
1037 * character. If an error occurs then the index of the
1038 * invalid input is stored here.
1039 * @items_written: location to store number of characters written, or %NULL.
1040 * The value stored here does not include the trailing
1042 * @error: location to store the error occuring, or %NULL to ignore
1043 * errors. Any of the errors in #GConvertError other than
1044 * %G_CONVERT_ERROR_NO_CONVERSION may occur.
1046 * Convert a string from UTF-16 to UCS-4. The result will be
1047 * terminated with a 0 character.
1049 * Return value: a pointer to a newly allocated UCS-4 string.
1050 * This value must be freed with g_free(). If an
1051 * error occurs, %NULL will be returned and
1055 g_utf16_to_ucs4 (const gunichar2 *str,
1058 gint *items_written,
1061 const gunichar2 *in;
1063 gchar *result = NULL;
1065 gunichar high_surrogate;
1067 g_return_val_if_fail (str != 0, NULL);
1072 while ((len < 0 || in - str < len) && *in)
1077 if (c >= 0xdc00 && c < 0xe000) /* low surrogate */
1081 wc = SURROGATE_VALUE (high_surrogate, c);
1086 g_set_error (error, G_CONVERT_ERROR, G_CONVERT_ERROR_ILLEGAL_SEQUENCE,
1087 _("Invalid sequence in conversion input"));
1095 g_set_error (error, G_CONVERT_ERROR, G_CONVERT_ERROR_ILLEGAL_SEQUENCE,
1096 _("Invalid sequence in conversion input"));
1100 if (c >= 0xd800 && c < 0xdc00) /* high surrogate */
1109 /********** DIFFERENT for UTF8/UCS4 **********/
1110 n_bytes += sizeof (gunichar);
1116 if (high_surrogate && !items_read)
1118 g_set_error (error, G_CONVERT_ERROR, G_CONVERT_ERROR_PARTIAL_INPUT,
1119 _("Partial character sequence at end of input"));
1123 /* At this point, everything is valid, and we just need to convert
1125 /********** DIFFERENT for UTF8/UCS4 **********/
1126 result = g_malloc (n_bytes + 4);
1131 while (out < result + n_bytes)
1136 if (c >= 0xdc00 && c < 0xe000) /* low surrogate */
1138 wc = SURROGATE_VALUE (high_surrogate, c);
1141 else if (c >= 0xd800 && c < 0xdc00) /* high surrogate */
1149 /********** DIFFERENT for UTF8/UCS4 **********/
1150 *(gunichar *)out = wc;
1151 out += sizeof (gunichar);
1157 /********** DIFFERENT for UTF8/UCS4 **********/
1158 *(gunichar *)out = 0;
1161 /********** DIFFERENT for UTF8/UCS4 **********/
1162 *items_written = (out - result) / sizeof (gunichar);
1166 *items_read = in - str;
1168 return (gunichar *)result;
1173 * @str: a UTF-8 encoded string
1174 * @len: the maximum length of @str to use. If < 0, then
1175 * the string is %NULL terminated.
1177 * @items_read: location to store number of bytes read, or %NULL.
1178 * If %NULL, then %G_CONVERT_ERROR_PARTIAL_INPUT will be
1179 * returned in case @str contains a trailing partial
1180 * character. If an error occurs then the index of the
1181 * invalid input is stored here.
1182 * @items_written: location to store number of words written, or %NULL.
1183 * The value stored here does not include the trailing
1185 * @error: location to store the error occuring, or %NULL to ignore
1186 * errors. Any of the errors in #GConvertError other than
1187 * %G_CONVERT_ERROR_NO_CONVERSION may occur.
1189 * Convert a string from UTF-8 to UTF-16. A 0 word will be
1190 * added to the result after the converted text.
1192 * Return value: a pointer to a newly allocated UTF-16 string.
1193 * This value must be freed with g_free(). If an
1194 * error occurs, %NULL will be returned and
1198 g_utf8_to_utf16 (const gchar *str,
1201 gint *items_written,
1204 gunichar2 *result = NULL;
1209 g_return_val_if_fail (str != NULL, NULL);
1213 while ((len < 0 || str + len - in > 0) && *in)
1215 gunichar wc = g_utf8_get_char_extended (in, str + len - in);
1216 if (wc & 0x80000000)
1218 if (wc == (gunichar)-2)
1223 g_set_error (error, G_CONVERT_ERROR, G_CONVERT_ERROR_PARTIAL_INPUT,
1224 _("Partial character sequence at end of input"));
1227 g_set_error (error, G_CONVERT_ERROR, G_CONVERT_ERROR_ILLEGAL_SEQUENCE,
1228 _("Invalid byte sequence in conversion input"));
1235 else if (wc < 0xe000)
1237 g_set_error (error, G_CONVERT_ERROR, G_CONVERT_ERROR_ILLEGAL_SEQUENCE,
1238 _("Invalid sequence in conversion input"));
1242 else if (wc < 0x10000)
1244 else if (wc < 0x110000)
1248 g_set_error (error, G_CONVERT_ERROR, G_CONVERT_ERROR_ILLEGAL_SEQUENCE,
1249 _("Character out of range for UTF-16"));
1254 in = g_utf8_next_char (in);
1257 result = g_new (gunichar2, n16 + 1);
1260 for (i = 0; i < n16;)
1262 gunichar wc = g_utf8_get_char (in);
1270 result[i++] = (wc - 0x10000) / 0x400 + 0xd800;
1271 result[i++] = (wc - 0x10000) % 0x400 + 0xdc00;
1274 in = g_utf8_next_char (in);
1280 *items_written = n16;
1284 *items_read = in - str;
1291 * @str: a UCS-4 encoded string
1292 * @len: the maximum length of @str to use. If < 0, then
1293 * the string is terminated with a zero character.
1294 * @items_read: location to store number of bytes read, or %NULL.
1295 * If an error occurs then the index of the invalid input
1297 * @items_written: location to store number of words written, or %NULL.
1298 * The value stored here does not include the trailing
1300 * @error: location to store the error occuring, or %NULL to ignore
1301 * errors. Any of the errors in #GConvertError other than
1302 * %G_CONVERT_ERROR_NO_CONVERSION may occur.
1304 * Convert a string from UCS-4 to UTF-16. A 0 word will be
1305 * added to the result after the converted text.
1307 * Return value: a pointer to a newly allocated UTF-16 string.
1308 * This value must be freed with g_free(). If an
1309 * error occurs, %NULL will be returned and
1313 g_ucs4_to_utf16 (const gunichar *str,
1316 gint *items_written,
1319 gunichar2 *result = NULL;
1325 while ((len < 0 || i < len) && str[i])
1327 gunichar wc = str[i];
1331 else if (wc < 0xe000)
1333 g_set_error (error, G_CONVERT_ERROR, G_CONVERT_ERROR_ILLEGAL_SEQUENCE,
1334 _("Invalid sequence in conversion input"));
1338 else if (wc < 0x10000)
1340 else if (wc < 0x110000)
1344 g_set_error (error, G_CONVERT_ERROR, G_CONVERT_ERROR_ILLEGAL_SEQUENCE,
1345 _("Character out of range for UTF-16"));
1353 result = g_new (gunichar2, n16 + 1);
1355 for (i = 0, j = 0; j < n16; i++)
1357 gunichar wc = str[i];
1365 result[j++] = (wc - 0x10000) / 0x400 + 0xd800;
1366 result[j++] = (wc - 0x10000) % 0x400 + 0xdc00;
1372 *items_written = n16;
1383 * @str: a pointer to character data
1384 * @max_len: max bytes to validate, or -1 to go until nul
1385 * @end: return location for end of valid data
1387 * Validates UTF-8 encoded text. @str is the text to validate;
1388 * if @str is nul-terminated, then @max_len can be -1, otherwise
1389 * @max_len should be the number of bytes to validate.
1390 * If @end is non-NULL, then the end of the valid range
1391 * will be stored there (i.e. the address of the first invalid byte
1392 * if some bytes were invalid, or the end of the text being validated
1395 * Returns TRUE if all of @str was valid. Many GLib and GTK+
1396 * routines <emphasis>require</emphasis> valid UTF8 as input;
1397 * so data read from a file or the network should be checked
1398 * with g_utf8_validate() before doing anything else with it.
1400 * Return value: TRUE if the text was valid UTF-8.
1403 g_utf8_validate (const gchar *str,
1410 g_return_val_if_fail (str != NULL, FALSE);
1417 while ((max_len < 0 || (p - str) < max_len) && *p)
1419 int i, mask = 0, len;
1421 unsigned char c = (unsigned char) *p;
1423 UTF8_COMPUTE (c, mask, len);
1428 /* check that the expected number of bytes exists in str */
1430 ((max_len - (p - str)) < len))
1433 UTF8_GET (result, p, i, mask, len);
1435 if (UTF8_LENGTH (result) != len) /* Check for overlong UTF-8 */
1438 if (result == (gunichar)-1)
1441 if (!UNICODE_VALID (result))
1450 /* See that we covered the entire length if a length was
1451 * passed in, or that we ended on a nul if not
1454 p != (str + max_len))
1456 else if (max_len < 0 &&
1464 * g_unichar_validate:
1465 * @ch: a Unicode character
1467 * Checks whether @ch is a valid Unicode character. Some possible
1468 * integer values of @ch will not be valid. 0 is considered a valid
1469 * character, though it's normally a string terminator.
1471 * Return value: %TRUE if @ch is a valid Unicode character
1474 g_unichar_validate (gunichar ch)
1476 return UNICODE_VALID (ch);