From 07201b7bdcff5612f573aee10f3caa007a9d98d5 Mon Sep 17 00:00:00 2001
From: Přemysl Janouch
Date: Tue, 8 Dec 2015 22:39:16 +0100
Subject: degesch: compactify word wrapping algorithm
---
degesch.c | 19 +++++--------------
1 file changed, 5 insertions(+), 14 deletions(-)
diff --git a/degesch.c b/degesch.c
index 52141fc..14e2c40 100644
--- a/degesch.c
+++ b/degesch.c
@@ -6794,7 +6794,7 @@ static size_t
wrap_text_for_single_line (const char *text, size_t text_len,
size_t line_len, struct str *output)
{
- int eaten = 0;
+ size_t eaten = 0;
// First try going word by word
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);
// 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);
- hard_assert (next);
-
- 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;
+ eaten = p - text;
+ hard_assert ((p = utf8_next (p, text_len - eaten, NULL)));
}
+ str_append_data (output, text, eaten);
return eaten;
}
--
cgit v1.2.3-70-g09d2