diff options
| author | Markus Mittendrein <maxmitti@maxmitti.tk> | 2022-01-11 03:20:17 +0100 |
|---|---|---|
| committer | Markus Mittendrein <maxmitti@maxmitti.tk> | 2022-01-11 03:20:17 +0100 |
| commit | efc7278fb065d873eca54563c5b89f2768237564 (patch) | |
| tree | d7ce92c949b77531304c04fb36d9b8882ffafbbc | |
| parent | 21ef5d530404aff3c87d6bbc0cfc2890cb1045d8 (diff) | |
| download | cxxformat-efc7278fb065d873eca54563c5b89f2768237564.tar.gz cxxformat-efc7278fb065d873eca54563c5b89f2768237564.zip | |
Add ' flag for strings to quote and escape them
| -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); |
