From a459bc13dc9554054c0e0140ec29fa08050b8da9 Mon Sep 17 00:00:00 2001 From: ChuuniMage Date: Fri, 21 Oct 2022 10:23:10 +1100 Subject: [PATCH] Add caprintf and ctprintf to fmt Formatted cstring procs to work with ubiquitous cstring APIs --- core/fmt/fmt.odin | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/core/fmt/fmt.odin b/core/fmt/fmt.odin index 7429a093d..037cc20fc 100644 --- a/core/fmt/fmt.odin +++ b/core/fmt/fmt.odin @@ -162,7 +162,25 @@ panicf :: proc(fmt: string, args: ..any, loc := #caller_location) -> ! { p("Panic", message, loc) } +// formatted printing for cstrings +caprintf :: proc(format: string, args: ..any) -> cstring { + str: strings.Builder + strings.builder_init(&str) + fmt.sbprintf(&str, format, ..args) + strings.write_byte(&str, 0) + s := strings.to_string(str) + return cstring(raw_data(s)) +} +// c string with temp allocator +ctprintf :: proc(format: string, args: ..any) -> cstring { + str: strings.Builder + strings.builder_init(&str, context.temp_allocator) + fmt.sbprintf(&str, format, ..args) + strings.write_byte(&str, 0) + s := strings.to_string(str) + return cstring(raw_data(s)) +} // sbprint formats using the default print settings and writes to buf sbprint :: proc(buf: ^strings.Builder, args: ..any, sep := " ") -> string {