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.
33 #ifdef G_PLATFORM_WIN32
40 #include "libcharset/libcharset.h"
44 #define UTF8_COMPUTE(Char, Mask, Len) \
50 else if ((Char & 0xe0) == 0xc0) \
55 else if ((Char & 0xf0) == 0xe0) \
60 else if ((Char & 0xf8) == 0xf0) \
65 else if ((Char & 0xfc) == 0xf8) \
70 else if ((Char & 0xfe) == 0xfc) \
78 #define UTF8_LENGTH(Char) \
79 ((Char) < 0x80 ? 1 : \
80 ((Char) < 0x800 ? 2 : \
81 ((Char) < 0x10000 ? 3 : \
82 ((Char) < 0x200000 ? 4 : \
83 ((Char) < 0x4000000 ? 5 : 6)))))
86 #define UTF8_GET(Result, Chars, Count, Mask, Len) \
87 (Result) = (Chars)[0] & (Mask); \
88 for ((Count) = 1; (Count) < (Len); ++(Count)) \
90 if (((Chars)[(Count)] & 0xc0) != 0x80) \
96 (Result) |= ((Chars)[(Count)] & 0x3f); \
99 #define UNICODE_VALID(Char) \
100 ((Char) < 0x110000 && \
101 (((Char) & 0xFFFFF800) != 0xD800) && \
102 ((Char) < 0xFDD0 || (Char) > 0xFDEF) && \
103 ((Char) & 0xFFFE) != 0xFFFE)
106 static const gchar utf8_skip_data[256] = {
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 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,
112 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,
113 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,
114 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
117 const gchar * const g_utf8_skip = utf8_skip_data;
120 * g_utf8_find_prev_char:
121 * @str: pointer to the beginning of a UTF-8 encoded string
122 * @p: pointer to some position within @str
124 * Given a position @p with a UTF-8 encoded string @str, find the start
125 * of the previous UTF-8 character starting before @p. Returns %NULL if no
126 * UTF-8 characters are present in @p before @str.
128 * @p does not have to be at the beginning of a UTF-8 character. No check
129 * is made to see if the character found is actually valid other than
130 * it starts with an appropriate byte.
132 * Return value: a pointer to the found character or %NULL.
135 g_utf8_find_prev_char (const char *str,
138 for (--p; p >= str; --p)
140 if ((*p & 0xc0) != 0x80)
147 * g_utf8_find_next_char:
148 * @p: a pointer to a position within a UTF-8 encoded string
149 * @end: a pointer to the end of the string, or %NULL to indicate
150 * that the string is nul-terminated, in which case
151 * the returned value will be
153 * Finds the start of the next UTF-8 character in the string after @p.
155 * @p does not have to be at the beginning of a UTF-8 character. No check
156 * is made to see if the character found is actually valid other than
157 * it starts with an appropriate byte.
159 * Return value: a pointer to the found character or %NULL
162 g_utf8_find_next_char (const gchar *p,
168 for (++p; p < end && (*p & 0xc0) == 0x80; ++p)
171 for (++p; (*p & 0xc0) == 0x80; ++p)
174 return (p == end) ? NULL : (gchar *)p;
179 * @p: a pointer to a position within a UTF-8 encoded string
181 * Finds the previous UTF-8 character in the string before @p.
183 * @p does not have to be at the beginning of a UTF-8 character. No check
184 * is made to see if the character found is actually valid other than
185 * it starts with an appropriate byte. If @p might be the first
186 * character of the string, you must use g_utf8_find_prev_char() instead.
188 * Return value: a pointer to the found character.
191 g_utf8_prev_char (const gchar *p)
196 if ((*p & 0xc0) != 0x80)
203 * @p: pointer to the start of a UTF-8 encoded string.
204 * @max: the maximum number of bytes to examine. If @max
205 * is less than 0, then the string is assumed to be
206 * nul-terminated. If @max is 0, @p will not be examined and
209 * Returns the length of the string in characters.
211 * Return value: the length of the string in characters
214 g_utf8_strlen (const gchar *p,
218 const gchar *start = p;
219 g_return_val_if_fail (p != NULL || max == 0, 0);
225 p = g_utf8_next_char (p);
234 p = g_utf8_next_char (p);
236 while (p - start < max && *p)
239 p = g_utf8_next_char (p);
242 /* only do the last len increment if we got a complete
243 * char (don't count partial chars)
245 if (p - start == max)
254 * @p: a pointer to Unicode character encoded as UTF-8
256 * Converts a sequence of bytes encoded as UTF-8 to a Unicode character.
257 * If @p does not point to a valid UTF-8 encoded character, results are
258 * undefined. If you are not sure that the bytes are complete
259 * valid Unicode characters, you should use g_utf8_get_char_validated()
262 * Return value: the resulting character
265 g_utf8_get_char (const gchar *p)
267 int i, mask = 0, len;
269 unsigned char c = (unsigned char) *p;
271 UTF8_COMPUTE (c, mask, len);
274 UTF8_GET (result, p, i, mask, len);
280 * g_utf8_offset_to_pointer:
281 * @str: a UTF-8 encoded string
282 * @offset: a character offset within @str
284 * Converts from an integer character offset to a pointer to a position
287 * Return value: the resulting pointer
290 g_utf8_offset_to_pointer (const gchar *str,
293 const gchar *s = str;
295 s = g_utf8_next_char (s);
301 * g_utf8_pointer_to_offset:
302 * @str: a UTF-8 encoded string
303 * @pos: a pointer to a position within @str
305 * Converts from a pointer to position within a string to a integer
308 * Return value: the resulting character offset
311 g_utf8_pointer_to_offset (const gchar *str,
314 const gchar *s = str;
319 s = g_utf8_next_char (s);
329 * @dest: buffer to fill with characters from @src
330 * @src: UTF-8 encoded string
331 * @n: character count
333 * Like the standard C strncpy() function, but
334 * copies a given number of characters instead of a given number of
335 * bytes. The @src string must be valid UTF-8 encoded text.
336 * (Use g_utf8_validate() on all text before trying to use UTF-8
337 * utility functions with it.)
339 * Return value: @dest
342 g_utf8_strncpy (gchar *dest,
346 const gchar *s = src;
349 s = g_utf8_next_char(s);
352 strncpy(dest, src, s - src);
357 G_LOCK_DEFINE_STATIC (aliases);
360 get_alias_hash (void)
362 static GHashTable *alias_hash = NULL;
369 alias_hash = g_hash_table_new (g_str_hash, g_str_equal);
371 aliases = _g_locale_get_charset_aliases ();
372 while (*aliases != '\0')
374 const char *canonical;
376 const char **alias_array;
380 aliases += strlen (aliases) + 1;
382 aliases += strlen (aliases) + 1;
384 alias_array = g_hash_table_lookup (alias_hash, canonical);
387 while (alias_array[count])
391 alias_array = g_renew (const char *, alias_array, count + 2);
392 alias_array[count] = alias;
393 alias_array[count + 1] = NULL;
395 g_hash_table_insert (alias_hash, (char *)canonical, alias_array);
404 /* As an abuse of the alias table, the following routines gets
405 * the charsets that are aliases for the canonical name.
408 _g_charset_get_aliases (const char *canonical_name)
410 GHashTable *alias_hash = get_alias_hash ();
412 return g_hash_table_lookup (alias_hash, canonical_name);
416 g_utf8_get_charset_internal (const char *raw_data,
419 const char *charset = getenv("CHARSET");
421 if (charset && *charset)
425 if (charset && strstr (charset, "UTF-8"))
431 /* The libcharset code tries to be thread-safe without
432 * a lock, but has a memory leak and a missing memory
433 * barrier, so we lock for it
436 charset = _g_locale_charset_unalias (raw_data);
439 if (charset && *charset)
443 if (charset && strstr (charset, "UTF-8"))
449 /* Assume this for compatibility at present. */
455 typedef struct _GCharsetCache GCharsetCache;
457 struct _GCharsetCache {
464 charset_cache_free (gpointer data)
466 GCharsetCache *cache = data;
468 g_free (cache->charset);
474 * @charset: return location for character set name
476 * Obtains the character set for the current locale; you might use
477 * this character set as an argument to g_convert(), to convert from
478 * the current locale's encoding to some other encoding. (Frequently
479 * g_locale_to_utf8() and g_locale_from_utf8() are nice shortcuts,
482 * The return value is %TRUE if the locale's encoding is UTF-8, in that
483 * case you can perhaps avoid calling g_convert().
485 * The string returned in @charset is not allocated, and should not be
488 * Return value: %TRUE if the returned charset is UTF-8
491 g_get_charset (G_CONST_RETURN char **charset)
493 static GStaticPrivate cache_private = G_STATIC_PRIVATE_INIT;
494 GCharsetCache *cache = g_static_private_get (&cache_private);
499 cache = g_new0 (GCharsetCache, 1);
500 g_static_private_set (&cache_private, cache, charset_cache_free);
503 raw = _g_locale_charset_raw ();
505 if (!(cache->raw && strcmp (cache->raw, raw) == 0))
507 const gchar *new_charset;
510 g_free (cache->charset);
511 cache->raw = g_strdup (raw);
512 cache->is_utf8 = g_utf8_get_charset_internal (raw, &new_charset);
513 cache->charset = g_strdup (new_charset);
517 *charset = cache->charset;
519 return cache->is_utf8;
526 * @c: a ISO10646 character code
527 * @outbuf: output buffer, must have at least 6 bytes of space.
528 * If %NULL, the length will be computed and returned
529 * and nothing will be written to @outbuf.
531 * Converts a single character to UTF-8.
533 * Return value: number of bytes written
536 g_unichar_to_utf8 (gunichar c,
539 /* If this gets modified, also update the copy in g_string_insert_unichar() */
554 else if (c < 0x10000)
559 else if (c < 0x200000)
564 else if (c < 0x4000000)
577 for (i = len - 1; i > 0; --i)
579 outbuf[i] = (c & 0x3f) | 0x80;
582 outbuf[0] = c | first;
590 * @p: a nul-terminated UTF-8 encoded string
591 * @len: the maximum length of @p
592 * @c: a ISO10646 character
594 * Finds the leftmost occurrence of the given ISO10646 character
595 * in a UTF-8 encoded string, while limiting the search to @len bytes.
596 * If @len is -1, allow unbounded search.
598 * Return value: %NULL if the string does not contain the character,
599 * otherwise, a pointer to the start of the leftmost occurrence of
600 * the character in the string.
603 g_utf8_strchr (const char *p,
609 gint charlen = g_unichar_to_utf8 (c, ch);
612 return g_strstr_len (p, len, ch);
618 * @p: a nul-terminated UTF-8 encoded string
619 * @len: the maximum length of @p
620 * @c: a ISO10646 character
622 * Find the rightmost occurrence of the given ISO10646 character
623 * in a UTF-8 encoded string, while limiting the search to @len bytes.
624 * If @len is -1, allow unbounded search.
626 * Return value: %NULL if the string does not contain the character,
627 * otherwise, a pointer to the start of the rightmost occurrence of the
628 * character in the string.
631 g_utf8_strrchr (const char *p,
637 gint charlen = g_unichar_to_utf8 (c, ch);
640 return g_strrstr_len (p, len, ch);
644 /* Like g_utf8_get_char, but take a maximum length
645 * and return (gunichar)-2 on incomplete trailing character
647 static inline gunichar
648 g_utf8_get_char_extended (const gchar *p,
652 gunichar wc = (guchar) *p;
692 if (max_len >= 0 && len > max_len)
694 for (i = 1; i < max_len; i++)
696 if ((((guchar *)p)[i] & 0xc0) != 0x80)
702 for (i = 1; i < len; ++i)
704 gunichar ch = ((guchar *)p)[i];
706 if ((ch & 0xc0) != 0x80)
718 if (UTF8_LENGTH(wc) != len)
725 * g_utf8_get_char_validated:
726 * @p: a pointer to Unicode character encoded as UTF-8
727 * @max_len: the maximum number of bytes to read, or -1, for no maximum.
729 * Convert a sequence of bytes encoded as UTF-8 to a Unicode character.
730 * This function checks for incomplete characters, for invalid characters
731 * such as characters that are out of the range of Unicode, and for
732 * overlong encodings of valid characters.
734 * Return value: the resulting character. If @p points to a partial
735 * sequence at the end of a string that could begin a valid
736 * character, returns (gunichar)-2; otherwise, if @p does not point
737 * to a valid UTF-8 encoded Unicode character, returns (gunichar)-1.
740 g_utf8_get_char_validated (const gchar *p,
743 gunichar result = g_utf8_get_char_extended (p, max_len);
745 if (result & 0x80000000)
747 else if (!UNICODE_VALID (result))
754 * g_utf8_to_ucs4_fast:
755 * @str: a UTF-8 encoded string
756 * @len: the maximum length of @str to use. If @len < 0, then
757 * the string is nul-terminated.
758 * @items_written: location to store the number of characters in the
761 * Convert a string from UTF-8 to a 32-bit fixed width
762 * representation as UCS-4, assuming valid UTF-8 input.
763 * This function is roughly twice as fast as g_utf8_to_ucs4()
764 * but does no error checking on the input.
766 * Return value: a pointer to a newly allocated UCS-4 string.
767 * This value must be freed with g_free().
770 g_utf8_to_ucs4_fast (const gchar *str,
772 glong *items_written)
779 g_return_val_if_fail (str != NULL, NULL);
787 p = g_utf8_next_char (p);
793 while (p < str + len && *p)
795 p = g_utf8_next_char (p);
800 result = g_new (gunichar, n_chars + 1);
803 for (i=0; i < n_chars; i++)
805 gunichar wc = ((unsigned char *)p)[0];
840 for (j = 1; j < charlen; j++)
843 wc |= ((unsigned char *)p)[j] & 0x3f;
860 * @str: a UTF-8 encoded string
861 * @len: the maximum length of @str to use. If @len < 0, then
862 * the string is nul-terminated.
863 * @items_read: location to store number of bytes read, or %NULL.
864 * If %NULL, then %G_CONVERT_ERROR_PARTIAL_INPUT will be
865 * returned in case @str contains a trailing partial
866 * character. If an error occurs then the index of the
867 * invalid input is stored here.
868 * @items_written: location to store number of characters written or %NULL.
869 * The value here stored does not include the trailing 0
871 * @error: location to store the error occuring, or %NULL to ignore
872 * errors. Any of the errors in #GConvertError other than
873 * %G_CONVERT_ERROR_NO_CONVERSION may occur.
875 * Convert a string from UTF-8 to a 32-bit fixed width
876 * representation as UCS-4. A trailing 0 will be added to the
877 * string after the converted text.
879 * Return value: a pointer to a newly allocated UCS-4 string.
880 * This value must be freed with g_free(). If an
881 * error occurs, %NULL will be returned and
885 g_utf8_to_ucs4 (const gchar *str,
888 glong *items_written,
891 gunichar *result = NULL;
897 while ((len < 0 || str + len - in > 0) && *in)
899 gunichar wc = g_utf8_get_char_extended (in, str + len - in);
902 if (wc == (gunichar)-2)
907 g_set_error (error, G_CONVERT_ERROR, G_CONVERT_ERROR_PARTIAL_INPUT,
908 _("Partial character sequence at end of input"));
911 g_set_error (error, G_CONVERT_ERROR, G_CONVERT_ERROR_ILLEGAL_SEQUENCE,
912 _("Invalid byte sequence in conversion input"));
919 in = g_utf8_next_char (in);
922 result = g_new (gunichar, n_chars + 1);
925 for (i=0; i < n_chars; i++)
927 result[i] = g_utf8_get_char (in);
928 in = g_utf8_next_char (in);
933 *items_written = n_chars;
937 *items_read = in - str;
944 * @str: a UCS-4 encoded string
945 * @len: the maximum length of @str to use. If @len < 0, then
946 * the string is terminated with a 0 character.
947 * @items_read: location to store number of characters read read, or %NULL.
948 * @items_written: location to store number of bytes written or %NULL.
949 * The value here stored does not include the trailing 0
951 * @error: location to store the error occuring, or %NULL to ignore
952 * errors. Any of the errors in #GConvertError other than
953 * %G_CONVERT_ERROR_NO_CONVERSION may occur.
955 * Convert a string from a 32-bit fixed width representation as UCS-4.
956 * to UTF-8. The result will be terminated with a 0 byte.
958 * Return value: a pointer to a newly allocated UTF-8 string.
959 * This value must be freed with g_free(). If an
960 * error occurs, %NULL will be returned and
961 * @error set. In that case, @items_read will be
962 * set to the position of the first invalid input
966 g_ucs4_to_utf8 (const gunichar *str,
969 glong *items_written,
973 gchar *result = NULL;
978 for (i = 0; len < 0 || i < len ; i++)
983 if (str[i] >= 0x80000000)
985 g_set_error (error, G_CONVERT_ERROR, G_CONVERT_ERROR_ILLEGAL_SEQUENCE,
986 _("Character out of range for UTF-8"));
990 result_length += UTF8_LENGTH (str[i]);
993 result = g_malloc (result_length + 1);
997 while (p < result + result_length)
998 p += g_unichar_to_utf8 (str[i++], p);
1003 *items_written = p - result;
1012 #define SURROGATE_VALUE(h,l) (((h) - 0xd800) * 0x400 + (l) - 0xdc00 + 0x10000)
1016 * @str: a UTF-16 encoded string
1017 * @len: the maximum length of @str to use. If @len < 0, then
1018 * the string is terminated with a 0 character.
1019 * @items_read: location to store number of words read, or %NULL.
1020 * If %NULL, then %G_CONVERT_ERROR_PARTIAL_INPUT will be
1021 * returned in case @str contains a trailing partial
1022 * character. If an error occurs then the index of the
1023 * invalid input is stored here.
1024 * @items_written: location to store number of bytes written, or %NULL.
1025 * The value stored here does not include the trailing
1027 * @error: location to store the error occuring, or %NULL to ignore
1028 * errors. Any of the errors in #GConvertError other than
1029 * %G_CONVERT_ERROR_NO_CONVERSION may occur.
1031 * Convert a string from UTF-16 to UTF-8. The result will be
1032 * terminated with a 0 byte.
1034 * Note that the input is expected to be already in native endianness,
1035 * an initial byte-order-mark character is not handled specially.
1036 * g_convert() can be used to convert a byte buffer of UTF-16 data of
1037 * ambiguous endianess.
1039 * Return value: a pointer to a newly allocated UTF-8 string.
1040 * This value must be freed with g_free(). If an
1041 * error occurs, %NULL will be returned and
1045 g_utf16_to_utf8 (const gunichar2 *str,
1048 glong *items_written,
1051 /* This function and g_utf16_to_ucs4 are almost exactly identical - The lines that differ
1054 const gunichar2 *in;
1056 gchar *result = NULL;
1058 gunichar high_surrogate;
1060 g_return_val_if_fail (str != 0, NULL);
1065 while ((len < 0 || in - str < len) && *in)
1070 if (c >= 0xdc00 && c < 0xe000) /* low surrogate */
1074 wc = SURROGATE_VALUE (high_surrogate, c);
1079 g_set_error (error, G_CONVERT_ERROR, G_CONVERT_ERROR_ILLEGAL_SEQUENCE,
1080 _("Invalid sequence in conversion input"));
1088 g_set_error (error, G_CONVERT_ERROR, G_CONVERT_ERROR_ILLEGAL_SEQUENCE,
1089 _("Invalid sequence in conversion input"));
1093 if (c >= 0xd800 && c < 0xdc00) /* high surrogate */
1102 /********** DIFFERENT for UTF8/UCS4 **********/
1103 n_bytes += UTF8_LENGTH (wc);
1109 if (high_surrogate && !items_read)
1111 g_set_error (error, G_CONVERT_ERROR, G_CONVERT_ERROR_PARTIAL_INPUT,
1112 _("Partial character sequence at end of input"));
1116 /* At this point, everything is valid, and we just need to convert
1118 /********** DIFFERENT for UTF8/UCS4 **********/
1119 result = g_malloc (n_bytes + 1);
1124 while (out < result + n_bytes)
1129 if (c >= 0xdc00 && c < 0xe000) /* low surrogate */
1131 wc = SURROGATE_VALUE (high_surrogate, c);
1134 else if (c >= 0xd800 && c < 0xdc00) /* high surrogate */
1142 /********** DIFFERENT for UTF8/UCS4 **********/
1143 out += g_unichar_to_utf8 (wc, out);
1149 /********** DIFFERENT for UTF8/UCS4 **********/
1153 /********** DIFFERENT for UTF8/UCS4 **********/
1154 *items_written = out - result;
1158 *items_read = in - str;
1165 * @str: a UTF-16 encoded string
1166 * @len: the maximum length of @str to use. If @len < 0, then
1167 * the string is terminated with a 0 character.
1168 * @items_read: location to store number of words read, or %NULL.
1169 * If %NULL, then %G_CONVERT_ERROR_PARTIAL_INPUT will be
1170 * returned in case @str contains a trailing partial
1171 * character. If an error occurs then the index of the
1172 * invalid input is stored here.
1173 * @items_written: location to store number of characters written, or %NULL.
1174 * The value stored here does not include the trailing
1176 * @error: location to store the error occuring, or %NULL to ignore
1177 * errors. Any of the errors in #GConvertError other than
1178 * %G_CONVERT_ERROR_NO_CONVERSION may occur.
1180 * Convert a string from UTF-16 to UCS-4. The result will be
1181 * terminated with a 0 character.
1183 * Return value: a pointer to a newly allocated UCS-4 string.
1184 * This value must be freed with g_free(). If an
1185 * error occurs, %NULL will be returned and
1189 g_utf16_to_ucs4 (const gunichar2 *str,
1192 glong *items_written,
1195 const gunichar2 *in;
1197 gchar *result = NULL;
1199 gunichar high_surrogate;
1201 g_return_val_if_fail (str != 0, NULL);
1206 while ((len < 0 || in - str < len) && *in)
1211 if (c >= 0xdc00 && c < 0xe000) /* low surrogate */
1215 wc = SURROGATE_VALUE (high_surrogate, c);
1220 g_set_error (error, G_CONVERT_ERROR, G_CONVERT_ERROR_ILLEGAL_SEQUENCE,
1221 _("Invalid sequence in conversion input"));
1229 g_set_error (error, G_CONVERT_ERROR, G_CONVERT_ERROR_ILLEGAL_SEQUENCE,
1230 _("Invalid sequence in conversion input"));
1234 if (c >= 0xd800 && c < 0xdc00) /* high surrogate */
1243 /********** DIFFERENT for UTF8/UCS4 **********/
1244 n_bytes += sizeof (gunichar);
1250 if (high_surrogate && !items_read)
1252 g_set_error (error, G_CONVERT_ERROR, G_CONVERT_ERROR_PARTIAL_INPUT,
1253 _("Partial character sequence at end of input"));
1257 /* At this point, everything is valid, and we just need to convert
1259 /********** DIFFERENT for UTF8/UCS4 **********/
1260 result = g_malloc (n_bytes + 4);
1265 while (out < result + n_bytes)
1270 if (c >= 0xdc00 && c < 0xe000) /* low surrogate */
1272 wc = SURROGATE_VALUE (high_surrogate, c);
1275 else if (c >= 0xd800 && c < 0xdc00) /* high surrogate */
1283 /********** DIFFERENT for UTF8/UCS4 **********/
1284 *(gunichar *)out = wc;
1285 out += sizeof (gunichar);
1291 /********** DIFFERENT for UTF8/UCS4 **********/
1292 *(gunichar *)out = 0;
1295 /********** DIFFERENT for UTF8/UCS4 **********/
1296 *items_written = (out - result) / sizeof (gunichar);
1300 *items_read = in - str;
1302 return (gunichar *)result;
1307 * @str: a UTF-8 encoded string
1308 * @len: the maximum length of @str to use. If @len < 0, then
1309 * the string is nul-terminated.
1310 * @items_read: location to store number of bytes read, or %NULL.
1311 * If %NULL, then %G_CONVERT_ERROR_PARTIAL_INPUT will be
1312 * returned in case @str contains a trailing partial
1313 * character. If an error occurs then the index of the
1314 * invalid input is stored here.
1315 * @items_written: location to store number of words written, or %NULL.
1316 * The value stored here does not include the trailing
1318 * @error: location to store the error occuring, or %NULL to ignore
1319 * errors. Any of the errors in #GConvertError other than
1320 * %G_CONVERT_ERROR_NO_CONVERSION may occur.
1322 * Convert a string from UTF-8 to UTF-16. A 0 word will be
1323 * added to the result after the converted text.
1325 * Return value: a pointer to a newly allocated UTF-16 string.
1326 * This value must be freed with g_free(). If an
1327 * error occurs, %NULL will be returned and
1331 g_utf8_to_utf16 (const gchar *str,
1334 glong *items_written,
1337 gunichar2 *result = NULL;
1342 g_return_val_if_fail (str != NULL, NULL);
1346 while ((len < 0 || str + len - in > 0) && *in)
1348 gunichar wc = g_utf8_get_char_extended (in, str + len - in);
1349 if (wc & 0x80000000)
1351 if (wc == (gunichar)-2)
1356 g_set_error (error, G_CONVERT_ERROR, G_CONVERT_ERROR_PARTIAL_INPUT,
1357 _("Partial character sequence at end of input"));
1360 g_set_error (error, G_CONVERT_ERROR, G_CONVERT_ERROR_ILLEGAL_SEQUENCE,
1361 _("Invalid byte sequence in conversion input"));
1368 else if (wc < 0xe000)
1370 g_set_error (error, G_CONVERT_ERROR, G_CONVERT_ERROR_ILLEGAL_SEQUENCE,
1371 _("Invalid sequence in conversion input"));
1375 else if (wc < 0x10000)
1377 else if (wc < 0x110000)
1381 g_set_error (error, G_CONVERT_ERROR, G_CONVERT_ERROR_ILLEGAL_SEQUENCE,
1382 _("Character out of range for UTF-16"));
1387 in = g_utf8_next_char (in);
1390 result = g_new (gunichar2, n16 + 1);
1393 for (i = 0; i < n16;)
1395 gunichar wc = g_utf8_get_char (in);
1403 result[i++] = (wc - 0x10000) / 0x400 + 0xd800;
1404 result[i++] = (wc - 0x10000) % 0x400 + 0xdc00;
1407 in = g_utf8_next_char (in);
1413 *items_written = n16;
1417 *items_read = in - str;
1424 * @str: a UCS-4 encoded string
1425 * @len: the maximum length of @str to use. If @len < 0, then
1426 * the string is terminated with a 0 character.
1427 * @items_read: location to store number of bytes read, or %NULL.
1428 * If an error occurs then the index of the invalid input
1430 * @items_written: location to store number of words written, or %NULL.
1431 * The value stored here does not include the trailing
1433 * @error: location to store the error occuring, or %NULL to ignore
1434 * errors. Any of the errors in #GConvertError other than
1435 * %G_CONVERT_ERROR_NO_CONVERSION may occur.
1437 * Convert a string from UCS-4 to UTF-16. A 0 word will be
1438 * added to the result after the converted text.
1440 * Return value: a pointer to a newly allocated UTF-16 string.
1441 * This value must be freed with g_free(). If an
1442 * error occurs, %NULL will be returned and
1446 g_ucs4_to_utf16 (const gunichar *str,
1449 glong *items_written,
1452 gunichar2 *result = NULL;
1458 while ((len < 0 || i < len) && str[i])
1460 gunichar wc = str[i];
1464 else if (wc < 0xe000)
1466 g_set_error (error, G_CONVERT_ERROR, G_CONVERT_ERROR_ILLEGAL_SEQUENCE,
1467 _("Invalid sequence in conversion input"));
1471 else if (wc < 0x10000)
1473 else if (wc < 0x110000)
1477 g_set_error (error, G_CONVERT_ERROR, G_CONVERT_ERROR_ILLEGAL_SEQUENCE,
1478 _("Character out of range for UTF-16"));
1486 result = g_new (gunichar2, n16 + 1);
1488 for (i = 0, j = 0; j < n16; i++)
1490 gunichar wc = str[i];
1498 result[j++] = (wc - 0x10000) / 0x400 + 0xd800;
1499 result[j++] = (wc - 0x10000) % 0x400 + 0xdc00;
1505 *items_written = n16;
1514 #define CONTINUATION_CHAR \
1516 if ((*(guchar *)p & 0xc0) != 0x80) /* 10xxxxxx */ \
1519 val |= (*(guchar *)p) & 0x3f; \
1522 static const gchar *
1523 fast_validate (const char *str)
1530 for (p = str; *p; p++)
1532 if (*(guchar *)p < 128)
1539 if ((*(guchar *)p & 0xe0) == 0xc0) /* 110xxxxx */
1541 if (G_UNLIKELY ((*(guchar *)p & 0x1e) == 0))
1544 if (G_UNLIKELY ((*(guchar *)p & 0xc0) != 0x80)) /* 10xxxxxx */
1549 if ((*(guchar *)p & 0xf0) == 0xe0) /* 1110xxxx */
1552 val = *(guchar *)p & 0x0f;
1555 else if ((*(guchar *)p & 0xf8) == 0xf0) /* 11110xxx */
1558 val = *(guchar *)p & 0x07;
1571 if (G_UNLIKELY (val < min))
1574 if (G_UNLIKELY (!UNICODE_VALID(val)))
1588 static const gchar *
1589 fast_validate_len (const char *str,
1597 for (p = str; (max_len < 0 || (p - str) < max_len) && *p; p++)
1599 if (*(guchar *)p < 128)
1606 if ((*(guchar *)p & 0xe0) == 0xc0) /* 110xxxxx */
1608 if (G_UNLIKELY (max_len >= 0 && max_len - (p - str) < 2))
1611 if (G_UNLIKELY ((*(guchar *)p & 0x1e) == 0))
1614 if (G_UNLIKELY ((*(guchar *)p & 0xc0) != 0x80)) /* 10xxxxxx */
1619 if ((*(guchar *)p & 0xf0) == 0xe0) /* 1110xxxx */
1621 if (G_UNLIKELY (max_len >= 0 && max_len - (p - str) < 3))
1625 val = *(guchar *)p & 0x0f;
1628 else if ((*(guchar *)p & 0xf8) == 0xf0) /* 11110xxx */
1630 if (G_UNLIKELY (max_len >= 0 && max_len - (p - str) < 4))
1634 val = *(guchar *)p & 0x07;
1647 if (G_UNLIKELY (val < min))
1649 if (G_UNLIKELY (!UNICODE_VALID(val)))
1665 * @str: a pointer to character data
1666 * @max_len: max bytes to validate, or -1 to go until NUL
1667 * @end: return location for end of valid data
1669 * Validates UTF-8 encoded text. @str is the text to validate;
1670 * if @str is nul-terminated, then @max_len can be -1, otherwise
1671 * @max_len should be the number of bytes to validate.
1672 * If @end is non-%NULL, then the end of the valid range
1673 * will be stored there (i.e. the start of the first invalid
1674 * character if some bytes were invalid, or the end of the text
1675 * being validated otherwise).
1677 * Note that g_utf8_validate() returns %FALSE if @max_len is
1678 * positive and NUL is met before @max_len bytes have been read.
1680 * Returns %TRUE if all of @str was valid. Many GLib and GTK+
1681 * routines <emphasis>require</emphasis> valid UTF-8 as input;
1682 * so data read from a file or the network should be checked
1683 * with g_utf8_validate() before doing anything else with it.
1685 * Return value: %TRUE if the text was valid UTF-8
1688 g_utf8_validate (const char *str,
1696 p = fast_validate (str);
1698 p = fast_validate_len (str, max_len);
1703 if ((max_len >= 0 && p != str + max_len) ||
1704 (max_len < 0 && *p != '\0'))
1712 * g_unichar_validate:
1713 * @ch: a Unicode character
1715 * Checks whether @ch is a valid Unicode character. Some possible
1716 * integer values of @ch will not be valid. 0 is considered a valid
1717 * character, though it's normally a string terminator.
1719 * Return value: %TRUE if @ch is a valid Unicode character
1722 g_unichar_validate (gunichar ch)
1724 return UNICODE_VALID (ch);
1728 * g_utf8_strreverse:
1729 * @str: a UTF-8 encoded string
1730 * @len: the maximum length of @str to use. If @len < 0, then
1731 * the string is nul-terminated.
1733 * Reverses a UTF-8 string. @str must be valid UTF-8 encoded text.
1734 * (Use g_utf8_validate() on all text before trying to use UTF-8
1735 * utility functions with it.)
1737 * Note that unlike g_strreverse(), this function returns
1738 * newly-allocated memory, which should be freed with g_free() when
1741 * Returns: a newly-allocated string which is the reverse of @str.
1746 g_utf8_strreverse (const gchar *str,
1756 result = g_new (gchar, len + 1);
1761 skip = g_utf8_skip[*(guchar*)p];
1763 for (m = r; skip; skip--)
1771 #define __G_UTF8_C__
1772 #include "galiasdef.c"