From ee9951222c4f0e3970466f6aba2e42dea21a6cdc Mon Sep 17 00:00:00 2001 From: Max Kellermann Date: Thu, 2 Oct 2008 19:01:02 +0200 Subject: [PATCH] charset: renamed my_strlen() to utf8_width() my_strlen() is a bad name for the function, since the return value is not a length, but a visible width on the screen. Rename it to utf8_width() and change its return type to "unsigned". --- src/charset.c | 6 +++--- src/charset.h | 3 ++- src/list_window.c | 2 +- src/screen.c | 14 +++++++------- src/support.c | 2 +- src/wreadln.c | 6 +++--- 6 files changed, 17 insertions(+), 16 deletions(-) diff --git a/src/charset.c b/src/charset.c index 41269f3..aaec39c 100644 --- a/src/charset.c +++ b/src/charset.c @@ -34,14 +34,14 @@ charset_init(bool disable) noconvert = disable; } -size_t -my_strlen(const char *str) +unsigned +utf8_width(const char *str) { assert(str != NULL); if (g_utf8_validate(str, -1, NULL)) { size_t len = g_utf8_strlen(str, -1); - size_t width = 0; + unsigned width = 0; gunichar c; while (len--) { diff --git a/src/charset.h b/src/charset.h index 1199377..24f7ef6 100644 --- a/src/charset.h +++ b/src/charset.h @@ -28,7 +28,8 @@ void charset_init(bool disable); /** * Returns the number of terminal cells occupied by this string. */ -size_t my_strlen(const char *str); +unsigned +utf8_width(const char *str); char *utf8_to_locale(const char *str); char *locale_to_utf8(const char *str); diff --git a/src/list_window.c b/src/list_window.c index 001b606..93bed86 100644 --- a/src/list_window.c +++ b/src/list_window.c @@ -185,7 +185,7 @@ list_window_paint(struct list_window *lw, if (label) { int selected = lw->start + i == lw->selected; - size_t len = my_strlen(label); + unsigned len = utf8_width(label); if (highlight) colors_use(lw->w, COLOR_LIST_BOLD); diff --git a/src/screen.c b/src/screen.c index 8e6ddef..87d7388 100644 --- a/src/screen.c +++ b/src/screen.c @@ -165,7 +165,7 @@ paint_top_window2(const char *header, mpdclient_t *c) g_snprintf(buf, 32, _(" Volume %d%%"), c->status->volume); } colors_use(w, COLOR_TITLE); - mvwaddstr(w, 0, screen.top_window.cols-my_strlen(buf), buf); + mvwaddstr(w, 0, screen.top_window.cols - utf8_width(buf), buf); flags[0] = 0; if (c->status != NULL) { @@ -196,11 +196,11 @@ static void paint_top_window(const char *header, mpdclient_t *c, int full_repaint) { static int prev_volume = -1; - static size_t prev_header_len = -1; + static unsigned prev_header_len = -1; WINDOW *w = screen.top_window.w; - if (prev_header_len!=my_strlen(header)) { - prev_header_len = my_strlen(header); + if (prev_header_len != utf8_width(header)) { + prev_header_len = utf8_width(header); full_repaint = 1; } @@ -277,7 +277,7 @@ paint_status_window(mpdclient_t *c) if (str) { waddstr(w, str); - x += my_strlen(str)+1; + x += utf8_width(str) + 1; } /* create time string */ @@ -329,7 +329,7 @@ paint_status_window(mpdclient_t *c) if (status != NULL && (IS_PLAYING(status->state) || IS_PAUSED(status->state))) { char songname[MAX_SONGNAME_LENGTH]; - int width = COLS-x-my_strlen(screen.buf); + int width = COLS - x - utf8_width(screen.buf); if (song) strfsong(songname, MAX_SONGNAME_LENGTH, @@ -339,7 +339,7 @@ paint_status_window(mpdclient_t *c) colors_use(w, COLOR_STATUS); /* scroll if the song name is to long */ - if (options.scroll && my_strlen(songname) > (size_t)width) { + if (options.scroll && utf8_width(songname) > (unsigned)width) { static scroll_state_t st = { 0, 0 }; char *tmp = strscroll(songname, options.scroll_sep, width, &st); diff --git a/src/support.c b/src/support.c index ebbff73..3f30671 100644 --- a/src/support.c +++ b/src/support.c @@ -115,7 +115,7 @@ strscroll(char *str, char *separator, int width, scroll_state_t *st) tmp = g_malloc(size); g_strlcpy(tmp, str, size); g_strlcat(tmp, separator, size); - len = my_strlen(tmp); + len = utf8_width(tmp); if (st->offset >= len) st->offset = 0; diff --git a/src/wreadln.c b/src/wreadln.c index 840b73b..d01cfcb 100644 --- a/src/wreadln.c +++ b/src/wreadln.c @@ -116,7 +116,7 @@ static inline void drawline(gint cursor, /* clear input area */ whline(w, ' ', width); /* print visible part of the line buffer */ - if(masked == TRUE) whline(w, '*', my_strlen(line)-start); + if(masked == TRUE) whline(w, '*', utf8_width(line) - start); else waddnstr(w, line+start, width); /* move the cursor to the correct position */ wmove(w, y, x0 + cursor-start); @@ -274,7 +274,7 @@ _wreadln(WINDOW *w, line[cursor] = 0; break; case KEY_CTRL_U: - cursor = my_strlen(line); + cursor = utf8_width(line); for (i = 0;i < cursor; i++) line[i] = '\0'; cursor = 0; @@ -290,7 +290,7 @@ _wreadln(WINDOW *w, break; case KEY_DC: /* handle delete key. As above */ case KEY_CTRL_D: - if (cursor <= (gint)my_strlen(line) - 1) { + if (cursor <= (gint)utf8_width(line) - 1) { for (i = cursor; line[i] != 0; i++) line[i] = line[i + 1]; } -- 2.30.2