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.
30 #ifdef G_PLATFORM_WIN32
39 #include "gstrfuncs.h"
40 #include "gtestutils.h"
45 #define UTF8_COMPUTE(Char, Mask, Len) \
51 else if ((Char & 0xe0) == 0xc0) \
56 else if ((Char & 0xf0) == 0xe0) \
61 else if ((Char & 0xf8) == 0xf0) \
66 else if ((Char & 0xfc) == 0xf8) \
71 else if ((Char & 0xfe) == 0xfc) \
79 #define UTF8_LENGTH(Char) \
80 ((Char) < 0x80 ? 1 : \
81 ((Char) < 0x800 ? 2 : \
82 ((Char) < 0x10000 ? 3 : \
83 ((Char) < 0x200000 ? 4 : \
84 ((Char) < 0x4000000 ? 5 : 6)))))
87 #define UTF8_GET(Result, Chars, Count, Mask, Len) \
88 (Result) = (Chars)[0] & (Mask); \
89 for ((Count) = 1; (Count) < (Len); ++(Count)) \
91 if (((Chars)[(Count)] & 0xc0) != 0x80) \
97 (Result) |= ((Chars)[(Count)] & 0x3f); \
101 * Check whether a Unicode (5.2) char is in a valid range.
103 * The first check comes from the Unicode guarantee to never encode
104 * a point above 0x0010ffff, since UTF-16 couldn't represent it.
106 * The second check covers surrogate pairs (category Cs).
108 * The last two checks cover "Noncharacter": defined as:
109 * "A code point that is permanently reserved for
110 * internal use, and that should never be interchanged. In
111 * Unicode 3.1, these consist of the values U+nFFFE and U+nFFFF
112 * (where n is from 0 to 10_16) and the values U+FDD0..U+FDEF."
114 * @param Char the character
116 #define UNICODE_VALID(Char) \
117 ((Char) < 0x110000 && \
118 (((Char) & 0xFFFFF800) != 0xD800) && \
119 ((Char) < 0xFDD0 || (Char) > 0xFDEF) && \
120 ((Char) & 0xFFFE) != 0xFFFE)
123 static const gchar utf8_skip_data[256] = {
124 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,
125 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,
126 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,
127 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,
128 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,
129 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,
130 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,
131 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
134 const gchar * const g_utf8_skip = utf8_skip_data;
137 * g_utf8_find_prev_char:
138 * @str: pointer to the beginning of a UTF-8 encoded string
139 * @p: pointer to some position within @str
141 * Given a position @p with a UTF-8 encoded string @str, find the start
142 * of the previous UTF-8 character starting before @p. Returns %NULL if no
143 * UTF-8 characters are present in @str before @p.
145 * @p does not have to be at the beginning of a UTF-8 character. No check
146 * is made to see if the character found is actually valid other than
147 * it starts with an appropriate byte.
149 * Return value: a pointer to the found character or %NULL.
152 g_utf8_find_prev_char (const char *str,
155 for (--p; p >= str; --p)
157 if ((*p & 0xc0) != 0x80)
164 * g_utf8_find_next_char:
165 * @p: a pointer to a position within a UTF-8 encoded string
166 * @end: a pointer to the byte following the end of the string,
167 * or %NULL to indicate that the string is nul-terminated.
169 * Finds the start of the next UTF-8 character in the string after @p.
171 * @p does not have to be at the beginning of a UTF-8 character. No check
172 * is made to see if the character found is actually valid other than
173 * it starts with an appropriate byte.
175 * Return value: a pointer to the found character or %NULL
178 g_utf8_find_next_char (const gchar *p,
184 for (++p; p < end && (*p & 0xc0) == 0x80; ++p)
187 for (++p; (*p & 0xc0) == 0x80; ++p)
190 return (p == end) ? NULL : (gchar *)p;
195 * @p: a pointer to a position within a UTF-8 encoded string
197 * Finds the previous UTF-8 character in the string before @p.
199 * @p does not have to be at the beginning of a UTF-8 character. No check
200 * is made to see if the character found is actually valid other than
201 * it starts with an appropriate byte. If @p might be the first
202 * character of the string, you must use g_utf8_find_prev_char() instead.
204 * Return value: a pointer to the found character.
207 g_utf8_prev_char (const gchar *p)
212 if ((*p & 0xc0) != 0x80)
219 * @p: pointer to the start of a UTF-8 encoded string
220 * @max: the maximum number of bytes to examine. If @max
221 * is less than 0, then the string is assumed to be
222 * nul-terminated. If @max is 0, @p will not be examined and
225 * Computes the length of the string in characters, not including
226 * the terminating nul character.
228 * Return value: the length of the string in characters
231 g_utf8_strlen (const gchar *p,
235 const gchar *start = p;
236 g_return_val_if_fail (p != NULL || max == 0, 0);
242 p = g_utf8_next_char (p);
251 p = g_utf8_next_char (p);
253 while (p - start < max && *p)
256 p = g_utf8_next_char (p);
259 /* only do the last len increment if we got a complete
260 * char (don't count partial chars)
262 if (p - start <= max)
271 * @str: a UTF-8 encoded string
272 * @start_pos: a character offset within @str
273 * @end_pos: another character offset within @str
275 * Copies a substring out of a UTF-8 encoded string.
276 * The substring will contain @end_pos - @start_pos
279 * Returns: a newly allocated copy of the requested
280 * substring. Free with g_free() when no longer needed.
285 g_utf8_substring (const gchar *str,
289 gchar *start, *end, *out;
291 start = g_utf8_offset_to_pointer (str, start_pos);
292 end = g_utf8_offset_to_pointer (start, end_pos - start_pos);
294 out = g_malloc (end - start + 1);
295 memcpy (out, start, end - start);
296 out[end - start] = 0;
303 * @p: a pointer to Unicode character encoded as UTF-8
305 * Converts a sequence of bytes encoded as UTF-8 to a Unicode character.
306 * If @p does not point to a valid UTF-8 encoded character, results are
307 * undefined. If you are not sure that the bytes are complete
308 * valid Unicode characters, you should use g_utf8_get_char_validated()
311 * Return value: the resulting character
314 g_utf8_get_char (const gchar *p)
316 int i, mask = 0, len;
318 unsigned char c = (unsigned char) *p;
320 UTF8_COMPUTE (c, mask, len);
323 UTF8_GET (result, p, i, mask, len);
329 * g_utf8_offset_to_pointer:
330 * @str: a UTF-8 encoded string
331 * @offset: a character offset within @str
333 * Converts from an integer character offset to a pointer to a position
336 * Since 2.10, this function allows to pass a negative @offset to
337 * step backwards. It is usually worth stepping backwards from the end
338 * instead of forwards if @offset is in the last fourth of the string,
339 * since moving forward is about 3 times faster than moving backward.
342 * This function doesn't abort when reaching the end of @str. Therefore
343 * you should be sure that @offset is within string boundaries before
344 * calling that function. Call g_utf8_strlen() when unsure.
346 * This limitation exists as this function is called frequently during
347 * text rendering and therefore has to be as fast as possible.
350 * Return value: the resulting pointer
353 g_utf8_offset_to_pointer (const gchar *str,
356 const gchar *s = str;
360 s = g_utf8_next_char (s);
365 /* This nice technique for fast backwards stepping
366 * through a UTF-8 string was dubbed "stutter stepping"
367 * by its inventor, Larry Ewing.
373 while ((*s & 0xc0) == 0x80)
376 offset += g_utf8_pointer_to_offset (s, s1);
384 * g_utf8_pointer_to_offset:
385 * @str: a UTF-8 encoded string
386 * @pos: a pointer to a position within @str
388 * Converts from a pointer to position within a string to a integer
391 * Since 2.10, this function allows @pos to be before @str, and returns
392 * a negative offset in this case.
394 * Return value: the resulting character offset
397 g_utf8_pointer_to_offset (const gchar *str,
400 const gchar *s = str;
404 offset = - g_utf8_pointer_to_offset (pos, str);
408 s = g_utf8_next_char (s);
418 * @dest: buffer to fill with characters from @src
419 * @src: UTF-8 encoded string
420 * @n: character count
422 * Like the standard C strncpy() function, but
423 * copies a given number of characters instead of a given number of
424 * bytes. The @src string must be valid UTF-8 encoded text.
425 * (Use g_utf8_validate() on all text before trying to use UTF-8
426 * utility functions with it.)
428 * Return value: @dest
431 g_utf8_strncpy (gchar *dest,
435 const gchar *s = src;
438 s = g_utf8_next_char(s);
441 strncpy(dest, src, s - src);
450 * @c: a Unicode character code
451 * @outbuf: output buffer, must have at least 6 bytes of space.
452 * If %NULL, the length will be computed and returned
453 * and nothing will be written to @outbuf.
455 * Converts a single character to UTF-8.
457 * Return value: number of bytes written
460 g_unichar_to_utf8 (gunichar c,
463 /* If this gets modified, also update the copy in g_string_insert_unichar() */
478 else if (c < 0x10000)
483 else if (c < 0x200000)
488 else if (c < 0x4000000)
501 for (i = len - 1; i > 0; --i)
503 outbuf[i] = (c & 0x3f) | 0x80;
506 outbuf[0] = c | first;
514 * @p: a nul-terminated UTF-8 encoded string
515 * @len: the maximum length of @p
516 * @c: a Unicode character
518 * Finds the leftmost occurrence of the given Unicode character
519 * in a UTF-8 encoded string, while limiting the search to @len bytes.
520 * If @len is -1, allow unbounded search.
522 * Return value: %NULL if the string does not contain the character,
523 * otherwise, a pointer to the start of the leftmost occurrence of
524 * the character in the string.
527 g_utf8_strchr (const char *p,
533 gint charlen = g_unichar_to_utf8 (c, ch);
536 return g_strstr_len (p, len, ch);
542 * @p: a nul-terminated UTF-8 encoded string
543 * @len: the maximum length of @p
544 * @c: a Unicode character
546 * Find the rightmost occurrence of the given Unicode character
547 * in a UTF-8 encoded string, while limiting the search to @len bytes.
548 * If @len is -1, allow unbounded search.
550 * Return value: %NULL if the string does not contain the character,
551 * otherwise, a pointer to the start of the rightmost occurrence of the
552 * character in the string.
555 g_utf8_strrchr (const char *p,
561 gint charlen = g_unichar_to_utf8 (c, ch);
564 return g_strrstr_len (p, len, ch);
568 /* Like g_utf8_get_char, but take a maximum length
569 * and return (gunichar)-2 on incomplete trailing character;
570 * also check for malformed or overlong sequences
571 * and return (gunichar)-1 in this case.
573 static inline gunichar
574 g_utf8_get_char_extended (const gchar *p,
579 gunichar wc = (guchar) *p;
585 else if (G_UNLIKELY (wc < 0xc0))
624 if (G_UNLIKELY (max_len >= 0 && len > max_len))
626 for (i = 1; i < max_len; i++)
628 if ((((guchar *)p)[i] & 0xc0) != 0x80)
634 for (i = 1; i < len; ++i)
636 gunichar ch = ((guchar *)p)[i];
638 if (G_UNLIKELY ((ch & 0xc0) != 0x80))
650 if (G_UNLIKELY (wc < min_code))
657 * g_utf8_get_char_validated:
658 * @p: a pointer to Unicode character encoded as UTF-8
659 * @max_len: the maximum number of bytes to read, or -1, for no maximum or
660 * if @p is nul-terminated
662 * Convert a sequence of bytes encoded as UTF-8 to a Unicode character.
663 * This function checks for incomplete characters, for invalid characters
664 * such as characters that are out of the range of Unicode, and for
665 * overlong encodings of valid characters.
667 * Return value: the resulting character. If @p points to a partial
668 * sequence at the end of a string that could begin a valid
669 * character (or if @max_len is zero), returns (gunichar)-2;
670 * otherwise, if @p does not point to a valid UTF-8 encoded
671 * Unicode character, returns (gunichar)-1.
674 g_utf8_get_char_validated (const gchar *p,
682 result = g_utf8_get_char_extended (p, max_len);
684 if (result & 0x80000000)
686 else if (!UNICODE_VALID (result))
693 * g_utf8_to_ucs4_fast:
694 * @str: a UTF-8 encoded string
695 * @len: the maximum length of @str to use, in bytes. If @len < 0,
696 * then the string is nul-terminated.
697 * @items_written: location to store the number of characters in the
700 * Convert a string from UTF-8 to a 32-bit fixed width
701 * representation as UCS-4, assuming valid UTF-8 input.
702 * This function is roughly twice as fast as g_utf8_to_ucs4()
703 * but does no error checking on the input. A trailing 0 character
704 * will be added to the string after the converted text.
706 * Return value: a pointer to a newly allocated UCS-4 string.
707 * This value must be freed with g_free().
710 g_utf8_to_ucs4_fast (const gchar *str,
712 glong *items_written)
718 g_return_val_if_fail (str != NULL, NULL);
726 p = g_utf8_next_char (p);
732 while (p < str + len && *p)
734 p = g_utf8_next_char (p);
739 result = g_new (gunichar, n_chars + 1);
742 for (i=0; i < n_chars; i++)
744 gunichar wc = (guchar)*p++;
752 gunichar mask = 0x40;
754 if (G_UNLIKELY ((wc & mask) == 0))
756 /* It's an out-of-sequence 10xxxxxxx byte.
757 * Rather than making an ugly hash of this and the next byte
758 * and overrunning the buffer, it's more useful to treat it
759 * with a replacement character */
767 wc |= (guchar)(*p++) & 0x3f;
770 while((wc & mask) != 0);
787 * @str: a UTF-8 encoded string
788 * @len: the maximum length of @str to use, in bytes. If @len < 0,
789 * then the string is nul-terminated.
790 * @items_read: location to store number of bytes read, or %NULL.
791 * If %NULL, then %G_CONVERT_ERROR_PARTIAL_INPUT will be
792 * returned in case @str contains a trailing partial
793 * character. If an error occurs then the index of the
794 * invalid input is stored here.
795 * @items_written: location to store number of characters written or %NULL.
796 * The value here stored does not include the trailing 0
798 * @error: location to store the error occurring, or %NULL to ignore
799 * errors. Any of the errors in #GConvertError other than
800 * %G_CONVERT_ERROR_NO_CONVERSION may occur.
802 * Convert a string from UTF-8 to a 32-bit fixed width
803 * representation as UCS-4. A trailing 0 character will be added to the
804 * string after the converted text.
806 * Return value: a pointer to a newly allocated UCS-4 string.
807 * This value must be freed with g_free(). If an
808 * error occurs, %NULL will be returned and
812 g_utf8_to_ucs4 (const gchar *str,
815 glong *items_written,
818 gunichar *result = NULL;
824 while ((len < 0 || str + len - in > 0) && *in)
826 gunichar wc = g_utf8_get_char_extended (in, len < 0 ? 6 : str + len - in);
829 if (wc == (gunichar)-2)
834 g_set_error_literal (error, G_CONVERT_ERROR, G_CONVERT_ERROR_PARTIAL_INPUT,
835 _("Partial character sequence at end of input"));
838 g_set_error_literal (error, G_CONVERT_ERROR, G_CONVERT_ERROR_ILLEGAL_SEQUENCE,
839 _("Invalid byte sequence in conversion input"));
846 in = g_utf8_next_char (in);
849 result = g_new (gunichar, n_chars + 1);
852 for (i=0; i < n_chars; i++)
854 result[i] = g_utf8_get_char (in);
855 in = g_utf8_next_char (in);
860 *items_written = n_chars;
864 *items_read = in - str;
871 * @str: a UCS-4 encoded string
872 * @len: the maximum length (number of characters) of @str to use.
873 * If @len < 0, then the string is nul-terminated.
874 * @items_read: location to store number of characters read, or %NULL.
875 * @items_written: location to store number of bytes written or %NULL.
876 * The value here stored does not include the trailing 0
878 * @error: location to store the error occurring, or %NULL to ignore
879 * errors. Any of the errors in #GConvertError other than
880 * %G_CONVERT_ERROR_NO_CONVERSION may occur.
882 * Convert a string from a 32-bit fixed width representation as UCS-4.
883 * to UTF-8. The result will be terminated with a 0 byte.
885 * Return value: a pointer to a newly allocated UTF-8 string.
886 * This value must be freed with g_free(). If an
887 * error occurs, %NULL will be returned and
888 * @error set. In that case, @items_read will be
889 * set to the position of the first invalid input
893 g_ucs4_to_utf8 (const gunichar *str,
896 glong *items_written,
900 gchar *result = NULL;
905 for (i = 0; len < 0 || i < len ; i++)
910 if (str[i] >= 0x80000000)
912 g_set_error_literal (error, G_CONVERT_ERROR, G_CONVERT_ERROR_ILLEGAL_SEQUENCE,
913 _("Character out of range for UTF-8"));
917 result_length += UTF8_LENGTH (str[i]);
920 result = g_malloc (result_length + 1);
924 while (p < result + result_length)
925 p += g_unichar_to_utf8 (str[i++], p);
930 *items_written = p - result;
939 #define SURROGATE_VALUE(h,l) (((h) - 0xd800) * 0x400 + (l) - 0xdc00 + 0x10000)
943 * @str: a UTF-16 encoded string
944 * @len: the maximum length (number of <type>gunichar2</type>) of @str to use.
945 * If @len < 0, then the string is nul-terminated.
946 * @items_read: location to store number of words read, or %NULL.
947 * If %NULL, then %G_CONVERT_ERROR_PARTIAL_INPUT will be
948 * returned in case @str contains a trailing partial
949 * character. If an error occurs then the index of the
950 * invalid input is stored here.
951 * @items_written: location to store number of bytes written, or %NULL.
952 * The value stored here does not include the trailing
954 * @error: location to store the error occurring, or %NULL to ignore
955 * errors. Any of the errors in #GConvertError other than
956 * %G_CONVERT_ERROR_NO_CONVERSION may occur.
958 * Convert a string from UTF-16 to UTF-8. The result will be
959 * terminated with a 0 byte.
961 * Note that the input is expected to be already in native endianness,
962 * an initial byte-order-mark character is not handled specially.
963 * g_convert() can be used to convert a byte buffer of UTF-16 data of
964 * ambiguous endianess.
966 * Further note that this function does not validate the result
967 * string; it may e.g. include embedded NUL characters. The only
968 * validation done by this function is to ensure that the input can
969 * be correctly interpreted as UTF-16, i.e. it doesn't contain
970 * things unpaired surrogates.
972 * Return value: a pointer to a newly allocated UTF-8 string.
973 * This value must be freed with g_free(). If an
974 * error occurs, %NULL will be returned and
978 g_utf16_to_utf8 (const gunichar2 *str,
981 glong *items_written,
984 /* This function and g_utf16_to_ucs4 are almost exactly identical - The lines that differ
989 gchar *result = NULL;
991 gunichar high_surrogate;
993 g_return_val_if_fail (str != NULL, NULL);
998 while ((len < 0 || in - str < len) && *in)
1003 if (c >= 0xdc00 && c < 0xe000) /* low surrogate */
1007 wc = SURROGATE_VALUE (high_surrogate, c);
1012 g_set_error_literal (error, G_CONVERT_ERROR, G_CONVERT_ERROR_ILLEGAL_SEQUENCE,
1013 _("Invalid sequence in conversion input"));
1021 g_set_error_literal (error, G_CONVERT_ERROR, G_CONVERT_ERROR_ILLEGAL_SEQUENCE,
1022 _("Invalid sequence in conversion input"));
1026 if (c >= 0xd800 && c < 0xdc00) /* high surrogate */
1035 /********** DIFFERENT for UTF8/UCS4 **********/
1036 n_bytes += UTF8_LENGTH (wc);
1042 if (high_surrogate && !items_read)
1044 g_set_error_literal (error, G_CONVERT_ERROR, G_CONVERT_ERROR_PARTIAL_INPUT,
1045 _("Partial character sequence at end of input"));
1049 /* At this point, everything is valid, and we just need to convert
1051 /********** DIFFERENT for UTF8/UCS4 **********/
1052 result = g_malloc (n_bytes + 1);
1057 while (out < result + n_bytes)
1062 if (c >= 0xdc00 && c < 0xe000) /* low surrogate */
1064 wc = SURROGATE_VALUE (high_surrogate, c);
1067 else if (c >= 0xd800 && c < 0xdc00) /* high surrogate */
1075 /********** DIFFERENT for UTF8/UCS4 **********/
1076 out += g_unichar_to_utf8 (wc, out);
1082 /********** DIFFERENT for UTF8/UCS4 **********/
1086 /********** DIFFERENT for UTF8/UCS4 **********/
1087 *items_written = out - result;
1091 *items_read = in - str;
1098 * @str: a UTF-16 encoded string
1099 * @len: the maximum length (number of <type>gunichar2</type>) of @str to use.
1100 * If @len < 0, then the string is nul-terminated.
1101 * @items_read: location to store number of words read, or %NULL.
1102 * If %NULL, then %G_CONVERT_ERROR_PARTIAL_INPUT will be
1103 * returned in case @str contains a trailing partial
1104 * character. If an error occurs then the index of the
1105 * invalid input is stored here.
1106 * @items_written: location to store number of characters written, or %NULL.
1107 * The value stored here does not include the trailing
1109 * @error: location to store the error occurring, or %NULL to ignore
1110 * errors. Any of the errors in #GConvertError other than
1111 * %G_CONVERT_ERROR_NO_CONVERSION may occur.
1113 * Convert a string from UTF-16 to UCS-4. The result will be
1116 * Return value: a pointer to a newly allocated UCS-4 string.
1117 * This value must be freed with g_free(). If an
1118 * error occurs, %NULL will be returned and
1122 g_utf16_to_ucs4 (const gunichar2 *str,
1125 glong *items_written,
1128 const gunichar2 *in;
1130 gchar *result = NULL;
1132 gunichar high_surrogate;
1134 g_return_val_if_fail (str != NULL, NULL);
1139 while ((len < 0 || in - str < len) && *in)
1143 if (c >= 0xdc00 && c < 0xe000) /* low surrogate */
1151 g_set_error_literal (error, G_CONVERT_ERROR, G_CONVERT_ERROR_ILLEGAL_SEQUENCE,
1152 _("Invalid sequence in conversion input"));
1160 g_set_error_literal (error, G_CONVERT_ERROR, G_CONVERT_ERROR_ILLEGAL_SEQUENCE,
1161 _("Invalid sequence in conversion input"));
1165 if (c >= 0xd800 && c < 0xdc00) /* high surrogate */
1172 /********** DIFFERENT for UTF8/UCS4 **********/
1173 n_bytes += sizeof (gunichar);
1179 if (high_surrogate && !items_read)
1181 g_set_error_literal (error, G_CONVERT_ERROR, G_CONVERT_ERROR_PARTIAL_INPUT,
1182 _("Partial character sequence at end of input"));
1186 /* At this point, everything is valid, and we just need to convert
1188 /********** DIFFERENT for UTF8/UCS4 **********/
1189 result = g_malloc (n_bytes + 4);
1194 while (out < result + n_bytes)
1199 if (c >= 0xdc00 && c < 0xe000) /* low surrogate */
1201 wc = SURROGATE_VALUE (high_surrogate, c);
1204 else if (c >= 0xd800 && c < 0xdc00) /* high surrogate */
1212 /********** DIFFERENT for UTF8/UCS4 **********/
1213 *(gunichar *)out = wc;
1214 out += sizeof (gunichar);
1220 /********** DIFFERENT for UTF8/UCS4 **********/
1221 *(gunichar *)out = 0;
1224 /********** DIFFERENT for UTF8/UCS4 **********/
1225 *items_written = (out - result) / sizeof (gunichar);
1229 *items_read = in - str;
1231 return (gunichar *)result;
1236 * @str: a UTF-8 encoded string
1237 * @len: the maximum length (number of bytes) of @str to use.
1238 * If @len < 0, then the string is nul-terminated.
1239 * @items_read: location to store number of bytes read, or %NULL.
1240 * If %NULL, then %G_CONVERT_ERROR_PARTIAL_INPUT will be
1241 * returned in case @str contains a trailing partial
1242 * character. If an error occurs then the index of the
1243 * invalid input is stored here.
1244 * @items_written: location to store number of <type>gunichar2</type> written,
1246 * The value stored here does not include the trailing 0.
1247 * @error: location to store the error occurring, or %NULL to ignore
1248 * errors. Any of the errors in #GConvertError other than
1249 * %G_CONVERT_ERROR_NO_CONVERSION may occur.
1251 * Convert a string from UTF-8 to UTF-16. A 0 character will be
1252 * added to the result after the converted text.
1254 * Return value: a pointer to a newly allocated UTF-16 string.
1255 * This value must be freed with g_free(). If an
1256 * error occurs, %NULL will be returned and
1260 g_utf8_to_utf16 (const gchar *str,
1263 glong *items_written,
1266 gunichar2 *result = NULL;
1271 g_return_val_if_fail (str != NULL, NULL);
1275 while ((len < 0 || str + len - in > 0) && *in)
1277 gunichar wc = g_utf8_get_char_extended (in, len < 0 ? 6 : str + len - in);
1278 if (wc & 0x80000000)
1280 if (wc == (gunichar)-2)
1285 g_set_error_literal (error, G_CONVERT_ERROR, G_CONVERT_ERROR_PARTIAL_INPUT,
1286 _("Partial character sequence at end of input"));
1289 g_set_error_literal (error, G_CONVERT_ERROR, G_CONVERT_ERROR_ILLEGAL_SEQUENCE,
1290 _("Invalid byte sequence in conversion input"));
1297 else if (wc < 0xe000)
1299 g_set_error_literal (error, G_CONVERT_ERROR, G_CONVERT_ERROR_ILLEGAL_SEQUENCE,
1300 _("Invalid sequence in conversion input"));
1304 else if (wc < 0x10000)
1306 else if (wc < 0x110000)
1310 g_set_error_literal (error, G_CONVERT_ERROR, G_CONVERT_ERROR_ILLEGAL_SEQUENCE,
1311 _("Character out of range for UTF-16"));
1316 in = g_utf8_next_char (in);
1319 result = g_new (gunichar2, n16 + 1);
1322 for (i = 0; i < n16;)
1324 gunichar wc = g_utf8_get_char (in);
1332 result[i++] = (wc - 0x10000) / 0x400 + 0xd800;
1333 result[i++] = (wc - 0x10000) % 0x400 + 0xdc00;
1336 in = g_utf8_next_char (in);
1342 *items_written = n16;
1346 *items_read = in - str;
1353 * @str: a UCS-4 encoded string
1354 * @len: the maximum length (number of characters) of @str to use.
1355 * If @len < 0, then the string is nul-terminated.
1356 * @items_read: location to store number of bytes read, or %NULL.
1357 * If an error occurs then the index of the invalid input
1359 * @items_written: location to store number of <type>gunichar2</type>
1360 * written, or %NULL. The value stored here does not
1361 * include the trailing 0.
1362 * @error: location to store the error occurring, or %NULL to ignore
1363 * errors. Any of the errors in #GConvertError other than
1364 * %G_CONVERT_ERROR_NO_CONVERSION may occur.
1366 * Convert a string from UCS-4 to UTF-16. A 0 character will be
1367 * added to the result after the converted text.
1369 * Return value: a pointer to a newly allocated UTF-16 string.
1370 * This value must be freed with g_free(). If an
1371 * error occurs, %NULL will be returned and
1375 g_ucs4_to_utf16 (const gunichar *str,
1378 glong *items_written,
1381 gunichar2 *result = NULL;
1387 while ((len < 0 || i < len) && str[i])
1389 gunichar wc = str[i];
1393 else if (wc < 0xe000)
1395 g_set_error_literal (error, G_CONVERT_ERROR, G_CONVERT_ERROR_ILLEGAL_SEQUENCE,
1396 _("Invalid sequence in conversion input"));
1400 else if (wc < 0x10000)
1402 else if (wc < 0x110000)
1406 g_set_error_literal (error, G_CONVERT_ERROR, G_CONVERT_ERROR_ILLEGAL_SEQUENCE,
1407 _("Character out of range for UTF-16"));
1415 result = g_new (gunichar2, n16 + 1);
1417 for (i = 0, j = 0; j < n16; i++)
1419 gunichar wc = str[i];
1427 result[j++] = (wc - 0x10000) / 0x400 + 0xd800;
1428 result[j++] = (wc - 0x10000) % 0x400 + 0xdc00;
1434 *items_written = n16;
1443 #define CONTINUATION_CHAR \
1445 if ((*(guchar *)p & 0xc0) != 0x80) /* 10xxxxxx */ \
1448 val |= (*(guchar *)p) & 0x3f; \
1451 static const gchar *
1452 fast_validate (const char *str)
1459 for (p = str; *p; p++)
1461 if (*(guchar *)p < 128)
1468 if ((*(guchar *)p & 0xe0) == 0xc0) /* 110xxxxx */
1470 if (G_UNLIKELY ((*(guchar *)p & 0x1e) == 0))
1473 if (G_UNLIKELY ((*(guchar *)p & 0xc0) != 0x80)) /* 10xxxxxx */
1478 if ((*(guchar *)p & 0xf0) == 0xe0) /* 1110xxxx */
1481 val = *(guchar *)p & 0x0f;
1484 else if ((*(guchar *)p & 0xf8) == 0xf0) /* 11110xxx */
1487 val = *(guchar *)p & 0x07;
1500 if (G_UNLIKELY (val < min))
1503 if (G_UNLIKELY (!UNICODE_VALID(val)))
1517 static const gchar *
1518 fast_validate_len (const char *str,
1526 g_assert (max_len >= 0);
1528 for (p = str; ((p - str) < max_len) && *p; p++)
1530 if (*(guchar *)p < 128)
1537 if ((*(guchar *)p & 0xe0) == 0xc0) /* 110xxxxx */
1539 if (G_UNLIKELY (max_len - (p - str) < 2))
1542 if (G_UNLIKELY ((*(guchar *)p & 0x1e) == 0))
1545 if (G_UNLIKELY ((*(guchar *)p & 0xc0) != 0x80)) /* 10xxxxxx */
1550 if ((*(guchar *)p & 0xf0) == 0xe0) /* 1110xxxx */
1552 if (G_UNLIKELY (max_len - (p - str) < 3))
1556 val = *(guchar *)p & 0x0f;
1559 else if ((*(guchar *)p & 0xf8) == 0xf0) /* 11110xxx */
1561 if (G_UNLIKELY (max_len - (p - str) < 4))
1565 val = *(guchar *)p & 0x07;
1578 if (G_UNLIKELY (val < min))
1580 if (G_UNLIKELY (!UNICODE_VALID(val)))
1596 * @str: a pointer to character data
1597 * @max_len: max bytes to validate, or -1 to go until NUL
1598 * @end: (allow-none) (out): return location for end of valid data
1600 * Validates UTF-8 encoded text. @str is the text to validate;
1601 * if @str is nul-terminated, then @max_len can be -1, otherwise
1602 * @max_len should be the number of bytes to validate.
1603 * If @end is non-%NULL, then the end of the valid range
1604 * will be stored there (i.e. the start of the first invalid
1605 * character if some bytes were invalid, or the end of the text
1606 * being validated otherwise).
1608 * Note that g_utf8_validate() returns %FALSE if @max_len is
1609 * positive and NUL is met before @max_len bytes have been read.
1611 * Returns %TRUE if all of @str was valid. Many GLib and GTK+
1612 * routines <emphasis>require</emphasis> valid UTF-8 as input;
1613 * so data read from a file or the network should be checked
1614 * with g_utf8_validate() before doing anything else with it.
1616 * Return value: %TRUE if the text was valid UTF-8
1619 g_utf8_validate (const char *str,
1627 p = fast_validate (str);
1629 p = fast_validate_len (str, max_len);
1634 if ((max_len >= 0 && p != str + max_len) ||
1635 (max_len < 0 && *p != '\0'))
1642 * g_unichar_validate:
1643 * @ch: a Unicode character
1645 * Checks whether @ch is a valid Unicode character. Some possible
1646 * integer values of @ch will not be valid. 0 is considered a valid
1647 * character, though it's normally a string terminator.
1649 * Return value: %TRUE if @ch is a valid Unicode character
1652 g_unichar_validate (gunichar ch)
1654 return UNICODE_VALID (ch);
1658 * g_utf8_strreverse:
1659 * @str: a UTF-8 encoded string
1660 * @len: the maximum length of @str to use, in bytes. If @len < 0,
1661 * then the string is nul-terminated.
1663 * Reverses a UTF-8 string. @str must be valid UTF-8 encoded text.
1664 * (Use g_utf8_validate() on all text before trying to use UTF-8
1665 * utility functions with it.)
1667 * This function is intended for programmatic uses of reversed strings.
1668 * It pays no attention to decomposed characters, combining marks, byte
1669 * order marks, directional indicators (LRM, LRO, etc) and similar
1670 * characters which might need special handling when reversing a string
1671 * for display purposes.
1673 * Note that unlike g_strreverse(), this function returns
1674 * newly-allocated memory, which should be freed with g_free() when
1677 * Returns: a newly-allocated string which is the reverse of @str.
1682 g_utf8_strreverse (const gchar *str,
1691 result = g_new (gchar, len + 1);
1696 gchar *m, skip = g_utf8_skip[*(guchar*) p];
1698 for (m = r; skip; skip--)
1708 _g_utf8_make_valid (const gchar *name)
1711 const gchar *remainder, *invalid;
1712 gint remaining_bytes, valid_bytes;
1714 g_return_val_if_fail (name != NULL, NULL);
1718 remaining_bytes = strlen (name);
1720 while (remaining_bytes != 0)
1722 if (g_utf8_validate (remainder, remaining_bytes, &invalid))
1724 valid_bytes = invalid - remainder;
1727 string = g_string_sized_new (remaining_bytes);
1729 g_string_append_len (string, remainder, valid_bytes);
1730 /* append U+FFFD REPLACEMENT CHARACTER */
1731 g_string_append (string, "\357\277\275");
1733 remaining_bytes -= valid_bytes + 1;
1734 remainder = invalid + 1;
1738 return g_strdup (name);
1740 g_string_append (string, remainder);
1742 g_assert (g_utf8_validate (string->str, -1, NULL));
1744 return g_string_free (string, FALSE);