degesch: compactify word wrapping algorithm

This commit is contained in:
Přemysl Eric Janouch 2015-12-08 22:39:16 +01:00
parent 2ae916fc1a
commit 07201b7bdc
1 changed files with 5 additions and 14 deletions

View File

@ -6794,7 +6794,7 @@ static size_t
wrap_text_for_single_line (const char *text, size_t text_len, wrap_text_for_single_line (const char *text, size_t text_len,
size_t line_len, struct str *output) size_t line_len, struct str *output)
{ {
int eaten = 0; size_t eaten = 0;
// First try going word by word // First try going word by word
const char *word_start; const char *word_start;
@ -6822,21 +6822,12 @@ wrap_text_for_single_line (const char *text, size_t text_len,
return eaten + (word_start - text); return eaten + (word_start - text);
// And if that doesn't help, cut the longest valid block of characters // And if that doesn't help, cut the longest valid block of characters
while (true) for (const char *p = text; (size_t) (p - text) <= line_len; )
{ {
const char *next = utf8_next (text, text_len - eaten, NULL); eaten = p - text;
hard_assert (next); hard_assert ((p = utf8_next (p, text_len - eaten, NULL)));
size_t char_len = next - text;
if (char_len > line_len)
break;
str_append_data (output, text, char_len);
text += char_len;
eaten += char_len;
line_len -= char_len;
} }
str_append_data (output, text, eaten);
return eaten; return eaten;
} }