X-Git-Url: http://review.tizen.org/git/?a=blobdiff_plain;f=glib%2Fgutf8.c;h=579c017f7393fc30a3fe1e0f9fae5929a3853a92;hb=d217429729aad360f372633f2ec99778c0fc08d5;hp=27b1e4ce6536306ca2c5a1c8c1ef89f20b0e88a2;hpb=a4e38786750d538b334b8a7a7cc9f5a3ff48bc33;p=platform%2Fupstream%2Fglib.git diff --git a/glib/gutf8.c b/glib/gutf8.c index 27b1e4c..579c017 100644 --- a/glib/gutf8.c +++ b/glib/gutf8.c @@ -14,9 +14,7 @@ * Lesser General Public License for more details. * * You should have received a copy of the GNU Lesser General Public - * License along with this library; if not, write to the - * Free Software Foundation, Inc., 59 Temple Place - Suite 330, - * Boston, MA 02111-1307, USA. + * License along with this library; if not, see . */ #include "config.h" @@ -27,8 +25,6 @@ #endif #include -#include "glib.h" - #ifdef G_PLATFORM_WIN32 #include #define STRICT @@ -36,10 +32,13 @@ #undef STRICT #endif -#include "libcharset/libcharset.h" - +#include "gconvert.h" +#include "ghash.h" +#include "gstrfuncs.h" +#include "gtestutils.h" +#include "gtypes.h" +#include "gthread.h" #include "glibintl.h" -#include "galias.h" #define UTF8_COMPUTE(Char, Mask, Len) \ if (Char < 128) \ @@ -96,29 +95,21 @@ (Result) |= ((Chars)[(Count)] & 0x3f); \ } -/** +/* * Check whether a Unicode (5.2) char is in a valid range. * * The first check comes from the Unicode guarantee to never encode * a point above 0x0010ffff, since UTF-16 couldn't represent it. * * The second check covers surrogate pairs (category Cs). - * - * The last two checks cover "Noncharacter": defined as: - * "A code point that is permanently reserved for - * internal use, and that should never be interchanged. In - * Unicode 3.1, these consist of the values U+nFFFE and U+nFFFF - * (where n is from 0 to 10_16) and the values U+FDD0..U+FDEF." * * @param Char the character */ #define UNICODE_VALID(Char) \ ((Char) < 0x110000 && \ - (((Char) & 0xFFFFF800) != 0xD800) && \ - ((Char) < 0xFDD0 || (Char) > 0xFDEF) && \ - ((Char) & 0xFFFE) != 0xFFFE) - - + (((Char) & 0xFFFFF800) != 0xD800)) + + static const gchar utf8_skip_data[256] = { 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, 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, @@ -145,8 +136,8 @@ const gchar * const g_utf8_skip = utf8_skip_data; * is made to see if the character found is actually valid other than * it starts with an appropriate byte. * - * Return value: a pointer to the found character or %NULL. - **/ + * Returns: a pointer to the found character or %NULL. + */ gchar * g_utf8_find_prev_char (const char *str, const char *p) @@ -163,7 +154,7 @@ g_utf8_find_prev_char (const char *str, * g_utf8_find_next_char: * @p: a pointer to a position within a UTF-8 encoded string * @end: a pointer to the byte following the end of the string, - * or %NULL to indicate that the string is nul-terminated. + * or %NULL to indicate that the string is nul-terminated * * Finds the start of the next UTF-8 character in the string after @p. * @@ -171,8 +162,8 @@ g_utf8_find_prev_char (const char *str, * is made to see if the character found is actually valid other than * it starts with an appropriate byte. * - * Return value: a pointer to the found character or %NULL - **/ + * Returns: a pointer to the found character or %NULL + */ gchar * g_utf8_find_next_char (const gchar *p, const gchar *end) @@ -200,8 +191,8 @@ g_utf8_find_next_char (const gchar *p, * it starts with an appropriate byte. If @p might be the first * character of the string, you must use g_utf8_find_prev_char() instead. * - * Return value: a pointer to the found character. - **/ + * Returns: a pointer to the found character + */ gchar * g_utf8_prev_char (const gchar *p) { @@ -212,20 +203,22 @@ g_utf8_prev_char (const gchar *p) return (gchar *)p; } } - + /** * g_utf8_strlen: * @p: pointer to the start of a UTF-8 encoded string * @max: the maximum number of bytes to examine. If @max * is less than 0, then the string is assumed to be * nul-terminated. If @max is 0, @p will not be examined and - * may be %NULL. + * may be %NULL. If @max is greater than 0, up to @max + * bytes are examined * * Computes the length of the string in characters, not including - * the terminating nul character. + * the terminating nul character. If the @max'th byte falls in the + * middle of a character, the last (partial) character is not counted. * - * Return value: the length of the string in characters - **/ + * Returns: the length of the string in characters + */ glong g_utf8_strlen (const gchar *p, gssize max) @@ -266,17 +259,49 @@ g_utf8_strlen (const gchar *p, } /** + * g_utf8_substring: + * @str: a UTF-8 encoded string + * @start_pos: a character offset within @str + * @end_pos: another character offset within @str + * + * Copies a substring out of a UTF-8 encoded string. + * The substring will contain @end_pos - @start_pos characters. + * + * Returns: a newly allocated copy of the requested + * substring. Free with g_free() when no longer needed. + * + * Since: 2.30 + */ +gchar * +g_utf8_substring (const gchar *str, + glong start_pos, + glong end_pos) +{ + gchar *start, *end, *out; + + start = g_utf8_offset_to_pointer (str, start_pos); + end = g_utf8_offset_to_pointer (start, end_pos - start_pos); + + out = g_malloc (end - start + 1); + memcpy (out, start, end - start); + out[end - start] = 0; + + return out; +} + +/** * g_utf8_get_char: * @p: a pointer to Unicode character encoded as UTF-8 * * Converts a sequence of bytes encoded as UTF-8 to a Unicode character. - * If @p does not point to a valid UTF-8 encoded character, results are - * undefined. If you are not sure that the bytes are complete + * + * If @p does not point to a valid UTF-8 encoded character, results + * are undefined. If you are not sure that the bytes are complete * valid Unicode characters, you should use g_utf8_get_char_validated() * instead. * - * Return value: the resulting character - **/ + * Returns: the resulting character + */ gunichar g_utf8_get_char (const gchar *p) { @@ -305,17 +330,14 @@ g_utf8_get_char (const gchar *p) * instead of forwards if @offset is in the last fourth of the string, * since moving forward is about 3 times faster than moving backward. * - * - * This function doesn't abort when reaching the end of @str. Therefore - * you should be sure that @offset is within string boundaries before - * calling that function. Call g_utf8_strlen() when unsure. - * + * Note that this function doesn't abort when reaching the end of @str. + * Therefore you should be sure that @offset is within string boundaries + * before calling that function. Call g_utf8_strlen() when unsure. * This limitation exists as this function is called frequently during * text rendering and therefore has to be as fast as possible. - * * - * Return value: the resulting pointer - **/ + * Returns: the resulting pointer + */ gchar * g_utf8_offset_to_pointer (const gchar *str, glong offset) @@ -358,8 +380,8 @@ g_utf8_offset_to_pointer (const gchar *str, * Since 2.10, this function allows @pos to be before @str, and returns * a negative offset in this case. * - * Return value: the resulting character offset - **/ + * Returns: the resulting character offset + */ glong g_utf8_pointer_to_offset (const gchar *str, const gchar *pos) @@ -386,14 +408,13 @@ g_utf8_pointer_to_offset (const gchar *str, * @src: UTF-8 encoded string * @n: character count * - * Like the standard C strncpy() function, but - * copies a given number of characters instead of a given number of - * bytes. The @src string must be valid UTF-8 encoded text. - * (Use g_utf8_validate() on all text before trying to use UTF-8 - * utility functions with it.) + * Like the standard C strncpy() function, but copies a given number + * of characters instead of a given number of bytes. The @src string + * must be valid UTF-8 encoded text. (Use g_utf8_validate() on all + * text before trying to use UTF-8 utility functions with it.) * - * Return value: @dest - **/ + * Returns: @dest + */ gchar * g_utf8_strncpy (gchar *dest, const gchar *src, @@ -410,177 +431,6 @@ g_utf8_strncpy (gchar *dest, return dest; } -G_LOCK_DEFINE_STATIC (aliases); - -static GHashTable * -get_alias_hash (void) -{ - static GHashTable *alias_hash = NULL; - const char *aliases; - - G_LOCK (aliases); - - if (!alias_hash) - { - alias_hash = g_hash_table_new (g_str_hash, g_str_equal); - - aliases = _g_locale_get_charset_aliases (); - while (*aliases != '\0') - { - const char *canonical; - const char *alias; - const char **alias_array; - int count = 0; - - alias = aliases; - aliases += strlen (aliases) + 1; - canonical = aliases; - aliases += strlen (aliases) + 1; - - alias_array = g_hash_table_lookup (alias_hash, canonical); - if (alias_array) - { - while (alias_array[count]) - count++; - } - - alias_array = g_renew (const char *, alias_array, count + 2); - alias_array[count] = alias; - alias_array[count + 1] = NULL; - - g_hash_table_insert (alias_hash, (char *)canonical, alias_array); - } - } - - G_UNLOCK (aliases); - - return alias_hash; -} - -/* As an abuse of the alias table, the following routines gets - * the charsets that are aliases for the canonical name. - */ -G_GNUC_INTERNAL const char ** -_g_charset_get_aliases (const char *canonical_name) -{ - GHashTable *alias_hash = get_alias_hash (); - - return g_hash_table_lookup (alias_hash, canonical_name); -} - -static gboolean -g_utf8_get_charset_internal (const char *raw_data, - const char **a) -{ - const char *charset = getenv("CHARSET"); - - if (charset && *charset) - { - *a = charset; - - if (charset && strstr (charset, "UTF-8")) - return TRUE; - else - return FALSE; - } - - /* The libcharset code tries to be thread-safe without - * a lock, but has a memory leak and a missing memory - * barrier, so we lock for it - */ - G_LOCK (aliases); - charset = _g_locale_charset_unalias (raw_data); - G_UNLOCK (aliases); - - if (charset && *charset) - { - *a = charset; - - if (charset && strstr (charset, "UTF-8")) - return TRUE; - else - return FALSE; - } - - /* Assume this for compatibility at present. */ - *a = "US-ASCII"; - - return FALSE; -} - -typedef struct _GCharsetCache GCharsetCache; - -struct _GCharsetCache { - gboolean is_utf8; - gchar *raw; - gchar *charset; -}; - -static void -charset_cache_free (gpointer data) -{ - GCharsetCache *cache = data; - g_free (cache->raw); - g_free (cache->charset); - g_free (cache); -} - -/** - * g_get_charset: - * @charset: return location for character set name - * - * Obtains the character set for the current - * locale; you might use this character set as an argument to - * g_convert(), to convert from the current locale's encoding to some - * other encoding. (Frequently g_locale_to_utf8() and g_locale_from_utf8() - * are nice shortcuts, though.) - * - * On Windows the character set returned by this function is the - * so-called system default ANSI code-page. That is the character set - * used by the "narrow" versions of C library and Win32 functions that - * handle file names. It might be different from the character set - * used by the C library's current locale. - * - * The return value is %TRUE if the locale's encoding is UTF-8, in that - * case you can perhaps avoid calling g_convert(). - * - * The string returned in @charset is not allocated, and should not be - * freed. - * - * Return value: %TRUE if the returned charset is UTF-8 - **/ -gboolean -g_get_charset (G_CONST_RETURN char **charset) -{ - static GStaticPrivate cache_private = G_STATIC_PRIVATE_INIT; - GCharsetCache *cache = g_static_private_get (&cache_private); - const gchar *raw; - - if (!cache) - { - cache = g_new0 (GCharsetCache, 1); - g_static_private_set (&cache_private, cache, charset_cache_free); - } - - raw = _g_locale_charset_raw (); - - if (!(cache->raw && strcmp (cache->raw, raw) == 0)) - { - const gchar *new_charset; - - g_free (cache->raw); - g_free (cache->charset); - cache->raw = g_strdup (raw); - cache->is_utf8 = g_utf8_get_charset_internal (raw, &new_charset); - cache->charset = g_strdup (new_charset); - } - - if (charset) - *charset = cache->charset; - - return cache->is_utf8; -} - /* unicode_strchr */ /** @@ -592,8 +442,8 @@ g_get_charset (G_CONST_RETURN char **charset) * * Converts a single character to UTF-8. * - * Return value: number of bytes written - **/ + * Returns: number of bytes written + */ int g_unichar_to_utf8 (gunichar c, gchar *outbuf) @@ -657,10 +507,10 @@ g_unichar_to_utf8 (gunichar c, * in a UTF-8 encoded string, while limiting the search to @len bytes. * If @len is -1, allow unbounded search. * - * Return value: %NULL if the string does not contain the character, - * otherwise, a pointer to the start of the leftmost occurrence of - * the character in the string. - **/ + * Returns: %NULL if the string does not contain the character, + * otherwise, a pointer to the start of the leftmost occurrence + * of the character in the string. + */ gchar * g_utf8_strchr (const char *p, gssize len, @@ -685,10 +535,10 @@ g_utf8_strchr (const char *p, * in a UTF-8 encoded string, while limiting the search to @len bytes. * If @len is -1, allow unbounded search. * - * Return value: %NULL if the string does not contain the character, - * otherwise, a pointer to the start of the rightmost occurrence of the - * character in the string. - **/ + * Returns: %NULL if the string does not contain the character, + * otherwise, a pointer to the start of the rightmost occurrence + * of the character in the string. + */ gchar * g_utf8_strrchr (const char *p, gssize len, @@ -704,20 +554,23 @@ g_utf8_strrchr (const char *p, /* Like g_utf8_get_char, but take a maximum length - * and return (gunichar)-2 on incomplete trailing character + * and return (gunichar)-2 on incomplete trailing character; + * also check for malformed or overlong sequences + * and return (gunichar)-1 in this case. */ static inline gunichar g_utf8_get_char_extended (const gchar *p, - gssize max_len) + gssize max_len) { guint i, len; + gunichar min_code; gunichar wc = (guchar) *p; if (wc < 0x80) { return wc; } - else if (wc < 0xc0) + else if (G_UNLIKELY (wc < 0xc0)) { return (gunichar)-1; } @@ -725,33 +578,38 @@ g_utf8_get_char_extended (const gchar *p, { len = 2; wc &= 0x1f; + min_code = 1 << 7; } else if (wc < 0xf0) { len = 3; wc &= 0x0f; + min_code = 1 << 11; } else if (wc < 0xf8) { len = 4; wc &= 0x07; + min_code = 1 << 16; } else if (wc < 0xfc) { len = 5; wc &= 0x03; + min_code = 1 << 21; } else if (wc < 0xfe) { len = 6; wc &= 0x01; + min_code = 1 << 26; } else { return (gunichar)-1; } - - if (max_len >= 0 && len > max_len) + + if (G_UNLIKELY (max_len >= 0 && len > max_len)) { for (i = 1; i < max_len; i++) { @@ -764,8 +622,8 @@ g_utf8_get_char_extended (const gchar *p, for (i = 1; i < len; ++i) { gunichar ch = ((guchar *)p)[i]; - - if ((ch & 0xc0) != 0x80) + + if (G_UNLIKELY ((ch & 0xc0) != 0x80)) { if (ch) return (gunichar)-1; @@ -777,9 +635,9 @@ g_utf8_get_char_extended (const gchar *p, wc |= (ch & 0x3f); } - if (UTF8_LENGTH(wc) != len) + if (G_UNLIKELY (wc < min_code)) return (gunichar)-1; - + return wc; } @@ -787,22 +645,22 @@ g_utf8_get_char_extended (const gchar *p, * g_utf8_get_char_validated: * @p: a pointer to Unicode character encoded as UTF-8 * @max_len: the maximum number of bytes to read, or -1, for no maximum or - * if @p is nul-terminated + * if @p is nul-terminated * * Convert a sequence of bytes encoded as UTF-8 to a Unicode character. * This function checks for incomplete characters, for invalid characters * such as characters that are out of the range of Unicode, and for * overlong encodings of valid characters. * - * Return value: the resulting character. If @p points to a partial - * sequence at the end of a string that could begin a valid - * character (or if @max_len is zero), returns (gunichar)-2; - * otherwise, if @p does not point to a valid UTF-8 encoded - * Unicode character, returns (gunichar)-1. - **/ + * Returns: the resulting character. If @p points to a partial + * sequence at the end of a string that could begin a valid + * character (or if @max_len is zero), returns (gunichar)-2; + * otherwise, if @p does not point to a valid UTF-8 encoded + * Unicode character, returns (gunichar)-1. + */ gunichar -g_utf8_get_char_validated (const gchar *p, - gssize max_len) +g_utf8_get_char_validated (const gchar *p, + gssize max_len) { gunichar result; @@ -823,24 +681,24 @@ g_utf8_get_char_validated (const gchar *p, * g_utf8_to_ucs4_fast: * @str: a UTF-8 encoded string * @len: the maximum length of @str to use, in bytes. If @len < 0, - * then the string is nul-terminated. - * @items_written: location to store the number of characters in the - * result, or %NULL. + * then the string is nul-terminated. + * @items_written: (allow-none): location to store the number of + * characters in the result, or %NULL. * * Convert a string from UTF-8 to a 32-bit fixed width * representation as UCS-4, assuming valid UTF-8 input. * This function is roughly twice as fast as g_utf8_to_ucs4() - * but does no error checking on the input. + * but does no error checking on the input. A trailing 0 character + * will be added to the string after the converted text. * - * Return value: a pointer to a newly allocated UCS-4 string. - * This value must be freed with g_free(). - **/ + * Returns: a pointer to a newly allocated UCS-4 string. + * This value must be freed with g_free(). + */ gunichar * g_utf8_to_ucs4_fast (const gchar *str, glong len, glong *items_written) { - gint j, charlen; gunichar *result; gint n_chars, i; const gchar *p; @@ -871,49 +729,38 @@ g_utf8_to_ucs4_fast (const gchar *str, p = str; for (i=0; i < n_chars; i++) { - gunichar wc = ((unsigned char *)p)[0]; + gunichar wc = (guchar)*p++; if (wc < 0x80) { result[i] = wc; - p++; } else { - if (wc < 0xe0) - { - charlen = 2; - wc &= 0x1f; - } - else if (wc < 0xf0) - { - charlen = 3; - wc &= 0x0f; - } - else if (wc < 0xf8) - { - charlen = 4; - wc &= 0x07; - } - else if (wc < 0xfc) - { - charlen = 5; - wc &= 0x03; - } - else + gunichar mask = 0x40; + + if (G_UNLIKELY ((wc & mask) == 0)) { - charlen = 6; - wc &= 0x01; + /* It's an out-of-sequence 10xxxxxxx byte. + * Rather than making an ugly hash of this and the next byte + * and overrunning the buffer, it's more useful to treat it + * with a replacement character + */ + result[i] = 0xfffd; + continue; } - for (j = 1; j < charlen; j++) + do { wc <<= 6; - wc |= ((unsigned char *)p)[j] & 0x3f; + wc |= (guchar)(*p++) & 0x3f; + mask <<= 5; } + while((wc & mask) != 0); + + wc &= mask - 1; result[i] = wc; - p += charlen; } } result[i] = 0; @@ -924,32 +771,41 @@ g_utf8_to_ucs4_fast (const gchar *str, return result; } +static gpointer +try_malloc_n (gsize n_blocks, gsize n_block_bytes, GError **error) +{ + gpointer ptr = g_try_malloc_n (n_blocks, n_block_bytes); + if (ptr == NULL) + g_set_error_literal (error, G_CONVERT_ERROR, G_CONVERT_ERROR_NO_MEMORY, + _("Failed to allocate memory")); + return ptr; +} + /** * g_utf8_to_ucs4: * @str: a UTF-8 encoded string * @len: the maximum length of @str to use, in bytes. If @len < 0, - * then the string is nul-terminated. - * @items_read: location to store number of bytes read, or %NULL. - * If %NULL, then %G_CONVERT_ERROR_PARTIAL_INPUT will be - * returned in case @str contains a trailing partial - * character. If an error occurs then the index of the - * invalid input is stored here. - * @items_written: location to store number of characters written or %NULL. - * The value here stored does not include the trailing 0 - * character. - * @error: location to store the error occuring, or %NULL to ignore - * errors. Any of the errors in #GConvertError other than - * %G_CONVERT_ERROR_NO_CONVERSION may occur. + * then the string is nul-terminated. + * @items_read: (allow-none): location to store number of bytes read, or %NULL. + * If %NULL, then %G_CONVERT_ERROR_PARTIAL_INPUT will be + * returned in case @str contains a trailing partial + * character. If an error occurs then the index of the + * invalid input is stored here. + * @items_written: (allow-none): location to store number of characters + * written or %NULL. The value here stored does not include the + * trailing 0 character. + * @error: location to store the error occurring, or %NULL to ignore + * errors. Any of the errors in #GConvertError other than + * %G_CONVERT_ERROR_NO_CONVERSION may occur. * * Convert a string from UTF-8 to a 32-bit fixed width - * representation as UCS-4. A trailing 0 will be added to the + * representation as UCS-4. A trailing 0 character will be added to the * string after the converted text. * - * Return value: a pointer to a newly allocated UCS-4 string. - * This value must be freed with g_free(). If an - * error occurs, %NULL will be returned and - * @error set. - **/ + * Returns: a pointer to a newly allocated UCS-4 string. + * This value must be freed with g_free(). If an error occurs, + * %NULL will be returned and @error set. + */ gunichar * g_utf8_to_ucs4 (const gchar *str, glong len, @@ -988,8 +844,10 @@ g_utf8_to_ucs4 (const gchar *str, in = g_utf8_next_char (in); } - result = g_new (gunichar, n_chars + 1); - + result = try_malloc_n (n_chars + 1, sizeof (gunichar), error); + if (result == NULL) + goto err_out; + in = str; for (i=0; i < n_chars; i++) { @@ -1012,25 +870,24 @@ g_utf8_to_ucs4 (const gchar *str, * g_ucs4_to_utf8: * @str: a UCS-4 encoded string * @len: the maximum length (number of characters) of @str to use. - * If @len < 0, then the string is nul-terminated. - * @items_read: location to store number of characters read, or %NULL. - * @items_written: location to store number of bytes written or %NULL. - * The value here stored does not include the trailing 0 - * byte. - * @error: location to store the error occuring, or %NULL to ignore + * If @len < 0, then the string is nul-terminated. + * @items_read: (allow-none): location to store number of characters + * read, or %NULL. + * @items_written: (allow-none): location to store number of bytes + * written or %NULL. The value here stored does not include the + * trailing 0 byte. + * @error: location to store the error occurring, or %NULL to ignore * errors. Any of the errors in #GConvertError other than * %G_CONVERT_ERROR_NO_CONVERSION may occur. * * Convert a string from a 32-bit fixed width representation as UCS-4. * to UTF-8. The result will be terminated with a 0 byte. * - * Return value: a pointer to a newly allocated UTF-8 string. - * This value must be freed with g_free(). If an - * error occurs, %NULL will be returned and - * @error set. In that case, @items_read will be - * set to the position of the first invalid input - * character. - **/ + * Returns: a pointer to a newly allocated UTF-8 string. + * This value must be freed with g_free(). If an error occurs, + * %NULL will be returned and @error set. In that case, @items_read + * will be set to the position of the first invalid input character. + */ gchar * g_ucs4_to_utf8 (const gunichar *str, glong len, @@ -1059,7 +916,10 @@ g_ucs4_to_utf8 (const gunichar *str, result_length += UTF8_LENGTH (str[i]); } - result = g_malloc (result_length + 1); + result = try_malloc_n (result_length + 1, 1, error); + if (result == NULL) + goto err_out; + p = result; i = 0; @@ -1083,19 +943,17 @@ g_ucs4_to_utf8 (const gunichar *str, /** * g_utf16_to_utf8: * @str: a UTF-16 encoded string - * @len: the maximum length (number of gunichar2) of @str to use. - * If @len < 0, then the string is nul-terminated. - * @items_read: location to store number of words read, or %NULL. - * If %NULL, then %G_CONVERT_ERROR_PARTIAL_INPUT will be - * returned in case @str contains a trailing partial - * character. If an error occurs then the index of the - * invalid input is stored here. - * @items_written: location to store number of bytes written, or %NULL. - * The value stored here does not include the trailing - * 0 byte. - * @error: location to store the error occuring, or %NULL to ignore - * errors. Any of the errors in #GConvertError other than - * %G_CONVERT_ERROR_NO_CONVERSION may occur. + * @len: the maximum length (number of #gunichar2) of @str to use. + * If @len < 0, then the string is nul-terminated. + * @items_read: (allow-none): location to store number of words read, + * or %NULL. If %NULL, then %G_CONVERT_ERROR_PARTIAL_INPUT will be + * returned in case @str contains a trailing partial character. If + * an error occurs then the index of the invalid input is stored here. + * @items_written: (allow-none): location to store number of bytes written, + * or %NULL. The value stored here does not include the trailing 0 byte. + * @error: location to store the error occurring, or %NULL to ignore + * errors. Any of the errors in #GConvertError other than + * %G_CONVERT_ERROR_NO_CONVERSION may occur. * * Convert a string from UTF-16 to UTF-8. The result will be * terminated with a 0 byte. @@ -1111,10 +969,9 @@ g_ucs4_to_utf8 (const gunichar *str, * be correctly interpreted as UTF-16, i.e. it doesn't contain * things unpaired surrogates. * - * Return value: a pointer to a newly allocated UTF-8 string. - * This value must be freed with g_free(). If an - * error occurs, %NULL will be returned and - * @error set. + * Returns: a pointer to a newly allocated UTF-8 string. + * This value must be freed with g_free(). If an error occurs, + * %NULL will be returned and @error set. **/ gchar * g_utf16_to_utf8 (const gunichar2 *str, @@ -1123,8 +980,8 @@ g_utf16_to_utf8 (const gunichar2 *str, glong *items_written, GError **error) { - /* This function and g_utf16_to_ucs4 are almost exactly identical - The lines that differ - * are marked. + /* This function and g_utf16_to_ucs4 are almost exactly identical - + * The lines that differ are marked. */ const gunichar2 *in; gchar *out; @@ -1191,8 +1048,10 @@ g_utf16_to_utf8 (const gunichar2 *str, /* At this point, everything is valid, and we just need to convert */ /********** DIFFERENT for UTF8/UCS4 **********/ - result = g_malloc (n_bytes + 1); - + result = try_malloc_n (n_bytes + 1, 1, error); + if (result == NULL) + goto err_out; + high_surrogate = 0; out = result; in = str; @@ -1238,28 +1097,26 @@ g_utf16_to_utf8 (const gunichar2 *str, /** * g_utf16_to_ucs4: * @str: a UTF-16 encoded string - * @len: the maximum length (number of gunichar2) of @str to use. - * If @len < 0, then the string is nul-terminated. - * @items_read: location to store number of words read, or %NULL. - * If %NULL, then %G_CONVERT_ERROR_PARTIAL_INPUT will be - * returned in case @str contains a trailing partial - * character. If an error occurs then the index of the - * invalid input is stored here. - * @items_written: location to store number of characters written, or %NULL. - * The value stored here does not include the trailing - * 0 character. - * @error: location to store the error occuring, or %NULL to ignore - * errors. Any of the errors in #GConvertError other than - * %G_CONVERT_ERROR_NO_CONVERSION may occur. + * @len: the maximum length (number of #gunichar2) of @str to use. + * If @len < 0, then the string is nul-terminated. + * @items_read: (allow-none): location to store number of words read, + * or %NULL. If %NULL, then %G_CONVERT_ERROR_PARTIAL_INPUT will be + * returned in case @str contains a trailing partial character. If + * an error occurs then the index of the invalid input is stored here. + * @items_written: (allow-none): location to store number of characters + * written, or %NULL. The value stored here does not include the trailing + * 0 character. + * @error: location to store the error occurring, or %NULL to ignore + * errors. Any of the errors in #GConvertError other than + * %G_CONVERT_ERROR_NO_CONVERSION may occur. * * Convert a string from UTF-16 to UCS-4. The result will be * nul-terminated. * - * Return value: a pointer to a newly allocated UCS-4 string. - * This value must be freed with g_free(). If an - * error occurs, %NULL will be returned and - * @error set. - **/ + * Returns: a pointer to a newly allocated UCS-4 string. + * This value must be freed with g_free(). If an error occurs, + * %NULL will be returned and @error set. + */ gunichar * g_utf16_to_ucs4 (const gunichar2 *str, glong len, @@ -1281,13 +1138,11 @@ g_utf16_to_ucs4 (const gunichar2 *str, while ((len < 0 || in - str < len) && *in) { gunichar2 c = *in; - gunichar wc; if (c >= 0xdc00 && c < 0xe000) /* low surrogate */ { if (high_surrogate) { - wc = SURROGATE_VALUE (high_surrogate, c); high_surrogate = 0; } else @@ -1311,8 +1166,6 @@ g_utf16_to_ucs4 (const gunichar2 *str, high_surrogate = c; goto next1; } - else - wc = c; } /********** DIFFERENT for UTF8/UCS4 **********/ @@ -1332,8 +1185,10 @@ g_utf16_to_ucs4 (const gunichar2 *str, /* At this point, everything is valid, and we just need to convert */ /********** DIFFERENT for UTF8/UCS4 **********/ - result = g_malloc (n_bytes + 4); - + result = try_malloc_n (n_bytes + 4, 1, error); + if (result == NULL) + goto err_out; + high_surrogate = 0; out = result; in = str; @@ -1381,27 +1236,25 @@ g_utf16_to_ucs4 (const gunichar2 *str, * g_utf8_to_utf16: * @str: a UTF-8 encoded string * @len: the maximum length (number of bytes) of @str to use. - * If @len < 0, then the string is nul-terminated. - * @items_read: location to store number of bytes read, or %NULL. - * If %NULL, then %G_CONVERT_ERROR_PARTIAL_INPUT will be - * returned in case @str contains a trailing partial - * character. If an error occurs then the index of the - * invalid input is stored here. - * @items_written: location to store number of gunichar2 written, - * or %NULL. - * The value stored here does not include the trailing 0. - * @error: location to store the error occuring, or %NULL to ignore - * errors. Any of the errors in #GConvertError other than - * %G_CONVERT_ERROR_NO_CONVERSION may occur. + * If @len < 0, then the string is nul-terminated. + * @items_read: (allow-none): location to store number of bytes read, + * or %NULL. If %NULL, then %G_CONVERT_ERROR_PARTIAL_INPUT will be + * returned in case @str contains a trailing partial character. If + * an error occurs then the index of the invalid input is stored here. + * @items_written: (allow-none): location to store number of #gunichar2 + * written, or %NULL. The value stored here does not include the + * trailing 0. + * @error: location to store the error occurring, or %NULL to ignore + * errors. Any of the errors in #GConvertError other than + * %G_CONVERT_ERROR_NO_CONVERSION may occur. * * Convert a string from UTF-8 to UTF-16. A 0 character will be * added to the result after the converted text. * - * Return value: a pointer to a newly allocated UTF-16 string. - * This value must be freed with g_free(). If an - * error occurs, %NULL will be returned and - * @error set. - **/ + * Returns: a pointer to a newly allocated UTF-16 string. + * This value must be freed with g_free(). If an error occurs, + * %NULL will be returned and @error set. + */ gunichar2 * g_utf8_to_utf16 (const gchar *str, glong len, @@ -1462,8 +1315,10 @@ g_utf8_to_utf16 (const gchar *str, in = g_utf8_next_char (in); } - result = g_new (gunichar2, n16 + 1); - + result = try_malloc_n (n16 + 1, sizeof (gunichar2), error); + if (result == NULL) + goto err_out; + in = str; for (i = 0; i < n16;) { @@ -1498,25 +1353,24 @@ g_utf8_to_utf16 (const gchar *str, * g_ucs4_to_utf16: * @str: a UCS-4 encoded string * @len: the maximum length (number of characters) of @str to use. - * If @len < 0, then the string is nul-terminated. - * @items_read: location to store number of bytes read, or %NULL. - * If an error occurs then the index of the invalid input - * is stored here. - * @items_written: location to store number of gunichar2 - * written, or %NULL. The value stored here does not - * include the trailing 0. - * @error: location to store the error occuring, or %NULL to ignore - * errors. Any of the errors in #GConvertError other than - * %G_CONVERT_ERROR_NO_CONVERSION may occur. + * If @len < 0, then the string is nul-terminated. + * @items_read: (allow-none): location to store number of bytes read, + * or %NULL. If an error occurs then the index of the invalid input + * is stored here. + * @items_written: (allow-none): location to store number of #gunichar2 + * written, or %NULL. The value stored here does not include the + * trailing 0. + * @error: location to store the error occurring, or %NULL to ignore + * errors. Any of the errors in #GConvertError other than + * %G_CONVERT_ERROR_NO_CONVERSION may occur. * * Convert a string from UCS-4 to UTF-16. A 0 character will be * added to the result after the converted text. * - * Return value: a pointer to a newly allocated UTF-16 string. - * This value must be freed with g_free(). If an - * error occurs, %NULL will be returned and - * @error set. - **/ + * Returns: a pointer to a newly allocated UTF-16 string. + * This value must be freed with g_free(). If an error occurs, + * %NULL will be returned and @error set. + */ gunichar2 * g_ucs4_to_utf16 (const gunichar *str, glong len, @@ -1557,9 +1411,11 @@ g_ucs4_to_utf16 (const gunichar *str, i++; } - - result = g_new (gunichar2, n16 + 1); - + + result = try_malloc_n (n16 + 1, sizeof (gunichar2), error); + if (result == NULL) + goto err_out; + for (i = 0, j = 0; j < n16; i++) { gunichar wc = str[i]; @@ -1739,9 +1595,9 @@ fast_validate_len (const char *str, /** * g_utf8_validate: - * @str: a pointer to character data + * @str: (array length=max_len) (element-type guint8): a pointer to character data * @max_len: max bytes to validate, or -1 to go until NUL - * @end: return location for end of valid data + * @end: (allow-none) (out) (transfer none): return location for end of valid data * * Validates UTF-8 encoded text. @str is the text to validate; * if @str is nul-terminated, then @max_len can be -1, otherwise @@ -1752,15 +1608,15 @@ fast_validate_len (const char *str, * being validated otherwise). * * Note that g_utf8_validate() returns %FALSE if @max_len is - * positive and NUL is met before @max_len bytes have been read. + * positive and any of the @max_len bytes are nul. * * Returns %TRUE if all of @str was valid. Many GLib and GTK+ - * routines require valid UTF-8 as input; - * so data read from a file or the network should be checked - * with g_utf8_validate() before doing anything else with it. + * routines require valid UTF-8 as input; so data read from a file + * or the network should be checked with g_utf8_validate() before + * doing anything else with it. * - * Return value: %TRUE if the text was valid UTF-8 - **/ + * Returns: %TRUE if the text was valid UTF-8 + */ gboolean g_utf8_validate (const char *str, gssize max_len, @@ -1792,7 +1648,7 @@ g_utf8_validate (const char *str, * integer values of @ch will not be valid. 0 is considered a valid * character, though it's normally a string terminator. * - * Return value: %TRUE if @ch is a valid Unicode character + * Returns: %TRUE if @ch is a valid Unicode character **/ gboolean g_unichar_validate (gunichar ch) @@ -1804,7 +1660,7 @@ g_unichar_validate (gunichar ch) * g_utf8_strreverse: * @str: a UTF-8 encoded string * @len: the maximum length of @str to use, in bytes. If @len < 0, - * then the string is nul-terminated. + * then the string is nul-terminated. * * Reverses a UTF-8 string. @str must be valid UTF-8 encoded text. * (Use g_utf8_validate() on all text before trying to use UTF-8 @@ -1820,7 +1676,7 @@ g_unichar_validate (gunichar ch) * newly-allocated memory, which should be freed with g_free() when * no longer needed. * - * Returns: a newly-allocated string which is the reverse of @str. + * Returns: a newly-allocated string which is the reverse of @str * * Since: 2.2 */ @@ -1889,7 +1745,3 @@ _g_utf8_make_valid (const gchar *name) return g_string_free (string, FALSE); } - - -#define __G_UTF8_C__ -#include "galiasdef.c"