From c712de0cd07a6e7c5d34570228a2612326cc2acf Mon Sep 17 00:00:00 2001 From: Feoramund <161657516+Feoramund@users.noreply.github.com> Date: Sun, 28 Apr 2024 17:17:01 -0400 Subject: [PATCH 1/3] Require results for non-buffered `print` procs --- core/fmt/fmt.odin | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/core/fmt/fmt.odin b/core/fmt/fmt.odin index 018c66bd3..2209af6e2 100644 --- a/core/fmt/fmt.odin +++ b/core/fmt/fmt.odin @@ -120,6 +120,7 @@ register_user_formatter :: proc(id: typeid, formatter: User_Formatter) -> Regist // // Returns: A formatted string. // +@(require_results) aprint :: proc(args: ..any, sep := " ", allocator := context.allocator) -> string { str: strings.Builder strings.builder_init(&str, allocator) @@ -136,6 +137,7 @@ aprint :: proc(args: ..any, sep := " ", allocator := context.allocator) -> strin // // Returns: A formatted string with a newline character at the end. // +@(require_results) aprintln :: proc(args: ..any, sep := " ", allocator := context.allocator) -> string { str: strings.Builder strings.builder_init(&str, allocator) @@ -153,6 +155,7 @@ aprintln :: proc(args: ..any, sep := " ", allocator := context.allocator) -> str // // Returns: A formatted string. The returned string must be freed accordingly. // +@(require_results) aprintf :: proc(fmt: string, args: ..any, allocator := context.allocator, newline := false) -> string { str: strings.Builder strings.builder_init(&str, allocator) @@ -169,6 +172,7 @@ aprintf :: proc(fmt: string, args: ..any, allocator := context.allocator, newlin // // Returns: A formatted string. The returned string must be freed accordingly. // +@(require_results) aprintfln :: proc(fmt: string, args: ..any, allocator := context.allocator) -> string { return aprintf(fmt, ..args, allocator=allocator, newline=true) } @@ -182,6 +186,7 @@ aprintfln :: proc(fmt: string, args: ..any, allocator := context.allocator) -> s // // Returns: A formatted string. // +@(require_results) tprint :: proc(args: ..any, sep := " ") -> string { str: strings.Builder strings.builder_init(&str, context.temp_allocator) @@ -198,6 +203,7 @@ tprint :: proc(args: ..any, sep := " ") -> string { // // Returns: A formatted string with a newline character at the end. // +@(require_results) tprintln :: proc(args: ..any, sep := " ") -> string { str: strings.Builder strings.builder_init(&str, context.temp_allocator) @@ -215,6 +221,7 @@ tprintln :: proc(args: ..any, sep := " ") -> string { // // Returns: A formatted string. // +@(require_results) tprintf :: proc(fmt: string, args: ..any, newline := false) -> string { str: strings.Builder strings.builder_init(&str, context.temp_allocator) @@ -231,6 +238,7 @@ tprintf :: proc(fmt: string, args: ..any, newline := false) -> string { // // Returns: A formatted string. // +@(require_results) tprintfln :: proc(fmt: string, args: ..any) -> string { return tprintf(fmt, ..args, newline=true) } @@ -339,6 +347,7 @@ panicf :: proc(fmt: string, args: ..any, loc := #caller_location) -> ! { // // Returns: A formatted C string // +@(require_results) caprintf :: proc(format: string, args: ..any, newline := false) -> cstring { str: strings.Builder strings.builder_init(&str) @@ -357,6 +366,7 @@ caprintf :: proc(format: string, args: ..any, newline := false) -> cstring { // // Returns: A formatted C string // +@(require_results) caprintfln :: proc(format: string, args: ..any) -> cstring { return caprintf(format, ..args, newline=true) } @@ -371,6 +381,7 @@ caprintfln :: proc(format: string, args: ..any) -> cstring { // // Returns: A formatted C string // +@(require_results) ctprintf :: proc(format: string, args: ..any, newline := false) -> cstring { str: strings.Builder strings.builder_init(&str, context.temp_allocator) @@ -389,6 +400,7 @@ ctprintf :: proc(format: string, args: ..any, newline := false) -> cstring { // // Returns: A formatted C string // +@(require_results) ctprintfln :: proc(format: string, args: ..any) -> cstring { return ctprintf(format, ..args, newline=true) } From 700f9c94bd44dfcf77d7bef3bf7d0222c39e798e Mon Sep 17 00:00:00 2001 From: Feoramund <161657516+Feoramund@users.noreply.github.com> Date: Sun, 28 Apr 2024 17:18:46 -0400 Subject: [PATCH 2/3] Combine adjacent `sbprint*`/`to_string` calls The `sbprint*` procs already return a string conversion. --- core/fmt/fmt.odin | 18 ++++++------------ 1 file changed, 6 insertions(+), 12 deletions(-) diff --git a/core/fmt/fmt.odin b/core/fmt/fmt.odin index 2209af6e2..867257491 100644 --- a/core/fmt/fmt.odin +++ b/core/fmt/fmt.odin @@ -124,8 +124,7 @@ register_user_formatter :: proc(id: typeid, formatter: User_Formatter) -> Regist aprint :: proc(args: ..any, sep := " ", allocator := context.allocator) -> string { str: strings.Builder strings.builder_init(&str, allocator) - sbprint(&str, ..args, sep=sep) - return strings.to_string(str) + return sbprint(&str, ..args, sep=sep) } // Creates a formatted string with a newline character at the end // @@ -141,8 +140,7 @@ aprint :: proc(args: ..any, sep := " ", allocator := context.allocator) -> strin aprintln :: proc(args: ..any, sep := " ", allocator := context.allocator) -> string { str: strings.Builder strings.builder_init(&str, allocator) - sbprintln(&str, ..args, sep=sep) - return strings.to_string(str) + return sbprintln(&str, ..args, sep=sep) } // Creates a formatted string using a format string and arguments // @@ -159,8 +157,7 @@ aprintln :: proc(args: ..any, sep := " ", allocator := context.allocator) -> str aprintf :: proc(fmt: string, args: ..any, allocator := context.allocator, newline := false) -> string { str: strings.Builder strings.builder_init(&str, allocator) - sbprintf(&str, fmt, ..args, newline=newline) - return strings.to_string(str) + return sbprintf(&str, fmt, ..args, newline=newline) } // Creates a formatted string using a format string and arguments, followed by a newline. // @@ -190,8 +187,7 @@ aprintfln :: proc(fmt: string, args: ..any, allocator := context.allocator) -> s tprint :: proc(args: ..any, sep := " ") -> string { str: strings.Builder strings.builder_init(&str, context.temp_allocator) - sbprint(&str, ..args, sep=sep) - return strings.to_string(str) + return sbprint(&str, ..args, sep=sep) } // Creates a formatted string with a newline character at the end // @@ -207,8 +203,7 @@ tprint :: proc(args: ..any, sep := " ") -> string { tprintln :: proc(args: ..any, sep := " ") -> string { str: strings.Builder strings.builder_init(&str, context.temp_allocator) - sbprintln(&str, ..args, sep=sep) - return strings.to_string(str) + return sbprintln(&str, ..args, sep=sep) } // Creates a formatted string using a format string and arguments // @@ -225,8 +220,7 @@ tprintln :: proc(args: ..any, sep := " ") -> string { tprintf :: proc(fmt: string, args: ..any, newline := false) -> string { str: strings.Builder strings.builder_init(&str, context.temp_allocator) - sbprintf(&str, fmt, ..args, newline=newline) - return strings.to_string(str) + return sbprintf(&str, fmt, ..args, newline=newline) } // Creates a formatted string using a format string and arguments, followed by a newline. // From bbebb4ad60996cea544507cf551f7b2a4fa86520 Mon Sep 17 00:00:00 2001 From: Feoramund <161657516+Feoramund@users.noreply.github.com> Date: Sun, 28 Apr 2024 17:20:52 -0400 Subject: [PATCH 3/3] Fix unseen `print` call in demo --- examples/demo/demo.odin | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/examples/demo/demo.odin b/examples/demo/demo.odin index 1f6d337e8..a62c11310 100644 --- a/examples/demo/demo.odin +++ b/examples/demo/demo.odin @@ -2385,7 +2385,7 @@ matrix_type :: proc() { c := a * b #assert(type_of(c) == matrix[2, 2]f32) - fmt.tprintln("c = a * b", c) + fmt.println("c = a * b", c) } { // Matrices support multiplication between matrices and arrays