Explorar o código

Clean up `fmt` usage with `io.Writer` and `strings.Builder`

gingerBill %!s(int64=4) %!d(string=hai) anos
pai
achega
5d0db4c63a
Modificáronse 3 ficheiros con 169 adicións e 208 borrados
  1. 35 49
      core/fmt/fmt.odin
  2. 134 129
      core/reflect/types.odin
  3. 0 30
      core/strings/builder.odin

+ 35 - 49
core/fmt/fmt.odin

@@ -63,55 +63,28 @@ register_user_formatter :: proc(id: typeid, formatter: User_Formatter) -> Regist
 }
 
 
-Flush_Data :: struct {
-	handle: os.Handle,
-	bytes_written: int,
-}
-
-fmt_file_builder :: proc(data: []byte, fd: ^Flush_Data) -> strings.Builder {
-	b := strings.builder_from_slice(data);
-	b.flush_data = rawptr(fd);
-	b.flush_proc = proc(b: ^strings.Builder) -> (do_reset: bool) {
-		fd := (^Flush_Data)(b.flush_data);
-		res := strings.to_string(b^);
-		n, _ := os.write_string(fd.handle, res);
-		fd.bytes_written += max(n, 0);
-		return true;
-	};
-
-	return b;
-}
-
 fprint :: proc(fd: os.Handle, args: ..any, sep := " ") -> int {
-	data: [DEFAULT_BUFFER_SIZE]byte;
-	flush_data := Flush_Data{handle=fd};
-	buf := fmt_file_builder(data[:], &flush_data);
-	_ = sbprint(buf=&buf, args=args, sep=sep);
-	return flush_data.bytes_written;
+	w, _ := io.to_writer(os.stream_from_handle(fd));
+	return wprint(w=w, args=args, sep=sep);
 }
 
 fprintln :: proc(fd: os.Handle, args: ..any, sep := " ") -> int {
-	data: [DEFAULT_BUFFER_SIZE]byte;
-	flush_data := Flush_Data{handle=fd};
-	buf := fmt_file_builder(data[:], &flush_data);
-	_ = sbprintln(buf=&buf, args=args, sep=sep);
-	return flush_data.bytes_written;
+	w, _ := io.to_writer(os.stream_from_handle(fd));
+	return wprintln(w=w, args=args, sep=sep);
 }
 fprintf :: proc(fd: os.Handle, fmt: string, args: ..any) -> int {
-	data: [DEFAULT_BUFFER_SIZE]byte;
-	flush_data := Flush_Data{handle=fd};
-	buf := fmt_file_builder(data[:], &flush_data);
-	_ = sbprintf(&buf, fmt, ..args);
-	return flush_data.bytes_written;
+	w, _ := io.to_writer(os.stream_from_handle(fd));
+	return wprintf(w, fmt, ..args);
 }
 fprint_type :: proc(fd: os.Handle, info: ^runtime.Type_Info) -> int {
-	data: [DEFAULT_BUFFER_SIZE]byte;
-	flush_data := Flush_Data{handle=fd};
-	buf := fmt_file_builder(data[:], &flush_data);
-	reflect.write_type(&buf, info);
-	strings.flush_builder(&buf);
-	return flush_data.bytes_written;
+	w, _ := io.to_writer(os.stream_from_handle(fd));
+	return wprint_type(w, info);
+}
+fprint_typeid :: proc(fd: os.Handle, id: typeid) -> int {
+	w, _ := io.to_writer(os.stream_from_handle(fd));
+	return wprint_typeid(w, id);
 }
+
 // print* procedures return the number of bytes written
 print   :: proc(args: ..any, sep := " ") -> int { return fprint(fd=os.stdout, args=args, sep=sep); }
 println :: proc(args: ..any, sep := " ") -> int { return fprintln(fd=os.stdout, args=args, sep=sep); }
