From efc7278fb065d873eca54563c5b89f2768237564 Mon Sep 17 00:00:00 2001 From: Markus Mittendrein Date: Tue, 11 Jan 2022 03:20:17 +0100 Subject: Add ' flag for strings to quote and escape them --- include/cxxformat/formatters.hpp | 43 +++++++++++++++++++++++++++++++++++++--- 1 file changed, 40 insertions(+), 3 deletions(-) (limited to 'include') diff --git a/include/cxxformat/formatters.hpp b/include/cxxformat/formatters.hpp index b9d6fcc..31f0fef 100644 --- a/include/cxxformat/formatters.hpp +++ b/include/cxxformat/formatters.hpp @@ -590,7 +590,46 @@ namespace format { val = val.substr(0, *precision); } - formatPadded(out, val, ' ', spec.flags.leftJustified, minWidth); + if (spec.flags.quote) + { + const auto quoteExtra = std::count_if(val.begin(), val.end(), "\"\\"_contains) + 2; + std::size_t padding = 0; + if (minWidth) + { + const auto totalLength = val.size() + quoteExtra; + if (*minWidth > totalLength) + { + padding = *minWidth - totalLength; + } + } + if (!spec.flags.leftJustified) + { + out(' ', padding); + } + + out('"'); + for (std::size_t pos = 0, startPos = 0; pos < val.size(); ) + { + pos = val.find_first_of("\"\\", pos); + out(val.substr(startPos, pos - startPos)); + if (pos != std::string_view::npos) + { + out('\\'); + startPos = pos; + ++pos; + } + } + out('"'); + + if (spec.flags.leftJustified) + { + out(' ', padding); + } + } + else + { + formatPadded(out, val, ' ', spec.flags.leftJustified, minWidth); + } } static constexpr void conversionSupported(format_specifier spec) @@ -599,7 +638,6 @@ namespace format { noAlternative(spec.flags.alternative, "stringy types"); noSignFlags(spec.addSign, "stringy types"); noZeroPadding(spec.padding, "stringy types"); - noQuote(spec.flags.quote, "stringy types"); } }; @@ -677,7 +715,6 @@ namespace format { noAlternative(spec.flags.alternative, "char pointer"); noSignFlags(spec.addSign, "char pointer"); noZeroPadding(spec.padding, "char pointer"); - noQuote(spec.flags.quote, "char pointer"); } }; -- cgit v1.2.3-54-g00ecf