From 579f35511afebba2ea3b36c140f793b7efebf6d3 Mon Sep 17 00:00:00 2001 From: James Carty Date: Sun, 12 Aug 2018 22:45:03 +0100 Subject: [PATCH 1/3] Add support for scietific notation flag for MessageBox --- components/compiler/lineparser.cpp | 2 +- components/compiler/lineparser.hpp | 2 +- components/interpreter/miscopcodes.hpp | 8 ++++++-- components/misc/messageformatparser.cpp | 10 ++++++---- components/misc/messageformatparser.hpp | 8 +++++++- 5 files changed, 21 insertions(+), 9 deletions(-) diff --git a/components/compiler/lineparser.cpp b/components/compiler/lineparser.cpp index 2d551348d..602bb826f 100644 --- a/components/compiler/lineparser.cpp +++ b/components/compiler/lineparser.cpp @@ -557,7 +557,7 @@ namespace Compiler mExplicit.clear(); } - void GetArgumentsFromMessageFormat::visitedPlaceholder(Placeholder placeholder, char /*padding*/, int /*width*/, int /*precision*/) + void GetArgumentsFromMessageFormat::visitedPlaceholder(Placeholder placeholder, char /*padding*/, int /*width*/, int /*precision*/, Notation /*notation*/) { switch (placeholder) { diff --git a/components/compiler/lineparser.hpp b/components/compiler/lineparser.hpp index d92c4895e..8f7f64bf2 100644 --- a/components/compiler/lineparser.hpp +++ b/components/compiler/lineparser.hpp @@ -83,7 +83,7 @@ namespace Compiler std::string mArguments; protected: - virtual void visitedPlaceholder(Placeholder placeholder, char padding, int width, int precision); + virtual void visitedPlaceholder(Placeholder placeholder, char padding, int width, int precision, Notation notation); virtual void visitedCharacter(char c) {} public: diff --git a/components/interpreter/miscopcodes.hpp b/components/interpreter/miscopcodes.hpp index 20a1b52be..8bca0aec1 100644 --- a/components/interpreter/miscopcodes.hpp +++ b/components/interpreter/miscopcodes.hpp @@ -24,7 +24,7 @@ namespace Interpreter Runtime& mRuntime; protected: - virtual void visitedPlaceholder(Placeholder placeholder, char padding, int width, int precision) + virtual void visitedPlaceholder(Placeholder placeholder, char padding, int width, int precision, Notation notation) { std::ostringstream out; out.fill(padding); @@ -58,7 +58,11 @@ namespace Interpreter float value = mRuntime[0].mFloat; mRuntime.pop(); - out << std::fixed << value; + if (notation == FixedNotation) + out << std::fixed << value; + else + out << std::scientific << value; + mFormattedMessage += out.str(); } break; diff --git a/components/misc/messageformatparser.cpp b/components/misc/messageformatparser.cpp index 3a35c83ea..708d9565d 100644 --- a/components/misc/messageformatparser.cpp +++ b/components/misc/messageformatparser.cpp @@ -49,11 +49,13 @@ namespace Misc width = (widthSet) ? width : -1; if (m[i] == 'S' || m[i] == 's') - visitedPlaceholder(StringPlaceholder, pad, width, precision); - else if (m[i] == 'g' || m[i] == 'G') - visitedPlaceholder(IntegerPlaceholder, pad, width, precision); + visitedPlaceholder(StringPlaceholder, pad, width, precision, FixedNotation); + else if (m[i] == 'd' || m[i] == 'i') + visitedPlaceholder(IntegerPlaceholder, pad, width, precision, FixedNotation); else if (m[i] == 'f' || m[i] == 'F') - visitedPlaceholder(FloatPlaceholder, pad, width, precision); + visitedPlaceholder(FloatPlaceholder, pad, width, precision, FixedNotation); + else if (m[i] == 'e' || m[i] == 'E') + visitedPlaceholder(FloatPlaceholder, pad, width, precision, ScientificNotation); } } } diff --git a/components/misc/messageformatparser.hpp b/components/misc/messageformatparser.hpp index c12b9352a..10da624d4 100644 --- a/components/misc/messageformatparser.hpp +++ b/components/misc/messageformatparser.hpp @@ -15,7 +15,13 @@ namespace Misc FloatPlaceholder }; - virtual void visitedPlaceholder(Placeholder placeholder, char padding, int width, int precision) = 0; + enum Notation + { + FixedNotation, + ScientificNotation + }; + + virtual void visitedPlaceholder(Placeholder placeholder, char padding, int width, int precision, Notation notation) = 0; virtual void visitedCharacter(char c) = 0; public: From 1c4969805350c0c7f4f7657e2584ec3f38107970 Mon Sep 17 00:00:00 2001 From: James Carty Date: Mon, 13 Aug 2018 20:31:11 +0100 Subject: [PATCH 2/3] 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 8bca0aec1..03b7e186f 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 708d9565d..6f0e47132 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 10da624d4..db2a8b0af 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; From 331016c782a3ac8582919165f79a6898df09ff49 Mon Sep 17 00:00:00 2001 From: James Carty Date: Fri, 17 Aug 2018 13:48:02 +0100 Subject: [PATCH 3/3] Update Authors --- AUTHORS.md | 1 + 1 file changed, 1 insertion(+) diff --git a/AUTHORS.md b/AUTHORS.md index d2fd05180..bc22b230f 100644 --- a/AUTHORS.md +++ b/AUTHORS.md @@ -172,6 +172,7 @@ Programmers Vincent Heuken vocollapse zelurker + James Carty (MrTopCat) Documentation -------------