From 4273c703e9d43c9dec80b3b69b2d25b52aaf07f1 Mon Sep 17 00:00:00 2001 From: Josh Holtrop Date: Sat, 21 Oct 2023 16:45:41 -0400 Subject: [PATCH] writef: split into writef() and writefv() --- src/hello/console.d | 4 ++-- src/hulk/klog.d | 4 ++-- src/hulk/serial.d | 4 ++-- src/hulk/writef.d | 21 +++++++++++++++++++-- 4 files changed, 25 insertions(+), 8 deletions(-) diff --git a/src/hello/console.d b/src/hello/console.d index e811aed..28b4f37 100644 --- a/src/hello/console.d +++ b/src/hello/console.d @@ -33,9 +33,9 @@ struct Console */ public static void writef(string s, va_list args) { - hulk.writef.writef(s, args, function void(ubyte ch) { + hulk.writef.writefv(function void(ubyte ch) { Console.write(ch); - }); + }, s, args); } /** diff --git a/src/hulk/klog.d b/src/hulk/klog.d index 3153ab2..420583c 100644 --- a/src/hulk/klog.d +++ b/src/hulk/klog.d @@ -40,9 +40,9 @@ struct Klog */ public static void writef(string s, va_list args) { - hulk.writef.writef(s, args, function void(ubyte ch) { + hulk.writef.writefv(function void(ubyte ch) { Console.write(ch); - }); + }, s, args); } /** diff --git a/src/hulk/serial.d b/src/hulk/serial.d index 6249ae4..017dbe0 100644 --- a/src/hulk/serial.d +++ b/src/hulk/serial.d @@ -40,9 +40,9 @@ struct Serial */ public static void writef(string s, va_list args) { - hulk.writef.writef(s, args, function void(ubyte ch) { + hulk.writef.writefv(function void(ubyte ch) { Serial.write(ch); - }); + }, s, args); } /** diff --git a/src/hulk/writef.d b/src/hulk/writef.d index d39d1ec..8c4408f 100644 --- a/src/hulk/writef.d +++ b/src/hulk/writef.d @@ -10,13 +10,13 @@ alias ch_out_fn = void function(ubyte); /** * Format a string and write characters to the given output function. * + * @param ch_out Character output function. * @param s Format string. * @param args Variable arguments structure. - * @param ch_out Character output function. * * @return Number of characters written. */ -size_t writef(string s, va_list args, ch_out_fn ch_out) +size_t writefv(ch_out_fn ch_out, string s, va_list args) { size_t length_written; bool escape = false; @@ -90,6 +90,23 @@ size_t writef(string s, va_list args, ch_out_fn ch_out) return length_written; } +/** + * Format a string and write characters to the given output function. + * + * @param ch_out Character output function. + * @param s Format string. + * + * @return Number of characters written. + */ +extern(C) size_t writef(ch_out_fn ch_out, string s, ...) +{ + va_list args; + va_start(args, s); + size_t rv = writefv(ch_out, s, args); + va_end(args); + return rv; +} + /** * Format a value in hexadecimal to the given output function. *