aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorChuuniMage <krisdabrowski1992@gmail.com>2022-10-21 10:23:10 +1100
committerGitHub <noreply@github.com>2022-10-21 10:23:10 +1100
commita459bc13dc9554054c0e0140ec29fa08050b8da9 (patch)
treeeffb988ae35451d4950458d2aea0df2ed0d42129
parent53e84b7f31a218102034c60e157b60d22adcf303 (diff)
Add caprintf and ctprintf to fmt
Formatted cstring procs to work with ubiquitous cstring APIs
-rw-r--r--core/fmt/fmt.odin18
1 files changed, 18 insertions, 0 deletions
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 {