Add caprintf and ctprintf to fmt

Formatted cstring procs to work with ubiquitous cstring APIs
This commit is contained in:
ChuuniMage
2022-10-21 10:23:10 +11:00
committed by GitHub
parent 53e84b7f31
commit a459bc13dc

View File

@@ -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 {