1
0
Fork 0

Codechange: Use Utf8View in Textbuf.

pull/13960/head
frosch 2025-04-01 18:17:37 +02:00 committed by frosch
parent f19e75b606
commit 22ab0244d8
1 changed files with 26 additions and 30 deletions

View File

@ -12,6 +12,7 @@
#include "textbuf_type.h" #include "textbuf_type.h"
#include "string_func.h" #include "string_func.h"
#include "strings_func.h" #include "strings_func.h"
#include "core/utf8.hpp"
#include "gfx_type.h" #include "gfx_type.h"
#include "gfx_func.h" #include "gfx_func.h"
#include "gfx_layout.h" #include "gfx_layout.h"
@ -58,45 +59,44 @@ bool Textbuf::DeleteChar(uint16_t keycode)
if (!CanDelChar(backspace)) return false; if (!CanDelChar(backspace)) return false;
auto s = this->buf.begin() + this->caretpos; size_t start;
uint16_t len = 0; size_t len;
if (word) { if (word) {
/* Delete a complete word. */ /* Delete a complete word. */
if (backspace) { if (backspace) {
/* Delete whitespace and word in front of the caret. */ /* Delete whitespace and word in front of the caret. */
len = this->caretpos - (uint16_t)this->char_iter->Prev(StringIterator::ITER_WORD); start = this->char_iter->Prev(StringIterator::ITER_WORD);
s -= len; len = this->caretpos - start;
} else { } else {
/* Delete word and following whitespace following the caret. */ /* Delete word and following whitespace following the caret. */
len = (uint16_t)this->char_iter->Next(StringIterator::ITER_WORD) - this->caretpos; start = this->caretpos;
len = this->char_iter->Next(StringIterator::ITER_WORD) - start;
} }
/* Update character count. */ /* Update character count. */
for (auto ss = s; ss < s + len; Utf8Consume(ss)) { this->chars -= static_cast<uint16_t>(Utf8StringLength(std::string_view(this->buf).substr(start, len)));
this->chars--;
}
} else { } else {
/* Delete a single character. */ /* Delete a single character. */
if (backspace) { if (backspace) {
/* Delete the last code point in front of the caret. */ /* Delete the last code point in front of the caret. */
s = Utf8PrevChar(s); Utf8View view(this->buf);
char32_t c; auto it = view.GetIterAtByte(this->caretpos);
len = (uint16_t)Utf8Decode(&c, s); --it;
start = it.GetByteOffset();
len = this->caretpos - start;
this->chars--; this->chars--;
} else { } else {
/* Delete the complete character following the caret. */ /* Delete the complete character following the caret. */
len = (uint16_t)this->char_iter->Next(StringIterator::ITER_CHARACTER) - this->caretpos; start = this->caretpos;
len = this->char_iter->Next(StringIterator::ITER_CHARACTER) - start;
/* Update character count. */ /* Update character count. */
for (auto ss = s; ss < s + len; Utf8Consume(ss)) { this->chars -= static_cast<uint16_t>(Utf8StringLength(std::string_view(this->buf).substr(start, len)));
this->chars--;
}
} }
} }
/* Move the remaining characters over the marker */ /* Move the remaining characters over the marker */
this->buf.erase(s - this->buf.begin(), len); this->buf.erase(start, len);
if (backspace) this->caretpos -= len; if (backspace) this->caretpos -= static_cast<uint16_t>(len);
this->UpdateStringIter(); this->UpdateStringIter();
this->UpdateWidth(); this->UpdateWidth();
@ -232,17 +232,11 @@ bool Textbuf::InsertClipboard()
*/ */
void Textbuf::DeleteText(uint16_t from, uint16_t to, bool update) void Textbuf::DeleteText(uint16_t from, uint16_t to, bool update)
{ {
uint c = 0; assert(from <= to);
auto s = this->buf.begin() + from;
auto end = this->buf.begin() + to;
while (s < end) {
Utf8Consume(s);
c++;
}
/* Strip marked characters from buffer. */ /* Strip marked characters from buffer. */
this->chars -= static_cast<uint16_t>(Utf8StringLength(std::string_view(this->buf).substr(from, to - from)));
this->buf.erase(from, to - from); this->buf.erase(from, to - from);
this->chars -= c;
auto fixup = [&](uint16_t &pos) { auto fixup = [&](uint16_t &pos) {
if (pos <= from) return; if (pos <= from) return;
@ -426,11 +420,13 @@ void Textbuf::Assign(const std::string_view text)
/* Make sure the name isn't too long for the text buffer in the number of /* Make sure the name isn't too long for the text buffer in the number of
* characters (not bytes). max_chars also counts the '\0' characters. */ * characters (not bytes). max_chars also counts the '\0' characters. */
auto iter = this->buf.begin(); Utf8View view(text);
for (size_t len = 1; len < this->max_chars && iter != this->buf.end(); ++len) Utf8Consume(iter); auto it = view.begin();
const auto end = view.end();
for (size_t len = 1; len < this->max_chars && it != end; ++len) ++it;
if (iter != this->buf.end()) { if (it != end) {
this->buf.erase(iter, this->buf.end()); this->buf.erase(it.GetByteOffset(), std::string::npos);
} }
this->UpdateSize(); this->UpdateSize();