* Boston, MA 02111-1307, USA.
*/
-#include <config.h>
+#include "config.h"
#include <stdlib.h>
#ifdef HAVE_CODESET
#include <string.h>
#include "glib.h"
-#include "galias.h"
#ifdef G_PLATFORM_WIN32
#include <stdio.h>
#include "libcharset/libcharset.h"
#include "glibintl.h"
+#include "galias.h"
#define UTF8_COMPUTE(Char, Mask, Len) \
if (Char < 128) \
*
* Given a position @p with a UTF-8 encoded string @str, find the start
* of the previous UTF-8 character starting before @p. Returns %NULL if no
- * UTF-8 characters are present in @p before @str.
+ * UTF-8 characters are present in @str before @p.
*
* @p does not have to be at the beginning of a UTF-8 character. No check
* is made to see if the character found is actually valid other than
/**
* g_utf8_find_next_char:
* @p: a pointer to a position within a UTF-8 encoded string
- * @end: a pointer to the end of the string, or %NULL to indicate
- * that the string is nul-terminated, in which case
- * the returned value will be
+ * @end: a pointer to the byte following the end of the string,
+ * or %NULL to indicate that the string is nul-terminated.
*
* Finds the start of the next UTF-8 character in the string after @p.
*
/* only do the last len increment if we got a complete
* char (don't count partial chars)
*/
- if (p - start == max)
+ if (p - start <= max)
++len;
}
* Converts from an integer character offset to a pointer to a position
* within the string.
*
+ * Since 2.10, this function allows to pass a negative @offset to
+ * step backwards. It is usually worth stepping backwards from the end
+ * instead of forwards if @offset is in the last fourth of the string,
+ * since moving forward is about 3 times faster than moving backward.
+ *
* Return value: the resulting pointer
**/
gchar *
glong offset)
{
const gchar *s = str;
- while (offset--)
- s = g_utf8_next_char (s);
-
+
+ if (offset > 0)
+ while (offset--)
+ s = g_utf8_next_char (s);
+ else
+ {
+ const char *s1;
+
+ /* This nice technique for fast backwards stepping
+ * through a UTF-8 string was dubbed "stutter stepping"
+ * by its inventor, Larry Ewing.
+ */
+ while (offset)
+ {
+ s1 = s;
+ s += offset;
+ while ((*s & 0xc0) == 0x80)
+ s--;
+
+ offset += g_utf8_pointer_to_offset (s, s1);
+ }
+ }
+
return (gchar *)s;
}
*
* Converts from a pointer to position within a string to a integer
* character offset.
+ *
+ * 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
**/
{
const gchar *s = str;
glong offset = 0;
-
- while (s < pos)
- {
- s = g_utf8_next_char (s);
- offset++;
- }
+ if (pos < str)
+ offset = - g_utf8_pointer_to_offset (pos, str);
+ else
+ while (s < pos)
+ {
+ s = g_utf8_next_char (s);
+ offset++;
+ }
+
return offset;
}
/* As an abuse of the alias table, the following routines gets
* the charsets that are aliases for the canonical name.
*/
-const char **
+G_GNUC_INTERNAL const char **
_g_charset_get_aliases (const char *canonical_name)
{
GHashTable *alias_hash = get_alias_hash ();
* 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.)
+ * Obtains the character set for the <link linkend="setlocale">current
+ * locale</link>; 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().
/**
* 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.
+ * @max_len: the maximum number of bytes to read, or -1, for no maximum or
+ * 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
*
* Return value: the resulting character. If @p points to a partial
* sequence at the end of a string that could begin a valid
- * character, returns (gunichar)-2; otherwise, if @p does not point
- * to a valid UTF-8 encoded Unicode character, returns (gunichar)-1.
+ * 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)
{
- gunichar result = g_utf8_get_char_extended (p, max_len);
+ gunichar result;
+
+ if (max_len == 0)
+ return (gunichar)-2;
+
+ result = g_utf8_get_char_extended (p, max_len);
if (result & 0x80000000)
return result;
/**
* g_utf8_to_ucs4_fast:
* @str: a UTF-8 encoded string
- * @len: the maximum length of @str to use. If @len < 0, then
- * the string is nul-terminated.
+ * @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.
*
/**
* g_utf8_to_ucs4:
* @str: a UTF-8 encoded string
- * @len: the maximum length of @str to use. If @len < 0, then
- * the string is nul-terminated.
+ * @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
if (items_read)
break;
else
- g_set_error (error, G_CONVERT_ERROR, G_CONVERT_ERROR_PARTIAL_INPUT,
- _("Partial character sequence at end of input"));
+ g_set_error_literal (error, G_CONVERT_ERROR, G_CONVERT_ERROR_PARTIAL_INPUT,
+ _("Partial character sequence at end of input"));
}
else
- g_set_error (error, G_CONVERT_ERROR, G_CONVERT_ERROR_ILLEGAL_SEQUENCE,
- _("Invalid byte sequence in conversion input"));
+ g_set_error_literal (error, G_CONVERT_ERROR, G_CONVERT_ERROR_ILLEGAL_SEQUENCE,
+ _("Invalid byte sequence in conversion input"));
goto err_out;
}
/**
* g_ucs4_to_utf8:
* @str: a UCS-4 encoded string
- * @len: the maximum length of @str to use. If @len < 0, then
- * the string is terminated with a 0 character.
+ * @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
if (str[i] >= 0x80000000)
{
- g_set_error (error, G_CONVERT_ERROR, G_CONVERT_ERROR_ILLEGAL_SEQUENCE,
- _("Character out of range for UTF-8"));
+ g_set_error_literal (error, G_CONVERT_ERROR, G_CONVERT_ERROR_ILLEGAL_SEQUENCE,
+ _("Character out of range for UTF-8"));
goto err_out;
}
/**
* g_utf16_to_utf8:
* @str: a UTF-16 encoded string
- * @len: the maximum length of @str to use. If @len < 0, then
- * the string is terminated with a 0 character.
+ * @len: the maximum length (number of <type>gunichar2</type>) 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
gint n_bytes;
gunichar high_surrogate;
- g_return_val_if_fail (str != 0, NULL);
+ g_return_val_if_fail (str != NULL, NULL);
n_bytes = 0;
in = str;
}
else
{
- g_set_error (error, G_CONVERT_ERROR, G_CONVERT_ERROR_ILLEGAL_SEQUENCE,
- _("Invalid sequence in conversion input"));
+ g_set_error_literal (error, G_CONVERT_ERROR, G_CONVERT_ERROR_ILLEGAL_SEQUENCE,
+ _("Invalid sequence in conversion input"));
goto err_out;
}
}
{
if (high_surrogate)
{
- g_set_error (error, G_CONVERT_ERROR, G_CONVERT_ERROR_ILLEGAL_SEQUENCE,
- _("Invalid sequence in conversion input"));
+ g_set_error_literal (error, G_CONVERT_ERROR, G_CONVERT_ERROR_ILLEGAL_SEQUENCE,
+ _("Invalid sequence in conversion input"));
goto err_out;
}
if (high_surrogate && !items_read)
{
- g_set_error (error, G_CONVERT_ERROR, G_CONVERT_ERROR_PARTIAL_INPUT,
- _("Partial character sequence at end of input"));
+ g_set_error_literal (error, G_CONVERT_ERROR, G_CONVERT_ERROR_PARTIAL_INPUT,
+ _("Partial character sequence at end of input"));
goto err_out;
}
/**
* g_utf16_to_ucs4:
* @str: a UTF-16 encoded string
- * @len: the maximum length of @str to use. If @len < 0, then
- * the string is terminated with a 0 character.
+ * @len: the maximum length (number of <type>gunichar2</type>) 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
* %G_CONVERT_ERROR_NO_CONVERSION may occur.
*
* Convert a string from UTF-16 to UCS-4. The result will be
- * terminated with a 0 character.
+ * nul-terminated.
*
* Return value: a pointer to a newly allocated UCS-4 string.
* This value must be freed with g_free(). If an
gint n_bytes;
gunichar high_surrogate;
- g_return_val_if_fail (str != 0, NULL);
+ g_return_val_if_fail (str != NULL, NULL);
n_bytes = 0;
in = str;
}
else
{
- g_set_error (error, G_CONVERT_ERROR, G_CONVERT_ERROR_ILLEGAL_SEQUENCE,
- _("Invalid sequence in conversion input"));
+ g_set_error_literal (error, G_CONVERT_ERROR, G_CONVERT_ERROR_ILLEGAL_SEQUENCE,
+ _("Invalid sequence in conversion input"));
goto err_out;
}
}
{
if (high_surrogate)
{
- g_set_error (error, G_CONVERT_ERROR, G_CONVERT_ERROR_ILLEGAL_SEQUENCE,
- _("Invalid sequence in conversion input"));
+ g_set_error_literal (error, G_CONVERT_ERROR, G_CONVERT_ERROR_ILLEGAL_SEQUENCE,
+ _("Invalid sequence in conversion input"));
goto err_out;
}
if (high_surrogate && !items_read)
{
- g_set_error (error, G_CONVERT_ERROR, G_CONVERT_ERROR_PARTIAL_INPUT,
- _("Partial character sequence at end of input"));
+ g_set_error_literal (error, G_CONVERT_ERROR, G_CONVERT_ERROR_PARTIAL_INPUT,
+ _("Partial character sequence at end of input"));
goto err_out;
}
/**
* g_utf8_to_utf16:
* @str: a UTF-8 encoded string
- * @len: the maximum length of @str to use. If @len < 0, then
- * the string is nul-terminated.
+ * @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 %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 words written, or %NULL.
- * The value stored here does not include the trailing
- * 0 word.
+ * @items_written: location to store number of <type>gunichar2</type> 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.
*
- * Convert a string from UTF-8 to UTF-16. A 0 word will be
+ * 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.
if (items_read)
break;
else
- g_set_error (error, G_CONVERT_ERROR, G_CONVERT_ERROR_PARTIAL_INPUT,
- _("Partial character sequence at end of input"));
+ g_set_error_literal (error, G_CONVERT_ERROR, G_CONVERT_ERROR_PARTIAL_INPUT,
+ _("Partial character sequence at end of input"));
}
else
- g_set_error (error, G_CONVERT_ERROR, G_CONVERT_ERROR_ILLEGAL_SEQUENCE,
- _("Invalid byte sequence in conversion input"));
+ g_set_error_literal (error, G_CONVERT_ERROR, G_CONVERT_ERROR_ILLEGAL_SEQUENCE,
+ _("Invalid byte sequence in conversion input"));
goto err_out;
}
n16 += 1;
else if (wc < 0xe000)
{
- g_set_error (error, G_CONVERT_ERROR, G_CONVERT_ERROR_ILLEGAL_SEQUENCE,
- _("Invalid sequence in conversion input"));
+ g_set_error_literal (error, G_CONVERT_ERROR, G_CONVERT_ERROR_ILLEGAL_SEQUENCE,
+ _("Invalid sequence in conversion input"));
goto err_out;
}
n16 += 2;
else
{
- g_set_error (error, G_CONVERT_ERROR, G_CONVERT_ERROR_ILLEGAL_SEQUENCE,
- _("Character out of range for UTF-16"));
+ g_set_error_literal (error, G_CONVERT_ERROR, G_CONVERT_ERROR_ILLEGAL_SEQUENCE,
+ _("Character out of range for UTF-16"));
goto err_out;
}
/**
* g_ucs4_to_utf16:
* @str: a UCS-4 encoded string
- * @len: the maximum length of @str to use. If @len < 0, then
- * the string is terminated with a 0 character.
+ * @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 words written, or %NULL.
- * The value stored here does not include the trailing
- * 0 word.
+ * @items_written: location to store number of <type>gunichar2</type>
+ * 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.
*
- * Convert a string from UCS-4 to UTF-16. A 0 word will be
+ * 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.
n16 += 1;
else if (wc < 0xe000)
{
- g_set_error (error, G_CONVERT_ERROR, G_CONVERT_ERROR_ILLEGAL_SEQUENCE,
- _("Invalid sequence in conversion input"));
+ g_set_error_literal (error, G_CONVERT_ERROR, G_CONVERT_ERROR_ILLEGAL_SEQUENCE,
+ _("Invalid sequence in conversion input"));
goto err_out;
}
n16 += 2;
else
{
- g_set_error (error, G_CONVERT_ERROR, G_CONVERT_ERROR_ILLEGAL_SEQUENCE,
- _("Character out of range for UTF-16"));
+ g_set_error_literal (error, G_CONVERT_ERROR, G_CONVERT_ERROR_ILLEGAL_SEQUENCE,
+ _("Character out of range for UTF-16"));
goto err_out;
}
gunichar min = 0;
const gchar *p;
- for (p = str; (max_len < 0 || (p - str) < max_len) && *p; p++)
+ g_assert (max_len >= 0);
+
+ for (p = str; ((p - str) < max_len) && *p; p++)
{
if (*(guchar *)p < 128)
/* done */;
last = p;
if ((*(guchar *)p & 0xe0) == 0xc0) /* 110xxxxx */
{
- if (G_UNLIKELY (max_len >= 0 && max_len - (p - str) < 2))
+ if (G_UNLIKELY (max_len - (p - str) < 2))
goto error;
if (G_UNLIKELY ((*(guchar *)p & 0x1e) == 0))
{
if ((*(guchar *)p & 0xf0) == 0xe0) /* 1110xxxx */
{
- if (G_UNLIKELY (max_len >= 0 && max_len - (p - str) < 3))
+ if (G_UNLIKELY (max_len - (p - str) < 3))
goto error;
min = (1 << 11);
}
else if ((*(guchar *)p & 0xf8) == 0xf0) /* 11110xxx */
{
- if (G_UNLIKELY (max_len >= 0 && max_len - (p - str) < 4))
+ if (G_UNLIKELY (max_len - (p - str) < 4))
goto error;
min = (1 << 16);
return TRUE;
}
-
/**
* g_unichar_validate:
* @ch: a Unicode character
/**
* g_utf8_strreverse:
* @str: a UTF-8 encoded string
- * @len: the maximum length of @str to use. If @len < 0, then
- * the string is nul-terminated.
+ * @len: the maximum length of @str to use, in bytes. If @len < 0,
+ * 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
* utility functions with it.)
*
+ * This function is intended for programmatic uses of reversed strings.
+ * It pays no attention to decomposed characters, combining marks, byte
+ * order marks, directional indicators (LRM, LRO, etc) and similar
+ * characters which might need special handling when reversing a string
+ * for display purposes.
+ *
* Note that unlike g_strreverse(), this function returns
* newly-allocated memory, which should be freed with g_free() when
* no longer needed.
* Since: 2.2
*/
gchar *
-g_utf8_strreverse (const gchar *str,
- gssize len)
+g_utf8_strreverse (const gchar *str,
+ gssize len)
{
- gchar *result;
+ gchar *r, *result;
const gchar *p;
- gchar *m, *r, skip;
if (len < 0)
len = strlen (str);
result = g_new (gchar, len + 1);
r = result + len;
p = str;
- while (*p)
+ while (r > result)
{
- skip = g_utf8_skip[*(guchar*)p];
+ gchar *m, skip = g_utf8_skip[*(guchar*) p];
r -= skip;
for (m = r; skip; skip--)
*m++ = *p++;
return result;
}
+
+gchar *
+_g_utf8_make_valid (const gchar *name)
+{
+ GString *string;
+ const gchar *remainder, *invalid;
+ gint remaining_bytes, valid_bytes;
+
+ string = NULL;
+ remainder = name;
+ remaining_bytes = strlen (name);
+
+ while (remaining_bytes != 0)
+ {
+ if (g_utf8_validate (remainder, remaining_bytes, &invalid))
+ break;
+ valid_bytes = invalid - remainder;
+
+ if (string == NULL)
+ string = g_string_sized_new (remaining_bytes);
+
+ g_string_append_len (string, remainder, valid_bytes);
+ /* append U+FFFD REPLACEMENT CHARACTER */
+ g_string_append (string, "\357\277\275");
+
+ remaining_bytes -= valid_bytes + 1;
+ remainder = invalid + 1;
+ }
+
+ if (string == NULL)
+ return g_strdup (name);
+
+ g_string_append (string, remainder);
+
+ g_assert (g_utf8_validate (string->str, -1, NULL));
+
+ return g_string_free (string, FALSE);
+}
+
+
#define __G_UTF8_C__
#include "galiasdef.c"