From 1c4969805350c0c7f4f7657e2584ec3f38107970 Mon Sep 17 00:00:00 2001 From: James Carty Date: Mon, 13 Aug 2018 20:31:11 +0100 Subject: [PATCH] Implement 'g' flag --- components/interpreter/miscopcodes.hpp | 26 +++++++++++++++++++++++-- components/misc/messageformatparser.cpp | 2 ++ components/misc/messageformatparser.hpp | 3 ++- 3 files changed, 28 insertions(+), 3 deletions(-) diff --git a/components/interpreter/miscopcodes.hpp b/components/interpreter/miscopcodes.hpp index 8bca0aec13..03b7e186f5 100644 --- a/components/interpreter/miscopcodes.hpp +++ b/components/interpreter/miscopcodes.hpp @@ -59,11 +59,33 @@ namespace Interpreter mRuntime.pop(); if (notation == FixedNotation) + { out << std::fixed << value; - else + mFormattedMessage += out.str(); + } + else if (notation == ShortestNotation) + { + std::string scientific; + std::string fixed; + out << std::scientific << value; - mFormattedMessage += out.str(); + scientific = out.str(); + + out.str(std::string()); + out.clear(); + + out << std::fixed << value; + + fixed = out.str(); + + mFormattedMessage += fixed.length() < scientific.length() ? fixed : scientific; + } + else + { + out << std::scientific << value; + mFormattedMessage += out.str(); + } } break; default: diff --git a/components/misc/messageformatparser.cpp b/components/misc/messageformatparser.cpp index 708d9565d1..6f0e471325 100644 --- a/components/misc/messageformatparser.cpp +++ b/components/misc/messageformatparser.cpp @@ -56,6 +56,8 @@ namespace Misc visitedPlaceholder(FloatPlaceholder, pad, width, precision, FixedNotation); else if (m[i] == 'e' || m[i] == 'E') visitedPlaceholder(FloatPlaceholder, pad, width, precision, ScientificNotation); + else if (m[i] == 'g' || m[i] == 'G') + visitedPlaceholder(FloatPlaceholder, pad, width, precision, ShortestNotation); } } } diff --git a/components/misc/messageformatparser.hpp b/components/misc/messageformatparser.hpp index 10da624d45..db2a8b0af4 100644 --- a/components/misc/messageformatparser.hpp +++ b/components/misc/messageformatparser.hpp @@ -18,7 +18,8 @@ namespace Misc enum Notation { FixedNotation, - ScientificNotation + ScientificNotation, + ShortestNotation }; virtual void visitedPlaceholder(Placeholder placeholder, char padding, int width, int precision, Notation notation) = 0;