From: Brendan Hansen Date: Wed, 8 Mar 2023 14:36:39 +0000 (-0600) Subject: refactored: `conv.format` to better use `dyn_str` X-Git-Url: https://git.brendanfh.com/?a=commitdiff_plain;h=b40a6e3c704f1b81f1ab81988ce0dbd459c182e9;p=onyx.git refactored: `conv.format` to better use `dyn_str` --- diff --git a/core/conv/format.onyx b/core/conv/format.onyx index 3c0dc145..1d89927b 100644 --- a/core/conv/format.onyx +++ b/core/conv/format.onyx @@ -181,7 +181,7 @@ Format :: struct { } #local -flush_to_dynstr :: (dynstr: &[..] u8, to_write: str) => { +flush_to_dyn_str :: (dynstr: &dyn_str, to_write: str) => { array.concat(dynstr, to_write); return true; } @@ -210,25 +210,22 @@ format :: (output: &Format_Output, format: str, va: ..any) -> str { } #overload -format :: (buffer: &[..] u8, format: str, va: ..any) { - buffer.count = buffer.capacity; - out := format_va(*buffer, format, ~~va); - buffer.count = out.count; +format :: (format: str, va: ..any) -> str { + out := make(dyn_str); + return format_va(&out, format, ~~va); } #overload -format :: (format: str, va: ..any) -> str { - buffer : [256] u8; - out := make([..] u8); +format :: (buffer: &dyn_str, format: str, va: ..any) -> str { + internal_buffer : [256] u8; output := Format_Output.{ - ~~buffer, 0, buffer.count, - flush=.{ &out, flush_to_dynstr } + ~~internal_buffer, 0, internal_buffer.count, + flush=.{ buffer, flush_to_dyn_str } }; final := format_va(&output, format, ~~va); - array.concat(&out, final); - - return out; + string.concat(buffer, final); + return *buffer; } // @@ -242,25 +239,22 @@ format_va :: (buffer: [] u8, format: str, va: [] any, flush := Format_Flush_Call } #overload -format_va :: (buffer: &[..] u8, format: str, va: [] any, flush := Format_Flush_Callback.{}) { - buffer.count = buffer.capacity; - out := format_va(*buffer, format, va, flush); - buffer.count = out.count; +format_va :: (format: [] u8, va: [] any, allocator := context.allocator) -> str { + out := make(dyn_str, allocator=allocator); + return format_va(&out, format, ~~va); } #overload -format_va :: (format: [] u8, va: [] any, allocator := context.allocator) -> str { - buffer : [256] u8; - out := make([..] u8, allocator=allocator); +format_va :: (buffer: &dyn_str, format: str, va: [] any) -> str { + internal_buffer : [256] u8; output := Format_Output.{ - ~~buffer, 0, buffer.count, - flush=.{ &out, flush_to_dynstr } + ~~internal_buffer, 0, internal_buffer.count, + flush=.{ buffer, flush_to_dyn_str } }; final := format_va(&output, format, ~~va); - array.concat(&out, final); - - return out; + string.concat(buffer, final); + return *buffer; } #overload diff --git a/tests/dyn_str b/tests/dyn_str index 8ab686ea..e179f4d4 100644 --- a/tests/dyn_str +++ b/tests/dyn_str @@ -1 +1,2 @@ Hello, World! +This is another sentence. diff --git a/tests/dyn_str.onyx b/tests/dyn_str.onyx index 1918c79b..0169f808 100644 --- a/tests/dyn_str.onyx +++ b/tests/dyn_str.onyx @@ -12,4 +12,12 @@ main :: () { string.insert(&output, 5, ", "); println(output); + + + // `conv.format` appends to the dynamic string. Use + // `string.clear` to empty it first. + string.clear(&output); + conv.format(&output, "This is another {}.", "sentence"); + + println(output); } \ No newline at end of file