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
39 #include "libcharset/libcharset.h"
43 #define UTF8_COMPUTE(Char, Mask, Len) \
49 else if ((Char & 0xe0) == 0xc0) \
54 else if ((Char & 0xf0) == 0xe0) \
59 else if ((Char & 0xf8) == 0xf0) \
64 else if ((Char & 0xfc) == 0xf8) \
69 else if ((Char & 0xfe) == 0xfc) \
77 #define UTF8_LENGTH(Char) \
78 ((Char) < 0x80 ? 1 : \
79 ((Char) < 0x800 ? 2 : \
80 ((Char) < 0x10000 ? 3 : \
81 ((Char) < 0x200000 ? 4 : \
82 ((Char) < 0x4000000 ? 5 : 6)))))
85 #define UTF8_GET(Result, Chars, Count, Mask, Len) \
86 (Result) = (Chars)[0] & (Mask); \
87 for ((Count) = 1; (Count) < (Len); ++(Count)) \
89 if (((Chars)[(Count)] & 0xc0) != 0x80) \
95 (Result) |= ((Chars)[(Count)] & 0x3f); \
98 #define UNICODE_VALID(Char) \
99 ((Char) < 0x110000 && \
100 ((Char) < 0xD800 || (Char) >= 0xE000) && \
101 (Char) != 0xFFFE && (Char) != 0xFFFF)
104 static const gchar utf8_skip_data[256] = {
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 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,
110 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,
111 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,
112 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,1,1
115 const gchar * const g_utf8_skip = utf8_skip_data;
118 * g_utf8_find_prev_char:
119 * @str: pointer to the beginning of a UTF-8 encoded string
120 * @p: pointer to some position within @str
122 * Given a position @p with a UTF-8 encoded string @str, find the start
123 * of the previous UTF-8 character starting before @p. Returns %NULL if no
124 * UTF-8 characters are present in @p before @str.
126 * @p does not have to be at the beginning of a UTF-8 character. No check
127 * is made to see if the character found is actually valid other than
128 * it starts with an appropriate byte.
130 * Return value: a pointer to the found character or %NULL.
133 g_utf8_find_prev_char (const char *str,
136 for (--p; p >= str; --p)
138 if ((*p & 0xc0) != 0x80)
145 * g_utf8_find_next_char:
146 * @p: a pointer to a position within a UTF-8 encoded string
147 * @end: a pointer to the end of the string, or %NULL to indicate
148 * that the string is nul-terminated, in which case
149 * the returned value will be
151 * Finds the start of the next UTF-8 character in the string after @p.
153 * @p does not have to be at the beginning of a UTF-8 character. No check
154 * is made to see if the character found is actually valid other than
155 * it starts with an appropriate byte.
157 * Return value: a pointer to the found character or %NULL
160 g_utf8_find_next_char (const gchar *p,
166 for (++p; p < end && (*p & 0xc0) == 0x80; ++p)
169 for (++p; (*p & 0xc0) == 0x80; ++p)
172 return (p == end) ? NULL : (gchar *)p;
177 * @p: a pointer to a position within a UTF-8 encoded string
179 * Finds the previous UTF-8 character in the string before @p.
181 * @p does not have to be at the beginning of a UTF-8 character. No check
182 * is made to see if the character found is actually valid other than
183 * it starts with an appropriate byte. If @p might be the first
184 * character of the string, you must use g_utf8_find_prev_char() instead.
186 * Return value: a pointer to the found character.
189 g_utf8_prev_char (const gchar *p)
194 if ((*p & 0xc0) != 0x80)
201 * @p: pointer to the start of a UTF-8 encoded string.
202 * @max: the maximum number of bytes to examine. If @max
203 * is less than 0, then the string is assumed to be
206 * Returns the length of the string in characters.
208 * Return value: the length of the string in characters
211 g_utf8_strlen (const gchar *p,
215 const gchar *start = p;
221 p = g_utf8_next_char (p);
230 p = g_utf8_next_char (p);
232 while (p - start < max && *p)
235 p = g_utf8_next_char (p);
238 /* only do the last len increment if we got a complete
239 * char (don't count partial chars)
241 if (p - start == max)
250 * @p: a pointer to Unicode character encoded as UTF-8
252 * Converts a sequence of bytes encoded as UTF-8 to a Unicode character.
253 * If @p does not point to a valid UTF-8 encoded character, results are
254 * undefined. If you are not sure that the bytes are complete
255 * valid Unicode characters, you should use g_utf8_get_char_validated()
258 * Return value: the resulting character
261 g_utf8_get_char (const gchar *p)
263 int i, mask = 0, len;
265 unsigned char c = (unsigned char) *p;
267 UTF8_COMPUTE (c, mask, len);
270 UTF8_GET (result, p, i, mask, len);
276 * g_utf8_offset_to_pointer:
277 * @str: a UTF-8 encoded string
278 * @offset: a character offset within @str
280 * Converts from an integer character offset to a pointer to a position
283 * Return value: the resulting pointer
286 g_utf8_offset_to_pointer (const gchar *str,
289 const gchar *s = str;
291 s = g_utf8_next_char (s);
297 * g_utf8_pointer_to_offset:
298 * @str: a UTF-8 encoded string
299 * @pos: a pointer to a position within @str
301 * Converts from a pointer to position within a string to a integer
304 * Return value: the resulting character offset
307 g_utf8_pointer_to_offset (const gchar *str,
310 const gchar *s = str;
315 s = g_utf8_next_char (s);
325 * @dest: buffer to fill with characters from @src
326 * @src: UTF-8 encoded string
327 * @n: character count
329 * Like the standard C <function>strncpy()</function> function, but
330 * copies a given number of characters instead of a given number of
331 * bytes. The @src string must be valid UTF-8 encoded text.
332 * (Use g_utf8_validate() on all text before trying to use UTF-8
333 * utility functions with it.)
335 * Return value: @dest
338 g_utf8_strncpy (gchar *dest,
342 const gchar *s = src;
345 s = g_utf8_next_char(s);
348 strncpy(dest, src, s - src);
353 G_LOCK_DEFINE_STATIC (aliases);
356 get_alias_hash (void)
358 static GHashTable *alias_hash = NULL;
365 alias_hash = g_hash_table_new (g_str_hash, g_str_equal);
367 aliases = _g_locale_get_charset_aliases ();
368 while (*aliases != '\0')
370 const char *canonical;
372 const char **alias_array;
376 aliases += strlen (aliases) + 1;
378 aliases += strlen (aliases) + 1;
380 alias_array = g_hash_table_lookup (alias_hash, canonical);
383 while (alias_array[count])
387 alias_array = g_renew (const char *, alias_array, count + 2);
388 alias_array[count] = alias;
389 alias_array[count + 1] = NULL;
391 g_hash_table_insert (alias_hash, (char *)canonical, alias_array);
400 /* As an abuse of the alias table, the following routines gets
401 * the charsets that are aliases for the canonical name.
404 _g_charset_get_aliases (const char *canonical_name)
406 GHashTable *alias_hash = get_alias_hash ();
408 return g_hash_table_lookup (alias_hash, canonical_name);
412 g_utf8_get_charset_internal (const char *raw_data,
415 const char *charset = getenv("CHARSET");
417 if (charset && *charset)
421 if (charset && strstr (charset, "UTF-8"))
427 /* The libcharset code tries to be thread-safe without
428 * a lock, but has a memory leak and a missing memory
429 * barrier, so we lock for it
432 charset = _g_locale_charset_unalias (raw_data);
435 if (charset && *charset)
439 if (charset && strstr (charset, "UTF-8"))
445 /* Assume this for compatibility at present. */
451 typedef struct _GCharsetCache GCharsetCache;
453 struct _GCharsetCache {
460 charset_cache_free (gpointer data)
462 GCharsetCache *cache = data;
464 g_free (cache->charset);
470 * @charset: return location for character set name
472 * Obtains the character set for the current locale; you might use
473 * this character set as an argument to g_convert(), to convert from
474 * the current locale's encoding to some other encoding. (Frequently
475 * g_locale_to_utf8() and g_locale_from_utf8() are nice shortcuts,
478 * The return value is %TRUE if the locale's encoding is UTF-8, in that
479 * case you can perhaps avoid calling g_convert().
481 * The string returned in @charset is not allocated, and should not be
484 * Return value: %TRUE if the returned charset is UTF-8
487 g_get_charset (G_CONST_RETURN char **charset)
489 static GStaticPrivate cache_private = G_STATIC_PRIVATE_INIT;
490 GCharsetCache *cache = g_static_private_get (&cache_private);
495 cache = g_new0 (GCharsetCache, 1);
496 g_static_private_set (&cache_private, cache, charset_cache_free);
499 raw = _g_locale_charset_raw ();
501 if (!(cache->raw && strcmp (cache->raw, raw) == 0))
503 const gchar *new_charset;
506 g_free (cache->charset);
507 cache->raw = g_strdup (raw);
508 cache->is_utf8 = g_utf8_get_charset_internal (raw, &new_charset);
509 cache->charset = g_strdup (new_charset);
513 *charset = cache->charset;
515 return cache->is_utf8;
522 * @c: a ISO10646 character code
523 * @outbuf: output buffer, must have at least 6 bytes of space.
524 * If %NULL, the length will be computed and returned
525 * and nothing will be written to @outbuf.
527 * Converts a single character to UTF-8.
529 * Return value: number of bytes written
532 g_unichar_to_utf8 (gunichar c,
549 else if (c < 0x10000)
554 else if (c < 0x200000)
559 else if (c < 0x4000000)
572 for (i = len - 1; i > 0; --i)
574 outbuf[i] = (c & 0x3f) | 0x80;
577 outbuf[0] = c | first;
585 * @p: a nul-terminated UTF-8 encoded string
586 * @len: the maximum length of @p
587 * @c: a ISO10646 character
589 * Finds the leftmost occurrence of the given ISO10646 character
590 * in a UTF-8 encoded string, while limiting the search to @len bytes.
591 * If @len is -1, allow unbounded search.
593 * Return value: %NULL if the string does not contain the character,
594 * otherwise, a pointer to the start of the leftmost occurrence of
595 * the character in the string.
598 g_utf8_strchr (const char *p,
604 gint charlen = g_unichar_to_utf8 (c, ch);
607 return g_strstr_len (p, len, ch);
613 * @p: a nul-terminated UTF-8 encoded string
614 * @len: the maximum length of @p
615 * @c: a ISO10646 character
617 * Find the rightmost occurrence of the given ISO10646 character
618 * in a UTF-8 encoded string, while limiting the search to @len bytes.
619 * If @len is -1, allow unbounded search.
621 * Return value: %NULL if the string does not contain the character,
622 * otherwise, a pointer to the start of the rightmost occurrence of the
623 * character in the string.
626 g_utf8_strrchr (const char *p,
632 gint charlen = g_unichar_to_utf8 (c, ch);
635 return g_strrstr_len (p, len, ch);
639 /* Like g_utf8_get_char, but take a maximum length
640 * and return (gunichar)-2 on incomplete trailing character
642 static inline gunichar
643 g_utf8_get_char_extended (const gchar *p,
647 gunichar wc = (guchar) *p;
687 if (max_len >= 0 && len > max_len)
689 for (i = 1; i < max_len; i++)
691 if ((((guchar *)p)[i] & 0xc0) != 0x80)
697 for (i = 1; i < len; ++i)
699 gunichar ch = ((guchar *)p)[i];
701 if ((ch & 0xc0) != 0x80)
713 if (UTF8_LENGTH(wc) != len)
720 * g_utf8_get_char_validated:
721 * @p: a pointer to Unicode character encoded as UTF-8
722 * @max_len: the maximum number of bytes to read, or -1, for no maximum.
724 * Convert a sequence of bytes encoded as UTF-8 to a Unicode character.
725 * This function checks for incomplete characters, for invalid characters
726 * such as characters that are out of the range of Unicode, and for
727 * overlong encodings of valid characters.
729 * Return value: the resulting character. If @p points to a partial
730 * sequence at the end of a string that could begin a valid character,
731 * returns (gunichar)-2; otherwise, if @p does not point to a valid
732 * UTF-8 encoded Unicode character, returns (gunichar)-1.
735 g_utf8_get_char_validated (const gchar *p,
738 gunichar result = g_utf8_get_char_extended (p, max_len);
740 if (result & 0x80000000)
742 else if (!UNICODE_VALID (result))
749 * g_utf8_to_ucs4_fast:
750 * @str: a UTF-8 encoded string
751 * @len: the maximum length of @str to use. If @len < 0, then
752 * the string is nul-terminated.
753 * @items_written: location to store the number of characters in the
756 * Convert a string from UTF-8 to a 32-bit fixed width
757 * representation as UCS-4, assuming valid UTF-8 input.
758 * This function is roughly twice as fast as g_utf8_to_ucs4()
759 * but does no error checking on the input.
761 * Return value: a pointer to a newly allocated UCS-4 string.
762 * This value must be freed with g_free().
765 g_utf8_to_ucs4_fast (const gchar *str,
767 glong *items_written)
774 g_return_val_if_fail (str != NULL, NULL);
782 p = g_utf8_next_char (p);
788 while (p < str + len && *p)
790 p = g_utf8_next_char (p);
795 result = g_new (gunichar, n_chars + 1);
798 for (i=0; i < n_chars; i++)
800 gunichar wc = ((unsigned char *)p)[0];
835 for (j = 1; j < charlen; j++)
838 wc |= ((unsigned char *)p)[j] & 0x3f;
855 * @str: a UTF-8 encoded string
856 * @len: the maximum length of @str to use. If @len < 0, then
857 * the string is nul-terminated.
858 * @items_read: location to store number of bytes read, or %NULL.
859 * If %NULL, then %G_CONVERT_ERROR_PARTIAL_INPUT will be
860 * returned in case @str contains a trailing partial
861 * character. If an error occurs then the index of the
862 * invalid input is stored here.
863 * @items_written: location to store number of characters written or %NULL.
864 * The value here stored does not include the trailing 0
866 * @error: location to store the error occuring, or %NULL to ignore
867 * errors. Any of the errors in #GConvertError other than
868 * %G_CONVERT_ERROR_NO_CONVERSION may occur.
870 * Convert a string from UTF-8 to a 32-bit fixed width
871 * representation as UCS-4. A trailing 0 will be added to the
872 * string after the converted text.
874 * Return value: a pointer to a newly allocated UCS-4 string.
875 * This value must be freed with g_free(). If an
876 * error occurs, %NULL will be returned and
880 g_utf8_to_ucs4 (const gchar *str,
883 glong *items_written,
886 gunichar *result = NULL;
892 while ((len < 0 || str + len - in > 0) && *in)
894 gunichar wc = g_utf8_get_char_extended (in, str + len - in);
897 if (wc == (gunichar)-2)
902 g_set_error (error, G_CONVERT_ERROR, G_CONVERT_ERROR_PARTIAL_INPUT,
903 _("Partial character sequence at end of input"));
906 g_set_error (error, G_CONVERT_ERROR, G_CONVERT_ERROR_ILLEGAL_SEQUENCE,
907 _("Invalid byte sequence in conversion input"));
914 in = g_utf8_next_char (in);
917 result = g_new (gunichar, n_chars + 1);
920 for (i=0; i < n_chars; i++)
922 result[i] = g_utf8_get_char (in);
923 in = g_utf8_next_char (in);
928 *items_written = n_chars;
932 *items_read = in - str;
939 * @str: a UCS-4 encoded string
940 * @len: the maximum length of @str to use. If @len < 0, then
941 * the string is terminated with a 0 character.
942 * @items_read: location to store number of characters read read, or %NULL.
943 * @items_written: location to store number of bytes written or %NULL.
944 * The value here stored does not include the trailing 0
946 * @error: location to store the error occuring, or %NULL to ignore
947 * errors. Any of the errors in #GConvertError other than
948 * %G_CONVERT_ERROR_NO_CONVERSION may occur.
950 * Convert a string from a 32-bit fixed width representation as UCS-4.
951 * to UTF-8. The result will be terminated with a 0 byte.
953 * Return value: a pointer to a newly allocated UTF-8 string.
954 * This value must be freed with g_free(). If an
955 * error occurs, %NULL will be returned and
959 g_ucs4_to_utf8 (const gunichar *str,
962 glong *items_written,
966 gchar *result = NULL;
971 for (i = 0; len < 0 || i < len ; i++)
976 if (str[i] >= 0x80000000)
981 g_set_error (error, G_CONVERT_ERROR, G_CONVERT_ERROR_ILLEGAL_SEQUENCE,
982 _("Character out of range for UTF-8"));
986 result_length += UTF8_LENGTH (str[i]);
989 result = g_malloc (result_length + 1);
993 while (p < result + result_length)
994 p += g_unichar_to_utf8 (str[i++], p);
999 *items_written = p - result;
1008 #define SURROGATE_VALUE(h,l) (((h) - 0xd800) * 0x400 + (l) - 0xdc00 + 0x10000)
1012 * @str: a UTF-16 encoded string
1013 * @len: the maximum length of @str to use. If @len < 0, then
1014 * the string is terminated with a 0 character.
1015 * @items_read: location to store number of words read, or %NULL.
1016 * If %NULL, then %G_CONVERT_ERROR_PARTIAL_INPUT will be
1017 * returned in case @str contains a trailing partial
1018 * character. If an error occurs then the index of the
1019 * invalid input is stored here.
1020 * @items_written: location to store number of bytes written, or %NULL.
1021 * The value stored here does not include the trailing
1023 * @error: location to store the error occuring, or %NULL to ignore
1024 * errors. Any of the errors in #GConvertError other than
1025 * %G_CONVERT_ERROR_NO_CONVERSION may occur.
1027 * Convert a string from UTF-16 to UTF-8. The result will be
1028 * terminated with a 0 byte.
1030 * Return value: a pointer to a newly allocated UTF-8 string.
1031 * This value must be freed with g_free(). If an
1032 * error occurs, %NULL will be returned and
1036 g_utf16_to_utf8 (const gunichar2 *str,
1039 glong *items_written,
1042 /* This function and g_utf16_to_ucs4 are almost exactly identical - The lines that differ
1045 const gunichar2 *in;
1047 gchar *result = NULL;
1049 gunichar high_surrogate;
1051 g_return_val_if_fail (str != 0, NULL);
1056 while ((len < 0 || in - str < len) && *in)
1061 if (c >= 0xdc00 && c < 0xe000) /* low surrogate */
1065 wc = SURROGATE_VALUE (high_surrogate, c);
1070 g_set_error (error, G_CONVERT_ERROR, G_CONVERT_ERROR_ILLEGAL_SEQUENCE,
1071 _("Invalid sequence in conversion input"));
1079 g_set_error (error, G_CONVERT_ERROR, G_CONVERT_ERROR_ILLEGAL_SEQUENCE,
1080 _("Invalid sequence in conversion input"));
1084 if (c >= 0xd800 && c < 0xdc00) /* high surrogate */
1093 /********** DIFFERENT for UTF8/UCS4 **********/
1094 n_bytes += UTF8_LENGTH (wc);
1100 if (high_surrogate && !items_read)
1102 g_set_error (error, G_CONVERT_ERROR, G_CONVERT_ERROR_PARTIAL_INPUT,
1103 _("Partial character sequence at end of input"));
1107 /* At this point, everything is valid, and we just need to convert
1109 /********** DIFFERENT for UTF8/UCS4 **********/
1110 result = g_malloc (n_bytes + 1);
1115 while (out < result + n_bytes)
1120 if (c >= 0xdc00 && c < 0xe000) /* low surrogate */
1122 wc = SURROGATE_VALUE (high_surrogate, c);
1125 else if (c >= 0xd800 && c < 0xdc00) /* high surrogate */
1133 /********** DIFFERENT for UTF8/UCS4 **********/
1134 out += g_unichar_to_utf8 (wc, out);
1140 /********** DIFFERENT for UTF8/UCS4 **********/
1144 /********** DIFFERENT for UTF8/UCS4 **********/
1145 *items_written = out - result;
1149 *items_read = in - str;
1156 * @str: a UTF-16 encoded string
1157 * @len: the maximum length of @str to use. If @len < 0, then
1158 * the string is terminated with a 0 character.
1159 * @items_read: location to store number of words read, or %NULL.
1160 * If %NULL, then %G_CONVERT_ERROR_PARTIAL_INPUT will be
1161 * returned in case @str contains a trailing partial
1162 * character. If an error occurs then the index of the
1163 * invalid input is stored here.
1164 * @items_written: location to store number of characters written, or %NULL.
1165 * The value stored here does not include the trailing
1167 * @error: location to store the error occuring, or %NULL to ignore
1168 * errors. Any of the errors in #GConvertError other than
1169 * %G_CONVERT_ERROR_NO_CONVERSION may occur.
1171 * Convert a string from UTF-16 to UCS-4. The result will be
1172 * terminated with a 0 character.
1174 * Return value: a pointer to a newly allocated UCS-4 string.
1175 * This value must be freed with g_free(). If an
1176 * error occurs, %NULL will be returned and
1180 g_utf16_to_ucs4 (const gunichar2 *str,
1183 glong *items_written,
1186 const gunichar2 *in;
1188 gchar *result = NULL;
1190 gunichar high_surrogate;
1192 g_return_val_if_fail (str != 0, NULL);
1197 while ((len < 0 || in - str < len) && *in)
1202 if (c >= 0xdc00 && c < 0xe000) /* low surrogate */
1206 wc = SURROGATE_VALUE (high_surrogate, c);
1211 g_set_error (error, G_CONVERT_ERROR, G_CONVERT_ERROR_ILLEGAL_SEQUENCE,
1212 _("Invalid sequence in conversion input"));
1220 g_set_error (error, G_CONVERT_ERROR, G_CONVERT_ERROR_ILLEGAL_SEQUENCE,
1221 _("Invalid sequence in conversion input"));
1225 if (c >= 0xd800 && c < 0xdc00) /* high surrogate */
1234 /********** DIFFERENT for UTF8/UCS4 **********/
1235 n_bytes += sizeof (gunichar);
1241 if (high_surrogate && !items_read)
1243 g_set_error (error, G_CONVERT_ERROR, G_CONVERT_ERROR_PARTIAL_INPUT,
1244 _("Partial character sequence at end of input"));
1248 /* At this point, everything is valid, and we just need to convert
1250 /********** DIFFERENT for UTF8/UCS4 **********/
1251 result = g_malloc (n_bytes + 4);
1256 while (out < result + n_bytes)
1261 if (c >= 0xdc00 && c < 0xe000) /* low surrogate */
1263 wc = SURROGATE_VALUE (high_surrogate, c);
1266 else if (c >= 0xd800 && c < 0xdc00) /* high surrogate */
1274 /********** DIFFERENT for UTF8/UCS4 **********/
1275 *(gunichar *)out = wc;
1276 out += sizeof (gunichar);
1282 /********** DIFFERENT for UTF8/UCS4 **********/
1283 *(gunichar *)out = 0;
1286 /********** DIFFERENT for UTF8/UCS4 **********/
1287 *items_written = (out - result) / sizeof (gunichar);
1291 *items_read = in - str;
1293 return (gunichar *)result;
1298 * @str: a UTF-8 encoded string
1299 * @len: the maximum length of @str to use. If @len < 0, then
1300 * the string is nul-terminated.
1301 * @items_read: location to store number of bytes read, or %NULL.
1302 * If %NULL, then %G_CONVERT_ERROR_PARTIAL_INPUT will be
1303 * returned in case @str contains a trailing partial
1304 * character. If an error occurs then the index of the
1305 * invalid input is stored here.
1306 * @items_written: location to store number of words written, or %NULL.
1307 * The value stored here does not include the trailing
1309 * @error: location to store the error occuring, or %NULL to ignore
1310 * errors. Any of the errors in #GConvertError other than
1311 * %G_CONVERT_ERROR_NO_CONVERSION may occur.
1313 * Convert a string from UTF-8 to UTF-16. A 0 word will be
1314 * added to the result after the converted text.
1316 * Return value: a pointer to a newly allocated UTF-16 string.
1317 * This value must be freed with g_free(). If an
1318 * error occurs, %NULL will be returned and
1322 g_utf8_to_utf16 (const gchar *str,
1325 glong *items_written,
1328 gunichar2 *result = NULL;
1333 g_return_val_if_fail (str != NULL, NULL);
1337 while ((len < 0 || str + len - in > 0) && *in)
1339 gunichar wc = g_utf8_get_char_extended (in, str + len - in);
1340 if (wc & 0x80000000)
1342 if (wc == (gunichar)-2)
1347 g_set_error (error, G_CONVERT_ERROR, G_CONVERT_ERROR_PARTIAL_INPUT,
1348 _("Partial character sequence at end of input"));
1351 g_set_error (error, G_CONVERT_ERROR, G_CONVERT_ERROR_ILLEGAL_SEQUENCE,
1352 _("Invalid byte sequence in conversion input"));
1359 else if (wc < 0xe000)
1361 g_set_error (error, G_CONVERT_ERROR, G_CONVERT_ERROR_ILLEGAL_SEQUENCE,
1362 _("Invalid sequence in conversion input"));
1366 else if (wc < 0x10000)
1368 else if (wc < 0x110000)
1372 g_set_error (error, G_CONVERT_ERROR, G_CONVERT_ERROR_ILLEGAL_SEQUENCE,
1373 _("Character out of range for UTF-16"));
1378 in = g_utf8_next_char (in);
1381 result = g_new (gunichar2, n16 + 1);
1384 for (i = 0; i < n16;)
1386 gunichar wc = g_utf8_get_char (in);
1394 result[i++] = (wc - 0x10000) / 0x400 + 0xd800;
1395 result[i++] = (wc - 0x10000) % 0x400 + 0xdc00;
1398 in = g_utf8_next_char (in);
1404 *items_written = n16;
1408 *items_read = in - str;
1415 * @str: a UCS-4 encoded string
1416 * @len: the maximum length of @str to use. If @len < 0, then
1417 * the string is terminated with a 0 character.
1418 * @items_read: location to store number of bytes read, or %NULL.
1419 * If an error occurs then the index of the invalid input
1421 * @items_written: location to store number of words written, or %NULL.
1422 * The value stored here does not include the trailing
1424 * @error: location to store the error occuring, or %NULL to ignore
1425 * errors. Any of the errors in #GConvertError other than
1426 * %G_CONVERT_ERROR_NO_CONVERSION may occur.
1428 * Convert a string from UCS-4 to UTF-16. A 0 word will be
1429 * added to the result after the converted text.
1431 * Return value: a pointer to a newly allocated UTF-16 string.
1432 * This value must be freed with g_free(). If an
1433 * error occurs, %NULL will be returned and
1437 g_ucs4_to_utf16 (const gunichar *str,
1440 glong *items_written,
1443 gunichar2 *result = NULL;
1449 while ((len < 0 || i < len) && str[i])
1451 gunichar wc = str[i];
1455 else if (wc < 0xe000)
1457 g_set_error (error, G_CONVERT_ERROR, G_CONVERT_ERROR_ILLEGAL_SEQUENCE,
1458 _("Invalid sequence in conversion input"));
1462 else if (wc < 0x10000)
1464 else if (wc < 0x110000)
1468 g_set_error (error, G_CONVERT_ERROR, G_CONVERT_ERROR_ILLEGAL_SEQUENCE,
1469 _("Character out of range for UTF-16"));
1477 result = g_new (gunichar2, n16 + 1);
1479 for (i = 0, j = 0; j < n16; i++)
1481 gunichar wc = str[i];
1489 result[j++] = (wc - 0x10000) / 0x400 + 0xd800;
1490 result[j++] = (wc - 0x10000) % 0x400 + 0xdc00;
1496 *items_written = n16;
1507 * @str: a pointer to character data
1508 * @max_len: max bytes to validate, or -1 to go until nul
1509 * @end: return location for end of valid data
1511 * Validates UTF-8 encoded text. @str is the text to validate;
1512 * if @str is nul-terminated, then @max_len can be -1, otherwise
1513 * @max_len should be the number of bytes to validate.
1514 * If @end is non-%NULL, then the end of the valid range
1515 * will be stored there (i.e. the address of the first invalid byte
1516 * if some bytes were invalid, or the end of the text being validated
1519 * Returns %TRUE if all of @str was valid. Many GLib and GTK+
1520 * routines <emphasis>require</emphasis> valid UTF-8 as input;
1521 * so data read from a file or the network should be checked
1522 * with g_utf8_validate() before doing anything else with it.
1524 * Return value: %TRUE if the text was valid UTF-8
1527 g_utf8_validate (const gchar *str,
1534 g_return_val_if_fail (str != NULL, FALSE);
1541 while ((max_len < 0 || (p - str) < max_len) && *p)
1543 int i, mask = 0, len;
1545 unsigned char c = (unsigned char) *p;
1547 UTF8_COMPUTE (c, mask, len);
1552 /* check that the expected number of bytes exists in str */
1554 ((max_len - (p - str)) < len))
1557 UTF8_GET (result, p, i, mask, len);
1559 if (UTF8_LENGTH (result) != len) /* Check for overlong UTF-8 */
1562 if (result == (gunichar)-1)
1565 if (!UNICODE_VALID (result))
1574 /* See that we covered the entire length if a length was
1575 * passed in, or that we ended on a nul if not
1578 p != (str + max_len))
1580 else if (max_len < 0 &&
1588 * g_unichar_validate:
1589 * @ch: a Unicode character
1591 * Checks whether @ch is a valid Unicode character. Some possible
1592 * integer values of @ch will not be valid. 0 is considered a valid
1593 * character, though it's normally a string terminator.
1595 * Return value: %TRUE if @ch is a valid Unicode character
1598 g_unichar_validate (gunichar ch)
1600 return UNICODE_VALID (ch);
1604 * g_utf8_strreverse:
1605 * @str: a UTF-8 encoded string
1606 * @len: the maximum length of @str to use. If @len < 0, then
1607 * the string is nul-terminated.
1609 * Reverses a UTF-8 string. @str must be valid UTF-8 encoded text.
1610 * (Use g_utf8_validate() on all text before trying to use UTF-8
1611 * utility functions with it.)
1613 * Note that unlike g_strreverse(), this function returns
1614 * newly-allocated memory, which should be freed with g_free() when
1617 * Returns: a newly-allocated string which is the reverse of @str.
1622 g_utf8_strreverse (const gchar *str,
1632 result = g_new (gchar, len + 1);
1637 skip = g_utf8_skip[*(guchar*)p];
1639 for (m = r; skip; skip--)