utils: distinguish between width (columns) and length (bytes)

master
Benno Schulenberg 2016-04-24 22:02:48 +02:00
parent 06b1fcad13
commit 981b414980
1 changed files with 16 additions and 17 deletions

View File

@ -484,38 +484,37 @@ size_t actual_x(const char *s, size_t column)
return i; return i;
} }
/* A strnlen() with tabs and multicolumn characters factored in, similar /* A strnlen() with tabs and multicolumn characters factored in:
* to xplustabs(). How many columns wide are the first maxlen characters * how many columns wide are the first maxlen bytes of text? */
* of s? */ size_t strnlenpt(const char *text, size_t maxlen)
size_t strnlenpt(const char *s, size_t maxlen)
{ {
size_t len = 0; size_t width = 0;
/* The screen display width to s[i]. */ /* The screen display width to text[maxlen]. */
if (maxlen == 0) if (maxlen == 0)
return 0; return 0;
assert(s != NULL); assert(text != NULL);
while (*s != '\0') { while (*text != '\0') {
int s_len = parse_mbchar(s, NULL, &len); int charlen = parse_mbchar(text, NULL, &width);
s += s_len; text += charlen;
if (maxlen <= s_len) if (maxlen <= charlen)
break; break;
maxlen -= s_len; maxlen -= charlen;
} }
return len; return width;
} }
/* A strlen() with tabs and multicolumn characters factored in, similar /* A strlen() with tabs and multicolumn characters factored in:
* to xplustabs(). How many columns wide is s? */ * how many columns wide is text? */
size_t strlenpt(const char *s) size_t strlenpt(const char *text)
{ {
return strnlenpt(s, (size_t)-1); return strnlenpt(text, (size_t)-1);
} }
/* Append a new magicline to filebot. */ /* Append a new magicline to filebot. */