summary | shortlog | log | commit | commitdiff | tree
raw | patch | inline | side by side (parent: a67a7dd)
raw | patch | inline | side by side (parent: a67a7dd)
author | Max Kellermann <max@duempel.org> | |
Thu, 18 Sep 2008 11:18:16 +0000 (13:18 +0200) | ||
committer | Max Kellermann <max@duempel.org> | |
Thu, 18 Sep 2008 11:18:16 +0000 (13:18 +0200) |
This patch was taken from bug 476, author unknown
(http://www.musicpd.org/mantis/view.php?id=476). It changes the
definition of my_strlen(): instead of returning the number of UTF-8
characters, it counts the number of terminal cells occupied by a
string.
(http://www.musicpd.org/mantis/view.php?id=476). It changes the
definition of my_strlen(): instead of returning the number of UTF-8
characters, it counts the number of terminal cells occupied by a
string.
src/support.c | patch | blob | history | |
src/support.h | patch | blob | history |
diff --git a/src/support.c b/src/support.c
index 949c8dc52c568a6743bffecb77642304e15a08b5..ad0f93bd2057d65661f479d3367f1291205297c1 100644 (file)
--- a/src/support.c
+++ b/src/support.c
size_t
my_strlen(const char *str)
{
- if (g_utf8_validate(str, -1, NULL))
- return g_utf8_strlen(str, -1);
- else
+ if (g_utf8_validate(str, -1, NULL)) {
+ size_t len = g_utf8_strlen(str, -1);
+ size_t width = 0;
+ gunichar c;
+
+ while (len--) {
+ c = g_utf8_get_char(str);
+ width += g_unichar_iswide(c) ? 2 : 1;
+ str += g_unichar_to_utf8(c, NULL);
+ }
+
+ return width;
+ } else
return strlen(str);
}
diff --git a/src/support.h b/src/support.h
index b80b9c8a2600379e47df2072c9993998d8820699..3377f9a8099ed6e97e89399c8f7e65d0a120cf1a 100644 (file)
--- a/src/support.h
+++ b/src/support.h
char *utf8_to_locale(const char *str);
char *locale_to_utf8(const char *str);
-/* number of characters in str */
+/**
+ * Returns the number of terminal cells occupied by this string.
+ */
size_t my_strlen(const char *str);
+
/* number of bytes in str */
size_t my_strsize(char *str);