1 /* gutf8.c - Operations on UTF-8 strings.
3 * Copyright (C) 1999 Tom Tromey
4 * Copyright (C) 2000 Red Hat, Inc.
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the
18 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
19 * Boston, MA 02111-1307, USA.
32 #ifdef G_PLATFORM_WIN32
41 #define UTF8_COMPUTE(Char, Mask, Len) \
47 else if ((Char & 0xe0) == 0xc0) \
52 else if ((Char & 0xf0) == 0xe0) \
57 else if ((Char & 0xf8) == 0xf0) \
62 else if ((Char & 0xfc) == 0xf8) \
67 else if ((Char & 0xfe) == 0xfc) \
75 #define UTF8_LENGTH(Char) \
76 ((Char) < 0x80 ? 1 : \
77 ((Char) < 0x800 ? 2 : \
78 ((Char) < 0x10000 ? 3 : \
79 ((Char) < 0x200000 ? 4 : \
80 ((Char) < 0x4000000 ? 5 : 6)))))
83 #define UTF8_GET(Result, Chars, Count, Mask, Len) \
84 (Result) = (Chars)[0] & (Mask); \
85 for ((Count) = 1; (Count) < (Len); ++(Count)) \
87 if (((Chars)[(Count)] & 0xc0) != 0x80) \
93 (Result) |= ((Chars)[(Count)] & 0x3f); \
96 #define UNICODE_VALID(Char) \
97 ((Char) < 0x110000 && \
98 ((Char) < 0xD800 || (Char) >= 0xE000) && \
99 (Char) != 0xFFFE && (Char) != 0xFFFF)
102 gchar g_utf8_skip
[256] = {
103 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,
104 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,
105 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,
106 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,
107 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,
108 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,
109 2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,
110 3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,4,4,4,4,4,4,4,4,5,5,5,5,6,6,0,0
114 * g_utf8_find_prev_char:
115 * @str: pointer to the beginning of a UTF-8 string
116 * @p: pointer to some position within @str
118 * Given a position @p with a UTF-8 encoded string @str, find the start
119 * of the previous UTF-8 character starting before @p. Returns %NULL if no
120 * UTF-8 characters are present in @p before @str.
122 * @p does not have to be at the beginning of a UTF-8 chracter. No check
123 * is made to see if the character found is actually valid other than
124 * it starts with an appropriate byte.
126 * Return value: a pointer to the found character or %NULL.
129 g_utf8_find_prev_char (const char *str
,
132 for (--p
; p
> str
; --p
)
134 if ((*p
& 0xc0) != 0x80)
141 * g_utf8_find_next_char:
142 * @p: a pointer to a position within a UTF-8 encoded string
143 * @end: a pointer to the end of the string, or %NULL to indicate
144 * that the string is NULL terminated, in which case
145 * the returned value will be
147 * Find the start of the next utf-8 character in the string after @p
149 * @p does not have to be at the beginning of a UTF-8 chracter. No check
150 * is made to see if the character found is actually valid other than
151 * it starts with an appropriate byte.
153 * Return value: a pointer to the found character or %NULL
156 g_utf8_find_next_char (const gchar
*p
,
162 for (++p
; p
< end
&& (*p
& 0xc0) == 0x80; ++p
)
165 for (++p
; (*p
& 0xc0) == 0x80; ++p
)
168 return (p
== end
) ? NULL
: (gchar
*)p
;
173 * @p: a pointer to a position within a UTF-8 encoded string
175 * Find the previous UTF-8 character in the string before @p
177 * @p does not have to be at the beginning of a UTF-8 character. No check
178 * is made to see if the character found is actually valid other than
179 * it starts with an appropriate byte. If @p might be the first
180 * character of the string, you must use g_utf8_find_prev_char instead.
182 * Return value: a pointer to the found character.
185 g_utf8_prev_char (const gchar
*p
)
190 if ((*p
& 0xc0) != 0x80)
197 * @p: pointer to the start of a UTF-8 string.
198 * @max: the maximum number of bytes to examine. If @max
199 * is less than 0, then the string is assumed to be
202 * Return value: the length of the string in characters
205 g_utf8_strlen (const gchar
*p
, gint max
)
208 const gchar
*start
= p
;
209 /* special case for the empty string */
212 /* Note that the test here and the test in the loop differ subtly.
213 In the loop we want to see if we've passed the maximum limit --
214 for instance if the buffer ends mid-character. Here at the top
215 of the loop we want to see if we've just reached the last byte. */
216 while (max
< 0 || p
- start
< max
)
218 p
= g_utf8_next_char (p
);
220 if (! *p
|| (max
> 0 && p
- start
> max
))
228 * @p: a pointer to unicode character encoded as UTF-8
230 * Convert a sequence of bytes encoded as UTF-8 to a unicode character.
232 * Return value: the resulting character or (gunichar)-1 if @p does
233 * not point to a valid UTF-8 encoded unicode character
236 g_utf8_get_char (const gchar
*p
)
238 int i
, mask
= 0, len
;
240 unsigned char c
= (unsigned char) *p
;
242 UTF8_COMPUTE (c
, mask
, len
);
245 UTF8_GET (result
, p
, i
, mask
, len
);
251 * g_utf8_offset_to_pointer:
252 * @str: a UTF-8 encoded string
253 * @offset: a character offset within the string.
255 * Converts from an integer character offset to a pointer to a position
258 * Return value: the resulting pointer
261 g_utf8_offset_to_pointer (const gchar
*str
,
264 const gchar
*s
= str
;
266 s
= g_utf8_next_char (s
);
272 * g_utf8_pointer_to_offset:
273 * @str: a UTF-8 encoded string
274 * @pos: a pointer to a position within @str
276 * Converts from a pointer to position within a string to a integer
279 * Return value: the resulting character offset
282 g_utf8_pointer_to_offset (const gchar
*str
,
285 const gchar
*s
= str
;
290 s
= g_utf8_next_char (s
);
299 g_utf8_strncpy (gchar
*dest
, const gchar
*src
, size_t n
)
301 const gchar
*s
= src
;
304 s
= g_utf8_next_char(s
);
307 strncpy(dest
, src
, s
- src
);
313 g_utf8_get_charset_internal (char **a
)
315 char *charset
= getenv("CHARSET");
317 if (charset
&& a
&& ! *a
)
320 if (charset
&& strstr (charset
, "UTF-8"))
324 charset
= nl_langinfo(CODESET
);
329 if (strcmp (charset
, "UTF-8") == 0)
334 #if 0 /* #ifdef _NL_CTYPE_CODESET_NAME */
335 charset
= nl_langinfo (_NL_CTYPE_CODESET_NAME
);
340 if (strcmp (charset
, "UTF-8") == 0)
345 #ifdef G_PLATFORM_WIN32
348 static char codepage
[10];
350 sprintf (codepage
, "CP%d", GetACP ());
352 /* What about codepage 1200? Is that UTF-8? */
360 /* Assume this for compatibility at present. */
364 static int utf8_locale_cache
= -1;
365 static char *utf8_charset_cache
= NULL
;
368 g_get_charset (char **charset
)
370 if (utf8_locale_cache
!= -1)
373 *charset
= utf8_charset_cache
;
374 return utf8_locale_cache
;
376 utf8_locale_cache
= g_utf8_get_charset_internal (&utf8_charset_cache
);
378 *charset
= utf8_charset_cache
;
379 return utf8_locale_cache
;
386 * @c: a ISO10646 character code
387 * @outbuf: output buffer, must have at least 6 bytes of space.
388 * If %NULL, the length will be computed and returned
389 * and nothing will be written to @out.
391 * Convert a single character to utf8
393 * Return value: number of bytes written
396 g_unichar_to_utf8 (gunichar c
, gchar
*outbuf
)
412 else if (c
< 0x10000)
417 else if (c
< 0x200000)
422 else if (c
< 0x4000000)
435 for (i
= len
- 1; i
> 0; --i
)
437 outbuf
[i
] = (c
& 0x3f) | 0x80;
440 outbuf
[0] = c
| first
;
448 * @p: a nul-terminated utf-8 string
449 * @c: a iso-10646 character/
451 * Find the leftmost occurence of the given iso-10646 character
454 * Return value: NULL if the string does not contain the character, otherwise, a
455 * a pointer to the start of the leftmost of the character in the string.
458 g_utf8_strchr (const char *p
, gunichar c
)
462 gint len
= g_unichar_to_utf8 (c
, ch
);
465 return strstr(p
, ch
);
471 * @p: a nul-terminated utf-8 string
472 * @c: a iso-10646 character/
474 * Find the rightmost occurence of the given iso-10646 character
477 * Return value: NULL if the string does not contain the character, otherwise, a
478 * a pointer to the start of the rightmost of the character in the string.
481 /* This is ifdefed out atm as there is no strrstr function in libc.
484 unicode_strrchr (const char *p
, gunichar c
)
488 len
= g_unichar_to_utf8 (c
, ch
);
491 return strrstr(p
, ch
);
496 /* Like g_utf8_get_char, but take a maximum length
497 * and return (gunichar)-2 on incomplete trailing character
499 static inline gunichar
500 g_utf8_get_char_extended (const gchar
*p
, int max_len
)
503 gunichar wc
= (guchar
) *p
;
545 if (max_len
>= 0 && len
> max_len
)
547 for (i
= 1; i
< max_len
; i
++)
549 if ((((guchar
*)p
)[i
] & 0xc0) != 0x80)
555 for (i
= 1; i
< len
; ++i
)
557 gunichar ch
= ((guchar
*)p
)[i
];
559 if ((ch
& 0xc0) != 0x80)
571 if (UTF8_LENGTH(wc
) != len
)
578 * g_utf8_to_ucs4_fast:
579 * @str: a UTF-8 encoded string
580 * @len: the maximum length of @str to use. If < 0, then
581 * the string is %NULL terminated.
582 * @items_written: location to store the number of characters in the
585 * Convert a string from UTF-8 to a 32-bit fixed width
586 * representation as UCS-4, assuming valid UTF-8 input.
587 * This function is roughly twice as fast as g_utf8_to_ucs4()
588 * but does no error checking on the input.
590 * Return value: a pointer to a newly allocated UCS-4 string.
591 * This value must be freed with g_free()
594 g_utf8_to_ucs4_fast (const gchar
*str
,
603 g_return_val_if_fail (str
!= NULL
, NULL
);
611 p
= g_utf8_next_char (p
);
617 while (*p
&& p
< str
+ len
)
619 p
= g_utf8_next_char (p
);
624 result
= g_new (gunichar
, n_chars
+ 1);
627 for (i
=0; i
< n_chars
; i
++)
629 gunichar wc
= ((unsigned char *)p
)[0];
664 for (j
= 1; j
< charlen
; j
++)
667 wc
|= ((unsigned char *)p
)[j
] & 0x3f;
684 * @str: a UTF-8 encoded string
685 * @len: the maximum length of @str to use. If < 0, then
686 * the string is %NULL terminated.
687 * @items_read: location to store number of bytes read, or %NULL.
688 * If %NULL, then %G_CONVERT_ERROR_PARTIAL_INPUT will be
689 * returned in case @str contains a trailing partial
690 * character. If an error occurs then the index of the
691 * invalid input is stored here.
692 * @items_written: location to store number of characters written or %NULL.
693 * The value here stored does not include the trailing 0
695 * @error: location to store the error occuring, or %NULL to ignore
696 * errors. Any of the errors in #GConvertError other than
697 * %G_CONVERT_ERROR_NO_CONVERSION may occur.
699 * Convert a string from UTF-8 to a 32-bit fixed width
700 * representation as UCS-4. A trailing 0 will be added to the
701 * string after the converted text.
703 * Return value: a pointer to a newly allocated UCS-4 string.
704 * This value must be freed with g_free(). If an
705 * error occurs, %NULL will be returned and
709 g_utf8_to_ucs4 (const gchar
*str
,
715 gunichar
*result
= NULL
;
721 while ((len
< 0 || str
+ len
- in
> 0) && *in
)
723 gunichar wc
= g_utf8_get_char_extended (in
, str
+ len
- in
);
726 if (wc
== (gunichar
)-2)
731 g_set_error (error
, G_CONVERT_ERROR
, G_CONVERT_ERROR_PARTIAL_INPUT
,
732 _("Partial character sequence at end of input"));
735 g_set_error (error
, G_CONVERT_ERROR
, G_CONVERT_ERROR_ILLEGAL_SEQUENCE
,
736 _("Invalid byte sequence in conversion input"));
743 in
= g_utf8_next_char (in
);
746 result
= g_new (gunichar
, n_chars
+ 1);
749 for (i
=0; i
< n_chars
; i
++)
751 result
[i
] = g_utf8_get_char (in
);
752 in
= g_utf8_next_char (in
);
757 *items_written
= n_chars
;
761 *items_read
= in
- str
;
768 * @str: a UCS-4 encoded string
769 * @len: the maximum length of @str to use. If < 0, then
770 * the string is %NULL terminated.
771 * @items_read: location to store number of characters read read, or %NULL.
772 * @items_written: location to store number of bytes written or %NULL.
773 * The value here stored does not include the trailing 0
775 * @error: location to store the error occuring, or %NULL to ignore
776 * errors. Any of the errors in #GConvertError other than
777 * %G_CONVERT_ERROR_NO_CONVERSION may occur.
779 * Convert a string from a 32-bit fixed width representation as UCS-4.
780 * to UTF-8. The result will be terminated with a 0 byte.
782 * Return value: a pointer to a newly allocated UTF-8 string.
783 * This value must be freed with g_free(). If an
784 * error occurs, %NULL will be returned and
788 g_ucs4_to_utf8 (const gunichar
*str
,
795 gchar
*result
= NULL
;
800 for (i
= 0; len
< 0 || i
< len
; i
++)
805 if (str
[i
] >= 0x80000000)
810 g_set_error (error
, G_CONVERT_ERROR
, G_CONVERT_ERROR_ILLEGAL_SEQUENCE
,
811 _("Character out of range for UTF-8"));
815 result_length
+= UTF8_LENGTH (str
[i
]);
818 result
= g_malloc (result_length
+ 1);
822 while (p
< result
+ result_length
)
823 p
+= g_unichar_to_utf8 (str
[i
++], p
);
828 *items_written
= p
- result
;
837 #define SURROGATE_VALUE(h,l) (((h) - 0xd800) * 0x400 + (l) - 0xdc00 + 0x10000)
841 * @str: a UTF-16 encoded string
842 * @len: the maximum length of @str to use. If < 0, then
843 * the string is terminated with a 0 character.
844 * @items_read: location to store number of words read, or %NULL.
845 * If %NULL, then %G_CONVERT_ERROR_PARTIAL_INPUT will be
846 * returned in case @str contains a trailing partial
847 * character. If an error occurs then the index of the
848 * invalid input is stored here.
849 * @items_written: location to store number of bytes written, or %NULL.
850 * The value stored here does not include the trailing
852 * @error: location to store the error occuring, or %NULL to ignore
853 * errors. Any of the errors in #GConvertError other than
854 * %G_CONVERT_ERROR_NO_CONVERSION may occur.
856 * Convert a string from UTF-16 to UTF-8. The result will be
857 * terminated with a 0 byte.
859 * Return value: a pointer to a newly allocated UTF-8 string.
860 * This value must be freed with g_free(). If an
861 * error occurs, %NULL will be returned and
865 g_utf16_to_utf8 (const gunichar2
*str
,
871 /* This function and g_utf16_to_ucs4 are almost exactly identical - The lines that differ
876 gchar
*result
= NULL
;
878 gunichar high_surrogate
;
880 g_return_val_if_fail (str
!= 0, NULL
);
885 while ((len
< 0 || in
- str
< len
) && *in
)
890 if (c
>= 0xdc00 && c
< 0xe000) /* low surrogate */
894 wc
= SURROGATE_VALUE (high_surrogate
, c
);
899 g_set_error (error
, G_CONVERT_ERROR
, G_CONVERT_ERROR_ILLEGAL_SEQUENCE
,
900 _("Invalid sequence in conversion input"));
908 g_set_error (error
, G_CONVERT_ERROR
, G_CONVERT_ERROR_ILLEGAL_SEQUENCE
,
909 _("Invalid sequence in conversion input"));
913 if (c
>= 0xd800 && c
< 0xdc00) /* high surrogate */
922 /********** DIFFERENT for UTF8/UCS4 **********/
923 n_bytes
+= UTF8_LENGTH (wc
);
929 if (high_surrogate
&& !items_read
)
931 g_set_error (error
, G_CONVERT_ERROR
, G_CONVERT_ERROR_PARTIAL_INPUT
,
932 _("Partial character sequence at end of input"));
936 /* At this point, everything is valid, and we just need to convert
938 /********** DIFFERENT for UTF8/UCS4 **********/
939 result
= g_malloc (n_bytes
+ 1);
944 while (out
< result
+ n_bytes
)
949 if (c
>= 0xdc00 && c
< 0xe000) /* low surrogate */
951 wc
= SURROGATE_VALUE (high_surrogate
, c
);
954 else if (c
>= 0xd800 && c
< 0xdc00) /* high surrogate */
962 /********** DIFFERENT for UTF8/UCS4 **********/
963 out
+= g_unichar_to_utf8 (wc
, out
);
969 /********** DIFFERENT for UTF8/UCS4 **********/
973 /********** DIFFERENT for UTF8/UCS4 **********/
974 *items_written
= out
- result
;
978 *items_read
= in
- str
;
985 * @str: a UTF-16 encoded string
986 * @len: the maximum length of @str to use. If < 0, then
987 * the string is terminated with a 0 character.
988 * @items_read: location to store number of words read, or %NULL.
989 * If %NULL, then %G_CONVERT_ERROR_PARTIAL_INPUT will be
990 * returned in case @str contains a trailing partial
991 * character. If an error occurs then the index of the
992 * invalid input is stored here.
993 * @items_written: location to store number of characters written, or %NULL.
994 * The value stored here does not include the trailing
996 * @error: location to store the error occuring, or %NULL to ignore
997 * errors. Any of the errors in #GConvertError other than
998 * %G_CONVERT_ERROR_NO_CONVERSION may occur.
1000 * Convert a string from UTF-16 to UCS-4. The result will be
1001 * terminated with a 0 character.
1003 * Return value: a pointer to a newly allocated UCS-4 string.
1004 * This value must be freed with g_free(). If an
1005 * error occurs, %NULL will be returned and
1009 g_utf16_to_ucs4 (const gunichar2
*str
,
1012 gint
*items_written
,
1015 const gunichar2
*in
;
1017 gchar
*result
= NULL
;
1019 gunichar high_surrogate
;
1021 g_return_val_if_fail (str
!= 0, NULL
);
1026 while ((len
< 0 || in
- str
< len
) && *in
)
1031 if (c
>= 0xdc00 && c
< 0xe000) /* low surrogate */
1035 wc
= SURROGATE_VALUE (high_surrogate
, c
);
1040 g_set_error (error
, G_CONVERT_ERROR
, G_CONVERT_ERROR_ILLEGAL_SEQUENCE
,
1041 _("Invalid sequence in conversion input"));
1049 g_set_error (error
, G_CONVERT_ERROR
, G_CONVERT_ERROR_ILLEGAL_SEQUENCE
,
1050 _("Invalid sequence in conversion input"));
1054 if (c
>= 0xd800 && c
< 0xdc00) /* high surrogate */
1063 /********** DIFFERENT for UTF8/UCS4 **********/
1064 n_bytes
+= sizeof (gunichar
);
1070 if (high_surrogate
&& !items_read
)
1072 g_set_error (error
, G_CONVERT_ERROR
, G_CONVERT_ERROR_PARTIAL_INPUT
,
1073 _("Partial character sequence at end of input"));
1077 /* At this point, everything is valid, and we just need to convert
1079 /********** DIFFERENT for UTF8/UCS4 **********/
1080 result
= g_malloc (n_bytes
+ 4);
1085 while (out
< result
+ n_bytes
)
1090 if (c
>= 0xdc00 && c
< 0xe000) /* low surrogate */
1092 wc
= SURROGATE_VALUE (high_surrogate
, c
);
1095 else if (c
>= 0xd800 && c
< 0xdc00) /* high surrogate */
1103 /********** DIFFERENT for UTF8/UCS4 **********/
1104 *(gunichar
*)out
= wc
;
1105 out
+= sizeof (gunichar
);
1111 /********** DIFFERENT for UTF8/UCS4 **********/
1112 *(gunichar
*)out
= 0;
1115 /********** DIFFERENT for UTF8/UCS4 **********/
1116 *items_written
= (out
- result
) / sizeof (gunichar
);
1120 *items_read
= in
- str
;
1122 return (gunichar
*)result
;
1127 * @str: a UTF-8 encoded string
1128 * @len: the maximum length of @str to use. If < 0, then
1129 * the string is %NULL terminated.
1131 * @items_read: location to store number of bytes read, or %NULL.
1132 * If %NULL, then %G_CONVERT_ERROR_PARTIAL_INPUT will be
1133 * returned in case @str contains a trailing partial
1134 * character. If an error occurs then the index of the
1135 * invalid input is stored here.
1136 * @items_written: location to store number of words written, or %NULL.
1137 * The value stored here does not include the trailing
1139 * @error: location to store the error occuring, or %NULL to ignore
1140 * errors. Any of the errors in #GConvertError other than
1141 * %G_CONVERT_ERROR_NO_CONVERSION may occur.
1143 * Convert a string from UTF-8 to UTF-16. A 0 word will be
1144 * added to the result after the converted text.
1146 * Return value: a pointer to a newly allocated UTF-16 string.
1147 * This value must be freed with g_free(). If an
1148 * error occurs, %NULL will be returned and
1152 g_utf8_to_utf16 (const gchar
*str
,
1155 gint
*items_written
,
1158 gunichar2
*result
= NULL
;
1163 g_return_val_if_fail (str
!= NULL
, NULL
);
1167 while ((len
< 0 || str
+ len
- in
> 0) && *in
)
1169 gunichar wc
= g_utf8_get_char_extended (in
, str
+ len
- in
);
1170 if (wc
& 0x80000000)
1172 if (wc
== (gunichar
)-2)
1177 g_set_error (error
, G_CONVERT_ERROR
, G_CONVERT_ERROR_PARTIAL_INPUT
,
1178 _("Partial character sequence at end of input"));
1181 g_set_error (error
, G_CONVERT_ERROR
, G_CONVERT_ERROR_ILLEGAL_SEQUENCE
,
1182 _("Invalid byte sequence in conversion input"));
1189 else if (wc
< 0xe000)
1191 g_set_error (error
, G_CONVERT_ERROR
, G_CONVERT_ERROR_ILLEGAL_SEQUENCE
,
1192 _("Invalid sequence in conversion input"));
1196 else if (wc
< 0x10000)
1198 else if (wc
< 0x110000)
1202 g_set_error (error
, G_CONVERT_ERROR
, G_CONVERT_ERROR_ILLEGAL_SEQUENCE
,
1203 _("Character out of range for UTF-16"));
1208 in
= g_utf8_next_char (in
);
1211 result
= g_new (gunichar2
, n16
+ 1);
1214 for (i
= 0; i
< n16
;)
1216 gunichar wc
= g_utf8_get_char (in
);
1224 result
[i
++] = (wc
- 0x10000) / 0x400 + 0xd800;
1225 result
[i
++] = (wc
- 0x10000) % 0x400 + 0xdc00;
1228 in
= g_utf8_next_char (in
);
1234 *items_written
= n16
;
1238 *items_read
= in
- str
;
1245 * @str: a UCS-4 encoded string
1246 * @len: the maximum length of @str to use. If < 0, then
1247 * the string is terminated with a zero character.
1248 * @items_read: location to store number of bytes read, or %NULL.
1249 * If an error occurs then the index of the invalid input
1251 * @items_written: location to store number of words written, or %NULL.
1252 * The value stored here does not include the trailing
1254 * @error: location to store the error occuring, or %NULL to ignore
1255 * errors. Any of the errors in #GConvertError other than
1256 * %G_CONVERT_ERROR_NO_CONVERSION may occur.
1258 * Convert a string from UCS-4 to UTF-16. A 0 word will be
1259 * added to the result after the converted text.
1261 * Return value: a pointer to a newly allocated UTF-16 string.
1262 * This value must be freed with g_free(). If an
1263 * error occurs, %NULL will be returned and
1267 g_ucs4_to_utf16 (const gunichar
*str
,
1270 gint
*items_written
,
1273 gunichar2
*result
= NULL
;
1279 while ((len
< 0 || i
< len
) && str
[i
])
1281 gunichar wc
= str
[i
];
1285 else if (wc
< 0xe000)
1287 g_set_error (error
, G_CONVERT_ERROR
, G_CONVERT_ERROR_ILLEGAL_SEQUENCE
,
1288 _("Invalid sequence in conversion input"));
1292 else if (wc
< 0x10000)
1294 else if (wc
< 0x110000)
1298 g_set_error (error
, G_CONVERT_ERROR
, G_CONVERT_ERROR_ILLEGAL_SEQUENCE
,
1299 _("Character out of range for UTF-16"));
1307 result
= g_new (gunichar2
, n16
+ 1);
1309 for (i
= 0, j
= 0; j
< n16
; i
++)
1311 gunichar wc
= str
[i
];
1319 result
[j
++] = (wc
- 0x10000) / 0x400 + 0xd800;
1320 result
[j
++] = (wc
- 0x10000) % 0x400 + 0xdc00;
1326 *items_written
= n16
;
1337 * @str: a pointer to character data
1338 * @max_len: max bytes to validate, or -1 to go until nul
1339 * @end: return location for end of valid data
1341 * Validates UTF-8 encoded text. @str is the text to validate;
1342 * if @str is nul-terminated, then @max_len can be -1, otherwise
1343 * @max_len should be the number of bytes to validate.
1344 * If @end is non-NULL, then the end of the valid range
1345 * will be stored there (i.e. the address of the first invalid byte
1346 * if some bytes were invalid, or the end of the text being validated
1349 * Returns TRUE if all of @str was valid. Many GLib and GTK+
1350 * routines <emphasis>require</emphasis> valid UTF8 as input;
1351 * so data read from a file or the network should be checked
1352 * with g_utf8_validate() before doing anything else with it.
1354 * Return value: TRUE if the text was valid UTF-8.
1357 g_utf8_validate (const gchar
*str
,
1364 g_return_val_if_fail (str
!= NULL
, FALSE
);
1371 while ((max_len
< 0 || (p
- str
) < max_len
) && *p
)
1373 int i
, mask
= 0, len
;
1375 unsigned char c
= (unsigned char) *p
;
1377 UTF8_COMPUTE (c
, mask
, len
);
1382 /* check that the expected number of bytes exists in str */
1384 ((max_len
- (p
- str
)) < len
))
1387 UTF8_GET (result
, p
, i
, mask
, len
);
1389 if (UTF8_LENGTH (result
) != len
) /* Check for overlong UTF-8 */
1392 if (result
== (gunichar
)-1)
1395 if (!UNICODE_VALID (result
))
1404 /* See that we covered the entire length if a length was
1405 * passed in, or that we ended on a nul if not
1408 p
!= (str
+ max_len
))
1410 else if (max_len
< 0 &&