mirror of
https://github.com/mawww/kakoune.git
synced 2024-12-18 08:51:46 +03:00
Change wrap_lines to return StringViews, move it to string.cc
This commit is contained in:
parent
d7a31f8983
commit
314d662c5f
@ -689,45 +689,6 @@ static CharCoord compute_pos(CharCoord anchor, CharCoord size,
|
|||||||
return pos;
|
return pos;
|
||||||
}
|
}
|
||||||
|
|
||||||
static std::vector<String> wrap_lines(StringView text, CharCount max_width)
|
|
||||||
{
|
|
||||||
enum CharCategory { Word, Blank, Eol };
|
|
||||||
static const auto categorize = [](Codepoint c) {
|
|
||||||
return is_blank(c) ? Blank
|
|
||||||
: is_eol(c) ? Eol : Word;
|
|
||||||
};
|
|
||||||
|
|
||||||
using Utf8It = utf8::iterator<const char*>;
|
|
||||||
Utf8It word_begin{text.begin()};
|
|
||||||
Utf8It word_end{word_begin};
|
|
||||||
Utf8It end{text.end()};
|
|
||||||
CharCount col = 0;
|
|
||||||
std::vector<String> lines;
|
|
||||||
String line;
|
|
||||||
while (word_begin != end)
|
|
||||||
{
|
|
||||||
CharCategory cat = categorize(*word_begin);
|
|
||||||
do
|
|
||||||
{
|
|
||||||
++word_end;
|
|
||||||
} while (word_end != end and categorize(*word_end) == cat);
|
|
||||||
|
|
||||||
col += word_end - word_begin;
|
|
||||||
if (col > max_width or *word_begin == '\n')
|
|
||||||
{
|
|
||||||
lines.push_back(std::move(line));
|
|
||||||
line = "";
|
|
||||||
col = 0;
|
|
||||||
}
|
|
||||||
if (*word_begin != '\n')
|
|
||||||
line += String{word_begin.base(), word_end.base()};
|
|
||||||
word_begin = word_end;
|
|
||||||
}
|
|
||||||
if (not line.empty())
|
|
||||||
lines.push_back(std::move(line));
|
|
||||||
return lines;
|
|
||||||
}
|
|
||||||
|
|
||||||
template<bool assist = true>
|
template<bool assist = true>
|
||||||
static String make_info_box(StringView title, StringView message,
|
static String make_info_box(StringView title, StringView message,
|
||||||
CharCount max_width)
|
CharCount max_width)
|
||||||
@ -746,7 +707,7 @@ static String make_info_box(StringView title, StringView message,
|
|||||||
assistant_size = { (int)assistant.size(), assistant[0].char_length() };
|
assistant_size = { (int)assistant.size(), assistant[0].char_length() };
|
||||||
|
|
||||||
const CharCount max_bubble_width = max_width - assistant_size.column - 6;
|
const CharCount max_bubble_width = max_width - assistant_size.column - 6;
|
||||||
std::vector<String> lines = wrap_lines(message, max_bubble_width);
|
std::vector<StringView> lines = wrap_lines(message, max_bubble_width);
|
||||||
|
|
||||||
CharCount bubble_width = title.char_length() + 2;
|
CharCount bubble_width = title.char_length() + 2;
|
||||||
for (auto& line : lines)
|
for (auto& line : lines)
|
||||||
|
@ -147,6 +147,45 @@ String expand_tabs(StringView line, CharCount tabstop, CharCount col)
|
|||||||
return res;
|
return res;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
std::vector<StringView> wrap_lines(StringView text, CharCount max_width)
|
||||||
|
{
|
||||||
|
enum CharCategory { Word, Blank, Eol };
|
||||||
|
static const auto categorize = [](Codepoint c) {
|
||||||
|
return is_blank(c) ? Blank
|
||||||
|
: is_eol(c) ? Eol : Word;
|
||||||
|
};
|
||||||
|
|
||||||
|
using Utf8It = utf8::iterator<const char*>;
|
||||||
|
Utf8It word_begin{text.begin()};
|
||||||
|
Utf8It word_end{word_begin};
|
||||||
|
Utf8It end{text.end()};
|
||||||
|
CharCount col = 0;
|
||||||
|
std::vector<StringView> lines;
|
||||||
|
const char* line_begin = text.begin();
|
||||||
|
while (word_begin != end)
|
||||||
|
{
|
||||||
|
CharCategory cat = categorize(*word_begin);
|
||||||
|
do
|
||||||
|
{
|
||||||
|
++word_end;
|
||||||
|
} while (word_end != end and categorize(*word_end) == cat);
|
||||||
|
|
||||||
|
col += word_end - word_begin;
|
||||||
|
if (col > max_width or *word_begin == '\n')
|
||||||
|
{
|
||||||
|
lines.emplace_back(line_begin, word_begin.base());
|
||||||
|
line_begin = word_begin.base();
|
||||||
|
if (*line_begin == '\n')
|
||||||
|
++line_begin;
|
||||||
|
col = 0;
|
||||||
|
}
|
||||||
|
word_begin = word_end;
|
||||||
|
}
|
||||||
|
if (line_begin != word_begin.base())
|
||||||
|
lines.emplace_back(line_begin, word_begin.base());
|
||||||
|
return lines;
|
||||||
|
}
|
||||||
|
|
||||||
[[gnu::always_inline]]
|
[[gnu::always_inline]]
|
||||||
static inline uint32_t rotl(uint32_t x, int8_t r)
|
static inline uint32_t rotl(uint32_t x, int8_t r)
|
||||||
{
|
{
|
||||||
|
@ -295,6 +295,8 @@ bool subsequence_match(StringView str, StringView subseq);
|
|||||||
|
|
||||||
String expand_tabs(StringView line, CharCount tabstop, CharCount col = 0);
|
String expand_tabs(StringView line, CharCount tabstop, CharCount col = 0);
|
||||||
|
|
||||||
|
std::vector<StringView> wrap_lines(StringView text, CharCount max_width);
|
||||||
|
|
||||||
size_t hash_data(const char* data, size_t len);
|
size_t hash_data(const char* data, size_t len);
|
||||||
|
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user