From: Max Kellermann Date: Mon, 2 Nov 2009 21:54:41 +0000 (+0100) Subject: charset: added function locale_width() X-Git-Tag: release-0.16~32 X-Git-Url: https://git.tokkee.org/?a=commitdiff_plain;h=690cdf4bf10abee65e745854f4f5039d80ee04b4;p=ncmpc.git charset: added function locale_width() This function is a rather expensive wrapper: it converts the string to UTF-8 and calls utf8_width(). --- 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.