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) & 0xFFFFF800) != 0xD800) && \
101 ((Char) < 0xFDD0 || (Char) > 0xFDEF) && \
102 ((Char) & 0xFFFE) != 0xFFFE)
105 static const gchar utf8_skip_data
[256] = {
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 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 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,
113 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
116 const gchar
* const g_utf8_skip
= utf8_skip_data
;
119 * g_utf8_find_prev_char:
120 * @str: pointer to the beginning of a UTF-8 encoded string
121 * @p: pointer to some position within @str
123 * Given a position @p with a UTF-8 encoded string @str, find the start
124 * of the previous UTF-8 character starting before @p. Returns %NULL if no
125 * UTF-8 characters are present in @p before @str.
127 * @p does not have to be at the beginning of a UTF-8 character. No check
128 * is made to see if the character found is actually valid other than
129 * it starts with an appropriate byte.
131 * Return value: a pointer to the found character or %NULL.
134 g_utf8_find_prev_char (const char *str
,
137 for (--p
; p
>= str
; --p
)
139 if ((*p
& 0xc0) != 0x80)
146 * g_utf8_find_next_char:
147 * @p: a pointer to a position within a UTF-8 encoded string
148 * @end: a pointer to the end of the string, or %NULL to indicate
149 * that the string is nul-terminated, in which case
150 * the returned value will be
152 * Finds the start of the next UTF-8 character in the string after @p.
154 * @p does not have to be at the beginning of a UTF-8 character. No check
155 * is made to see if the character found is actually valid other than
156 * it starts with an appropriate byte.
158 * Return value: a pointer to the found character or %NULL
161 g_utf8_find_next_char (const gchar
*p
,
167 for (++p
; p
< end
&& (*p
& 0xc0) == 0x80; ++p
)
170 for (++p
; (*p
& 0xc0) == 0x80; ++p
)
173 return (p
== end
) ? NULL
: (gchar
*)p
;
178 * @p: a pointer to a position within a UTF-8 encoded string
180 * Finds the previous UTF-8 character in the string before @p.
182 * @p does not have to be at the beginning of a UTF-8 character. No check
183 * is made to see if the character found is actually valid other than
184 * it starts with an appropriate byte. If @p might be the first
185 * character of the string, you must use g_utf8_find_prev_char() instead.
187 * Return value: a pointer to the found character.
190 g_utf8_prev_char (const gchar
*p
)
195 if ((*p
& 0xc0) != 0x80)
202 * @p: pointer to the start of a UTF-8 encoded string.
203 * @max: the maximum number of bytes to examine. If @max
204 * is less than 0, then the string is assumed to be
205 * nul-terminated. If @max is 0, @p will not be examined and
208 * Returns the length of the string in characters.
210 * Return value: the length of the string in characters
213 g_utf8_strlen (const gchar
*p
,
217 const gchar
*start
= p
;
218 g_return_val_if_fail (p
!= NULL
|| max
== 0, 0);
224 p
= g_utf8_next_char (p
);
233 p
= g_utf8_next_char (p
);
235 while (p
- start
< max
&& *p
)
238 p
= g_utf8_next_char (p
);
241 /* only do the last len increment if we got a complete
242 * char (don't count partial chars)
244 if (p
- start
== max
)
253 * @p: a pointer to Unicode character encoded as UTF-8
255 * Converts a sequence of bytes encoded as UTF-8 to a Unicode character.
256 * If @p does not point to a valid UTF-8 encoded character, results are
257 * undefined. If you are not sure that the bytes are complete
258 * valid Unicode characters, you should use g_utf8_get_char_validated()
261 * Return value: the resulting character
264 g_utf8_get_char (const gchar
*p
)
266 int i
, mask
= 0, len
;
268 unsigned char c
= (unsigned char) *p
;
270 UTF8_COMPUTE (c
, mask
, len
);
273 UTF8_GET (result
, p
, i
, mask
, len
);
279 * g_utf8_offset_to_pointer:
280 * @str: a UTF-8 encoded string
281 * @offset: a character offset within @str
283 * Converts from an integer character offset to a pointer to a position
286 * Return value: the resulting pointer
289 g_utf8_offset_to_pointer (const gchar
*str
,
292 const gchar
*s
= str
;
294 s
= g_utf8_next_char (s
);
300 * g_utf8_pointer_to_offset:
301 * @str: a UTF-8 encoded string
302 * @pos: a pointer to a position within @str
304 * Converts from a pointer to position within a string to a integer
307 * Return value: the resulting character offset
310 g_utf8_pointer_to_offset (const gchar
*str
,
313 const gchar
*s
= str
;
318 s
= g_utf8_next_char (s
);
328 * @dest: buffer to fill with characters from @src
329 * @src: UTF-8 encoded string
330 * @n: character count
332 * Like the standard C strncpy() function, but
333 * copies a given number of characters instead of a given number of
334 * bytes. The @src string must be valid UTF-8 encoded text.
335 * (Use g_utf8_validate() on all text before trying to use UTF-8
336 * utility functions with it.)
338 * Return value: @dest
341 g_utf8_strncpy (gchar
*dest
,
345 const gchar
*s
= src
;
348 s
= g_utf8_next_char(s
);
351 strncpy(dest
, src
, s
- src
);
356 G_LOCK_DEFINE_STATIC (aliases
);
359 get_alias_hash (void)
361 static GHashTable
*alias_hash
= NULL
;
368 alias_hash
= g_hash_table_new (g_str_hash
, g_str_equal
);
370 aliases
= _g_locale_get_charset_aliases ();
371 while (*aliases
!= '\0')
373 const char *canonical
;
375 const char **alias_array
;
379 aliases
+= strlen (aliases
) + 1;
381 aliases
+= strlen (aliases
) + 1;
383 alias_array
= g_hash_table_lookup (alias_hash
, canonical
);
386 while (alias_array
[count
])
390 alias_array
= g_renew (const char *, alias_array
, count
+ 2);
391 alias_array
[count
] = alias
;
392 alias_array
[count
+ 1] = NULL
;
394 g_hash_table_insert (alias_hash
, (char *)canonical
, alias_array
);
403 /* As an abuse of the alias table, the following routines gets
404 * the charsets that are aliases for the canonical name.
407 _g_charset_get_aliases (const char *canonical_name
)
409 GHashTable
*alias_hash
= get_alias_hash ();
411 return g_hash_table_lookup (alias_hash
, canonical_name
);
415 g_utf8_get_charset_internal (const char *raw_data
,
418 const char *charset
= getenv("CHARSET");
420 if (charset
&& *charset
)
424 if (charset
&& strstr (charset
, "UTF-8"))
430 /* The libcharset code tries to be thread-safe without
431 * a lock, but has a memory leak and a missing memory
432 * barrier, so we lock for it
435 charset
= _g_locale_charset_unalias (raw_data
);
438 if (charset
&& *charset
)
442 if (charset
&& strstr (charset
, "UTF-8"))
448 /* Assume this for compatibility at present. */
454 typedef struct _GCharsetCache GCharsetCache
;
456 struct _GCharsetCache
{
463 charset_cache_free (gpointer data
)
465 GCharsetCache
*cache
= data
;
467 g_free (cache
->charset
);
473 * @charset: return location for character set name
475 * Obtains the character set for the current locale; you might use
476 * this character set as an argument to g_convert(), to convert from
477 * the current locale's encoding to some other encoding. (Frequently
478 * g_locale_to_utf8() and g_locale_from_utf8() are nice shortcuts,
481 * The return value is %TRUE if the locale's encoding is UTF-8, in that
482 * case you can perhaps avoid calling g_convert().
484 * The string returned in @charset is not allocated, and should not be
487 * Return value: %TRUE if the returned charset is UTF-8
490 g_get_charset (G_CONST_RETURN
char **charset
)
492 static GStaticPrivate cache_private
= G_STATIC_PRIVATE_INIT
;
493 GCharsetCache
*cache
= g_static_private_get (&cache_private
);
498 cache
= g_new0 (GCharsetCache
, 1);
499 g_static_private_set (&cache_private
, cache
, charset_cache_free
);
502 raw
= _g_locale_charset_raw ();
504 if (!(cache
->raw
&& strcmp (cache
->raw
, raw
) == 0))
506 const gchar
*new_charset
;
509 g_free (cache
->charset
);
510 cache
->raw
= g_strdup (raw
);
511 cache
->is_utf8
= g_utf8_get_charset_internal (raw
, &new_charset
);
512 cache
->charset
= g_strdup (new_charset
);
516 *charset
= cache
->charset
;
518 return cache
->is_utf8
;
525 * @c: a ISO10646 character code
526 * @outbuf: output buffer, must have at least 6 bytes of space.
527 * If %NULL, the length will be computed and returned
528 * and nothing will be written to @outbuf.
530 * Converts a single character to UTF-8.
532 * Return value: number of bytes written
535 g_unichar_to_utf8 (gunichar c
,
552 else if (c
< 0x10000)
557 else if (c
< 0x200000)
562 else if (c
< 0x4000000)
575 for (i
= len
- 1; i
> 0; --i
)
577 outbuf
[i
] = (c
& 0x3f) | 0x80;
580 outbuf
[0] = c
| first
;
588 * @p: a nul-terminated UTF-8 encoded string
589 * @len: the maximum length of @p
590 * @c: a ISO10646 character
592 * Finds the leftmost occurrence of the given ISO10646 character
593 * in a UTF-8 encoded string, while limiting the search to @len bytes.
594 * If @len is -1, allow unbounded search.
596 * Return value: %NULL if the string does not contain the character,
597 * otherwise, a pointer to the start of the leftmost occurrence of
598 * the character in the string.
601 g_utf8_strchr (const char *p
,
607 gint charlen
= g_unichar_to_utf8 (c
, ch
);
610 return g_strstr_len (p
, len
, ch
);
616 * @p: a nul-terminated UTF-8 encoded string
617 * @len: the maximum length of @p
618 * @c: a ISO10646 character
620 * Find the rightmost occurrence of the given ISO10646 character
621 * in a UTF-8 encoded string, while limiting the search to @len bytes.
622 * If @len is -1, allow unbounded search.
624 * Return value: %NULL if the string does not contain the character,
625 * otherwise, a pointer to the start of the rightmost occurrence of the
626 * character in the string.
629 g_utf8_strrchr (const char *p
,
635 gint charlen
= g_unichar_to_utf8 (c
, ch
);
638 return g_strrstr_len (p
, len
, ch
);
642 /* Like g_utf8_get_char, but take a maximum length
643 * and return (gunichar)-2 on incomplete trailing character
645 static inline gunichar
646 g_utf8_get_char_extended (const gchar
*p
,
650 gunichar wc
= (guchar
) *p
;
690 if (max_len
>= 0 && len
> max_len
)
692 for (i
= 1; i
< max_len
; i
++)
694 if ((((guchar
*)p
)[i
] & 0xc0) != 0x80)
700 for (i
= 1; i
< len
; ++i
)
702 gunichar ch
= ((guchar
*)p
)[i
];
704 if ((ch
& 0xc0) != 0x80)
716 if (UTF8_LENGTH(wc
) != len
)
723 * g_utf8_get_char_validated:
724 * @p: a pointer to Unicode character encoded as UTF-8
725 * @max_len: the maximum number of bytes to read, or -1, for no maximum.
727 * Convert a sequence of bytes encoded as UTF-8 to a Unicode character.
728 * This function checks for incomplete characters, for invalid characters
729 * such as characters that are out of the range of Unicode, and for
730 * overlong encodings of valid characters.
732 * Return value: the resulting character. If @p points to a partial
733 * sequence at the end of a string that could begin a valid
734 * character, returns (gunichar)-2; otherwise, if @p does not point
735 * to a valid UTF-8 encoded Unicode character, returns (gunichar)-1.
738 g_utf8_get_char_validated (const gchar
*p
,
741 gunichar result
= g_utf8_get_char_extended (p
, max_len
);
743 if (result
& 0x80000000)
745 else if (!UNICODE_VALID (result
))
752 * g_utf8_to_ucs4_fast:
753 * @str: a UTF-8 encoded string
754 * @len: the maximum length of @str to use. If @len < 0, then
755 * the string is nul-terminated.
756 * @items_written: location to store the number of characters in the
759 * Convert a string from UTF-8 to a 32-bit fixed width
760 * representation as UCS-4, assuming valid UTF-8 input.
761 * This function is roughly twice as fast as g_utf8_to_ucs4()
762 * but does no error checking on the input.
764 * Return value: a pointer to a newly allocated UCS-4 string.
765 * This value must be freed with g_free().
768 g_utf8_to_ucs4_fast (const gchar
*str
,
770 glong
*items_written
)
777 g_return_val_if_fail (str
!= NULL
, NULL
);
785 p
= g_utf8_next_char (p
);
791 while (p
< str
+ len
&& *p
)
793 p
= g_utf8_next_char (p
);
798 result
= g_new (gunichar
, n_chars
+ 1);
801 for (i
=0; i
< n_chars
; i
++)
803 gunichar wc
= ((unsigned char *)p
)[0];
838 for (j
= 1; j
< charlen
; j
++)
841 wc
|= ((unsigned char *)p
)[j
] & 0x3f;
858 * @str: a UTF-8 encoded string
859 * @len: the maximum length of @str to use. If @len < 0, then
860 * the string is nul-terminated.
861 * @items_read: location to store number of bytes read, or %NULL.
862 * If %NULL, then %G_CONVERT_ERROR_PARTIAL_INPUT will be
863 * returned in case @str contains a trailing partial
864 * character. If an error occurs then the index of the
865 * invalid input is stored here.
866 * @items_written: location to store number of characters written or %NULL.
867 * The value here stored does not include the trailing 0
869 * @error: location to store the error occuring, or %NULL to ignore
870 * errors. Any of the errors in #GConvertError other than
871 * %G_CONVERT_ERROR_NO_CONVERSION may occur.
873 * Convert a string from UTF-8 to a 32-bit fixed width
874 * representation as UCS-4. A trailing 0 will be added to the
875 * string after the converted text.
877 * Return value: a pointer to a newly allocated UCS-4 string.
878 * This value must be freed with g_free(). If an
879 * error occurs, %NULL will be returned and
883 g_utf8_to_ucs4 (const gchar
*str
,
886 glong
*items_written
,
889 gunichar
*result
= NULL
;
895 while ((len
< 0 || str
+ len
- in
> 0) && *in
)
897 gunichar wc
= g_utf8_get_char_extended (in
, str
+ len
- in
);
900 if (wc
== (gunichar
)-2)
905 g_set_error (error
, G_CONVERT_ERROR
, G_CONVERT_ERROR_PARTIAL_INPUT
,
906 _("Partial character sequence at end of input"));
909 g_set_error (error
, G_CONVERT_ERROR
, G_CONVERT_ERROR_ILLEGAL_SEQUENCE
,
910 _("Invalid byte sequence in conversion input"));
917 in
= g_utf8_next_char (in
);
920 result
= g_new (gunichar
, n_chars
+ 1);
923 for (i
=0; i
< n_chars
; i
++)
925 result
[i
] = g_utf8_get_char (in
);
926 in
= g_utf8_next_char (in
);
931 *items_written
= n_chars
;
935 *items_read
= in
- str
;
942 * @str: a UCS-4 encoded string
943 * @len: the maximum length of @str to use. If @len < 0, then
944 * the string is terminated with a 0 character.
945 * @items_read: location to store number of characters read read, or %NULL.
946 * @items_written: location to store number of bytes written or %NULL.
947 * The value here stored does not include the trailing 0
949 * @error: location to store the error occuring, or %NULL to ignore
950 * errors. Any of the errors in #GConvertError other than
951 * %G_CONVERT_ERROR_NO_CONVERSION may occur.
953 * Convert a string from a 32-bit fixed width representation as UCS-4.
954 * to UTF-8. The result will be terminated with a 0 byte.
956 * Return value: a pointer to a newly allocated UTF-8 string.
957 * This value must be freed with g_free(). If an
958 * error occurs, %NULL will be returned and
962 g_ucs4_to_utf8 (const gunichar
*str
,
965 glong
*items_written
,
969 gchar
*result
= NULL
;
974 for (i
= 0; len
< 0 || i
< len
; i
++)
979 if (str
[i
] >= 0x80000000)
984 g_set_error (error
, G_CONVERT_ERROR
, G_CONVERT_ERROR_ILLEGAL_SEQUENCE
,
985 _("Character out of range for UTF-8"));
989 result_length
+= UTF8_LENGTH (str
[i
]);
992 result
= g_malloc (result_length
+ 1);
996 while (p
< result
+ result_length
)
997 p
+= g_unichar_to_utf8 (str
[i
++], p
);
1002 *items_written
= p
- result
;
1011 #define SURROGATE_VALUE(h,l) (((h) - 0xd800) * 0x400 + (l) - 0xdc00 + 0x10000)
1015 * @str: a UTF-16 encoded string
1016 * @len: the maximum length of @str to use. If @len < 0, then
1017 * the string is terminated with a 0 character.
1018 * @items_read: location to store number of words read, or %NULL.
1019 * If %NULL, then %G_CONVERT_ERROR_PARTIAL_INPUT will be
1020 * returned in case @str contains a trailing partial
1021 * character. If an error occurs then the index of the
1022 * invalid input is stored here.
1023 * @items_written: location to store number of bytes written, or %NULL.
1024 * The value stored here does not include the trailing
1026 * @error: location to store the error occuring, or %NULL to ignore
1027 * errors. Any of the errors in #GConvertError other than
1028 * %G_CONVERT_ERROR_NO_CONVERSION may occur.
1030 * Convert a string from UTF-16 to UTF-8. The result will be
1031 * terminated with a 0 byte.
1033 * Return value: a pointer to a newly allocated UTF-8 string.
1034 * This value must be freed with g_free(). If an
1035 * error occurs, %NULL will be returned and
1039 g_utf16_to_utf8 (const gunichar2
*str
,
1042 glong
*items_written
,
1045 /* This function and g_utf16_to_ucs4 are almost exactly identical - The lines that differ
1048 const gunichar2
*in
;
1050 gchar
*result
= NULL
;
1052 gunichar high_surrogate
;
1054 g_return_val_if_fail (str
!= 0, NULL
);
1059 while ((len
< 0 || in
- str
< len
) && *in
)
1064 if (c
>= 0xdc00 && c
< 0xe000) /* low surrogate */
1068 wc
= SURROGATE_VALUE (high_surrogate
, c
);
1073 g_set_error (error
, G_CONVERT_ERROR
, G_CONVERT_ERROR_ILLEGAL_SEQUENCE
,
1074 _("Invalid sequence in conversion input"));
1082 g_set_error (error
, G_CONVERT_ERROR
, G_CONVERT_ERROR_ILLEGAL_SEQUENCE
,
1083 _("Invalid sequence in conversion input"));
1087 if (c
>= 0xd800 && c
< 0xdc00) /* high surrogate */
1096 /********** DIFFERENT for UTF8/UCS4 **********/
1097 n_bytes
+= UTF8_LENGTH (wc
);
1103 if (high_surrogate
&& !items_read
)
1105 g_set_error (error
, G_CONVERT_ERROR
, G_CONVERT_ERROR_PARTIAL_INPUT
,
1106 _("Partial character sequence at end of input"));
1110 /* At this point, everything is valid, and we just need to convert
1112 /********** DIFFERENT for UTF8/UCS4 **********/
1113 result
= g_malloc (n_bytes
+ 1);
1118 while (out
< result
+ n_bytes
)
1123 if (c
>= 0xdc00 && c
< 0xe000) /* low surrogate */
1125 wc
= SURROGATE_VALUE (high_surrogate
, c
);
1128 else if (c
>= 0xd800 && c
< 0xdc00) /* high surrogate */
1136 /********** DIFFERENT for UTF8/UCS4 **********/
1137 out
+= g_unichar_to_utf8 (wc
, out
);
1143 /********** DIFFERENT for UTF8/UCS4 **********/
1147 /********** DIFFERENT for UTF8/UCS4 **********/
1148 *items_written
= out
- result
;
1152 *items_read
= in
- str
;
1159 * @str: a UTF-16 encoded string
1160 * @len: the maximum length of @str to use. If @len < 0, then
1161 * the string is terminated with a 0 character.
1162 * @items_read: location to store number of words read, or %NULL.
1163 * If %NULL, then %G_CONVERT_ERROR_PARTIAL_INPUT will be
1164 * returned in case @str contains a trailing partial
1165 * character. If an error occurs then the index of the
1166 * invalid input is stored here.
1167 * @items_written: location to store number of characters written, or %NULL.
1168 * The value stored here does not include the trailing
1170 * @error: location to store the error occuring, or %NULL to ignore
1171 * errors. Any of the errors in #GConvertError other than
1172 * %G_CONVERT_ERROR_NO_CONVERSION may occur.
1174 * Convert a string from UTF-16 to UCS-4. The result will be
1175 * terminated with a 0 character.
1177 * Return value: a pointer to a newly allocated UCS-4 string.
1178 * This value must be freed with g_free(). If an
1179 * error occurs, %NULL will be returned and
1183 g_utf16_to_ucs4 (const gunichar2
*str
,
1186 glong
*items_written
,
1189 const gunichar2
*in
;
1191 gchar
*result
= NULL
;
1193 gunichar high_surrogate
;
1195 g_return_val_if_fail (str
!= 0, NULL
);
1200 while ((len
< 0 || in
- str
< len
) && *in
)
1205 if (c
>= 0xdc00 && c
< 0xe000) /* low surrogate */
1209 wc
= SURROGATE_VALUE (high_surrogate
, c
);
1214 g_set_error (error
, G_CONVERT_ERROR
, G_CONVERT_ERROR_ILLEGAL_SEQUENCE
,
1215 _("Invalid sequence in conversion input"));
1223 g_set_error (error
, G_CONVERT_ERROR
, G_CONVERT_ERROR_ILLEGAL_SEQUENCE
,
1224 _("Invalid sequence in conversion input"));
1228 if (c
>= 0xd800 && c
< 0xdc00) /* high surrogate */
1237 /********** DIFFERENT for UTF8/UCS4 **********/
1238 n_bytes
+= sizeof (gunichar
);
1244 if (high_surrogate
&& !items_read
)
1246 g_set_error (error
, G_CONVERT_ERROR
, G_CONVERT_ERROR_PARTIAL_INPUT
,
1247 _("Partial character sequence at end of input"));
1251 /* At this point, everything is valid, and we just need to convert
1253 /********** DIFFERENT for UTF8/UCS4 **********/
1254 result
= g_malloc (n_bytes
+ 4);
1259 while (out
< result
+ n_bytes
)
1264 if (c
>= 0xdc00 && c
< 0xe000) /* low surrogate */
1266 wc
= SURROGATE_VALUE (high_surrogate
, c
);
1269 else if (c
>= 0xd800 && c
< 0xdc00) /* high surrogate */
1277 /********** DIFFERENT for UTF8/UCS4 **********/
1278 *(gunichar
*)out
= wc
;
1279 out
+= sizeof (gunichar
);
1285 /********** DIFFERENT for UTF8/UCS4 **********/
1286 *(gunichar
*)out
= 0;
1289 /********** DIFFERENT for UTF8/UCS4 **********/
1290 *items_written
= (out
- result
) / sizeof (gunichar
);
1294 *items_read
= in
- str
;
1296 return (gunichar
*)result
;
1301 * @str: a UTF-8 encoded string
1302 * @len: the maximum length of @str to use. If @len < 0, then
1303 * the string is nul-terminated.
1304 * @items_read: location to store number of bytes read, or %NULL.
1305 * If %NULL, then %G_CONVERT_ERROR_PARTIAL_INPUT will be
1306 * returned in case @str contains a trailing partial
1307 * character. If an error occurs then the index of the
1308 * invalid input is stored here.
1309 * @items_written: location to store number of words written, or %NULL.
1310 * The value stored here does not include the trailing
1312 * @error: location to store the error occuring, or %NULL to ignore
1313 * errors. Any of the errors in #GConvertError other than
1314 * %G_CONVERT_ERROR_NO_CONVERSION may occur.
1316 * Convert a string from UTF-8 to UTF-16. A 0 word will be
1317 * added to the result after the converted text.
1319 * Return value: a pointer to a newly allocated UTF-16 string.
1320 * This value must be freed with g_free(). If an
1321 * error occurs, %NULL will be returned and
1325 g_utf8_to_utf16 (const gchar
*str
,
1328 glong
*items_written
,
1331 gunichar2
*result
= NULL
;
1336 g_return_val_if_fail (str
!= NULL
, NULL
);
1340 while ((len
< 0 || str
+ len
- in
> 0) && *in
)
1342 gunichar wc
= g_utf8_get_char_extended (in
, str
+ len
- in
);
1343 if (wc
& 0x80000000)
1345 if (wc
== (gunichar
)-2)
1350 g_set_error (error
, G_CONVERT_ERROR
, G_CONVERT_ERROR_PARTIAL_INPUT
,
1351 _("Partial character sequence at end of input"));
1354 g_set_error (error
, G_CONVERT_ERROR
, G_CONVERT_ERROR_ILLEGAL_SEQUENCE
,
1355 _("Invalid byte sequence in conversion input"));
1362 else if (wc
< 0xe000)
1364 g_set_error (error
, G_CONVERT_ERROR
, G_CONVERT_ERROR_ILLEGAL_SEQUENCE
,
1365 _("Invalid sequence in conversion input"));
1369 else if (wc
< 0x10000)
1371 else if (wc
< 0x110000)
1375 g_set_error (error
, G_CONVERT_ERROR
, G_CONVERT_ERROR_ILLEGAL_SEQUENCE
,
1376 _("Character out of range for UTF-16"));
1381 in
= g_utf8_next_char (in
);
1384 result
= g_new (gunichar2
, n16
+ 1);
1387 for (i
= 0; i
< n16
;)
1389 gunichar wc
= g_utf8_get_char (in
);
1397 result
[i
++] = (wc
- 0x10000) / 0x400 + 0xd800;
1398 result
[i
++] = (wc
- 0x10000) % 0x400 + 0xdc00;
1401 in
= g_utf8_next_char (in
);
1407 *items_written
= n16
;
1411 *items_read
= in
- str
;
1418 * @str: a UCS-4 encoded string
1419 * @len: the maximum length of @str to use. If @len < 0, then
1420 * the string is terminated with a 0 character.
1421 * @items_read: location to store number of bytes read, or %NULL.
1422 * If an error occurs then the index of the invalid input
1424 * @items_written: location to store number of words written, or %NULL.
1425 * The value stored here does not include the trailing
1427 * @error: location to store the error occuring, or %NULL to ignore
1428 * errors. Any of the errors in #GConvertError other than
1429 * %G_CONVERT_ERROR_NO_CONVERSION may occur.
1431 * Convert a string from UCS-4 to UTF-16. A 0 word will be
1432 * added to the result after the converted text.
1434 * Return value: a pointer to a newly allocated UTF-16 string.
1435 * This value must be freed with g_free(). If an
1436 * error occurs, %NULL will be returned and
1440 g_ucs4_to_utf16 (const gunichar
*str
,
1443 glong
*items_written
,
1446 gunichar2
*result
= NULL
;
1452 while ((len
< 0 || i
< len
) && str
[i
])
1454 gunichar wc
= str
[i
];
1458 else if (wc
< 0xe000)
1460 g_set_error (error
, G_CONVERT_ERROR
, G_CONVERT_ERROR_ILLEGAL_SEQUENCE
,
1461 _("Invalid sequence in conversion input"));
1465 else if (wc
< 0x10000)
1467 else if (wc
< 0x110000)
1471 g_set_error (error
, G_CONVERT_ERROR
, G_CONVERT_ERROR_ILLEGAL_SEQUENCE
,
1472 _("Character out of range for UTF-16"));
1480 result
= g_new (gunichar2
, n16
+ 1);
1482 for (i
= 0, j
= 0; j
< n16
; i
++)
1484 gunichar wc
= str
[i
];
1492 result
[j
++] = (wc
- 0x10000) / 0x400 + 0xd800;
1493 result
[j
++] = (wc
- 0x10000) % 0x400 + 0xdc00;
1499 *items_written
= n16
;
1510 * @str: a pointer to character data
1511 * @max_len: max bytes to validate, or -1 to go until nul
1512 * @end: return location for end of valid data
1514 * Validates UTF-8 encoded text. @str is the text to validate;
1515 * if @str is nul-terminated, then @max_len can be -1, otherwise
1516 * @max_len should be the number of bytes to validate.
1517 * If @end is non-%NULL, then the end of the valid range
1518 * will be stored there (i.e. the address of the first invalid byte
1519 * if some bytes were invalid, or the end of the text being validated
1522 * Returns %TRUE if all of @str was valid. Many GLib and GTK+
1523 * routines <emphasis>require</emphasis> valid UTF-8 as input;
1524 * so data read from a file or the network should be checked
1525 * with g_utf8_validate() before doing anything else with it.
1527 * Return value: %TRUE if the text was valid UTF-8
1530 g_utf8_validate (const gchar
*str
,
1537 g_return_val_if_fail (str
!= NULL
, FALSE
);
1544 while ((max_len
< 0 || (p
- str
) < max_len
) && *p
)
1546 int i
, mask
= 0, len
;
1548 unsigned char c
= (unsigned char) *p
;
1550 UTF8_COMPUTE (c
, mask
, len
);
1555 /* check that the expected number of bytes exists in str */
1557 ((max_len
- (p
- str
)) < len
))
1560 UTF8_GET (result
, p
, i
, mask
, len
);
1562 if (UTF8_LENGTH (result
) != len
) /* Check for overlong UTF-8 */
1565 if (result
== (gunichar
)-1)
1568 if (!UNICODE_VALID (result
))
1577 /* See that we covered the entire length if a length was
1578 * passed in, or that we ended on a nul if not
1581 p
!= (str
+ max_len
))
1583 else if (max_len
< 0 &&
1591 * g_unichar_validate:
1592 * @ch: a Unicode character
1594 * Checks whether @ch is a valid Unicode character. Some possible
1595 * integer values of @ch will not be valid. 0 is considered a valid
1596 * character, though it's normally a string terminator.
1598 * Return value: %TRUE if @ch is a valid Unicode character
1601 g_unichar_validate (gunichar ch
)
1603 return UNICODE_VALID (ch
);
1607 * g_utf8_strreverse:
1608 * @str: a UTF-8 encoded string
1609 * @len: the maximum length of @str to use. If @len < 0, then
1610 * the string is nul-terminated.
1612 * Reverses a UTF-8 string. @str must be valid UTF-8 encoded text.
1613 * (Use g_utf8_validate() on all text before trying to use UTF-8
1614 * utility functions with it.)
1616 * Note that unlike g_strreverse(), this function returns
1617 * newly-allocated memory, which should be freed with g_free() when
1620 * Returns: a newly-allocated string which is the reverse of @str.
1625 g_utf8_strreverse (const gchar
*str
,
1635 result
= g_new (gchar
, len
+ 1);
1640 skip
= g_utf8_skip
[*(guchar
*)p
];
1642 for (m
= r
; skip
; skip
--)