aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorFeoramund <161657516+Feoramund@users.noreply.github.com>2024-06-12 18:48:01 -0400
committerFeoramund <161657516+Feoramund@users.noreply.github.com>2024-06-12 20:35:50 -0400
commit0c9f487783227a894fe08fdf51bbef0dc2f0dbc3 (patch)
tree984f3559fa1e7f13427bd13dc6db6df7eed79557
parent05972eb26bc1ba3ae2c067ca8fd4e39e623143b8 (diff)
Fix and document `%w` verb for `core:fmt`
-rw-r--r--core/fmt/doc.odin1
-rw-r--r--core/fmt/fmt.odin22
2 files changed, 17 insertions, 6 deletions
diff --git a/core/fmt/doc.odin b/core/fmt/doc.odin
index be666dcc4..d45e6c796 100644
--- a/core/fmt/doc.odin
+++ b/core/fmt/doc.odin
@@ -9,6 +9,7 @@ The verbs:
General:
%v the value in a default format
%#v an expanded format of %v with newlines and indentation
+ %w an Odin-syntax representation of the value
%T an Odin-syntax representation of the type of the value
%% a literal percent sign; consumes no value
{{ a literal open brace; consumes no value
diff --git a/core/fmt/fmt.odin b/core/fmt/fmt.odin
index f9113a7a7..4c65dd01f 100644
--- a/core/fmt/fmt.odin
+++ b/core/fmt/fmt.odin
@@ -1726,10 +1726,12 @@ fmt_bit_set :: proc(fi: ^Info, v: any, name: string = "", verb: rune = 'v') {
et := runtime.type_info_base(info.elem)
- if name != "" {
- io.write_string(fi.writer, name, &fi.n)
- } else {
- reflect.write_type(fi.writer, type_info, &fi.n)
+ if verb != 'w' {
+ if name != "" {
+ io.write_string(fi.writer, name, &fi.n)
+ } else {
+ reflect.write_type(fi.writer, type_info, &fi.n)
+ }
}
io.write_byte(fi.writer, '{', &fi.n)
defer io.write_byte(fi.writer, '}', &fi.n)
@@ -1746,9 +1748,17 @@ fmt_bit_set :: proc(fi: ^Info, v: any, name: string = "", verb: rune = 'v') {
}
if is_enum {
+ enum_name: string
+ if ti_named, is_named := info.elem.variant.(runtime.Type_Info_Named); is_named {
+ enum_name = ti_named.name
+ }
for ev, evi in e.values {
v := u64(ev)
if v == u64(i) {
+ if verb == 'w' {
+ io.write_string(fi.writer, enum_name, &fi.n)
+ io.write_byte(fi.writer, '.', &fi.n)
+ }
io.write_string(fi.writer, e.names[evi], &fi.n)
commas += 1
continue loop
@@ -2391,7 +2401,6 @@ fmt_named :: proc(fi: ^Info, v: any, verb: rune, info: runtime.Type_Info_Named)
runtime.Type_Info_Dynamic_Array,
runtime.Type_Info_Slice,
runtime.Type_Info_Struct,
- runtime.Type_Info_Union,
runtime.Type_Info_Enum,
runtime.Type_Info_Map,
runtime.Type_Info_Bit_Set,
@@ -2498,8 +2507,9 @@ fmt_matrix :: proc(fi: ^Info, v: any, verb: rune, info: runtime.Type_Info_Matrix
}
} else {
// Printed in Row-Major layout to match text layout
+ row_separator := ", " if verb == 'w' else "; "
for row in 0..<info.row_count {
- if row > 0 { io.write_string(fi.writer, "; ", &fi.n) }
+ if row > 0 { io.write_string(fi.writer, row_separator, &fi.n) }
for col in 0..<info.column_count {
if col > 0 { io.write_string(fi.writer, ", ", &fi.n) }