Merge branch 'parseconv' into 'master'

Use std::from_chars in MessageFormatParser

See merge request OpenMW/openmw!4867
This commit is contained in:
psi29a 2025-08-23 09:53:51 +00:00
commit c687a7fd79
3 changed files with 54 additions and 28 deletions

View File

@ -57,12 +57,12 @@ namespace Interpreter
float value = mRuntime[0].mFloat; float value = mRuntime[0].mFloat;
mRuntime.pop(); mRuntime.pop();
if (notation == FixedNotation) if (notation == Notation::Fixed)
{ {
out << std::fixed << value; out << std::fixed << value;
mFormattedMessage += out.str(); mFormattedMessage += out.str();
} }
else if (notation == ShortestNotation) else if (notation == Notation::Shortest)
{ {
out << value; out << value;
std::string standard = out.str(); std::string standard = out.str();
@ -75,6 +75,17 @@ namespace Interpreter
mFormattedMessage += standard.length() < scientific.length() ? standard : scientific; mFormattedMessage += standard.length() < scientific.length() ? standard : scientific;
} }
// TODO switch to std::format so the precision argument applies to these two
else if (notation == Notation::HexLower)
{
out << std::hexfloat << value;
mFormattedMessage += out.str();
}
else if (notation == Notation::HexUpper)
{
out << std::uppercase << std::hexfloat << value;
mFormattedMessage += out.str();
}
else else
{ {
out << std::scientific << value; out << std::scientific << value;

View File

@ -1,12 +1,31 @@
#include "messageformatparser.hpp" #include "messageformatparser.hpp"
#include <charconv>
namespace
{
int parseNumber(std::size_t& i, std::string_view m, int fallback)
{
if (i < m.size() && m[i] >= '0' && m[i] <= '9')
{
const char* start = m.data() + i;
int parsed;
auto [ptr, ec] = std::from_chars(start, m.data() + m.size(), parsed);
i += ptr - start;
if (ec == std::errc())
return parsed;
}
return fallback;
}
}
namespace Misc namespace Misc
{ {
MessageFormatParser::~MessageFormatParser() {} MessageFormatParser::~MessageFormatParser() = default;
void MessageFormatParser::process(std::string_view m) void MessageFormatParser::process(std::string_view m)
{ {
for (unsigned int i = 0; i < m.size(); ++i) for (std::size_t i = 0; i < m.size(); ++i)
{ {
if (m[i] == '%') if (m[i] == '%')
{ {
@ -23,41 +42,35 @@ namespace Misc
++i; ++i;
} }
int width = 0; int width = parseNumber(i, m, -1);
bool widthSet = false;
while (i < m.size() && m[i] >= '0' && m[i] <= '9')
{
width = width * 10 + (m[i] - '0');
widthSet = true;
++i;
}
if (i < m.size()) if (i < m.size())
{ {
int precision = -1; int precision = -1;
if (m[i] == '.') if (m[i] == '.')
{ {
precision = 0; ++i;
while (++i < m.size() && m[i] >= '0' && m[i] <= '9') precision = parseNumber(i, m, 0);
{
precision = precision * 10 + (m[i] - '0');
}
} }
if (i < m.size()) if (i < m.size())
{ {
width = (widthSet) ? width : -1;
if (m[i] == 'S' || m[i] == 's') if (m[i] == 'S' || m[i] == 's')
visitedPlaceholder(StringPlaceholder, pad, width, precision, FixedNotation); visitedPlaceholder(StringPlaceholder, pad, width, precision, Notation::Fixed);
else if (m[i] == 'd' || m[i] == 'i') else if (m[i] == 'd' || m[i] == 'i')
visitedPlaceholder(IntegerPlaceholder, pad, width, precision, FixedNotation); visitedPlaceholder(IntegerPlaceholder, pad, width, precision, Notation::Fixed);
else if (m[i] == 'f' || m[i] == 'F') else if (m[i] == 'f' || m[i] == 'F')
visitedPlaceholder(FloatPlaceholder, pad, width, precision, FixedNotation); visitedPlaceholder(FloatPlaceholder, pad, width, precision, Notation::Fixed);
else if (m[i] == 'e' || m[i] == 'E') else if (m[i] == 'e' || m[i] == 'E')
visitedPlaceholder(FloatPlaceholder, pad, width, precision, ScientificNotation); visitedPlaceholder(FloatPlaceholder, pad, width, precision, Notation::Scientific);
else if (m[i] == 'g' || m[i] == 'G') else if (m[i] == 'g' || m[i] == 'G')
visitedPlaceholder(FloatPlaceholder, pad, width, precision, ShortestNotation); visitedPlaceholder(FloatPlaceholder, pad, width, precision, Notation::Shortest);
else if (m[i] == 'a')
visitedPlaceholder(FloatPlaceholder, pad, width, precision, Notation::HexLower);
else if (m[i] == 'A')
visitedPlaceholder(FloatPlaceholder, pad, width, precision, Notation::HexUpper);
else
visitedCharacter(m[i]);
} }
} }
} }

View File

@ -15,11 +15,13 @@ namespace Misc
FloatPlaceholder FloatPlaceholder
}; };
enum Notation enum class Notation
{ {
FixedNotation, Fixed,
ScientificNotation, Scientific,
ShortestNotation Shortest,
HexUpper,
HexLower
}; };
virtual void visitedPlaceholder( virtual void visitedPlaceholder(