refactored: `conv.format` to better use `dyn_str`
authorBrendan Hansen <brendan.f.hansen@gmail.com>
Wed, 8 Mar 2023 14:36:39 +0000 (08:36 -0600)
committerBrendan Hansen <brendan.f.hansen@gmail.com>
Wed, 8 Mar 2023 14:36:39 +0000 (08:36 -0600)
core/conv/format.onyx
tests/dyn_str
tests/dyn_str.onyx

index 3c0dc1459a71b5fd6d5bc5a52462bfbc623b8cab..1d89927bf1f888904eb2c656dc3b04a2b16e7bf6 100644 (file)
@@ -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
index 8ab686eafeb1f44702738c8b0f24f2567c36da6d..e179f4d4474fb88e7e56dc19fb028691e2dcf5b0 100644 (file)
@@ -1 +1,2 @@
 Hello, World!
+This is another sentence.
index 1918c79b8b094814f11cb9cb2444370180c2a275..0169f8083d81ad79beee43857622909a7ef7195b 100644 (file)
@@ -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