diff options
Diffstat (limited to 'include')
| -rw-r--r-- | include/cxxformat/formatters.hpp | 43 |
1 files changed, 40 insertions, 3 deletions
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"); } }; |
