Improve line wrapping behaviour

This commit is contained in:
Maxime Coste 2014-11-20 18:45:10 +00:00
parent 04ecb2cc5a
commit b5ccc8bc73

View File

@ -155,8 +155,8 @@ std::vector<StringView> wrap_lines(StringView text, CharCount max_width)
Utf8It end{text.end()}; Utf8It end{text.end()};
CharCount col = 0; CharCount col = 0;
std::vector<StringView> lines; std::vector<StringView> lines;
const char* line_begin = text.begin(); Utf8It line_begin = text.begin();
const char* line_end = line_begin; Utf8It line_end = line_begin;
while (word_begin != end) while (word_begin != end)
{ {
const CharCategories cat = categorize(*word_begin); const CharCategories cat = categorize(*word_begin);
@ -166,21 +166,21 @@ std::vector<StringView> wrap_lines(StringView text, CharCount max_width)
} while (word_end != end and categorize(*word_end) == cat); } while (word_end != end and categorize(*word_end) == cat);
col += word_end - word_begin; col += word_end - word_begin;
if (col > max_width or cat == CharCategories::EndOfLine) if ((word_begin != line_begin and col > max_width) or
cat == CharCategories::EndOfLine)
{ {
lines.emplace_back(line_begin, line_end); lines.emplace_back(line_begin.base(), line_end.base());
line_begin = (cat == CharCategories::EndOfLine or line_begin = (cat == CharCategories::EndOfLine or
cat == CharCategories::Blank) ? word_end.base() cat == CharCategories::Blank) ? word_end : word_begin;
: word_begin.base(); col = word_end - line_begin;
col = word_end - Utf8It{line_begin};
} }
if (cat == CharCategories::Word or cat == CharCategories::Punctuation) if (cat == CharCategories::Word or cat == CharCategories::Punctuation)
line_end = word_end.base(); line_end = word_end;
word_begin = word_end; word_begin = word_end;
} }
if (line_begin != word_begin.base()) if (line_begin != word_begin)
lines.emplace_back(line_begin, word_begin.base()); lines.emplace_back(line_begin.base(), word_begin.base());
return lines; return lines;
} }