diff options
Diffstat (limited to 'e-util')
-rw-r--r-- | e-util/e-unicode.c | 26 | ||||
-rw-r--r-- | e-util/e-unicode.h | 1 |
2 files changed, 27 insertions, 0 deletions
diff --git a/e-util/e-unicode.c b/e-util/e-unicode.c index 88f6d0605a..2e8969f718 100644 --- a/e-util/e-unicode.c +++ b/e-util/e-unicode.c @@ -211,6 +211,32 @@ e_utf8_from_locale_string_sized (const gchar *string, gint bytes) } /** + * e_utf8_ensure_valid: + * @string String to make valid UTF-8. + * + * Ensures the returned string will be valid UTF-8 string, thus gtk functions expecting + * only valid UTF-8 texts will not crash. + * + * Returned pointer should be freed with g_free. + **/ +gchar * +e_utf8_ensure_valid (const gchar *string) +{ + gchar *res = g_strdup (string), *p; + + if (!res) + return res; + + p = res; + while (!g_utf8_validate (p, -1, (const gchar **) &p)) { + /* make all invalid characters appear as question marks */ + *p = '?'; + } + + return res; +} + +/** * e_unichar_to_utf8: * @c: a ISO10646 character code * @outbuf: output buffer, must have at least 6 bytes of space. diff --git a/e-util/e-unicode.h b/e-util/e-unicode.h index b22c5c988b..2979510aa6 100644 --- a/e-util/e-unicode.h +++ b/e-util/e-unicode.h @@ -47,6 +47,7 @@ gchar *e_utf8_to_charset_string_sized (const gchar *char gint bytes); gchar *e_utf8_from_locale_string_sized (const gchar *string, gint bytes); +gchar *e_utf8_ensure_valid (const gchar *string); /* * These are simple wrappers that save us some typing */ |