From 8f2c4a7ecfb268adbeac174cb74d029f09b966a4 Mon Sep 17 00:00:00 2001 From: Justin Snyder Date: Wed, 19 Jun 2024 11:55:19 -0600 Subject: [PATCH 1/4] Add builder to_cstring --- core/strings/builder.odin | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) diff --git a/core/strings/builder.odin b/core/strings/builder.odin index 11885b689..515e5141e 100644 --- a/core/strings/builder.odin +++ b/core/strings/builder.odin @@ -286,6 +286,33 @@ to_string :: proc(b: Builder) -> (res: string) { return string(b.buf[:]) } /* +Appends a trailing null byte if missing and then casts the Builder byte buffer to cstring + +Inputs: +- b: A pointer to builder + +Returns: +- res: A cstring of the Builder's buffer +*/ +to_cstring :: proc(b: ^Builder) -> (res: cstring) { + if b.buf[len(b.buf)-1] != 0 { + append(&b.buf, 0) + } + return cstring(raw_data(b.buf)) +} +/* +Casts the Builder byte buffer to cstring, assuming it is already null-terminated + +Inputs: +- b: A pointer to builder + +Returns: +- res: A cstring of the Builder's buffer +*/ +to_cstring_unsafe :: proc(b: Builder) -> (res: cstring) { + return cstring(raw_data(b.buf)) +} +/* Returns the length of the Builder's buffer, in bytes Inputs: From 1b7c4c2efe061a06a6f91ee040fc179340190afc Mon Sep 17 00:00:00 2001 From: Justin Snyder Date: Wed, 19 Jun 2024 17:35:16 -0600 Subject: [PATCH 2/4] trim added null byte --- core/strings/builder.odin | 19 +++---------------- 1 file changed, 3 insertions(+), 16 deletions(-) diff --git a/core/strings/builder.odin b/core/strings/builder.odin index 515e5141e..32c96ebbd 100644 --- a/core/strings/builder.odin +++ b/core/strings/builder.odin @@ -286,7 +286,7 @@ to_string :: proc(b: Builder) -> (res: string) { return string(b.buf[:]) } /* -Appends a trailing null byte if missing and then casts the Builder byte buffer to cstring +Appends a trailing null byte after the end of the current Builder byte buffer and then casts it to a cstring Inputs: - b: A pointer to builder @@ -295,21 +295,8 @@ Returns: - res: A cstring of the Builder's buffer */ to_cstring :: proc(b: ^Builder) -> (res: cstring) { - if b.buf[len(b.buf)-1] != 0 { - append(&b.buf, 0) - } - return cstring(raw_data(b.buf)) -} -/* -Casts the Builder byte buffer to cstring, assuming it is already null-terminated - -Inputs: -- b: A pointer to builder - -Returns: -- res: A cstring of the Builder's buffer -*/ -to_cstring_unsafe :: proc(b: Builder) -> (res: cstring) { + append(&b.buf, 0) + defer pop(&p.buf) return cstring(raw_data(b.buf)) } /* From 271782d2f43316c3b691946b9a5e8cf5f311ac36 Mon Sep 17 00:00:00 2001 From: Justin Snyder Date: Wed, 19 Jun 2024 17:53:34 -0600 Subject: [PATCH 3/4] fix typo apparently i had a moment of dyslexia --- core/strings/builder.odin | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/core/strings/builder.odin b/core/strings/builder.odin index 32c96ebbd..195eab56f 100644 --- a/core/strings/builder.odin +++ b/core/strings/builder.odin @@ -296,7 +296,7 @@ Returns: */ to_cstring :: proc(b: ^Builder) -> (res: cstring) { append(&b.buf, 0) - defer pop(&p.buf) + defer pop(&b.buf) return cstring(raw_data(b.buf)) } /* From 1a6bb5912559588a7823a363e279686b146f33fb Mon Sep 17 00:00:00 2001 From: Justin Snyder Date: Wed, 19 Jun 2024 18:25:42 -0600 Subject: [PATCH 4/4] drop unnecessary defer --- core/strings/builder.odin | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/core/strings/builder.odin b/core/strings/builder.odin index 195eab56f..3c5bc5793 100644 --- a/core/strings/builder.odin +++ b/core/strings/builder.odin @@ -296,7 +296,7 @@ Returns: */ to_cstring :: proc(b: ^Builder) -> (res: cstring) { append(&b.buf, 0) - defer pop(&b.buf) + pop(&b.buf) return cstring(raw_data(b.buf)) } /*