forked from OctaForge/libostd
make the dash flag toggle current escaping mode in format
parent
7b65f8b64e
commit
e90b0868aa
|
@ -75,7 +75,7 @@ int main() {
|
||||||
* items are escaped automatically (strings and chars)
|
* items are escaped automatically (strings and chars)
|
||||||
*/
|
*/
|
||||||
writefln("{ %#(%s: %d, %) }", m);
|
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);
|
writefln("{ %-#(%s: %d, %) }", m);
|
||||||
/* no expansion of the items, print entire tuple with default format,
|
/* no expansion of the items, print entire tuple with default format,
|
||||||
* gets you something like { <"baz", 15>, <"bar", 10>, <"foo", 5> }
|
* gets you something like { <"baz", 15>, <"bar", 10>, <"foo", 5> }
|
||||||
|
@ -83,12 +83,21 @@ int main() {
|
||||||
*/
|
*/
|
||||||
writefln("{ %(%s, %) }", m);
|
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
|
/* you can expand tuples similarly to ranges, with %<CONTENTS%> where
|
||||||
* CONTENTS is a regular format string like if the tuple was formatted
|
* 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};
|
std::tuple<std::string, int, float, std::string> tup{
|
||||||
writefln("the tuple contains %<%s, %d, %f%>.", 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
|
/* formatting a range of tuples, with each tuple expanded
|
||||||
* with a nested tuple expansion inside a range expansion
|
* with a nested tuple expansion inside a range expansion
|
||||||
|
|
|
@ -319,7 +319,12 @@ private:
|
||||||
|
|
||||||
bool read_spec_range(bool tuple = false) {
|
bool read_spec_range(bool tuple = false) {
|
||||||
int sflags = p_flags;
|
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();
|
p_fmt.pop_front();
|
||||||
string_range begin_inner(p_fmt);
|
string_range begin_inner(p_fmt);
|
||||||
if (!read_until_dummy()) {
|
if (!read_until_dummy()) {
|
||||||
|
@ -486,6 +491,9 @@ private:
|
||||||
if (has_precision()) {
|
if (has_precision()) {
|
||||||
n = std::min(n, size_t(precision()));
|
n = std::min(n, size_t(precision()));
|
||||||
}
|
}
|
||||||
|
if (p_flags & FMT_FLAG_DASH) {
|
||||||
|
escape = !escape;
|
||||||
|
}
|
||||||
write_spaces(writer, n, true);
|
write_spaces(writer, n, true);
|
||||||
if (escape) {
|
if (escape) {
|
||||||
writer.put('"');
|
writer.put('"');
|
||||||
|
@ -512,6 +520,9 @@ private:
|
||||||
/* char values */
|
/* char values */
|
||||||
template<typename R>
|
template<typename R>
|
||||||
void write_char(R &writer, bool escape, char val) const {
|
void write_char(R &writer, bool escape, char val) const {
|
||||||
|
if (p_flags & FMT_FLAG_DASH) {
|
||||||
|
escape = !escape;
|
||||||
|
}
|
||||||
if (escape) {
|
if (escape) {
|
||||||
char const *esc = detail::escape_fmt_char(val, '\'');
|
char const *esc = detail::escape_fmt_char(val, '\'');
|
||||||
if (esc) {
|
if (esc) {
|
||||||
|
|
Loading…
Reference in New Issue