From 690cdf4bf10abee65e745854f4f5039d80ee04b4 Mon Sep 17 00:00:00 2001 From: Max Kellermann Date: Mon, 2 Nov 2009 22:54:41 +0100 Subject: [PATCH] charset: added function locale_width() This function is a rather expensive wrapper: it converts the string to UTF-8 and calls utf8_width(). --- src/charset.c | 20 ++++++++++++++++++++ src/charset.h | 7 +++++++ 2 files changed, 27 insertions(+) diff --git a/src/charset.c b/src/charset.c index 8e7ed0f..6282fbf 100644 --- a/src/charset.c +++ b/src/charset.c @@ -62,6 +62,26 @@ utf8_width(const char *str) #endif } +unsigned +locale_width(const char *p) +{ +#if defined(ENABLE_LOCALE) && defined(ENABLE_MULTIBYTE) + char *utf8; + unsigned width; + + if (noconvert) + return utf8_width(p); + + utf8 = locale_to_utf8(p); + width = utf8_width(utf8); + g_free(utf8); + + return width; +#else + return strlen(str); +#endif +} + static inline unsigned ascii_cut_width(char *p, unsigned max_width) { diff --git a/src/charset.h b/src/charset.h index e64eb19..6515229 100644 --- a/src/charset.h +++ b/src/charset.h @@ -38,6 +38,13 @@ G_GNUC_PURE unsigned utf8_width(const char *str); +/** + * Returns the number of terminal cells occupied by this string. + */ +G_GNUC_PURE +unsigned +locale_width(const char *p); + /** * Limits the width of the specified string. Cuts it off before the * specified width is exceeded. -- 2.30.2