Browse Source

Handle empty structs in fmt with hash

Matias Fernandez 1 year ago
parent
commit
4baf101f15
1 changed files with 10 additions and 4 deletions
  1. 10 4
      core/fmt/fmt.odin

+ 10 - 4
core/fmt/fmt.odin

@@ -1973,11 +1973,13 @@ fmt_struct :: proc(fi: ^Info, v: any, the_verb: rune, info: runtime.Type_Info_St
 	// fi.hash = false;
 	// fi.hash = false;
 	fi.indent += 1
 	fi.indent += 1
 
 
-	if !is_soa && hash {
+	is_empty := len(info.names) == 0
+
+	if !is_soa && hash && !is_empty {
 		io.write_byte(fi.writer, '\n', &fi.n)
 		io.write_byte(fi.writer, '\n', &fi.n)
 	}
 	}
 	defer {
 	defer {
-		if hash {
+		if !is_soa && hash && !is_empty {
 			for _ in 0..<indent { io.write_byte(fi.writer, '\t', &fi.n) }
 			for _ in 0..<indent { io.write_byte(fi.writer, '\t', &fi.n) }
 		}
 		}
 		io.write_byte(fi.writer, ']' if is_soa && the_verb == 'v' else '}', &fi.n)
 		io.write_byte(fi.writer, ']' if is_soa && the_verb == 'v' else '}', &fi.n)
@@ -2025,9 +2027,9 @@ fmt_struct :: proc(fi: ^Info, v: any, the_verb: rune, info: runtime.Type_Info_St
 			}
 			}
 			io.write_string(fi.writer, base_type_name, &fi.n)
 			io.write_string(fi.writer, base_type_name, &fi.n)
 			io.write_byte(fi.writer, '{', &fi.n)
 			io.write_byte(fi.writer, '{', &fi.n)
-			if hash { io.write_byte(fi.writer, '\n', &fi.n) }
+			if hash && !is_empty { io.write_byte(fi.writer, '\n', &fi.n) }
 			defer {
 			defer {
-				if hash {
+				if hash && !is_empty {
 					fi.indent -= 1
 					fi.indent -= 1
 					fmt_write_indent(fi)
 					fmt_write_indent(fi)
 					fi.indent += 1
 					fi.indent += 1
@@ -2075,6 +2077,10 @@ fmt_struct :: proc(fi: ^Info, v: any, the_verb: rune, info: runtime.Type_Info_St
 				if hash { io.write_string(fi.writer, ",\n", &fi.n) }
 				if hash { io.write_string(fi.writer, ",\n", &fi.n) }
 			}
 			}
 		}
 		}
+
+		if hash && n > 0 {
+			for _ in 0..<indent { io.write_byte(fi.writer, '\t', &fi.n) }
+		}
 	} else {
 	} else {
 		field_count := -1
 		field_count := -1
 		for name, i in info.names {
 		for name, i in info.names {