33struct WordBreakPropertyInterval {
44constexpr auto g_extend_characters{[]()
constexpr {
46 constexpr size_t size = []()
constexpr {
49 if (interval.property == WBP::Extend) {
57 std::array<Interval, size> result{};
60 if (interval.property == WBP::Extend) {
61 result[index++] = {interval.first, interval.last};
69bool Bisearch(uint32_t ucs,
const std::array<Interval, N>& table) {
70 if (ucs < table.front().first || ucs > table.back().last) {
77 const int mid = (min + max) / 2;
78 if (ucs > table[mid].last) {
80 }
else if (ucs < table[mid].first) {
91template <
class C,
size_t N>
92bool Bisearch(uint32_t ucs,
const std::array<C, N>& table, C* out) {
93 if (ucs < table.front().first || ucs > table.back().last) {
100 const int mid = (min + max) / 2;
101 if (ucs > table[mid].last) {
103 }
else if (ucs < table[mid].first) {
114int codepoint_width(uint32_t ucs) {
146 if (start >= input.size()) {
150 const uint8_t C0 = input[start];
153 if ((C0 & 0b1000'0000) == 0b0000'0000) {
154 *ucs = C0 & 0b0111'1111;
160 if ((C0 & 0b1110'0000) == 0b1100'0000 &&
161 start + 1 < input.size()) {
162 const uint8_t C1 = input[start + 1];
164 *ucs += C0 & 0b0001'1111;
166 *ucs += C1 & 0b0011'1111;
172 if ((C0 & 0b1111'0000) == 0b1110'0000 &&
173 start + 2 < input.size()) {
174 const uint8_t C1 = input[start + 1];
175 const uint8_t C2 = input[start + 2];
177 *ucs += C0 & 0b0000'1111;
179 *ucs += C1 & 0b0011'1111;
181 *ucs += C2 & 0b0011'1111;
187 if ((C0 & 0b1111'1000) == 0b1111'0000 &&
188 start + 3 < input.size()) {
189 const uint8_t C1 = input[start + 1];
190 const uint8_t C2 = input[start + 2];
191 const uint8_t C3 = input[start + 3];
193 *ucs += C0 & 0b0000'0111;
195 *ucs += C1 & 0b0011'1111;
197 *ucs += C2 & 0b0011'1111;
199 *ucs += C3 & 0b0011'1111;
220 if (start >= input.size()) {
226 if constexpr (
sizeof(wchar_t) == 4) {
233 int32_t C0 = input[start];
236 if (C0 < 0xd800 || C0 >= 0xdc00) {
243 if (start + 1 >= input.size()) {
248 int32_t C1 = input[start + 1];
249 *ucs = ((C0 & 0x3ff) << 10) + (C1 & 0x3ff) + 0x10000;
255 return Bisearch(ucs, g_extend_characters);
271 const uint32_t LINE_FEED = 10;
272 return ucs != LINE_FEED;
274 if (ucs >= 0x7f && ucs < 0xa0) {
281 WordBreakPropertyInterval interval = {0, 0, WBP::ALetter};
283 return interval.property;
287 return codepoint_width(uint32_t(ucs));
293 for (
const wchar_t& it : text) {
311 if (input.size() == 1) {
312 const char c = input[0];
313 if (c >= 32 && c < 127) {
321 bool is_pure_ascii =
true;
322 for (
const char c : input) {
323 if (c < 31 || c >= 127) {
324 is_pure_ascii =
false;
329 return static_cast<int>(input.size());
334 while (start < input.size()) {
335 uint32_t codepoint = 0;
359 std::vector<std::string> out;
360 out.reserve(input.size());
363 while (start < input.size()) {
364 uint32_t codepoint = 0;
370 const auto append = input.substr(start, end - start);
381 out.back() += append;
391 out.emplace_back(append);
392 out.emplace_back(
"");
397 out.emplace_back(append);
411 if ((input[start] & 0b1100'0000) == 0b1000'0000) {
415 uint32_t codepoint = 0;
417 const bool eaten =
EatCodePoint(input, start, &end, &codepoint);
429 bool glyph_found =
false;
430 while (start < input.size()) {
432 uint32_t codepoint = 0;
433 const bool eaten =
EatCodePoint(input, start, &end, &codepoint);
446 return static_cast<int>(start);
454 return static_cast<int>(input.size());
457size_t GlyphIterate(std::string_view input,
int glyph_offset,
size_t start) {
458 if (glyph_offset >= 0) {
459 for (
int i = 0; i < glyph_offset; ++i) {
464 for (
int i = 0; i < -glyph_offset; ++i) {
473 std::vector<int> out;
474 out.reserve(input.size());
477 while (start < input.size()) {
478 uint32_t codepoint = 0;
479 const bool eaten =
EatCodePoint(input, start, &end, &codepoint);
518 while (start < input.size()) {
519 uint32_t codepoint = 0;
520 const bool eaten =
EatCodePoint(input, start, &end, &codepoint);
545 std::vector<WordBreakProperty> out;
546 out.reserve(input.size());
549 while (start < input.size()) {
550 uint32_t codepoint = 0;
567 WordBreakPropertyInterval interval = {0, 0, WBP::ALetter};
569 out.push_back(interval.property);
580 uint32_t codepoint = 0;
598 if (codepoint <= 0b000'0000'0111'1111) {
599 const uint8_t p1 = codepoint;
605 if (codepoint <= 0b000'0111'1111'1111) {
606 uint8_t p2 = codepoint & 0b111111;
608 uint8_t p1 = codepoint;
609 out.push_back(0b11000000 + p1);
610 out.push_back(0b10000000 + p2);
615 if (codepoint <= 0b1111'1111'1111'1111) {
616 uint8_t p3 = codepoint & 0b111111;
618 uint8_t p2 = codepoint & 0b111111;
620 uint8_t p1 = codepoint;
621 out.push_back(0b11100000 + p1);
622 out.push_back(0b10000000 + p2);
623 out.push_back(0b10000000 + p3);
628 if (codepoint <= 0b1'0000'1111'1111'1111'1111) {
629 uint8_t p4 = codepoint & 0b111111;
631 uint8_t p3 = codepoint & 0b111111;
633 uint8_t p2 = codepoint & 0b111111;
635 uint8_t p1 = codepoint;
636 out.push_back(0b11110000 + p1);
637 out.push_back(0b10000000 + p2);
638 out.push_back(0b10000000 + p3);
639 out.push_back(0b10000000 + p4);
654 uint32_t codepoint = 0;
658 if constexpr (
sizeof(wchar_t) == 4) {
659 out.push_back(codepoint);
669 if (codepoint < 0xD800 || (codepoint > 0xDFFF && codepoint < 0x10000)) {
670 uint16_t p0 = codepoint;
677 codepoint -= 0x010000;
678 uint16_t p0 = (((codepoint << 12) >> 22) + 0xD800);
679 uint16_t p1 = (((codepoint << 22) >> 22) + 0xDC00);
Decorator size(WidthOrHeight direction, Constraint constraint, int value)
Aplica una restricción al tamaño de un elemento.
El espacio de nombres ftxui:: de FTXUI.
bool IsControl(uint32_t ucs)
WordBreakProperty CodepointToWordBreakProperty(uint32_t codepoint)
size_t GlyphPrevious(std::string_view input, size_t start)
FTXUI_EXPORT(SCREEN) int string_width(std std::vector< std::string > Utf8ToGlyphs(std::string_view input)
int string_width(std::string_view input)
bool IsCombining(uint32_t ucs)
int wchar_width(wchar_t ucs)
bool EatCodePoint(std::string_view input, size_t start, size_t *end, uint32_t *ucs)
std::string to_string(std::wstring_view s)
int GlyphCount(std::string_view input)
std::vector< WordBreakProperty > Utf8ToWordBreakProperty(std::string_view input)
int wstring_width(const std::wstring &text)
std::vector< int > CellToGlyphIndex(std::string_view input)
FTXUI_EXPORT(SCREEN) std FTXUI_EXPORT(SCREEN) std std::wstring to_wstring(T s)
size_t GlyphIterate(std::string_view input, int glyph_offset, size_t start)
bool IsFullWidth(uint32_t ucs)
size_t GlyphNext(std::string_view input, size_t start)
constexpr std::array< Interval, 123 > g_full_width_characters
constexpr std::array< WordBreakPropertyInterval, 1100 > g_word_break_intervals