From 50ffc908e89bf984840c6f863b34d1115c49aeda Mon Sep 17 00:00:00 2001 From: Evil Eye Date: Fri, 3 Oct 2025 17:21:52 +0200 Subject: [PATCH] Use vformat_to and add more tests --- apps/openmw_tests/mwscript/testscripts.cpp | 13 ++++++++++--- components/interpreter/miscopcodes.hpp | 15 ++++++++++----- 2 files changed, 20 insertions(+), 8 deletions(-) diff --git a/apps/openmw_tests/mwscript/testscripts.cpp b/apps/openmw_tests/mwscript/testscripts.cpp index 1cc85e40ae..561a7bc3fa 100644 --- a/apps/openmw_tests/mwscript/testscripts.cpp +++ b/apps/openmw_tests/mwscript/testscripts.cpp @@ -138,6 +138,7 @@ set fVal to 12.34 MessageBox "hello world" MessageBox "%.0f" fVal +MessageBox "%.f" fVal MessageBox "a %03.0f b" fVal MessageBox "%+04.0f" fVal MessageBox "%+4.0f" fVal @@ -153,6 +154,8 @@ MessageBox "%#.5g" fVal MessageBox "%-5g" fVal MessageBox "%- 5g" fVal +MessageBox "%.1b" fVal + End)mwscript"; const std::string sIssue587 = R"mwscript(Begin stalresetScript @@ -616,6 +619,7 @@ End)mwscript"; constexpr std::array expected{ "hello world"sv, "12"sv, + "12"sv, "a 012 b"sv, "+012"sv, " +12"sv, @@ -630,11 +634,14 @@ End)mwscript"; "12.340"sv, "12.34"sv, " 12.34"sv, + + "b"sv, }; - for (std::size_t i = 0; i < context.getMessages().size(); i++) + const std::vector& output = context.getMessages(); + EXPECT_EQ(expected.size(), output.size()); + for (std::size_t i = 0; i < output.size(); i++) { - std::string_view message = context.getMessages()[i]; - EXPECT_EQ(expected.at(i), message); + EXPECT_EQ(expected[i], output[i]); } } else diff --git a/components/interpreter/miscopcodes.hpp b/components/interpreter/miscopcodes.hpp index c8da745c41..05791617a6 100644 --- a/components/interpreter/miscopcodes.hpp +++ b/components/interpreter/miscopcodes.hpp @@ -46,7 +46,8 @@ namespace Interpreter else formatString += '>'; formatString += "{}}"; - mFormattedMessage += std::vformat(formatString, std::make_format_args(value, width)); + std::vformat_to( + std::back_inserter(mFormattedMessage), formatString, std::make_format_args(value, width)); } } else @@ -75,13 +76,17 @@ namespace Interpreter formatString += '}'; const auto appendMessage = [&](auto value) { if (width >= 0 && precision >= 0) - mFormattedMessage += std::vformat(formatString, std::make_format_args(value, width, precision)); + std::vformat_to(std::back_inserter(mFormattedMessage), formatString, + std::make_format_args(value, width, precision)); else if (width >= 0) - mFormattedMessage += std::vformat(formatString, std::make_format_args(value, width)); + std::vformat_to( + std::back_inserter(mFormattedMessage), formatString, std::make_format_args(value, width)); else if (precision >= 0) - mFormattedMessage += std::vformat(formatString, std::make_format_args(value, precision)); + std::vformat_to(std::back_inserter(mFormattedMessage), formatString, + std::make_format_args(value, precision)); else - mFormattedMessage += std::vformat(formatString, std::make_format_args(value)); + std::vformat_to( + std::back_inserter(mFormattedMessage), formatString, std::make_format_args(value)); }; if (placeholder == FloatPlaceholder) {