made it easier to use custom formatters
authorBrendan Hansen <brendan.f.hansen@gmail.com>
Mon, 3 Jan 2022 04:34:52 +0000 (22:34 -0600)
committerBrendan Hansen <brendan.f.hansen@gmail.com>
Mon, 3 Jan 2022 04:34:52 +0000 (22:34 -0600)
core/container/map.onyx
core/conv.onyx
core/io/writer.onyx
tests/interfaces.onyx

index ff36f1b5284655b767769cefec02e451ec3a1b99..8d0e6b68443cc3d6702dbd0ad5238603a27d1f22 100644 (file)
@@ -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("}");
 }
index a29fc0a19f8e991e356c9bde7acdc97a5afa3b38..55600cbf575dca0673f1eaea8a47e8c94cf4481c 100644 (file)
@@ -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;
                     }
index 05766b12d2524a2fbcf5c28fd29917a4d39174f9..c80a5953364392be0468c7cc5114e2a77a55df17 100644 (file)
@@ -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);
     }
 }
index f113a483898412dde3237203762e126372ac6f50..33e18fd811f3d89fcb2da327f83e8b36e1d02634 100644 (file)
@@ -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 };