summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDan Goodliffe <dan@randomdan.homeip.net>2016-12-09 16:28:48 +0000
committerDan Goodliffe <dan@randomdan.homeip.net>2016-12-09 16:28:48 +0000
commit02f4420399a388f4c7cca0e9ab87ecf8e4286e08 (patch)
tree1a08600060cde4165a8168252f8c3ab16c34f19d
parentCustom stop chars are never used (diff)
downloadlibadhocutil-02f4420399a388f4c7cca0e9ab87ecf8e4286e08.tar.bz2
libadhocutil-02f4420399a388f4c7cca0e9ab87ecf8e4286e08.tar.xz
libadhocutil-02f4420399a388f4c7cca0e9ab87ecf8e4286e08.zip
Provide a way of formatting a literal percent sign
-rw-r--r--libadhocutil/compileTimeFormatter.h11
-rw-r--r--libadhocutil/unittests/testCompileTimeFormatter.cpp34
2 files changed, 44 insertions, 1 deletions
diff --git a/libadhocutil/compileTimeFormatter.h b/libadhocutil/compileTimeFormatter.h
index d9349dc..e17270a 100644
--- a/libadhocutil/compileTimeFormatter.h
+++ b/libadhocutil/compileTimeFormatter.h
@@ -80,6 +80,17 @@ namespace AdHoc {
}
};
+ // Escaped % stream writer formatter
+ template<const char * const & S, int start, typename stream, char ... sn>
+ struct StreamWriter<S, start, stream, '%', '%', sn...> {
+ template<typename ... Pn>
+ static void write(stream & s, const Pn & ... pn)
+ {
+ s << '%';
+ StreamWriter<S, start + 2, stream, sn...>::write(s, pn...);
+ }
+ };
+
// Unknown stream writer formatter
template<const char * const & S, int start, typename stream, char ... sn>
struct StreamWriter<S, start, stream, '%', sn...> {
diff --git a/libadhocutil/unittests/testCompileTimeFormatter.cpp b/libadhocutil/unittests/testCompileTimeFormatter.cpp
index ffb528b..b3e3dac 100644
--- a/libadhocutil/unittests/testCompileTimeFormatter.cpp
+++ b/libadhocutil/unittests/testCompileTimeFormatter.cpp
@@ -16,6 +16,10 @@ extern constexpr const char * formatEdgeCaseFormatLonely = "%?";
extern constexpr const char * formatStringLiteral = "literal";
extern constexpr const char * formatStringSingle = "single %?.";
extern constexpr const char * formatStringMulti = "First %?, then %?.";
+extern constexpr const char * formatStringEscape1 = "literal %% percentage.";
+extern constexpr const char * formatStringEscape2 = "literal %%? percentage.";
+extern constexpr const char * formatStringEscape3 = "literal %%%? percentage.";
+extern constexpr const char * formatStringEscape4 = "literal %%%?%% percentage.";
BOOST_AUTO_TEST_CASE ( empty )
{
@@ -84,10 +88,38 @@ BOOST_AUTO_TEST_CASE ( singlePath )
BOOST_AUTO_TEST_CASE ( multi )
{
std::stringstream buf;
- Formatter<formatStringMulti>::write(buf, "one", "two");
+ Formatter<formatStringMulti>::write(buf, "one", "two");
BOOST_REQUIRE_EQUAL(buf.str(), "First one, then two.");
}
+BOOST_AUTO_TEST_CASE ( escape1 )
+{
+ std::stringstream buf;
+ Formatter<formatStringEscape1>::write(buf);
+ BOOST_REQUIRE_EQUAL(buf.str(), "literal % percentage.");
+}
+
+BOOST_AUTO_TEST_CASE ( escape2 )
+{
+ std::stringstream buf;
+ Formatter<formatStringEscape2>::write(buf);
+ BOOST_REQUIRE_EQUAL(buf.str(), "literal %? percentage.");
+}
+
+BOOST_AUTO_TEST_CASE ( escape3 )
+{
+ std::stringstream buf;
+ Formatter<formatStringEscape3>::write(buf, 3);
+ BOOST_REQUIRE_EQUAL(buf.str(), "literal %3 percentage.");
+}
+
+BOOST_AUTO_TEST_CASE ( escape4 )
+{
+ std::stringstream buf;
+ Formatter<formatStringEscape4>::write(buf, 3);
+ BOOST_REQUIRE_EQUAL(buf.str(), "literal %3% percentage.");
+}
+
namespace AdHoc {
// Custom stream writer formatter, formats as (bracketed expression)
template<const char * const & S, int start, typename stream, char ... sn>