fprintf tweaked to avoid calling file_size

This commit is contained in:
Colin Davidson
2022-02-19 15:51:11 -08:00
parent 31c7945444
commit b3d797598e
+44 -22
View File
@@ -236,10 +236,11 @@ wprintln :: proc(w: io.Writer, args: ..any, sep := " ") -> int {
wprintf :: proc(w: io.Writer, fmt: string, args: ..any) -> int { wprintf :: proc(w: io.Writer, fmt: string, args: ..any) -> int {
fi: Info fi: Info
arg_index: int = 0 arg_index: int = 0
ret: int = 0
end := len(fmt) end := len(fmt)
was_prev_index := false was_prev_index := false
size0 := io.size(auto_cast w) total_size := 0
loop: for i := 0; i < end; /**/ { loop: for i := 0; i < end; /**/ {
fi = Info{writer = w, good_arg_index = true, reordered = fi.reordered} fi = Info{writer = w, good_arg_index = true, reordered = fi.reordered}
@@ -249,7 +250,8 @@ wprintf :: proc(w: io.Writer, fmt: string, args: ..any) -> int {
i += 1 i += 1
} }
if i > prev_i { if i > prev_i {
io.write_string(fi.writer, fmt[prev_i:i]) ret, _ = io.write_string(fi.writer, fmt[prev_i:i])
total_size += ret
} }
if i >= end { if i >= end {
break loop break loop
@@ -265,12 +267,14 @@ wprintf :: proc(w: io.Writer, fmt: string, args: ..any) -> int {
i += 1 i += 1
} }
io.write_byte(fi.writer, char) io.write_byte(fi.writer, char)
total_size += 1
continue loop continue loop
} else if char == '{' { } else if char == '{' {
if i < end && fmt[i] == char { if i < end && fmt[i] == char {
// Skip extra one // Skip extra one
i += 1 i += 1
io.write_byte(fi.writer, char) io.write_byte(fi.writer, char)
total_size += 1
continue loop continue loop
} }
} }
@@ -301,7 +305,8 @@ wprintf :: proc(w: io.Writer, fmt: string, args: ..any) -> int {
i += 1 i += 1
fi.width, arg_index, fi.width_set = int_from_arg(args, arg_index) fi.width, arg_index, fi.width_set = int_from_arg(args, arg_index)
if !fi.width_set { if !fi.width_set {
io.write_string(w, "%!(BAD WIDTH)") ret, _ = io.write_string(w, "%!(BAD WIDTH)")
total_size += ret
} }
if fi.width < 0 { if fi.width < 0 {
@@ -332,7 +337,8 @@ wprintf :: proc(w: io.Writer, fmt: string, args: ..any) -> int {
fi.prec_set = false fi.prec_set = false
} }
if !fi.prec_set { if !fi.prec_set {
io.write_string(fi.writer, "%!(BAD PRECISION)") ret, _ = io.write_string(fi.writer, "%!(BAD PRECISION)")
total_size += ret
} }
was_prev_index = false was_prev_index = false
} else { } else {
@@ -345,7 +351,8 @@ wprintf :: proc(w: io.Writer, fmt: string, args: ..any) -> int {
} }
if i >= end { if i >= end {
io.write_string(fi.writer, "%!(NO VERB)") ret, _ = io.write_string(fi.writer, "%!(NO VERB)")
total_size += ret
break loop break loop
} }
@@ -355,10 +362,13 @@ wprintf :: proc(w: io.Writer, fmt: string, args: ..any) -> int {
switch { switch {
case verb == '%': case verb == '%':
io.write_byte(fi.writer, '%') io.write_byte(fi.writer, '%')
total_size += 1
case !fi.good_arg_index: case !fi.good_arg_index:
io.write_string(fi.writer, "%!(BAD ARGUMENT NUMBER)") ret, _ = io.write_string(fi.writer, "%!(BAD ARGUMENT NUMBER)")
total_size += ret
case arg_index >= len(args): case arg_index >= len(args):
io.write_string(fi.writer, "%!(MISSING ARGUMENT)") ret, _ = io.write_string(fi.writer, "%!(MISSING ARGUMENT)")
total_size += ret
case: case:
fmt_arg(&fi, args[arg_index], verb) fmt_arg(&fi, args[arg_index], verb)
arg_index += 1 arg_index += 1
@@ -374,14 +384,16 @@ wprintf :: proc(w: io.Writer, fmt: string, args: ..any) -> int {
arg_index = new_arg_index arg_index = new_arg_index
i = new_i i = new_i
} else { } else {
io.write_string(fi.writer, "%!(BAD ARGUMENT NUMBER ") ret, _ = io.write_string(fi.writer, "%!(BAD ARGUMENT NUMBER ")
total_size += ret
// Skip over the bad argument // Skip over the bad argument
start_index := i start_index := i
for i < end && fmt[i] != '}' && fmt[i] != ':' { for i < end && fmt[i] != '}' && fmt[i] != ':' {
i += 1 i += 1
} }
fmt_arg(&fi, fmt[start_index:i], 'v') fmt_arg(&fi, fmt[start_index:i], 'v')
io.write_string(fi.writer, ")") ret, _ = io.write_string(fi.writer, ")")
total_size += ret
} }
} }
@@ -414,7 +426,8 @@ wprintf :: proc(w: io.Writer, fmt: string, args: ..any) -> int {
i += 1 i += 1
fi.width, arg_index, fi.width_set = int_from_arg(args, arg_index) fi.width, arg_index, fi.width_set = int_from_arg(args, arg_index)
if !fi.width_set { if !fi.width_set {
io.write_string(fi.writer, "%!(BAD WIDTH)") ret, _ = io.write_string(fi.writer, "%!(BAD WIDTH)")
total_size += ret
} }
if fi.width < 0 { if fi.width < 0 {
@@ -445,7 +458,8 @@ wprintf :: proc(w: io.Writer, fmt: string, args: ..any) -> int {
fi.prec_set = false fi.prec_set = false
} }
if !fi.prec_set { if !fi.prec_set {
io.write_string(fi.writer, "%!(BAD PRECISION)") ret, _ = io.write_string(fi.writer, "%!(BAD PRECISION)")
total_size += ret
} }
was_prev_index = false was_prev_index = false
} else { } else {
@@ -459,7 +473,8 @@ wprintf :: proc(w: io.Writer, fmt: string, args: ..any) -> int {
if i >= end { if i >= end {
io.write_string(fi.writer, "%!(NO VERB)") ret, _ = io.write_string(fi.writer, "%!(NO VERB)")
total_size += ret
break loop break loop
} }
@@ -469,7 +484,8 @@ wprintf :: proc(w: io.Writer, fmt: string, args: ..any) -> int {
} }
if i >= end { if i >= end {
io.write_string(fi.writer, "%!(MISSING CLOSE BRACE)") ret, _ = io.write_string(fi.writer, "%!(MISSING CLOSE BRACE)")
total_size += ret
break loop break loop
} }
@@ -478,11 +494,14 @@ wprintf :: proc(w: io.Writer, fmt: string, args: ..any) -> int {
switch { switch {
case brace != '}': case brace != '}':
io.write_string(fi.writer, "%!(MISSING CLOSE BRACE)") ret, _ = io.write_string(fi.writer, "%!(MISSING CLOSE BRACE)")
total_size += ret
case !fi.good_arg_index: case !fi.good_arg_index:
io.write_string(fi.writer, "%!(BAD ARGUMENT NUMBER)") ret, _ = io.write_string(fi.writer, "%!(BAD ARGUMENT NUMBER)")
total_size += ret
case arg_index >= len(args): case arg_index >= len(args):
io.write_string(fi.writer, "%!(MISSING ARGUMENT)") ret, _ = io.write_string(fi.writer, "%!(MISSING ARGUMENT)")
total_size += ret
case: case:
fmt_arg(&fi, args[arg_index], verb) fmt_arg(&fi, args[arg_index], verb)
arg_index += 1 arg_index += 1
@@ -491,25 +510,28 @@ wprintf :: proc(w: io.Writer, fmt: string, args: ..any) -> int {
} }
if !fi.reordered && arg_index < len(args) { if !fi.reordered && arg_index < len(args) {
io.write_string(fi.writer, "%!(EXTRA ") ret, _ = io.write_string(fi.writer, "%!(EXTRA ")
total_size += ret
for arg, index in args[arg_index:] { for arg, index in args[arg_index:] {
if index > 0 { if index > 0 {
io.write_string(fi.writer, ", ") ret, _ = io.write_string(fi.writer, ", ")
total_size += ret
} }
if arg == nil { if arg == nil {
io.write_string(fi.writer, "<nil>") ret, _ = io.write_string(fi.writer, "<nil>")
total_size += ret
} else { } else {
fmt_arg(&fi, args[index], 'v') fmt_arg(&fi, args[index], 'v')
} }
} }
io.write_string(fi.writer, ")") ret, _ = io.write_string(fi.writer, ")")
total_size += ret
} }
io.flush(auto_cast w) io.flush(auto_cast w)
size1 := io.size(auto_cast w) return int(total_size)
return int(size1 - size0)
} }
// wprint_type is a utility procedure to write a ^runtime.Type_Info value to w // wprint_type is a utility procedure to write a ^runtime.Type_Info value to w