Browse Source

Merge pull request #1519 from colrdavidson/hyperspeed_file_size

Maintain a running count printed, so fprintf returns correct sizes for non-files
gingerBill 3 years ago
parent
commit
46bcd18946
1 changed files with 24 additions and 25 deletions
  1. 24 25
      core/fmt/fmt.odin

+ 24 - 25
core/fmt/fmt.odin

@@ -239,7 +239,7 @@ wprintf :: proc(w: io.Writer, fmt: string, args: ..any) -> int {
 	end := len(fmt)
 	was_prev_index := false
 
-	size0 := io.size(auto_cast w)
+	total_size := 0
 
 	loop: for i := 0; i < end; /**/ {
 		fi = Info{writer = w, good_arg_index = true, reordered = fi.reordered}
@@ -249,7 +249,7 @@ wprintf :: proc(w: io.Writer, fmt: string, args: ..any) -> int {
 			i += 1
 		}
 		if i > prev_i {
-			io.write_string(fi.writer, fmt[prev_i:i])
+			io.write_string(fi.writer, fmt[prev_i:i], &total_size)
 		}
 		if i >= end {
 			break loop
@@ -264,13 +264,13 @@ wprintf :: proc(w: io.Writer, fmt: string, args: ..any) -> int {
 				// Skip extra one
 				i += 1
 			}
-			io.write_byte(fi.writer, char)
+			io.write_byte(fi.writer, char, &total_size)
 			continue loop
 		} else if char == '{' {
 			if i < end && fmt[i] == char {
 				// Skip extra one
 				i += 1
-				io.write_byte(fi.writer, char)
+				io.write_byte(fi.writer, char, &total_size)
 				continue loop
 			}
 		}
@@ -301,7 +301,7 @@ wprintf :: proc(w: io.Writer, fmt: string, args: ..any) -> int {
 				i += 1
 				fi.width, arg_index, fi.width_set = int_from_arg(args, arg_index)
 				if !fi.width_set {
-					io.write_string(w, "%!(BAD WIDTH)")
+					io.write_string(w, "%!(BAD WIDTH)", &total_size)
 				}
 
 				if fi.width < 0 {
@@ -332,7 +332,7 @@ wprintf :: proc(w: io.Writer, fmt: string, args: ..any) -> int {
 						fi.prec_set = false
 					}
 					if !fi.prec_set {
-						io.write_string(fi.writer, "%!(BAD PRECISION)")
+						io.write_string(fi.writer, "%!(BAD PRECISION)", &total_size)
 					}
 					was_prev_index = false
 				} else {
@@ -345,7 +345,7 @@ wprintf :: proc(w: io.Writer, fmt: string, args: ..any) -> int {
 			}
 
 			if i >= end {
-				io.write_string(fi.writer, "%!(NO VERB)")
+				io.write_string(fi.writer, "%!(NO VERB)", &total_size)
 				break loop
 			}
 
@@ -354,11 +354,11 @@ wprintf :: proc(w: io.Writer, fmt: string, args: ..any) -> int {
 
 			switch {
 			case verb == '%':
-				io.write_byte(fi.writer, '%')
+				io.write_byte(fi.writer, '%', &total_size)
 			case !fi.good_arg_index:
-				io.write_string(fi.writer, "%!(BAD ARGUMENT NUMBER)")
+				io.write_string(fi.writer, "%!(BAD ARGUMENT NUMBER)", &total_size)
 			case arg_index >= len(args):
-				io.write_string(fi.writer, "%!(MISSING ARGUMENT)")
+				io.write_string(fi.writer, "%!(MISSING ARGUMENT)", &total_size)
 			case:
 				fmt_arg(&fi, args[arg_index], verb)
 				arg_index += 1
@@ -374,14 +374,14 @@ wprintf :: proc(w: io.Writer, fmt: string, args: ..any) -> int {
 					arg_index = new_arg_index
 					i = new_i
 				} else {
-					io.write_string(fi.writer, "%!(BAD ARGUMENT NUMBER ")
+					io.write_string(fi.writer, "%!(BAD ARGUMENT NUMBER ", &total_size)
 					// Skip over the bad argument
 					start_index := i
 					for i < end && fmt[i] != '}' && fmt[i] != ':' {
 						i += 1
 					}
 					fmt_arg(&fi, fmt[start_index:i], 'v')
-					io.write_string(fi.writer, ")")
+					io.write_string(fi.writer, ")", &total_size)
 				}
 			}
 
@@ -414,7 +414,7 @@ wprintf :: proc(w: io.Writer, fmt: string, args: ..any) -> int {
 					i += 1
 					fi.width, arg_index, fi.width_set = int_from_arg(args, arg_index)
 					if !fi.width_set {
-						io.write_string(fi.writer, "%!(BAD WIDTH)")
+						io.write_string(fi.writer, "%!(BAD WIDTH)", &total_size)
 					}
 
 					if fi.width < 0 {
@@ -445,7 +445,7 @@ wprintf :: proc(w: io.Writer, fmt: string, args: ..any) -> int {
 							fi.prec_set = false
 						}
 						if !fi.prec_set {
-							io.write_string(fi.writer, "%!(BAD PRECISION)")
+							io.write_string(fi.writer, "%!(BAD PRECISION)", &total_size)
 						}
 						was_prev_index = false
 					} else {
@@ -459,7 +459,7 @@ wprintf :: proc(w: io.Writer, fmt: string, args: ..any) -> int {
 
 
 				if i >= end {
-					io.write_string(fi.writer, "%!(NO VERB)")
+					io.write_string(fi.writer, "%!(NO VERB)", &total_size)
 					break loop
 				}
 
@@ -469,7 +469,7 @@ wprintf :: proc(w: io.Writer, fmt: string, args: ..any) -> int {
 			}
 
 			if i >= end {
-				io.write_string(fi.writer, "%!(MISSING CLOSE BRACE)")
+				io.write_string(fi.writer, "%!(MISSING CLOSE BRACE)", &total_size)
 				break loop
 			}
 
@@ -478,11 +478,11 @@ wprintf :: proc(w: io.Writer, fmt: string, args: ..any) -> int {
 
 			switch {
 			case brace != '}':
-				io.write_string(fi.writer, "%!(MISSING CLOSE BRACE)")
+				io.write_string(fi.writer, "%!(MISSING CLOSE BRACE)", &total_size)
 			case !fi.good_arg_index:
-				io.write_string(fi.writer, "%!(BAD ARGUMENT NUMBER)")
+				io.write_string(fi.writer, "%!(BAD ARGUMENT NUMBER)", &total_size)
 			case arg_index >= len(args):
-				io.write_string(fi.writer, "%!(MISSING ARGUMENT)")
+				io.write_string(fi.writer, "%!(MISSING ARGUMENT)", &total_size)
 			case:
 				fmt_arg(&fi, args[arg_index], verb)
 				arg_index += 1
@@ -491,25 +491,24 @@ wprintf :: proc(w: io.Writer, fmt: string, args: ..any) -> int {
 	}
 
 	if !fi.reordered && arg_index < len(args) {
-		io.write_string(fi.writer, "%!(EXTRA ")
+		io.write_string(fi.writer, "%!(EXTRA ", &total_size)
 		for arg, index in args[arg_index:] {
 			if index > 0 {
-				io.write_string(fi.writer, ", ")
+				io.write_string(fi.writer, ", ", &total_size)
 			}
 
 			if arg == nil {
-				io.write_string(fi.writer, "<nil>")
+				io.write_string(fi.writer, "<nil>", &total_size)
 			} else {
 				fmt_arg(&fi, args[index], 'v')
 			}
 		}
-		io.write_string(fi.writer, ")")
+		io.write_string(fi.writer, ")", &total_size)
 	}
 
 	io.flush(auto_cast w)
 
-	size1 := io.size(auto_cast w)
-	return int(size1 - size0)
+	return int(total_size)
 }
 
 // wprint_type is a utility procedure to write a ^runtime.Type_Info value to w