[glib] Make g_utf8_make_valid optionally take a length
- From: Ignacio Casal Quinteiro <icq src gnome org>
- To: commits-list gnome org
- Cc:
- Subject: [glib] Make g_utf8_make_valid optionally take a length
- Date: Thu, 2 Mar 2017 11:30:02 +0000 (UTC)
commit f559bc01dc9ce53f6995600311bb0db1a6d347ef
Author: Paolo Borelli <pborelli gnome org>
Date: Thu Mar 2 09:10:35 2017 +0100
Make g_utf8_make_valid optionally take a length
g_utf8_make_valid was turned into a public API this cycle. However
now that it is public we should make the API more generic, allowing
the caller to specify the length. This is especially useful if
the function is called with a string that has \0 in the middle
or for chunks of a strings that are not nul terminated.
This is also consistent with most of the other utf8 utils.
Callers inside glib are updated to the new signature.
https://bugzilla.gnome.org/show_bug.cgi?id=779456
glib/gconvert.c | 2 +-
glib/gkeyfile.c | 16 ++++++++--------
glib/gmarkup.c | 2 +-
glib/gunicode.h | 3 ++-
glib/gutf8.c | 17 ++++++++++++-----
5 files changed, 24 insertions(+), 16 deletions(-)
---
diff --git a/glib/gconvert.c b/glib/gconvert.c
index 1e27f92..4f34846 100644
--- a/glib/gconvert.c
+++ b/glib/gconvert.c
@@ -1938,7 +1938,7 @@ g_filename_display_name (const gchar *filename)
* by a question mark
*/
if (!display_name)
- display_name = g_utf8_make_valid (filename);
+ display_name = g_utf8_make_valid (filename, -1);
return display_name;
}
diff --git a/glib/gkeyfile.c b/glib/gkeyfile.c
index b8a84db..3481466 100644
--- a/glib/gkeyfile.c
+++ b/glib/gkeyfile.c
@@ -1206,7 +1206,7 @@ g_key_file_parse_line (GKeyFile *key_file,
&parse_error);
else
{
- gchar *line_utf8 = g_utf8_make_valid (line);
+ gchar *line_utf8 = g_utf8_make_valid (line, length);
g_set_error (error, G_KEY_FILE_ERROR,
G_KEY_FILE_ERROR_PARSE,
_("Key file contains line “%s” which is not "
@@ -1338,7 +1338,7 @@ g_key_file_parse_key_value_pair (GKeyFile *key_file,
{
if (g_ascii_strcasecmp (value, "UTF-8") != 0)
{
- gchar *value_utf8 = g_utf8_make_valid (value);
+ gchar *value_utf8 = g_utf8_make_valid (value, value_len);
g_set_error (error, G_KEY_FILE_ERROR,
G_KEY_FILE_ERROR_UNKNOWN_ENCODING,
_("Key file contains unsupported "
@@ -1871,7 +1871,7 @@ g_key_file_get_string (GKeyFile *key_file,
if (!g_utf8_validate (value, -1, NULL))
{
- gchar *value_utf8 = g_utf8_make_valid (value);
+ gchar *value_utf8 = g_utf8_make_valid (value, -1);
g_set_error (error, G_KEY_FILE_ERROR,
G_KEY_FILE_ERROR_UNKNOWN_ENCODING,
_("Key file contains key “%s” with value “%s” "
@@ -1987,7 +1987,7 @@ g_key_file_get_string_list (GKeyFile *key_file,
if (!g_utf8_validate (value, -1, NULL))
{
- gchar *value_utf8 = g_utf8_make_valid (value);
+ gchar *value_utf8 = g_utf8_make_valid (value, -1);
g_set_error (error, G_KEY_FILE_ERROR,
G_KEY_FILE_ERROR_UNKNOWN_ENCODING,
_("Key file contains key “%s” with value “%s” "
@@ -4301,7 +4301,7 @@ g_key_file_parse_value_as_integer (GKeyFile *key_file,
if (*value == '\0' || (*eof_int != '\0' && !g_ascii_isspace(*eof_int)))
{
- gchar *value_utf8 = g_utf8_make_valid (value);
+ gchar *value_utf8 = g_utf8_make_valid (value, -1);
g_set_error (error, G_KEY_FILE_ERROR,
G_KEY_FILE_ERROR_INVALID_VALUE,
_("Value “%s” cannot be interpreted "
@@ -4314,7 +4314,7 @@ g_key_file_parse_value_as_integer (GKeyFile *key_file,
int_value = long_value;
if (int_value != long_value || errno == ERANGE)
{
- gchar *value_utf8 = g_utf8_make_valid (value);
+ gchar *value_utf8 = g_utf8_make_valid (value, -1);
g_set_error (error,
G_KEY_FILE_ERROR,
G_KEY_FILE_ERROR_INVALID_VALUE,
@@ -4348,7 +4348,7 @@ g_key_file_parse_value_as_double (GKeyFile *key_file,
if (*end_of_valid_d != '\0' || end_of_valid_d == value)
{
- gchar *value_utf8 = g_utf8_make_valid (value);
+ gchar *value_utf8 = g_utf8_make_valid (value, -1);
g_set_error (error, G_KEY_FILE_ERROR,
G_KEY_FILE_ERROR_INVALID_VALUE,
_("Value “%s” cannot be interpreted "
@@ -4387,7 +4387,7 @@ g_key_file_parse_value_as_boolean (GKeyFile *key_file,
else if (strcmp_sized (value, length, "false") == 0 || strcmp_sized (value, length, "0") == 0)
return FALSE;
- value_utf8 = g_utf8_make_valid (value);
+ value_utf8 = g_utf8_make_valid (value, -1);
g_set_error (error, G_KEY_FILE_ERROR,
G_KEY_FILE_ERROR_INVALID_VALUE,
_("Value “%s” cannot be interpreted "
diff --git a/glib/gmarkup.c b/glib/gmarkup.c
index 3c7fd49..46923b8 100644
--- a/glib/gmarkup.c
+++ b/glib/gmarkup.c
@@ -422,7 +422,7 @@ set_error (GMarkupParseContext *context,
/* Make sure that the GError message is valid UTF-8
* even if it is complaining about invalid UTF-8 in the markup
*/
- s_valid = g_utf8_make_valid (s);
+ s_valid = g_utf8_make_valid (s, -1);
set_error_literal (context, error, code, s);
g_free (s);
diff --git a/glib/gunicode.h b/glib/gunicode.h
index f331677..42ff540 100644
--- a/glib/gunicode.h
+++ b/glib/gunicode.h
@@ -885,7 +885,8 @@ gchar *g_utf8_collate_key_for_filename (const gchar *str,
gssize len) G_GNUC_MALLOC;
GLIB_AVAILABLE_IN_2_52
-gchar *g_utf8_make_valid (const gchar *str);
+gchar *g_utf8_make_valid (const gchar *str,
+ gssize len) G_GNUC_MALLOC;
G_END_DECLS
diff --git a/glib/gutf8.c b/glib/gutf8.c
index a3fe787..b16648e 100644
--- a/glib/gutf8.c
+++ b/glib/gutf8.c
@@ -1738,6 +1738,8 @@ g_utf8_strreverse (const gchar *str,
/**
* g_utf8_make_valid:
* @str: string to coerce into UTF-8
+ * @len: the maximum length of @str to use, in bytes. If @len < 0,
+ * then the string is nul-terminated.
*
* If the provided string is valid UTF-8, return a copy of it. If not,
* return a copy in which bytes that could not be interpreted as valid Unicode
@@ -1754,17 +1756,21 @@ g_utf8_strreverse (const gchar *str,
* Since: 2.52
*/
gchar *
-g_utf8_make_valid (const gchar *str)
+g_utf8_make_valid (const gchar *str,
+ gssize len)
{
GString *string;
const gchar *remainder, *invalid;
- gint remaining_bytes, valid_bytes;
+ gsize remaining_bytes, valid_bytes;
g_return_val_if_fail (str != NULL, NULL);
+ if (len < 0)
+ len = strlen (str);
+
string = NULL;
remainder = str;
- remaining_bytes = strlen (str);
+ remaining_bytes = len;
while (remaining_bytes != 0)
{
@@ -1784,11 +1790,12 @@ g_utf8_make_valid (const gchar *str)
}
if (string == NULL)
- return g_strdup (str);
+ return g_strndup (str, len);
g_string_append (string, remainder);
+ g_string_append_c (string, '\0');
g_assert (g_utf8_validate (string->str, -1, NULL));
-
+
return g_string_free (string, FALSE);
}
[
Date Prev][
Date Next] [
Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]