From 653e5e8b63d97639f7c2622f791f150960df3b8d Mon Sep 17 00:00:00 2001 From: Peter Nelson Date: Tue, 19 Nov 2024 18:05:03 +0000 Subject: [PATCH] Codechange: Use std::visit for formatting script strings. --- src/script/api/script_text.cpp | 22 +++++++++++++++------- 1 file changed, 15 insertions(+), 7 deletions(-) diff --git a/src/script/api/script_text.cpp b/src/script/api/script_text.cpp index e7a2d2dbc9..7c2a2c93ae 100644 --- a/src/script/api/script_text.cpp +++ b/src/script/api/script_text.cpp @@ -193,13 +193,21 @@ void ScriptText::ParamCheck::Encode(std::back_insert_iterator &outp { if (this->cmd == nullptr) this->cmd = cmd; if (this->used) return; - if (std::holds_alternative(*this->param)) fmt::format_to(output, ":\"{}\"", std::get(*this->param)); - if (std::holds_alternative(*this->param)) fmt::format_to(output, ":{:X}", std::get(*this->param)); - if (std::holds_alternative(*this->param)) { - fmt::format_to(output, ":"); - Utf8Encode(output, SCC_ENCODED); - fmt::format_to(output, "{:X}", std::get(*this->param)->string); - } + + struct visitor { + std::back_insert_iterator &output; + + void operator()(const std::string &value) { fmt::format_to(this->output, ":\"{}\"", value); } + void operator()(const SQInteger &value) { fmt::format_to(this->output, ":{:X}", value); } + void operator()(const ScriptTextRef &value) + { + fmt::format_to(this->output, ":"); + Utf8Encode(this->output, SCC_ENCODED); + fmt::format_to(this->output, "{:X}", value->string); + } + }; + + std::visit(visitor{output}, *this->param); this->used = true; }