From 87e8d7f3e8fa5a005c2aae06b6e00c4ba47d0c7e Mon Sep 17 00:00:00 2001 From: Max Kellermann Date: Mon, 19 Oct 2009 08:18:43 +0200 Subject: [PATCH] charset: added "replace" functions These functions are a shortcut for a common use case: convert an allocated string in-place. --- src/charset.c | 31 +++++++++++++++++++++++++++++++ src/charset.h | 15 +++++++++++++++ src/wreadln.c | 3 +-- 3 files changed, 47 insertions(+), 2 deletions(-) diff --git a/src/charset.c b/src/charset.c index 85cb891..59d1a26 100644 --- a/src/charset.c +++ b/src/charset.c @@ -107,3 +107,34 @@ locale_to_utf8(const char *localestr) return g_strdup(localestr); #endif } + +char * +replace_utf8_to_locale(char *src) +{ +#ifdef ENABLE_LOCALE + assert(src != NULL); + + if (noconvert) + return src; + + return utf8_to_locale(src); +#else + return src; +#endif +} + + +char * +replace_locale_to_utf8(char *src) +{ +#ifdef ENABLE_LOCALE + assert(src != NULL); + + if (noconvert) + return src; + + return locale_to_utf8(src); +#else + return src; +#endif +} diff --git a/src/charset.h b/src/charset.h index d1b3f75..a08054b 100644 --- a/src/charset.h +++ b/src/charset.h @@ -41,4 +41,19 @@ utf8_width(const char *str); char *utf8_to_locale(const char *str); char *locale_to_utf8(const char *str); +/** + * Converts the UTF-8 string to the locale, and frees the source + * pointer. Returns the source pointer as-is if no conversion is + * required. + */ +char * +replace_utf8_to_locale(char *src); + +/** + * Converts the locale string to UTF-8, and frees the source pointer. + * Returns the source pointer as-is if no conversion is required. + */ +char * +replace_locale_to_utf8(char *src); + #endif diff --git a/src/wreadln.c b/src/wreadln.c index ad1047f..66a1f1c 100644 --- a/src/wreadln.c +++ b/src/wreadln.c @@ -93,8 +93,7 @@ byte_to_screen(const gchar *data, size_t x) dup = g_strdup(data); dup[x] = 0; - p = locale_to_utf8(dup); - g_free(dup); + p = replace_locale_to_utf8(dup); width = utf8_width(p); g_free(p); -- 2.30.2