From e44db20f3143dc97b887c2b63c946b5cb2a8a6b1 Mon Sep 17 00:00:00 2001 From: Maarten ter Huurne Date: Thu, 24 Jul 2014 12:34:19 +0200 Subject: [PATCH] Changed word wrapping to avoid trailing empty line If a text file ends in a newline, the previous line splitting code would append an empty line at the end, which looked odd. So now we explicitly check that a newline is only inserted if more characters follow. --- src/font.cpp | 22 ++++++++++------------ 1 file changed, 10 insertions(+), 12 deletions(-) diff --git a/src/font.cpp b/src/font.cpp index 9737082..9f63ab2 100644 --- a/src/font.cpp +++ b/src/font.cpp @@ -76,21 +76,19 @@ int Font::getTextWidth(const string &text) string Font::wordWrap(const string &text, int width) { + const size_t len = text.length(); string result; - result.reserve(text.length()); + result.reserve(len); - size_t start = 0, end = text.find('\n'); - while (end != string::npos) { - if (start != end) { - result.append(wordWrapSingleLine(text, start, end, width)); - } - result.append("\n"); + size_t start = 0; + while (true) { + size_t end = min(text.find('\n', start), len); + result.append(wordWrapSingleLine(text, start, end, width)); start = end + 1; - end = text.find('\n', start); - } - - if (start < text.length()) { - result.append(wordWrapSingleLine(text, start, text.length(), width)); + if (start >= len) { + break; + } + result.push_back('\n'); } return result;