From 591788f1b9075eb6fe08be593127147aa86ec1ef Mon Sep 17 00:00:00 2001 From: Brendan Hansen Date: Sun, 2 Jan 2022 22:34:52 -0600 Subject: [PATCH] made it easier to use custom formatters --- core/container/map.onyx | 12 ++---------- core/conv.onyx | 20 +++++++++++++++++--- core/io/writer.onyx | 3 ++- tests/interfaces.onyx | 9 ++++++--- 4 files changed, 27 insertions(+), 17 deletions(-) diff --git a/core/container/map.onyx b/core/container/map.onyx index ff36f1b5..8d0e6b68 100644 --- a/core/container/map.onyx +++ b/core/container/map.onyx @@ -148,17 +148,9 @@ empty :: (use map: ^Map($K, $V)) -> bool { } format_map :: (output: ^conv.Format_Output, format: ^conv.Format, x: ^Map($K, $V)) { - old_quote_strings := format.quote_strings; - format.quote_strings = true; - defer format.quote_strings = old_quote_strings; - output->write("{\n"); - for^ e: x.entries { - output->write(" "); - conv.format_any(output, format, .{^e.key, K}); - output->write(" => "); - conv.format_any(output, format, .{^e.value, V}); - output->write("\n"); + for^ x.entries { + conv.format(output, " {\"} => {\"}\n", it.key, it.value); } output->write("}"); } diff --git a/core/conv.onyx b/core/conv.onyx index a29fc0a1..55600cbf 100644 --- a/core/conv.onyx +++ b/core/conv.onyx @@ -271,12 +271,21 @@ Format :: struct { } -format :: (buffer: [] u8, format: str, va: ..any) -> str { +format :: #match {} +#match format (buffer: [] u8, format: str, va: ..any) -> str { return format_va(buffer, format, ~~va); } +#match format (output: ^Format_Output, format: str, va: ..any) -> str { + return format_va(output, format, ~~va); +} -format_va :: (buffer: [] u8, format: str, va: [] any) -> str { +format_va :: #match {} +#match format_va (buffer: [] u8, format: str, va: [] any) -> str { output := Format_Output.{ buffer.data, 0, buffer.count }; + return format_va(^output, format, va); +} + +#match format_va (output: ^Format_Output, format: str, va: [] any) -> str { vararg_index := 0; while i := 0; i < format.count { @@ -357,10 +366,15 @@ format_va :: (buffer: [] u8, format: str, va: [] any) -> str { formatting.custom_format = false; } + case #char "\"" { + i += 1; + formatting.quote_strings = true; + } + case #char "}" { arg := va[vararg_index]; vararg_index += 1; - format_any(^output, ^formatting, arg); + format_any(output, ^formatting, arg); break break; } diff --git a/core/io/writer.onyx b/core/io/writer.onyx index 05766b12..c80a5953 100644 --- a/core/io/writer.onyx +++ b/core/io/writer.onyx @@ -127,7 +127,8 @@ write :: #match { write_format, // Catch all for any type. Has a high precedence so you can easily override it. - #precedence 1000 (w: ^Writer, a: $T) { + #precedence 1000 macro (w: ^Writer, a: $T) { + write_format :: write_format write_format(w, "{}", a); } } diff --git a/tests/interfaces.onyx b/tests/interfaces.onyx index f113a483..33e18fd8 100644 --- a/tests/interfaces.onyx +++ b/tests/interfaces.onyx @@ -53,9 +53,12 @@ BitField :: interface (T: type_expr) { T ^ T; } -Complex :: struct { x, y: f32; } -#match io.write (w: ^io.Writer, c: Complex) { - io.write_format(w, "{.2} + {.2}i", c.x, c.y); +Complex :: struct [conv.Custom_Format.{ format }] { + x, y: f32; + + format :: (output: ^conv.Format_Output, format: ^conv.Format, c: ^Complex) { + conv.format(output, "{.2} + {.2}i", c.x, c.y); + } } #operator + (c1, c2: Complex) => Complex.{ c1.x + c2.x, c1.y + c2.y }; -- 2.25.1