From 1cc1b83b6f12e615adbb1ca47f43e178ee702113 Mon Sep 17 00:00:00 2001 From: Christoffer Lerno Date: Sat, 7 Sep 2024 14:34:30 +0200 Subject: [PATCH] `format` functions are now functions and work better with splat. --- lib/std/core/string.c3 | 26 +++++++++++++------------- releasenotes.md | 1 + 2 files changed, 14 insertions(+), 13 deletions(-) diff --git a/lib/std/core/string.c3 b/lib/std/core/string.c3 index 45faf36c5..f5eeecf34 100644 --- a/lib/std/core/string.c3 +++ b/lib/std/core/string.c3 @@ -38,10 +38,10 @@ fault NumberConversion * * @param [in] fmt `The formatting string` **/ -macro ZString tformat_zstr(String fmt, ...) +fn ZString tformat_zstr(String fmt, args...) { - DString str = dstring::temp_with_capacity(fmt.len + $vacount * 8); - str.appendf(fmt, $vasplat); + DString str = dstring::temp_with_capacity(fmt.len + args.len * 8); + str.appendf(fmt, ...args); return str.zstr_view(); } @@ -51,12 +51,12 @@ macro ZString tformat_zstr(String fmt, ...) * @param [inout] allocator `The allocator to use` * @param [in] fmt `The formatting string` **/ -macro String format(String fmt, ..., Allocator allocator) +fn String format(String fmt, args..., Allocator allocator) { @pool(allocator) { - DString str = dstring::temp_with_capacity(fmt.len + $vacount * 8); - str.appendf(fmt, $vasplat); + DString str = dstring::temp_with_capacity(fmt.len + args.len * 8); + str.appendf(fmt, ...args); return str.copy_str(allocator); }; } @@ -66,17 +66,17 @@ macro String format(String fmt, ..., Allocator allocator) * * @param [in] fmt `The formatting string` **/ -macro String new_format(String fmt, ..., Allocator allocator = null) => format(fmt, $vasplat, allocator: allocator ?: allocator::heap()); +fn String new_format(String fmt, args..., Allocator allocator = null) => format(fmt, ...args, allocator: allocator ?: allocator::heap()); /** * Return a temporary String created using the formatting function. * * @param [in] fmt `The formatting string` **/ -macro String tformat(String fmt, ...) +fn String tformat(String fmt, args...) { - DString str = dstring::temp_with_capacity(fmt.len + $vacount * 8); - str.appendf(fmt, $vasplat); + DString str = dstring::temp_with_capacity(fmt.len + args.len * 8); + str.appendf(fmt, ...args); return str.str_view(); } @@ -86,12 +86,12 @@ macro String tformat(String fmt, ...) * @param [in] fmt `The formatting string` * @param [inout] allocator `The allocator to use` **/ -macro ZString new_format_zstr(String fmt, ..., Allocator allocator = allocator::heap()) +fn ZString new_format_zstr(String fmt, args..., Allocator allocator = allocator::heap()) { @pool(allocator) { - DString str = dstring::temp_with_capacity(fmt.len + $vacount * 8); - str.appendf(fmt, $vasplat); + DString str = dstring::temp_with_capacity(fmt.len + args.len * 8); + str.appendf(fmt, ...args); return str.copy_zstr(allocator); }; } diff --git a/releasenotes.md b/releasenotes.md index a0903fc10..2fdb8c2b8 100644 --- a/releasenotes.md +++ b/releasenotes.md @@ -18,6 +18,7 @@ ### Stdlib changes - Additional init functions for hashmap. +- `format` functions are now functions and work better with splat. ## 0.6.2 Change list