diff --git a/src/misc_gui.cpp b/src/misc_gui.cpp index 7495b71398..03dc1e2144 100644 --- a/src/misc_gui.cpp +++ b/src/misc_gui.cpp @@ -959,17 +959,7 @@ struct QueryStringWindow : public Window QueryStringWindow(StringID str, StringID caption, uint max_bytes, uint max_chars, WindowDesc *desc, Window *parent, CharSetFilter afilter, QueryStringFlags flags) : Window(desc), editbox(max_bytes, max_chars) { - char *last_of = &this->editbox.text.buf[this->editbox.text.max_bytes - 1]; - GetString(this->editbox.text.buf, str, last_of); - StrMakeValidInPlace(this->editbox.text.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->editbox.text.buf) + 1 > this->editbox.text.max_chars) { - *Utf8PrevChar(this->editbox.text.buf + strlen(this->editbox.text.buf)) = '\0'; - } - - this->editbox.text.UpdateSize(); + this->editbox.text.Assign(str); if ((flags & QSF_ACCEPT_UNCHANGED) == 0) this->editbox.orig = this->editbox.text.buf; diff --git a/src/textbuf.cpp b/src/textbuf.cpp index 151c753d92..7418cdf29a 100644 --- a/src/textbuf.cpp +++ b/src/textbuf.cpp @@ -388,8 +388,7 @@ Textbuf::~Textbuf() */ void Textbuf::Assign(StringID string) { - GetString(this->buf, string, &this->buf[this->max_bytes - 1]); - this->UpdateSize(); + this->Assign(GetString(string)); } /** @@ -398,7 +397,16 @@ void Textbuf::Assign(StringID string) */ void Textbuf::Assign(const std::string_view text) { - strecpy(this->buf, text.data(), &this->buf[this->max_bytes - 1]); + const char *last_of = &this->buf[this->max_bytes - 1]; + strecpy(this->buf, text.data(), last_of); + 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(); }