make the dash flag toggle current escaping mode in format

master
Daniel Kolesa 2017-02-25 17:33:18 +01:00
parent 7b65f8b64e
commit e90b0868aa
2 changed files with 25 additions and 5 deletions

View File

@ -75,7 +75,7 @@ int main() {
* items are escaped automatically (strings and chars)
*/
writefln("{ %#(%s: %d, %) }", m);
/* the - flag overrides escaping, so you get { baz: 15, bar: 10, foo: 5} */
/* the - flag toggles escaping, so you get { baz: 15, bar: 10, foo: 5} */
writefln("{ %-#(%s: %d, %) }", m);
/* no expansion of the items, print entire tuple with default format,
* gets you something like { <"baz", 15>, <"bar", 10>, <"foo", 5> }
@ -83,12 +83,21 @@ int main() {
*/
writefln("{ %(%s, %) }", m);
/* as the - flag toggles escaping on strings and chars, when auto-escape
* is turned on for some reason, you can toggle it for specific items,
* and you can even manually force escaping on things
*/
writefln("not escaped: %s, escaped: %-s", "foo", "bar");
/* you can expand tuples similarly to ranges, with %<CONTENTS%> where
* CONTENTS is a regular format string like if the tuple was formatted
* separately with each item of the tuple passed as a separate argument
* separately with each item of the tuple passed as a separate argument,
* keep in mind that this auto-escapes strings/chars just like ranges
*/
std::tuple<std::string, int, float> tup{"hello world", 1337, 3.14f};
writefln("the tuple contains %<%s, %d, %f%>.", tup);
std::tuple<std::string, int, float, std::string> tup{
"hello world", 1337, 3.14f, "test"
};
writefln("the tuple contains %<%s, %d, %f, %-s%>.", tup);
/* formatting a range of tuples, with each tuple expanded
* with a nested tuple expansion inside a range expansion

View File

@ -319,7 +319,12 @@ private:
bool read_spec_range(bool tuple = false) {
int sflags = p_flags;
p_nested_escape = !(sflags & FMT_FLAG_DASH);
/* printing ranges or tuples toggles escaping mode by default,
* but as the dash flag also toggles, it means no change
*/
if (!(sflags & FMT_FLAG_DASH)) {
p_nested_escape = !p_nested_escape;
}
p_fmt.pop_front();
string_range begin_inner(p_fmt);
if (!read_until_dummy()) {
@ -486,6 +491,9 @@ private:
if (has_precision()) {
n = std::min(n, size_t(precision()));
}
if (p_flags & FMT_FLAG_DASH) {
escape = !escape;
}
write_spaces(writer, n, true);
if (escape) {
writer.put('"');
@ -512,6 +520,9 @@ private:
/* char values */
template<typename R>
void write_char(R &writer, bool escape, char val) const {
if (p_flags & FMT_FLAG_DASH) {
escape = !escape;
}
if (escape) {
char const *esc = detail::escape_fmt_char(val, '\'');
if (esc) {