From 6a3d4777be776a47ac14f75bd63081e057db23f4 Mon Sep 17 00:00:00 2001 From: Ichthyostega Date: Thu, 27 Dec 2012 22:32:55 +0100 Subject: [PATCH] supplement special format handling for Symbol datatype --- src/lib/format-string.hpp | 20 ++++++++++++++++++++ src/lib/symbol.hpp | 2 +- tests/lib/format-string-test.cpp | 22 ++++++++++++++++++++++ 3 files changed, 43 insertions(+), 1 deletion(-) diff --git a/src/lib/format-string.hpp b/src/lib/format-string.hpp index f60d8932e..1f4c4033e 100644 --- a/src/lib/format-string.hpp +++ b/src/lib/format-string.hpp @@ -123,8 +123,13 @@ namespace std { // forward declaration to avoid including class basic_ostream; typedef basic_ostream > ostream; + } +namespace lib { + class Literal; + class Symbol; +} namespace util { @@ -351,6 +356,21 @@ namespace util { } }; + template<> + struct _Fmt::Converter + { + static void + dump (lib::Literal const& literal, Implementation& impl) + { + format (literal.empty()? "" : literal.c(), impl); + } + }; + + template<> + struct _Fmt::Converter + : _Fmt::Converter + { }; + /** some custom types explicitly provide a string representation */ template struct _Fmt::Converter >::type> diff --git a/src/lib/symbol.hpp b/src/lib/symbol.hpp index c032a3b95..7fb044bbc 100644 --- a/src/lib/symbol.hpp +++ b/src/lib/symbol.hpp @@ -25,7 +25,7 @@ ** ** @todo for the (currently just planned as of 11/08) rules based configuration ** in the Proc-Layer a explicit Symbol datatype will probably very helpful. - ** For now we just a typedef is sufficient. A real Symbol datatype should + ** For now just a typedef is sufficient. A real Symbol datatype should ** - be definable by string constant ** - integrate smoothly with std::string ** - provide a unique numeric index for each distinct Symbol diff --git a/tests/lib/format-string-test.cpp b/tests/lib/format-string-test.cpp index 34bf3a2c2..ec3c3e5cc 100644 --- a/tests/lib/format-string-test.cpp +++ b/tests/lib/format-string-test.cpp @@ -24,6 +24,7 @@ #include "lib/test/run.hpp" #include "lib/test/test-helper.hpp" #include "lib/format-string.hpp" +#include "lib/symbol.hpp" #include "lib/error.hpp" #include "lib/util.hpp" @@ -180,6 +181,24 @@ namespace test { CHECK (_Fmt("%10s") % str == " Lumiera" ); CHECK (_Fmt("%7.4s") %str == " Lumi" ); CHECK (_Fmt("%10c") % str == " L" ); + + const char* pch("edit"); + CHECK (_Fmt("%s") % pch == "edit" ); + CHECK (_Fmt("%10s") % pch == " edit" ); + CHECK (_Fmt("%7.3s") %pch == " edi" ); + CHECK (_Fmt("%10c") % pch == " e" ); + + lib::Literal lit("your"); + CHECK (_Fmt("%s") % lit == "your" ); + CHECK (_Fmt("%10s") % lit == " your" ); + CHECK (_Fmt("%7.2s") %lit == " yo" ); + CHECK (_Fmt("%10c") % lit == " y" ); + + lib::Symbol sym("freedom"); + CHECK (_Fmt("%s") % sym == "freedom" ); + CHECK (_Fmt("%10s") % sym == " freedom" ); + CHECK (_Fmt("%7.5s") %sym == " freed" ); + CHECK (_Fmt("%10c") % sym == " f" ); } @@ -248,6 +267,9 @@ namespace test { x.i_ = 42; CHECK (_Fmt("!!%s!!") % rv == "!!Number-042!!"); CHECK (_Fmt("!!%s!!") % x == "!!Number-042!!"); + + lib::Symbol sym("42"); + CHECK (_Fmt("!!%s!!") % sym == "!!42!!"); // but especially Symbol datatype is explicitly treated like a string }