Version 11, interface, binary age 0.
[glib.git] / glib / gutf8.c
blob2aedfdc97f4b5a3ae16e6e1ab68775e51493cde1
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.
22 #include <config.h>
24 #include <stdlib.h>
25 #ifdef HAVE_CODESET
26 #include <langinfo.h>
27 #endif
28 #include <string.h>
30 #include "glib.h"
32 #ifdef G_PLATFORM_WIN32
33 #include <stdio.h>
34 #define STRICT
35 #include <windows.h>
36 #undef STRICT
37 #endif
39 #include "libcharset/libcharset.h"
41 #include "glibintl.h"
43 #define UTF8_COMPUTE(Char, Mask, Len) \
44 if (Char < 128) \
45 { \
46 Len = 1; \
47 Mask = 0x7f; \
48 } \
49 else if ((Char & 0xe0) == 0xc0) \
50 { \
51 Len = 2; \
52 Mask = 0x1f; \
53 } \
54 else if ((Char & 0xf0) == 0xe0) \
55 { \
56 Len = 3; \
57 Mask = 0x0f; \
58 } \
59 else if ((Char & 0xf8) == 0xf0) \
60 { \
61 Len = 4; \
62 Mask = 0x07; \
63 } \
64 else if ((Char & 0xfc) == 0xf8) \
65 { \
66 Len = 5; \
67 Mask = 0x03; \
68 } \
69 else if ((Char & 0xfe) == 0xfc) \
70 { \
71 Len = 6; \
72 Mask = 0x01; \
73 } \
74 else \
75 Len = -1;
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)) \
88 { \
89 if (((Chars)[(Count)] & 0xc0) != 0x80) \
90 { \
91 (Result) = -1; \
92 break; \
93 } \
94 (Result) <<= 6; \
95 (Result) |= ((Chars)[(Count)] & 0x3f); \
98 #define UNICODE_VALID(Char) \
99 ((Char) < 0x110000 && \
100 ((Char) < 0xD800 || (Char) >= 0xE000) && \
101 (Char) != 0xFFFE && (Char) != 0xFFFF)
104 static const gchar utf8_skip_data[256] = {
105 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,
106 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,
107 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,
108 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,
109 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,
110 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,
111 2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,
112 3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,4,4,4,4,4,4,4,4,5,5,5,5,6,6,0,0
115 const gchar * const g_utf8_skip = utf8_skip_data;
118 * g_utf8_find_prev_char:
119 * @str: pointer to the beginning of a UTF-8 encoded string
120 * @p: pointer to some position within @str
122 * Given a position @p with a UTF-8 encoded string @str, find the start
123 * of the previous UTF-8 character starting before @p. Returns %NULL if no
124 * UTF-8 characters are present in @p before @str.
126 * @p does not have to be at the beginning of a UTF-8 character. No check
127 * is made to see if the character found is actually valid other than
128 * it starts with an appropriate byte.
130 * Return value: a pointer to the found character or %NULL.
132 gchar *
133 g_utf8_find_prev_char (const char *str,
134 const char *p)
136 for (--p; p >= str; --p)
138 if ((*p & 0xc0) != 0x80)
139 return (gchar *)p;
141 return NULL;
145 * g_utf8_find_next_char:
146 * @p: a pointer to a position within a UTF-8 encoded string
147 * @end: a pointer to the end of the string, or %NULL to indicate
148 * that the string is nul-terminated, in which case
149 * the returned value will be
151 * Finds the start of the next UTF-8 character in the string after @p.
153 * @p does not have to be at the beginning of a UTF-8 character. No check
154 * is made to see if the character found is actually valid other than
155 * it starts with an appropriate byte.
157 * Return value: a pointer to the found character or %NULL
159 gchar *
160 g_utf8_find_next_char (const gchar *p,
161 const gchar *end)
163 if (*p)
165 if (end)
166 for (++p; p < end && (*p & 0xc0) == 0x80; ++p)
168 else
169 for (++p; (*p & 0xc0) == 0x80; ++p)
172 return (p == end) ? NULL : (gchar *)p;
176 * g_utf8_prev_char:
177 * @p: a pointer to a position within a UTF-8 encoded string
179 * Finds the previous UTF-8 character in the string before @p.
181 * @p does not have to be at the beginning of a UTF-8 character. No check
182 * is made to see if the character found is actually valid other than
183 * it starts with an appropriate byte. If @p might be the first
184 * character of the string, you must use g_utf8_find_prev_char() instead.
186 * Return value: a pointer to the found character.
188 gchar *
189 g_utf8_prev_char (const gchar *p)
191 while (TRUE)
193 p--;
194 if ((*p & 0xc0) != 0x80)
195 return (gchar *)p;
200 * g_utf8_strlen:
201 * @p: pointer to the start of a UTF-8 encoded string.
202 * @max: the maximum number of bytes to examine. If @max
203 * is less than 0, then the string is assumed to be
204 * nul-terminated.
206 * Returns the length of the string in characters.
208 * Return value: the length of the string in characters
210 glong
211 g_utf8_strlen (const gchar *p,
212 gssize max)
214 glong len = 0;
215 const gchar *start = p;
217 if (max < 0)
219 while (*p)
221 p = g_utf8_next_char (p);
222 ++len;
225 else
227 if (max == 0 || !*p)
228 return 0;
230 p = g_utf8_next_char (p);
232 while (p - start < max && *p)
234 ++len;
235 p = g_utf8_next_char (p);
238 /* only do the last len increment if we got a complete
239 * char (don't count partial chars)
241 if (p - start == max)
242 ++len;
245 return len;
249 * g_utf8_get_char:
250 * @p: a pointer to Unicode character encoded as UTF-8
252 * Converts a sequence of bytes encoded as UTF-8 to a Unicode character.
253 * If @p does not point to a valid UTF-8 encoded character, results are
254 * undefined. If you are not sure that the bytes are complete
255 * valid Unicode characters, you should use g_utf8_get_char_validated()
256 * instead.
258 * Return value: the resulting character
260 gunichar
261 g_utf8_get_char (const gchar *p)
263 int i, mask = 0, len;
264 gunichar result;
265 unsigned char c = (unsigned char) *p;
267 UTF8_COMPUTE (c, mask, len);
268 if (len == -1)
269 return (gunichar)-1;
270 UTF8_GET (result, p, i, mask, len);
272 return result;
276 * g_utf8_offset_to_pointer:
277 * @str: a UTF-8 encoded string
278 * @offset: a character offset within @str
280 * Converts from an integer character offset to a pointer to a position
281 * within the string.
283 * Return value: the resulting pointer
285 gchar *
286 g_utf8_offset_to_pointer (const gchar *str,
287 glong offset)
289 const gchar *s = str;
290 while (offset--)
291 s = g_utf8_next_char (s);
293 return (gchar *)s;
297 * g_utf8_pointer_to_offset:
298 * @str: a UTF-8 encoded string
299 * @pos: a pointer to a position within @str
301 * Converts from a pointer to position within a string to a integer
302 * character offset.
304 * Return value: the resulting character offset
306 glong
307 g_utf8_pointer_to_offset (const gchar *str,
308 const gchar *pos)
310 const gchar *s = str;
311 glong offset = 0;
313 while (s < pos)
315 s = g_utf8_next_char (s);
316 offset++;
319 return offset;
324 * g_utf8_strncpy:
325 * @dest: buffer to fill with characters from @src
326 * @src: UTF-8 encoded string
327 * @n: character count
329 * Like the standard C <function>strncpy()</function> function, but
330 * copies a given number of characters instead of a given number of
331 * bytes. The @src string must be valid UTF-8 encoded text.
332 * (Use g_utf8_validate() on all text before trying to use UTF-8
333 * utility functions with it.)
335 * Return value: @dest
337 gchar *
338 g_utf8_strncpy (gchar *dest,
339 const gchar *src,
340 gsize n)
342 const gchar *s = src;
343 while (n && *s)
345 s = g_utf8_next_char(s);
346 n--;
348 strncpy(dest, src, s - src);
349 dest[s - src] = 0;
350 return dest;
353 G_LOCK_DEFINE_STATIC (aliases);
355 static GHashTable *
356 get_alias_hash (void)
358 static GHashTable *alias_hash = NULL;
359 const char *aliases;
361 G_LOCK (aliases);
363 if (!alias_hash)
365 alias_hash = g_hash_table_new (g_str_hash, g_str_equal);
367 aliases = _g_locale_get_charset_aliases ();
368 while (*aliases != '\0')
370 const char *canonical;
371 const char *alias;
372 const char **alias_array;
373 int count = 0;
375 alias = aliases;
376 aliases += strlen (aliases) + 1;
377 canonical = aliases;
378 aliases += strlen (aliases) + 1;
380 alias_array = g_hash_table_lookup (alias_hash, canonical);
381 if (alias_array)
383 while (alias_array[count])
384 count++;
387 alias_array = g_renew (const char *, alias_array, count + 2);
388 alias_array[count] = alias;
389 alias_array[count + 1] = NULL;
391 g_hash_table_insert (alias_hash, (char *)canonical, alias_array);
395 G_UNLOCK (aliases);
397 return alias_hash;
400 /* As an abuse of the alias table, the following routines gets
401 * the charsets that are aliases for the canonical name.
403 const char **
404 _g_charset_get_aliases (const char *canonical_name)
406 GHashTable *alias_hash = get_alias_hash ();
408 return g_hash_table_lookup (alias_hash, canonical_name);
411 static gboolean
412 g_utf8_get_charset_internal (const char **a)
414 const char *charset = getenv("CHARSET");
416 if (charset && *charset)
418 *a = charset;
420 if (charset && strstr (charset, "UTF-8"))
421 return TRUE;
422 else
423 return FALSE;
426 /* The libcharset code tries to be thread-safe without
427 * a lock, but has a memory leak and a missing memory
428 * barrier, so we lock for it
430 G_LOCK (aliases);
431 charset = _g_locale_charset ();
432 G_UNLOCK (aliases);
434 if (charset && *charset)
436 *a = charset;
438 if (charset && strstr (charset, "UTF-8"))
439 return TRUE;
440 else
441 return FALSE;
444 /* Assume this for compatibility at present. */
445 *a = "US-ASCII";
447 return FALSE;
450 static int utf8_locale_cache = -1;
451 static const char *utf8_charset_cache = NULL;
454 * g_get_charset:
455 * @charset: return location for character set name
457 * Obtains the character set for the current locale; you might use
458 * this character set as an argument to g_convert(), to convert from
459 * the current locale's encoding to some other encoding. (Frequently
460 * g_locale_to_utf8() and g_locale_from_utf8() are nice shortcuts,
461 * though.)
463 * The return value is %TRUE if the locale's encoding is UTF-8, in that
464 * case you can perhaps avoid calling g_convert().
466 * The string returned in @charset is not allocated, and should not be
467 * freed.
469 * Return value: %TRUE if the returned charset is UTF-8
471 gboolean
472 g_get_charset (G_CONST_RETURN char **charset)
474 if (utf8_locale_cache != -1)
476 if (charset)
477 *charset = utf8_charset_cache;
478 return utf8_locale_cache;
480 utf8_locale_cache = g_utf8_get_charset_internal (&utf8_charset_cache);
481 if (charset)
482 *charset = utf8_charset_cache;
483 return utf8_locale_cache;
486 /* unicode_strchr */
489 * g_unichar_to_utf8:
490 * @c: a ISO10646 character code
491 * @outbuf: output buffer, must have at least 6 bytes of space.
492 * If %NULL, the length will be computed and returned
493 * and nothing will be written to @outbuf.
495 * Converts a single character to UTF-8.
497 * Return value: number of bytes written
500 g_unichar_to_utf8 (gunichar c,
501 gchar *outbuf)
503 guint len = 0;
504 int first;
505 int i;
507 if (c < 0x80)
509 first = 0;
510 len = 1;
512 else if (c < 0x800)
514 first = 0xc0;
515 len = 2;
517 else if (c < 0x10000)
519 first = 0xe0;
520 len = 3;
522 else if (c < 0x200000)
524 first = 0xf0;
525 len = 4;
527 else if (c < 0x4000000)
529 first = 0xf8;
530 len = 5;
532 else
534 first = 0xfc;
535 len = 6;
538 if (outbuf)
540 for (i = len - 1; i > 0; --i)
542 outbuf[i] = (c & 0x3f) | 0x80;
543 c >>= 6;
545 outbuf[0] = c | first;
548 return len;
552 * g_utf8_strchr:
553 * @p: a nul-terminated UTF-8 encoded string
554 * @len: the maximum length of @p
555 * @c: a ISO10646 character
557 * Finds the leftmost occurrence of the given ISO10646 character
558 * in a UTF-8 encoded string, while limiting the search to @len bytes.
559 * If @len is -1, allow unbounded search.
561 * Return value: %NULL if the string does not contain the character,
562 * otherwise, a pointer to the start of the leftmost occurrence of
563 * the character in the string.
565 gchar *
566 g_utf8_strchr (const char *p,
567 gssize len,
568 gunichar c)
570 gchar ch[10];
572 gint charlen = g_unichar_to_utf8 (c, ch);
573 ch[charlen] = '\0';
575 return g_strstr_len (p, len, ch);
580 * g_utf8_strrchr:
581 * @p: a nul-terminated UTF-8 encoded string
582 * @len: the maximum length of @p
583 * @c: a ISO10646 character
585 * Find the rightmost occurrence of the given ISO10646 character
586 * in a UTF-8 encoded string, while limiting the search to @len bytes.
587 * If @len is -1, allow unbounded search.
589 * Return value: %NULL if the string does not contain the character,
590 * otherwise, a pointer to the start of the rightmost occurrence of the
591 * character in the string.
593 gchar *
594 g_utf8_strrchr (const char *p,
595 gssize len,
596 gunichar c)
598 gchar ch[10];
600 gint charlen = g_unichar_to_utf8 (c, ch);
601 ch[charlen] = '\0';
603 return g_strrstr_len (p, len, ch);
607 /* Like g_utf8_get_char, but take a maximum length
608 * and return (gunichar)-2 on incomplete trailing character
610 static inline gunichar
611 g_utf8_get_char_extended (const gchar *p,
612 gssize max_len)
614 guint i, len;
615 gunichar wc = (guchar) *p;
617 if (wc < 0x80)
619 return wc;
621 else if (wc < 0xc0)
623 return (gunichar)-1;
625 else if (wc < 0xe0)
627 len = 2;
628 wc &= 0x1f;
630 else if (wc < 0xf0)
632 len = 3;
633 wc &= 0x0f;
635 else if (wc < 0xf8)
637 len = 4;
638 wc &= 0x07;
640 else if (wc < 0xfc)
642 len = 5;
643 wc &= 0x03;
645 else if (wc < 0xfe)
647 len = 6;
648 wc &= 0x01;
650 else
652 return (gunichar)-1;
655 if (max_len >= 0 && len > max_len)
657 for (i = 1; i < max_len; i++)
659 if ((((guchar *)p)[i] & 0xc0) != 0x80)
660 return (gunichar)-1;
662 return (gunichar)-2;
665 for (i = 1; i < len; ++i)
667 gunichar ch = ((guchar *)p)[i];
669 if ((ch & 0xc0) != 0x80)
671 if (ch)
672 return (gunichar)-1;
673 else
674 return (gunichar)-2;
677 wc <<= 6;
678 wc |= (ch & 0x3f);
681 if (UTF8_LENGTH(wc) != len)
682 return (gunichar)-1;
684 return wc;
688 * g_utf8_get_char_validated:
689 * @p: a pointer to Unicode character encoded as UTF-8
690 * @max_len: the maximum number of bytes to read, or -1, for no maximum.
692 * Convert a sequence of bytes encoded as UTF-8 to a Unicode character.
693 * This function checks for incomplete characters, for invalid characters
694 * such as characters that are out of the range of Unicode, and for
695 * overlong encodings of valid characters.
697 * Return value: the resulting character. If @p points to a partial
698 * sequence at the end of a string that could begin a valid character,
699 * returns (gunichar)-2; otherwise, if @p does not point to a valid
700 * UTF-8 encoded Unicode character, returns (gunichar)-1.
702 gunichar
703 g_utf8_get_char_validated (const gchar *p,
704 gssize max_len)
706 gunichar result = g_utf8_get_char_extended (p, max_len);
708 if (result & 0x80000000)
709 return result;
710 else if (!UNICODE_VALID (result))
711 return (gunichar)-1;
712 else
713 return result;
717 * g_utf8_to_ucs4_fast:
718 * @str: a UTF-8 encoded string
719 * @len: the maximum length of @str to use. If @len < 0, then
720 * the string is nul-terminated.
721 * @items_written: location to store the number of characters in the
722 * result, or %NULL.
724 * Convert a string from UTF-8 to a 32-bit fixed width
725 * representation as UCS-4, assuming valid UTF-8 input.
726 * This function is roughly twice as fast as g_utf8_to_ucs4()
727 * but does no error checking on the input.
729 * Return value: a pointer to a newly allocated UCS-4 string.
730 * This value must be freed with g_free().
732 gunichar *
733 g_utf8_to_ucs4_fast (const gchar *str,
734 glong len,
735 glong *items_written)
737 gint j, charlen;
738 gunichar *result;
739 gint n_chars, i;
740 const gchar *p;
742 g_return_val_if_fail (str != NULL, NULL);
744 p = str;
745 n_chars = 0;
746 if (len < 0)
748 while (*p)
750 p = g_utf8_next_char (p);
751 ++n_chars;
754 else
756 while (p < str + len && *p)
758 p = g_utf8_next_char (p);
759 ++n_chars;
763 result = g_new (gunichar, n_chars + 1);
765 p = str;
766 for (i=0; i < n_chars; i++)
768 gunichar wc = ((unsigned char *)p)[0];
770 if (wc < 0x80)
772 result[i] = wc;
773 p++;
775 else
777 if (wc < 0xe0)
779 charlen = 2;
780 wc &= 0x1f;
782 else if (wc < 0xf0)
784 charlen = 3;
785 wc &= 0x0f;
787 else if (wc < 0xf8)
789 charlen = 4;
790 wc &= 0x07;
792 else if (wc < 0xfc)
794 charlen = 5;
795 wc &= 0x03;
797 else
799 charlen = 6;
800 wc &= 0x01;
803 for (j = 1; j < charlen; j++)
805 wc <<= 6;
806 wc |= ((unsigned char *)p)[j] & 0x3f;
809 result[i] = wc;
810 p += charlen;
813 result[i] = 0;
815 if (items_written)
816 *items_written = i;
818 return result;
822 * g_utf8_to_ucs4:
823 * @str: a UTF-8 encoded string
824 * @len: the maximum length of @str to use. If @len < 0, then
825 * the string is nul-terminated.
826 * @items_read: location to store number of bytes read, or %NULL.
827 * If %NULL, then %G_CONVERT_ERROR_PARTIAL_INPUT will be
828 * returned in case @str contains a trailing partial
829 * character. If an error occurs then the index of the
830 * invalid input is stored here.
831 * @items_written: location to store number of characters written or %NULL.
832 * The value here stored does not include the trailing 0
833 * character.
834 * @error: location to store the error occuring, or %NULL to ignore
835 * errors. Any of the errors in #GConvertError other than
836 * %G_CONVERT_ERROR_NO_CONVERSION may occur.
838 * Convert a string from UTF-8 to a 32-bit fixed width
839 * representation as UCS-4. A trailing 0 will be added to the
840 * string after the converted text.
842 * Return value: a pointer to a newly allocated UCS-4 string.
843 * This value must be freed with g_free(). If an
844 * error occurs, %NULL will be returned and
845 * @error set.
847 gunichar *
848 g_utf8_to_ucs4 (const gchar *str,
849 glong len,
850 glong *items_read,
851 glong *items_written,
852 GError **error)
854 gunichar *result = NULL;
855 gint n_chars, i;
856 const gchar *in;
858 in = str;
859 n_chars = 0;
860 while ((len < 0 || str + len - in > 0) && *in)
862 gunichar wc = g_utf8_get_char_extended (in, str + len - in);
863 if (wc & 0x80000000)
865 if (wc == (gunichar)-2)
867 if (items_read)
868 break;
869 else
870 g_set_error (error, G_CONVERT_ERROR, G_CONVERT_ERROR_PARTIAL_INPUT,
871 _("Partial character sequence at end of input"));
873 else
874 g_set_error (error, G_CONVERT_ERROR, G_CONVERT_ERROR_ILLEGAL_SEQUENCE,
875 _("Invalid byte sequence in conversion input"));
877 goto err_out;
880 n_chars++;
882 in = g_utf8_next_char (in);
885 result = g_new (gunichar, n_chars + 1);
887 in = str;
888 for (i=0; i < n_chars; i++)
890 result[i] = g_utf8_get_char (in);
891 in = g_utf8_next_char (in);
893 result[i] = 0;
895 if (items_written)
896 *items_written = n_chars;
898 err_out:
899 if (items_read)
900 *items_read = in - str;
902 return result;
906 * g_ucs4_to_utf8:
907 * @str: a UCS-4 encoded string
908 * @len: the maximum length of @str to use. If @len < 0, then
909 * the string is terminated with a 0 character.
910 * @items_read: location to store number of characters read read, or %NULL.
911 * @items_written: location to store number of bytes written or %NULL.
912 * The value here stored does not include the trailing 0
913 * byte.
914 * @error: location to store the error occuring, or %NULL to ignore
915 * errors. Any of the errors in #GConvertError other than
916 * %G_CONVERT_ERROR_NO_CONVERSION may occur.
918 * Convert a string from a 32-bit fixed width representation as UCS-4.
919 * to UTF-8. The result will be terminated with a 0 byte.
921 * Return value: a pointer to a newly allocated UTF-8 string.
922 * This value must be freed with g_free(). If an
923 * error occurs, %NULL will be returned and
924 * @error set.
926 gchar *
927 g_ucs4_to_utf8 (const gunichar *str,
928 glong len,
929 glong *items_read,
930 glong *items_written,
931 GError **error)
933 gint result_length;
934 gchar *result = NULL;
935 gchar *p;
936 gint i;
938 result_length = 0;
939 for (i = 0; len < 0 || i < len ; i++)
941 if (!str[i])
942 break;
944 if (str[i] >= 0x80000000)
946 if (items_read)
947 *items_read = i;
949 g_set_error (error, G_CONVERT_ERROR, G_CONVERT_ERROR_ILLEGAL_SEQUENCE,
950 _("Character out of range for UTF-8"));
951 goto err_out;
954 result_length += UTF8_LENGTH (str[i]);
957 result = g_malloc (result_length + 1);
958 p = result;
960 i = 0;
961 while (p < result + result_length)
962 p += g_unichar_to_utf8 (str[i++], p);
964 *p = '\0';
966 if (items_written)
967 *items_written = p - result;
969 err_out:
970 if (items_read)
971 *items_read = i;
973 return result;
976 #define SURROGATE_VALUE(h,l) (((h) - 0xd800) * 0x400 + (l) - 0xdc00 + 0x10000)
979 * g_utf16_to_utf8:
980 * @str: a UTF-16 encoded string
981 * @len: the maximum length of @str to use. If @len < 0, then
982 * the string is terminated with a 0 character.
983 * @items_read: location to store number of words read, or %NULL.
984 * If %NULL, then %G_CONVERT_ERROR_PARTIAL_INPUT will be
985 * returned in case @str contains a trailing partial
986 * character. If an error occurs then the index of the
987 * invalid input is stored here.
988 * @items_written: location to store number of bytes written, or %NULL.
989 * The value stored here does not include the trailing
990 * 0 byte.
991 * @error: location to store the error occuring, or %NULL to ignore
992 * errors. Any of the errors in #GConvertError other than
993 * %G_CONVERT_ERROR_NO_CONVERSION may occur.
995 * Convert a string from UTF-16 to UTF-8. The result will be
996 * terminated with a 0 byte.
998 * Return value: a pointer to a newly allocated UTF-8 string.
999 * This value must be freed with g_free(). If an
1000 * error occurs, %NULL will be returned and
1001 * @error set.
1003 gchar *
1004 g_utf16_to_utf8 (const gunichar2 *str,
1005 glong len,
1006 glong *items_read,
1007 glong *items_written,
1008 GError **error)
1010 /* This function and g_utf16_to_ucs4 are almost exactly identical - The lines that differ
1011 * are marked.
1013 const gunichar2 *in;
1014 gchar *out;
1015 gchar *result = NULL;
1016 gint n_bytes;
1017 gunichar high_surrogate;
1019 g_return_val_if_fail (str != 0, NULL);
1021 n_bytes = 0;
1022 in = str;
1023 high_surrogate = 0;
1024 while ((len < 0 || in - str < len) && *in)
1026 gunichar2 c = *in;
1027 gunichar wc;
1029 if (c >= 0xdc00 && c < 0xe000) /* low surrogate */
1031 if (high_surrogate)
1033 wc = SURROGATE_VALUE (high_surrogate, c);
1034 high_surrogate = 0;
1036 else
1038 g_set_error (error, G_CONVERT_ERROR, G_CONVERT_ERROR_ILLEGAL_SEQUENCE,
1039 _("Invalid sequence in conversion input"));
1040 goto err_out;
1043 else
1045 if (high_surrogate)
1047 g_set_error (error, G_CONVERT_ERROR, G_CONVERT_ERROR_ILLEGAL_SEQUENCE,
1048 _("Invalid sequence in conversion input"));
1049 goto err_out;
1052 if (c >= 0xd800 && c < 0xdc00) /* high surrogate */
1054 high_surrogate = c;
1055 goto next1;
1057 else
1058 wc = c;
1061 /********** DIFFERENT for UTF8/UCS4 **********/
1062 n_bytes += UTF8_LENGTH (wc);
1064 next1:
1065 in++;
1068 if (high_surrogate && !items_read)
1070 g_set_error (error, G_CONVERT_ERROR, G_CONVERT_ERROR_PARTIAL_INPUT,
1071 _("Partial character sequence at end of input"));
1072 goto err_out;
1075 /* At this point, everything is valid, and we just need to convert
1077 /********** DIFFERENT for UTF8/UCS4 **********/
1078 result = g_malloc (n_bytes + 1);
1080 high_surrogate = 0;
1081 out = result;
1082 in = str;
1083 while (out < result + n_bytes)
1085 gunichar2 c = *in;
1086 gunichar wc;
1088 if (c >= 0xdc00 && c < 0xe000) /* low surrogate */
1090 wc = SURROGATE_VALUE (high_surrogate, c);
1091 high_surrogate = 0;
1093 else if (c >= 0xd800 && c < 0xdc00) /* high surrogate */
1095 high_surrogate = c;
1096 goto next2;
1098 else
1099 wc = c;
1101 /********** DIFFERENT for UTF8/UCS4 **********/
1102 out += g_unichar_to_utf8 (wc, out);
1104 next2:
1105 in++;
1108 /********** DIFFERENT for UTF8/UCS4 **********/
1109 *out = '\0';
1111 if (items_written)
1112 /********** DIFFERENT for UTF8/UCS4 **********/
1113 *items_written = out - result;
1115 err_out:
1116 if (items_read)
1117 *items_read = in - str;
1119 return result;
1123 * g_utf16_to_ucs4:
1124 * @str: a UTF-16 encoded string
1125 * @len: the maximum length of @str to use. If @len < 0, then
1126 * the string is terminated with a 0 character.
1127 * @items_read: location to store number of words read, or %NULL.
1128 * If %NULL, then %G_CONVERT_ERROR_PARTIAL_INPUT will be
1129 * returned in case @str contains a trailing partial
1130 * character. If an error occurs then the index of the
1131 * invalid input is stored here.
1132 * @items_written: location to store number of characters written, or %NULL.
1133 * The value stored here does not include the trailing
1134 * 0 character.
1135 * @error: location to store the error occuring, or %NULL to ignore
1136 * errors. Any of the errors in #GConvertError other than
1137 * %G_CONVERT_ERROR_NO_CONVERSION may occur.
1139 * Convert a string from UTF-16 to UCS-4. The result will be
1140 * terminated with a 0 character.
1142 * Return value: a pointer to a newly allocated UCS-4 string.
1143 * This value must be freed with g_free(). If an
1144 * error occurs, %NULL will be returned and
1145 * @error set.
1147 gunichar *
1148 g_utf16_to_ucs4 (const gunichar2 *str,
1149 glong len,
1150 glong *items_read,
1151 glong *items_written,
1152 GError **error)
1154 const gunichar2 *in;
1155 gchar *out;
1156 gchar *result = NULL;
1157 gint n_bytes;
1158 gunichar high_surrogate;
1160 g_return_val_if_fail (str != 0, NULL);
1162 n_bytes = 0;
1163 in = str;
1164 high_surrogate = 0;
1165 while ((len < 0 || in - str < len) && *in)
1167 gunichar2 c = *in;
1168 gunichar wc;
1170 if (c >= 0xdc00 && c < 0xe000) /* low surrogate */
1172 if (high_surrogate)
1174 wc = SURROGATE_VALUE (high_surrogate, c);
1175 high_surrogate = 0;
1177 else
1179 g_set_error (error, G_CONVERT_ERROR, G_CONVERT_ERROR_ILLEGAL_SEQUENCE,
1180 _("Invalid sequence in conversion input"));
1181 goto err_out;
1184 else
1186 if (high_surrogate)
1188 g_set_error (error, G_CONVERT_ERROR, G_CONVERT_ERROR_ILLEGAL_SEQUENCE,
1189 _("Invalid sequence in conversion input"));
1190 goto err_out;
1193 if (c >= 0xd800 && c < 0xdc00) /* high surrogate */
1195 high_surrogate = c;
1196 goto next1;
1198 else
1199 wc = c;
1202 /********** DIFFERENT for UTF8/UCS4 **********/
1203 n_bytes += sizeof (gunichar);
1205 next1:
1206 in++;
1209 if (high_surrogate && !items_read)
1211 g_set_error (error, G_CONVERT_ERROR, G_CONVERT_ERROR_PARTIAL_INPUT,
1212 _("Partial character sequence at end of input"));
1213 goto err_out;
1216 /* At this point, everything is valid, and we just need to convert
1218 /********** DIFFERENT for UTF8/UCS4 **********/
1219 result = g_malloc (n_bytes + 4);
1221 high_surrogate = 0;
1222 out = result;
1223 in = str;
1224 while (out < result + n_bytes)
1226 gunichar2 c = *in;
1227 gunichar wc;
1229 if (c >= 0xdc00 && c < 0xe000) /* low surrogate */
1231 wc = SURROGATE_VALUE (high_surrogate, c);
1232 high_surrogate = 0;
1234 else if (c >= 0xd800 && c < 0xdc00) /* high surrogate */
1236 high_surrogate = c;
1237 goto next2;
1239 else
1240 wc = c;
1242 /********** DIFFERENT for UTF8/UCS4 **********/
1243 *(gunichar *)out = wc;
1244 out += sizeof (gunichar);
1246 next2:
1247 in++;
1250 /********** DIFFERENT for UTF8/UCS4 **********/
1251 *(gunichar *)out = 0;
1253 if (items_written)
1254 /********** DIFFERENT for UTF8/UCS4 **********/
1255 *items_written = (out - result) / sizeof (gunichar);
1257 err_out:
1258 if (items_read)
1259 *items_read = in - str;
1261 return (gunichar *)result;
1265 * g_utf8_to_utf16:
1266 * @str: a UTF-8 encoded string
1267 * @len: the maximum length of @str to use. If @len < 0, then
1268 * the string is nul-terminated.
1270 * @items_read: location to store number of bytes read, or %NULL.
1271 * If %NULL, then %G_CONVERT_ERROR_PARTIAL_INPUT will be
1272 * returned in case @str contains a trailing partial
1273 * character. If an error occurs then the index of the
1274 * invalid input is stored here.
1275 * @items_written: location to store number of words written, or %NULL.
1276 * The value stored here does not include the trailing
1277 * 0 word.
1278 * @error: location to store the error occuring, or %NULL to ignore
1279 * errors. Any of the errors in #GConvertError other than
1280 * %G_CONVERT_ERROR_NO_CONVERSION may occur.
1282 * Convert a string from UTF-8 to UTF-16. A 0 word will be
1283 * added to the result after the converted text.
1285 * Return value: a pointer to a newly allocated UTF-16 string.
1286 * This value must be freed with g_free(). If an
1287 * error occurs, %NULL will be returned and
1288 * @error set.
1290 gunichar2 *
1291 g_utf8_to_utf16 (const gchar *str,
1292 glong len,
1293 glong *items_read,
1294 glong *items_written,
1295 GError **error)
1297 gunichar2 *result = NULL;
1298 gint n16;
1299 const gchar *in;
1300 gint i;
1302 g_return_val_if_fail (str != NULL, NULL);
1304 in = str;
1305 n16 = 0;
1306 while ((len < 0 || str + len - in > 0) && *in)
1308 gunichar wc = g_utf8_get_char_extended (in, str + len - in);
1309 if (wc & 0x80000000)
1311 if (wc == (gunichar)-2)
1313 if (items_read)
1314 break;
1315 else
1316 g_set_error (error, G_CONVERT_ERROR, G_CONVERT_ERROR_PARTIAL_INPUT,
1317 _("Partial character sequence at end of input"));
1319 else
1320 g_set_error (error, G_CONVERT_ERROR, G_CONVERT_ERROR_ILLEGAL_SEQUENCE,
1321 _("Invalid byte sequence in conversion input"));
1323 goto err_out;
1326 if (wc < 0xd800)
1327 n16 += 1;
1328 else if (wc < 0xe000)
1330 g_set_error (error, G_CONVERT_ERROR, G_CONVERT_ERROR_ILLEGAL_SEQUENCE,
1331 _("Invalid sequence in conversion input"));
1333 goto err_out;
1335 else if (wc < 0x10000)
1336 n16 += 1;
1337 else if (wc < 0x110000)
1338 n16 += 2;
1339 else
1341 g_set_error (error, G_CONVERT_ERROR, G_CONVERT_ERROR_ILLEGAL_SEQUENCE,
1342 _("Character out of range for UTF-16"));
1344 goto err_out;
1347 in = g_utf8_next_char (in);
1350 result = g_new (gunichar2, n16 + 1);
1352 in = str;
1353 for (i = 0; i < n16;)
1355 gunichar wc = g_utf8_get_char (in);
1357 if (wc < 0x10000)
1359 result[i++] = wc;
1361 else
1363 result[i++] = (wc - 0x10000) / 0x400 + 0xd800;
1364 result[i++] = (wc - 0x10000) % 0x400 + 0xdc00;
1367 in = g_utf8_next_char (in);
1370 result[i] = 0;
1372 if (items_written)
1373 *items_written = n16;
1375 err_out:
1376 if (items_read)
1377 *items_read = in - str;
1379 return result;
1383 * g_ucs4_to_utf16:
1384 * @str: a UCS-4 encoded string
1385 * @len: the maximum length of @str to use. If @len < 0, then
1386 * the string is terminated with a 0 character.
1387 * @items_read: location to store number of bytes read, or %NULL.
1388 * If an error occurs then the index of the invalid input
1389 * is stored here.
1390 * @items_written: location to store number of words written, or %NULL.
1391 * The value stored here does not include the trailing
1392 * 0 word.
1393 * @error: location to store the error occuring, or %NULL to ignore
1394 * errors. Any of the errors in #GConvertError other than
1395 * %G_CONVERT_ERROR_NO_CONVERSION may occur.
1397 * Convert a string from UCS-4 to UTF-16. A 0 word will be
1398 * added to the result after the converted text.
1400 * Return value: a pointer to a newly allocated UTF-16 string.
1401 * This value must be freed with g_free(). If an
1402 * error occurs, %NULL will be returned and
1403 * @error set.
1405 gunichar2 *
1406 g_ucs4_to_utf16 (const gunichar *str,
1407 glong len,
1408 glong *items_read,
1409 glong *items_written,
1410 GError **error)
1412 gunichar2 *result = NULL;
1413 gint n16;
1414 gint i, j;
1416 n16 = 0;
1417 i = 0;
1418 while ((len < 0 || i < len) && str[i])
1420 gunichar wc = str[i];
1422 if (wc < 0xd800)
1423 n16 += 1;
1424 else if (wc < 0xe000)
1426 g_set_error (error, G_CONVERT_ERROR, G_CONVERT_ERROR_ILLEGAL_SEQUENCE,
1427 _("Invalid sequence in conversion input"));
1429 goto err_out;
1431 else if (wc < 0x10000)
1432 n16 += 1;
1433 else if (wc < 0x110000)
1434 n16 += 2;
1435 else
1437 g_set_error (error, G_CONVERT_ERROR, G_CONVERT_ERROR_ILLEGAL_SEQUENCE,
1438 _("Character out of range for UTF-16"));
1440 goto err_out;
1443 i++;
1446 result = g_new (gunichar2, n16 + 1);
1448 for (i = 0, j = 0; j < n16; i++)
1450 gunichar wc = str[i];
1452 if (wc < 0x10000)
1454 result[j++] = wc;
1456 else
1458 result[j++] = (wc - 0x10000) / 0x400 + 0xd800;
1459 result[j++] = (wc - 0x10000) % 0x400 + 0xdc00;
1462 result[j] = 0;
1464 if (items_written)
1465 *items_written = n16;
1467 err_out:
1468 if (items_read)
1469 *items_read = i;
1471 return result;
1475 * g_utf8_validate:
1476 * @str: a pointer to character data
1477 * @max_len: max bytes to validate, or -1 to go until nul
1478 * @end: return location for end of valid data
1480 * Validates UTF-8 encoded text. @str is the text to validate;
1481 * if @str is nul-terminated, then @max_len can be -1, otherwise
1482 * @max_len should be the number of bytes to validate.
1483 * If @end is non-%NULL, then the end of the valid range
1484 * will be stored there (i.e. the address of the first invalid byte
1485 * if some bytes were invalid, or the end of the text being validated
1486 * otherwise).
1488 * Returns %TRUE if all of @str was valid. Many GLib and GTK+
1489 * routines <emphasis>require</emphasis> valid UTF-8 as input;
1490 * so data read from a file or the network should be checked
1491 * with g_utf8_validate() before doing anything else with it.
1493 * Return value: %TRUE if the text was valid UTF-8
1495 gboolean
1496 g_utf8_validate (const gchar *str,
1497 gssize max_len,
1498 const gchar **end)
1501 const gchar *p;
1503 g_return_val_if_fail (str != NULL, FALSE);
1505 if (end)
1506 *end = str;
1508 p = str;
1510 while ((max_len < 0 || (p - str) < max_len) && *p)
1512 int i, mask = 0, len;
1513 gunichar result;
1514 unsigned char c = (unsigned char) *p;
1516 UTF8_COMPUTE (c, mask, len);
1518 if (len == -1)
1519 break;
1521 /* check that the expected number of bytes exists in str */
1522 if (max_len >= 0 &&
1523 ((max_len - (p - str)) < len))
1524 break;
1526 UTF8_GET (result, p, i, mask, len);
1528 if (UTF8_LENGTH (result) != len) /* Check for overlong UTF-8 */
1529 break;
1531 if (result == (gunichar)-1)
1532 break;
1534 if (!UNICODE_VALID (result))
1535 break;
1537 p += len;
1540 if (end)
1541 *end = p;
1543 /* See that we covered the entire length if a length was
1544 * passed in, or that we ended on a nul if not
1546 if (max_len >= 0 &&
1547 p != (str + max_len))
1548 return FALSE;
1549 else if (max_len < 0 &&
1550 *p != '\0')
1551 return FALSE;
1552 else
1553 return TRUE;
1557 * g_unichar_validate:
1558 * @ch: a Unicode character
1560 * Checks whether @ch is a valid Unicode character. Some possible
1561 * integer values of @ch will not be valid. 0 is considered a valid
1562 * character, though it's normally a string terminator.
1564 * Return value: %TRUE if @ch is a valid Unicode character
1566 gboolean
1567 g_unichar_validate (gunichar ch)
1569 return UNICODE_VALID (ch);