Use StringBuilder for GetString/GetStringWithArgs, as per upstream

Update dependent code as required
This commit is contained in:
Jonathan G Rennison
2024-01-05 21:12:54 +00:00
parent 1b7a5372ec
commit f034714559
61 changed files with 1064 additions and 1228 deletions

View File

@@ -48,7 +48,7 @@ bool Textbuf::CanDelChar(bool backspace)
* @param keycode Type of deletion, either WKC_BACKSPACE or WKC_DELETE
* @return Return true on successful change of Textbuf, or false otherwise
*/
bool Textbuf::DeleteChar(uint16 keycode)
bool Textbuf::DeleteChar(uint16_t keycode)
{
bool word = (keycode & WKC_CTRL) != 0;
@@ -60,17 +60,17 @@ bool Textbuf::DeleteChar(uint16 keycode)
if (!CanDelChar(backspace)) return false;
char *s = this->buf + this->caretpos;
uint16 len = 0;
uint16_t len = 0;
if (word) {
/* Delete a complete word. */
if (backspace) {
/* Delete whitespace and word in front of the caret. */
len = this->caretpos - (uint16)this->char_iter->Prev(StringIterator::ITER_WORD);
len = this->caretpos - (uint16_t)this->char_iter->Prev(StringIterator::ITER_WORD);
s -= len;
} else {
/* Delete word and following whitespace following the caret. */
len = (uint16)this->char_iter->Next(StringIterator::ITER_WORD) - this->caretpos;
len = (uint16_t)this->char_iter->Next(StringIterator::ITER_WORD) - this->caretpos;
}
/* Update character count. */
for (const char *ss = s; ss < s + len; Utf8Consume(&ss)) {
@@ -81,12 +81,12 @@ bool Textbuf::DeleteChar(uint16 keycode)
if (backspace) {
/* Delete the last code point in front of the caret. */
s = Utf8PrevChar(s);
WChar c;
len = (uint16)Utf8Decode(&c, s);
char32_t c;
len = (uint16_t)Utf8Decode(&c, s);
this->chars--;
} else {
/* Delete the complete character following the caret. */
len = (uint16)this->char_iter->Next(StringIterator::ITER_CHARACTER) - this->caretpos;
len = (uint16_t)this->char_iter->Next(StringIterator::ITER_CHARACTER) - this->caretpos;
/* Update character count. */
for (const char *ss = s; ss < s + len; Utf8Consume(&ss)) {
this->chars--;
@@ -128,9 +128,9 @@ void Textbuf::DeleteAll()
* @param key Character to be inserted
* @return Return true on successful change of Textbuf, or false otherwise
*/
bool Textbuf::InsertChar(WChar key)
bool Textbuf::InsertChar(char32_t key)
{
uint16 len = (uint16)Utf8CharLen(key);
uint16_t len = (uint16_t)Utf8CharLen(key);
if (this->bytes + len <= this->max_bytes && this->chars + 1 <= this->max_chars) {
memmove(this->buf + this->caretpos + len, this->buf + this->caretpos, this->bytes - this->caretpos);
Utf8Encode(this->buf + this->caretpos, key);
@@ -160,7 +160,7 @@ bool Textbuf::InsertChar(WChar key)
*/
bool Textbuf::InsertString(const char *str, bool marked, const char *caret, const char *insert_location, const char *replacement_end)
{
uint16 insertpos = (marked && this->marklength != 0) ? this->markpos : this->caretpos;
uint16_t insertpos = (marked && this->marklength != 0) ? this->markpos : this->caretpos;
if (insert_location != nullptr) {
insertpos = insert_location - this->buf;
if (insertpos > this->bytes) return false;
@@ -174,8 +174,8 @@ bool Textbuf::InsertString(const char *str, bool marked, const char *caret, cons
if (str == nullptr) return false;
uint16 bytes = 0, chars = 0;
WChar c;
uint16_t bytes = 0, chars = 0;
char32_t c;
for (const char *ptr = str; (c = Utf8Consume(&ptr)) != '\0';) {
if (!IsValidChar(c, this->afilter)) break;
@@ -235,7 +235,7 @@ bool Textbuf::InsertClipboard()
* @param to End of the text to delete.
* @param update Set to true if the internal state should be updated.
*/
void Textbuf::DeleteText(uint16 from, uint16 to, bool update)
void Textbuf::DeleteText(uint16_t from, uint16_t to, bool update)
{
uint c = 0;
const char *s = this->buf + from;
@@ -299,7 +299,7 @@ void Textbuf::UpdateStringIter()
{
this->char_iter->SetString(this->buf);
size_t pos = this->char_iter->SetCurPosition(this->caretpos);
this->caretpos = pos == StringIterator::END ? 0 : (uint16)pos;
this->caretpos = pos == StringIterator::END ? 0 : (uint16_t)pos;
}
/** Update pixel width of the text. */
@@ -331,7 +331,7 @@ void Textbuf::UpdateMarkedText()
* @param keycode Direction in which navigation occurs (WKC_CTRL |) WKC_LEFT, (WKC_CTRL |) WKC_RIGHT, WKC_END, WKC_HOME
* @return Return true on successful change of Textbuf, or false otherwise
*/
bool Textbuf::MovePos(uint16 keycode)
bool Textbuf::MovePos(uint16_t keycode)
{
switch (keycode) {
case WKC_LEFT:
@@ -341,7 +341,7 @@ bool Textbuf::MovePos(uint16 keycode)
size_t pos = this->char_iter->Prev(keycode & WKC_CTRL ? StringIterator::ITER_WORD : StringIterator::ITER_CHARACTER);
if (pos == StringIterator::END) return true;
this->caretpos = (uint16)pos;
this->caretpos = (uint16_t)pos;
this->UpdateCaretPosition();
return true;
}
@@ -353,7 +353,7 @@ bool Textbuf::MovePos(uint16 keycode)
size_t pos = this->char_iter->Next(keycode & WKC_CTRL ? StringIterator::ITER_WORD : StringIterator::ITER_CHARACTER);
if (pos == StringIterator::END) return true;
this->caretpos = (uint16)pos;
this->caretpos = (uint16_t)pos;
this->UpdateCaretPosition();
return true;
}
@@ -383,7 +383,7 @@ bool Textbuf::MovePos(uint16 keycode)
* @param max_bytes maximum size in bytes, including terminating '\0'
* @param max_chars maximum size in chars, including terminating '\0'
*/
Textbuf::Textbuf(uint16 max_bytes, uint16 max_chars)
Textbuf::Textbuf(uint16_t max_bytes, uint16_t max_chars)
: buf(MallocT<char>(max_bytes)), char_iter(StringIterator::Create())
{
assert(max_bytes != 0);
@@ -407,27 +407,26 @@ Textbuf::~Textbuf()
*/
void Textbuf::Assign(StringID string)
{
GetString(this->buf, string, &this->buf[this->max_bytes - 1]);
this->UpdateSize();
this->Assign(GetString(string));
}
/**
* Copy a string into the textbuffer.
* @param text Source.
*/
void Textbuf::Assign(const char *text)
void Textbuf::Assign(const std::string_view text)
{
strecpy(this->buf, text, &this->buf[this->max_bytes - 1]);
this->UpdateSize();
}
const char *last_of = &this->buf[this->max_bytes - 1];
strecpy(this->buf, text.data(), last_of);
StrMakeValidInPlace(this->buf, last_of, SVS_NONE);
/**
* Copy a string into the textbuffer.
* @param text Source.
*/
void Textbuf::Assign(const std::string &text)
{
this->Assign(text.c_str());
/* 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. */
while (Utf8StringLength(this->buf) + 1 > this->max_chars) {
*Utf8PrevChar(this->buf + strlen(this->buf)) = '\0';
}
this->UpdateSize();
}
/**
@@ -435,10 +434,20 @@ void Textbuf::Assign(const std::string &text)
*/
void Textbuf::Print(const char *format, ...)
{
const char *last_of = &this->buf[this->max_bytes - 1];
va_list va;
va_start(va, format);
vseprintf(this->buf, &this->buf[this->max_bytes - 1], format, va);
vseprintf(this->buf, last_of, format, va);
va_end(va);
StrMakeValidInPlace(this->buf, last_of, SVS_NONE);
/* 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. */
while (Utf8StringLength(this->buf) + 1 > this->max_chars) {
*Utf8PrevChar(this->buf + strlen(this->buf)) = '\0';
}
this->UpdateSize();
}
@@ -454,7 +463,7 @@ void Textbuf::UpdateSize()
this->chars = this->bytes = 1; // terminating zero
WChar c;
char32_t c;
while ((c = Utf8Consume(&buf)) != '\0') {
this->bytes += Utf8CharLen(c);
this->chars++;
@@ -486,7 +495,7 @@ bool Textbuf::HandleCaret()
return false;
}
HandleKeyPressResult Textbuf::HandleKeyPress(WChar key, uint16 keycode)
HandleKeyPressResult Textbuf::HandleKeyPress(char32_t key, uint16_t keycode)
{
bool edited = false;