@@ -212,19 +185,16 @@ panicf :: proc(fmt: string, args: ..any, loc := #caller_location) -> ! {
 
 sbprint :: proc(buf: ^strings.Builder, args: ..any, sep := " ") -> string {
 	wprint(w=strings.to_writer(buf), args=args, sep=sep);
-	strings.flush_builder(buf);
 	return strings.to_string(buf^);
 }
 
 sbprintln :: proc(buf: ^strings.Builder, args: ..any, sep := " ") -> string {
 	wprintln(w=strings.to_writer(buf), args=args, sep=sep);
-	strings.flush_builder(buf);
 	return strings.to_string(buf^);
 }
 
 sbprintf :: proc(buf: ^strings.Builder, fmt: string, args: ..any) -> string {
 	wprintf(w=strings.to_writer(buf), fmt=fmt, args=args);
-	strings.flush_builder(buf);
 	return strings.to_string(buf^);
 }
 
@@ -246,6 +216,10 @@ wprint :: proc(w: io.Writer, args: ..any, sep := " ") -> int {
 	// NOTE(bill, 2020-06-19): I have found that the previous approach was not what people were expecting
 	// and were expecting `*print` to be the same `*println` except for the added newline
 	// so I am going to keep the same behaviour as `*println` for `*print`
+
+
+	size0 := io.size(auto_cast w);
+
 	for _, i in args {
 		if i > 0 {
 			io.write_string(fi.writer, sep);
@@ -254,13 +228,15 @@ wprint :: proc(w: io.Writer, args: ..any, sep := " ") -> int {
 		fmt_value(&fi, args[i], 'v');
 	}
 	io.flush(auto_cast w);
-	return int(io.size(auto_cast w));
+	return int(io.size(auto_cast w) - size0);
 }
 
 wprintln :: proc(w: io.Writer, args: ..any, sep := " ") -> int {
 	fi: Info;
 	fi.writer = w;
 
+	size0 := io.size(auto_cast w);
+
 	for _, i in args {
 		if i > 0 {
 			io.write_string(fi.writer, sep);
@@ -270,7 +246,7 @@ wprintln :: proc(w: io.Writer, args: ..any, sep := " ") -> int {
 	}
 	io.write_byte(fi.writer, '\n');
 	io.flush(auto_cast w);
-	return int(io.size(auto_cast w));
+	return int(io.size(auto_cast w) - size0);
 }
 
 wprintf :: proc(w: io.Writer, fmt: string, args: ..any) -> int {
@@ -279,6 +255,8 @@ wprintf :: proc(w: io.Writer, fmt: string, args: ..any) -> int {
 	end := len(fmt);
 	was_prev_index := false;
 
+	size0 := io.size(auto_cast w);
+
 	loop: for i := 0; i < end; /**/ {
 		fi = Info{writer = w, good_arg_index = true, reordered = fi.reordered};
 
@@ -544,12 +522,20 @@ wprintf :: proc(w: io.Writer, fmt: string, args: ..any) -> int {
 		io.write_string(fi.writer, ")");
 	}
 
-	io.flush(auto_cast fi.writer);
-	return int(io.size(auto_cast fi.writer));
+	io.flush(auto_cast w);
+	return int(io.size(auto_cast w) - size0);
 }
 
-
-
+wprint_type :: proc(w: io.Writer, info: ^runtime.Type_Info) -> int {
+	n := reflect.write_type(w, info);
+	io.flush(auto_cast w);
+	return n;
+}
+wprint_typeid :: proc(w: io.Writer, id: typeid) -> int {
+	n := reflect.write_type(w, type_info_of(id));
+	io.flush(auto_cast w);
+	return n;
+}
 
 
 

+ 134 - 129
core/reflect/types.odin

@@ -378,67 +378,70 @@ write_type :: proc{
 	write_type_writer,
 };
 
-write_type_builder :: proc(buf: ^strings.Builder, ti: ^Type_Info) {
-	write_type_writer(strings.to_writer(buf), ti);
+write_type_builder :: proc(buf: ^strings.Builder, ti: ^Type_Info) -> int {
+	return write_type_writer(strings.to_writer(buf), ti);
 }
-write_type_writer :: proc(w: io.Writer, ti: ^Type_Info) {
+write_type_writer :: proc(w: io.Writer, ti: ^Type_Info) -> (n: int) {
 	using strings;
 	if ti == nil {
-		write_string(w, "nil");
-		return;
+		return write_string(w, "nil");
 	}
 
+	_n1 :: proc(err: io.Error) -> int { return 1 if err == nil else 0; };
+	_n2 :: proc(n: int, _: io.Error) -> int { return n; };
+	_n :: proc{_n1, _n2};
+
 	switch info in ti.variant {
 	case Type_Info_Named:
-		write_string(w, info.name);
+		return write_string(w, info.name);
 	case Type_Info_Integer:
 		switch ti.id {
-		case int:     write_string(w, "int");
-		case uint:    write_string(w, "uint");
-		case uintptr: write_string(w, "uintptr");
+		case int:     return write_string(w, "int");
+		case uint:    return write_string(w, "uint");
+		case uintptr: return write_string(w, "uintptr");
 		case:
-			io.write_byte(w, 'i' if info.signed else 'u');
-			io.write_i64(w, i64(8*ti.size), 10);
+			n += _n(io.write_byte(w, 'i' if info.signed else 'u'));
+			n += _n(io.write_i64(w, i64(8*ti.size), 10));
 			switch info.endianness {
 			case .Platform: // Okay
-			case .Little: write_string(w, "le");
-			case .Big:    write_string(w, "be");
+			case .Little: n += write_string(w, "le");
+			case .Big:    n += write_string(w, "be");
 			}
 		}
 	case Type_Info_Rune:
-		io.write_string(w, "rune");
+		n += _n(io.write_string(w, "rune"));
 	case Type_Info_Float:
-		io.write_byte(w, 'f');
-		io.write_i64(w, i64(8*ti.size), 10);
+		n += _n(io.write_byte(w, 'f'));
+		n += _n(io.write_i64(w, i64(8*ti.size), 10));
 		switch info.endianness {
 		case .Platform: // Okay
-		case .Little: write_string(w, "le");
-		case .Big:    write_string(w, "be");
+		case .Little: n += write_string(w, "le");
+		case .Big:    n += write_string(w, "be");
 		}
 	case Type_Info_Complex:
-		io.write_string(w, "complex");
-		io.write_i64(w, i64(8*ti.size), 10);
+		n += _n(io.write_string(w, "complex"));
+		n += _n(io.write_i64(w, i64(8*ti.size), 10));
 	case Type_Info_Quaternion:
-		io.write_string(w, "quaternion");
-		io.write_i64(w, i64(8*ti.size), 10);
+		n += _n(io.write_string(w, "quaternion"));
+		n += _n(io.write_i64(w, i64(8*ti.size), 10));
 	case Type_Info_String:
 		if info.is_cstring {
-			write_string(w, "cstring");
+			n += write_string(w, "cstring");
 		} else {
-			write_string(w, "string");
+			n += write_string(w, "string");
 		}
 	case Type_Info_Boolean:
 		switch ti.id {
-		case bool: write_string(w, "bool");
+		case bool: n += write_string(w, "bool");
 		case:
-			io.write_byte(w, 'b');
-			io.write_i64(w, i64(8*ti.size), 10);
+			n += _n(io.write_byte(w, 'b'));
+			n += _n(io.write_i64(w, i64(8*ti.size), 10));
 		}
 	case Type_Info_Any:
-		write_string(w, "any");
+		n += write_string(w, "any");
 
 	case Type_Info_Type_Id:
-		write_string(w, "typeid");
+		n += write_string(w, "typeid");
 
 	case Type_Info_Pointer:
 		if info.elem == nil {
@@ -448,186 +451,188 @@ write_type_writer :: proc(w: io.Writer, ti: ^Type_Info) {
 			write_type(w, info.elem);
 		}
 	case Type_Info_Procedure:
-		write_string(w, "proc");
+		n += write_string(w, "proc");
 		if info.params == nil {
-			write_string(w, "()");
+			n += write_string(w, "()");
 		} else {
 			t := info.params.variant.(Type_Info_Tuple);
-			write_string(w, "(");
+			n += write_string(w, "(");
 			for t, i in t.types {
 				if i > 0 {
-					write_string(w, ", ");
+					n += write_string(w, ", ");
 				}
-				write_type(w, t);
+				n += write_type(w, t);
 			}
-			write_string(w, ")");
+			n += write_string(w, ")");
 		}
 		if info.results != nil {
-			write_string(w, " -> ");
-			write_type(w, info.results);
+			n += write_string(w, " -> ");
+			n += write_type(w, info.results);
 		}
 	case Type_Info_Tuple:
 		count := len(info.names);
-		if count != 1 { write_string(w, "("); }
+		if count != 1 { n += write_string(w, "("); }
 		for name, i in info.names {
-			if i > 0 { write_string(w, ", "); }
+			if i > 0 { n += write_string(w, ", "); }
 
 			t := info.types[i];
 
 			if len(name) > 0 {
-				write_string(w, name);
-				write_string(w, ": ");
+				n += write_string(w, name);
+				n += write_string(w, ": ");
 			}
-			write_type(w, t);
+			n += write_type(w, t);
 		}
-		if count != 1 { write_string(w, ")"); }
+		if count != 1 { n += write_string(w, ")"); }
 
 	case Type_Info_Array:
-		io.write_string(w, "[");
-		io.write_i64(w, i64(info.count), 10);
-		io.write_string(w, "]");
-		write_type(w, info.elem);
+		n += _n(io.write_string(w, "["));
+		n += _n(io.write_i64(w, i64(info.count), 10));
+		n += _n(io.write_string(w, "]"));
+		n += write_type(w, info.elem);
 
 	case Type_Info_Enumerated_Array:
-		write_string(w, "[");
-		write_type(w, info.index);
-		write_string(w, "]");
-		write_type(w, info.elem);
+		n += write_string(w, "[");
+		n += write_type(w, info.index);
+		n += write_string(w, "]");
+		n += write_type(w, info.elem);
 
 	case Type_Info_Dynamic_Array:
-		io.write_string(w, "[dynamic]");
-		write_type(w, info.elem);
+		n += _n(io.write_string(w, "[dynamic]"));
+		n += write_type(w, info.elem);
 	case Type_Info_Slice:
-		io.write_string(w, "[]");
-		write_type(w, info.elem);
+		n += _n(io.write_string(w, "[]"));
+		n += write_type(w, info.elem);
 
 	case Type_Info_Map:
-		io.write_string(w, "map[");
-		write_type(w, info.key);
-		io.write_byte(w, ']');
-		write_type(w, info.value);
+		n += _n(io.write_string(w, "map["));
+		n += write_type(w, info.key);
+		n += _n(io.write_byte(w, ']'));
+		n += write_type(w, info.value);
 
 	case Type_Info_Struct:
 		switch info.soa_kind {
 		case .None: // Ignore
 		case .Fixed:
-			io.write_string(w, "#soa[");
-			io.write_i64(w, i64(info.soa_len));
-			io.write_byte(w, ']');
-			write_type(w, info.soa_base_type);
+			n += _n(io.write_string(w, "#soa["));
+			n += _n(io.write_i64(w, i64(info.soa_len)));
+			n += _n(io.write_byte(w, ']'));
+			n += write_type(w, info.soa_base_type);
 			return;
 		case .Slice:
-			io.write_string(w, "#soa[]");
-			write_type(w, info.soa_base_type);
+			n += _n(io.write_string(w, "#soa[]"));
+			n += write_type(w, info.soa_base_type);
 			return;
 		case .Dynamic:
-			io.write_string(w, "#soa[dynamic]");
-			write_type(w, info.soa_base_type);
+			n += _n(io.write_string(w, "#soa[dynamic]"));
+			n += write_type(w, info.soa_base_type);
 			return;
 		}
 
-		write_string(w, "struct ");
-		if info.is_packed    { write_string(w, "#packed "); }
-		if info.is_raw_union { write_string(w, "#raw_union "); }
+		n += write_string(w, "struct ");
+		if info.is_packed    { n += write_string(w, "#packed "); }
+		if info.is_raw_union { n += write_string(w, "#raw_union "); }
 		if info.custom_align {
-			io.write_string(w, "#align ");
-			io.write_i64(w, i64(ti.align), 10);
-			io.write_byte(w, ' ');
+			n += _n(io.write_string(w, "#align "));
+			n += _n(io.write_i64(w, i64(ti.align), 10));
+			n += _n(io.write_byte(w, ' '));
 		}
-		io.write_byte(w, '{');
+		n += _n(io.write_byte(w, '{'));
 		for name, i in info.names {
-			if i > 0 { write_string(w, ", "); }
-			io.write_string(w, name);
-			io.write_string(w, ": ");
-			write_type(w, info.types[i]);
+			if i > 0 { n += write_string(w, ", "); }
+			n += _n(io.write_string(w, name));
+			n += _n(io.write_string(w, ": "));
+			n += write_type(w, info.types[i]);
 		}
-		io.write_byte(w, '}');
+		n += _n(io.write_byte(w, '}'));
 
 	case Type_Info_Union:
-		write_string(w, "union ");
+		n += write_string(w, "union ");
 		if info.custom_align {
-			write_string(w, "#align ");
-			io.write_i64(w, i64(ti.align), 10);
-			io.write_byte(w, ' ');
+			n += write_string(w, "#align ");
+			n += _n(io.write_i64(w, i64(ti.align), 10));
+			n += _n(io.write_byte(w, ' '));
 		}
-		io.write_byte(w, '{');
+		n += _n(io.write_byte(w, '{'));
 		for variant, i in info.variants {
-			if i > 0 { write_string(w, ", "); }
-			write_type(w, variant);
+			if i > 0 { n += write_string(w, ", "); }
+			n += write_type(w, variant);
 		}
-		io.write_byte(w, '}');
+		n += _n(io.write_byte(w, '}'));
 
 	case Type_Info_Enum:
-		write_string(w, "enum ");
-		write_type(w, info.base);
-		write_string(w, " {");
+		n += write_string(w, "enum ");
+		n += write_type(w, info.base);
+		n += write_string(w, " {");
 		for name, i in info.names {
-			if i > 0 { write_string(w, ", "); }
-			write_string(w, name);
+			if i > 0 { n += write_string(w, ", "); }
+			n += write_string(w, name);
 		}
-		io.write_byte(w, '}');
+		n += _n(io.write_byte(w, '}'));
 
 	case Type_Info_Bit_Field:
-		write_string(w, "bit_field ");
+		n += write_string(w, "bit_field ");
 		if ti.align != 1 {
-			write_string(w, "#align ");
-			io.write_i64(w, i64(ti.align), 10);
-			io.write_byte(w, ' ');
+			n += write_string(w, "#align ");
+			n += _n(io.write_i64(w, i64(ti.align), 10));
+			n += _n(io.write_byte(w, ' '));
 		}
-		write_string(w, " {");
+		n += write_string(w, " {");
 		for name, i in info.names {
-			if i > 0 { write_string(w, ", "); }
-			write_string(w, name);
-			write_string(w, ": ");
-			io.write_i64(w, i64(info.bits[i]), 10);
+			if i > 0 { n += write_string(w, ", "); }
+			n += write_string(w, name);
+			n += write_string(w, ": ");
+			n += _n(io.write_i64(w, i64(info.bits[i]), 10));
 		}
-		io.write_byte(w, '}');
+		n += _n(io.write_byte(w, '}'));
 
 	case Type_Info_Bit_Set:
-		write_string(w, "bit_set[");
+		n += write_string(w, "bit_set[");
 		switch {
 		case is_enum(info.elem):
-			write_type(w, info.elem);
+			n += write_type(w, info.elem);
 		case is_rune(info.elem):
-			write_encoded_rune(w, rune(info.lower));
-			write_string(w, "..");
-			write_encoded_rune(w, rune(info.upper));
+			n += write_encoded_rune(w, rune(info.lower));
+			n += write_string(w, "..");
+			n += write_encoded_rune(w, rune(info.upper));
 		case:
-			io.write_i64(w, info.lower, 10);
-			write_string(w, "..");
-			io.write_i64(w, info.upper, 10);
+			n += _n(io.write_i64(w, info.lower, 10));
+			n += write_string(w, "..");
+			n += _n(io.write_i64(w, info.upper, 10));
 		}
 		if info.underlying != nil {
-			write_string(w, "; ");
-			write_type(w, info.underlying);
+			n += write_string(w, "; ");
+			n += write_type(w, info.underlying);
 		}
-		io.write_byte(w, ']');
+		n += _n(io.write_byte(w, ']'));
 
 	case Type_Info_Opaque:
-		write_string(w, "opaque ");
-		write_type(w, info.elem);
+		n += write_string(w, "opaque ");
+		n += write_type(w, info.elem);
 
 	case Type_Info_Simd_Vector:
 		if info.is_x86_mmx {
-			write_string(w, "intrinsics.x86_mmx");
+			n += write_string(w, "intrinsics.x86_mmx");
 		} else {
-			write_string(w, "#simd[");
-			io.write_i64(w, i64(info.count));
-			io.write_byte(w, ']');
-			write_type(w, info.elem);
+			n += write_string(w, "#simd[");
+			n += _n(io.write_i64(w, i64(info.count)));
+			n += _n(io.write_byte(w, ']'));
+			n += write_type(w, info.elem);
 		}
 
 	case Type_Info_Relative_Pointer:
-		write_string(w, "#relative(");
-		write_type(w, info.base_integer);
-		write_string(w, ") ");
-		write_type(w, info.pointer);
+		n += write_string(w, "#relative(");
+		n += write_type(w, info.base_integer);
+		n += write_string(w, ") ");
+		n += write_type(w, info.pointer);
 
 	case Type_Info_Relative_Slice:
-		write_string(w, "#relative(");
-		write_type(w, info.base_integer);
-		write_string(w, ") ");
-		write_type(w, info.slice);
+		n += write_string(w, "#relative(");
+		n += write_type(w, info.base_integer);
+		n += write_string(w, ") ");
+		n += write_type(w, info.slice);
 	}
+
+	return;
 }
 

+ 0 - 30
core/strings/builder.odin

@@ -9,10 +9,6 @@ Builder_Flush_Proc :: #type proc(b: ^Builder) -> (do_reset: bool);
 
 Builder :: struct {
 	buf: [dynamic]byte,
-
-	// The custom flush procedure allows for the ability to flush the buffer, i.e. write to file
-	flush_proc: Builder_Flush_Proc,
-	flush_data: rawptr,
 }
 
 make_builder_none :: proc(allocator := context.allocator) -> Builder {
@@ -53,11 +49,6 @@ init_builder :: proc{
 
 @(private)
 _builder_stream_vtable := &io.Stream_VTable{
-	impl_flush = proc(s: io.Stream) -> io.Error {
-		b := (^Builder)(s.stream_data);
-		flush_builder(b);
-		return nil;
-	},
 	impl_write = proc(s: io.Stream, p: []byte) -> (n: int, err: io.Error) {
 		b := (^Builder)(s.stream_data);
 		n = write_bytes(b, p);
@@ -80,7 +71,6 @@ _builder_stream_vtable := &io.Stream_VTable{
 	},
 	impl_destroy = proc(s: io.Stream) -> io.Error {
 		b := (^Builder)(s.stream_data);
-		flush_builder(b);
 		delete(b.buf);
 		return .None;
 	},
@@ -110,24 +100,6 @@ reset_builder :: proc(b: ^Builder) {
 	clear(&b.buf);
 }
 
-flush_builder :: proc(b: ^Builder) -> (was_reset: bool) {
-	if b.flush_proc != nil {
-		was_reset = b.flush_proc(b);
-		if was_reset {
-			reset_builder(b);
-
-		}
-	}
-	return;
-}
-
-flush_builder_check_space :: proc(b: ^Builder, required: int) -> (was_reset: bool) {
-	if n := max(cap(b.buf) - len(b.buf), 0); n < required {
-		was_reset = flush_builder(b);
-	}
-	return;
-}
-
 
 builder_from_slice :: proc(backing: []byte) -> Builder {
 	s := transmute(mem.Raw_Slice)backing;
@@ -156,7 +128,6 @@ builder_space :: proc(b: Builder) -> int {
 }
 
 write_byte :: proc(b: ^Builder, x: byte) -> (n: int) {
-	flush_builder_check_space(b, 1);
 	if builder_space(b^) > 0 {
 		append(&b.buf, x);
 		n += 1;
@@ -167,7 +138,6 @@ write_byte :: proc(b: ^Builder, x: byte) -> (n: int) {
 write_bytes :: proc(b: ^Builder, x: []byte) -> (n: int) {
 	x := x;
 	for len(x) != 0 {
-		flush_builder_check_space(b, len(x));
 		space := builder_space(b^);
 		if space == 0 {
 			break; // No need to append