diff options
| -rw-r--r-- | include/cxxformat/formatters.hpp | 43 | ||||
| -rw-r--r-- | main.cpp | 3 |
2 files changed, 43 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"); } }; @@ -30,6 +30,9 @@ int main(int argc, char* argv[]) format_to("Hallo %%du %1$s %s%c %s %u = %4$#x = %4$#X = %4$#o %f!\n", std::cout, "Knirp", 's', argv[0], 42u, 1.337); assert(format("Hallo %%du %1$s %s%c %s %u = %4$#x = %4$#X = %4$#o %f!\n", "Knirp", 's', argv[0], 42u, 1.337) == "Hallo %%du %1$s %s%c %s %u = %4$#x = %4$#X = %4$#o %f!\n"_format("Knirp", 's', argv[0], 42u, 1.337)); + "-%20s-\n"_format_to(stdout, "Hallo"); + "-%'20s-\n"_format_to(stdout, R"(H\"all"o)"); + "-%'-20s-\n"_format_to(stdout, R"(H\"all"o)"); "-%6s--%6s--%6u--%6u-\n"_format_to(stdout, true, false, true, false); "-%'030f-\n"_format_to(stdout, 1'000'000.337); "-%'030a-\n"_format_to(stdout, 1'000'000.337); |
