switch to passing total_size to the io.writers

This commit is contained in:
Colin Davidson
2022-02-19 16:38:33 -08:00
parent b3d797598e
commit ddf9c4a65b
+22 -45
View File
@@ -236,7 +236,6 @@ 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
@@ -250,8 +249,7 @@ wprintf :: proc(w: io.Writer, fmt: string, args: ..any) -> int {
i += 1 i += 1
} }
if i > prev_i { if i > prev_i {
ret, _ = io.write_string(fi.writer, fmt[prev_i:i]) io.write_string(fi.writer, fmt[prev_i:i], &total_size)
total_size += ret
} }
if i >= end { if i >= end {
break loop break loop
@@ -266,15 +264,13 @@ wprintf :: proc(w: io.Writer, fmt: string, args: ..any) -> int {
// Skip extra one // Skip extra one
i += 1 i += 1
} }
io.write_byte(fi.writer, char) io.write_byte(fi.writer, char, &total_size)
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)
total_size += 1
continue loop continue loop
} }
} }
@@ -305,8 +301,7 @@ 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 {
ret, _ = io.write_string(w, "%!(BAD WIDTH)") io.write_string(w, "%!(BAD WIDTH)", &total_size)
total_size += ret
} }
if fi.width < 0 { if fi.width < 0 {
@@ -337,8 +332,7 @@ 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 {
ret, _ = io.write_string(fi.writer, "%!(BAD PRECISION)") io.write_string(fi.writer, "%!(BAD PRECISION)", &total_size)
total_size += ret
} }
was_prev_index = false was_prev_index = false
} else { } else {
@@ -351,8 +345,7 @@ wprintf :: proc(w: io.Writer, fmt: string, args: ..any) -> int {
} }
if i >= end { if i >= end {
ret, _ = io.write_string(fi.writer, "%!(NO VERB)") io.write_string(fi.writer, "%!(NO VERB)", &total_size)
total_size += ret
break loop break loop
} }
@@ -361,14 +354,11 @@ 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)
total_size += 1
case !fi.good_arg_index: case !fi.good_arg_index:
ret, _ = io.write_string(fi.writer, "%!(BAD ARGUMENT NUMBER)") io.write_string(fi.writer, "%!(BAD ARGUMENT NUMBER)", &total_size)
total_size += ret
case arg_index >= len(args): case arg_index >= len(args):
ret, _ = io.write_string(fi.writer, "%!(MISSING ARGUMENT)") io.write_string(fi.writer, "%!(MISSING ARGUMENT)", &total_size)
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
@@ -384,16 +374,14 @@ 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 {
ret, _ = io.write_string(fi.writer, "%!(BAD ARGUMENT NUMBER ") io.write_string(fi.writer, "%!(BAD ARGUMENT NUMBER ", &total_size)
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')
ret, _ = io.write_string(fi.writer, ")") io.write_string(fi.writer, ")", &total_size)
total_size += ret
} }
} }
@@ -426,8 +414,7 @@ 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 {
ret, _ = io.write_string(fi.writer, "%!(BAD WIDTH)") io.write_string(fi.writer, "%!(BAD WIDTH)", &total_size)
total_size += ret
} }
if fi.width < 0 { if fi.width < 0 {
@@ -458,8 +445,7 @@ 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 {
ret, _ = io.write_string(fi.writer, "%!(BAD PRECISION)") io.write_string(fi.writer, "%!(BAD PRECISION)", &total_size)
total_size += ret
} }
was_prev_index = false was_prev_index = false
} else { } else {
@@ -473,8 +459,7 @@ wprintf :: proc(w: io.Writer, fmt: string, args: ..any) -> int {
if i >= end { if i >= end {
ret, _ = io.write_string(fi.writer, "%!(NO VERB)") io.write_string(fi.writer, "%!(NO VERB)", &total_size)
total_size += ret
break loop break loop
} }
@@ -484,8 +469,7 @@ wprintf :: proc(w: io.Writer, fmt: string, args: ..any) -> int {
} }
if i >= end { if i >= end {
ret, _ = io.write_string(fi.writer, "%!(MISSING CLOSE BRACE)") io.write_string(fi.writer, "%!(MISSING CLOSE BRACE)", &total_size)
total_size += ret
break loop break loop
} }
@@ -494,14 +478,11 @@ wprintf :: proc(w: io.Writer, fmt: string, args: ..any) -> int {
switch { switch {
case brace != '}': case brace != '}':
ret, _ = io.write_string(fi.writer, "%!(MISSING CLOSE BRACE)") io.write_string(fi.writer, "%!(MISSING CLOSE BRACE)", &total_size)
total_size += ret
case !fi.good_arg_index: case !fi.good_arg_index:
ret, _ = io.write_string(fi.writer, "%!(BAD ARGUMENT NUMBER)") io.write_string(fi.writer, "%!(BAD ARGUMENT NUMBER)", &total_size)
total_size += ret
case arg_index >= len(args): case arg_index >= len(args):
ret, _ = io.write_string(fi.writer, "%!(MISSING ARGUMENT)") io.write_string(fi.writer, "%!(MISSING ARGUMENT)", &total_size)
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
@@ -510,23 +491,19 @@ wprintf :: proc(w: io.Writer, fmt: string, args: ..any) -> int {
} }
if !fi.reordered && arg_index < len(args) { if !fi.reordered && arg_index < len(args) {
ret, _ = io.write_string(fi.writer, "%!(EXTRA ") io.write_string(fi.writer, "%!(EXTRA ", &total_size)
total_size += ret
for arg, index in args[arg_index:] { for arg, index in args[arg_index:] {
if index > 0 { if index > 0 {
ret, _ = io.write_string(fi.writer, ", ") io.write_string(fi.writer, ", ", &total_size)
total_size += ret
} }
if arg == nil { if arg == nil {
ret, _ = io.write_string(fi.writer, "<nil>") io.write_string(fi.writer, "<nil>", &total_size)
total_size += ret
} else { } else {
fmt_arg(&fi, args[index], 'v') fmt_arg(&fi, args[index], 'v')
} }
} }
ret, _ = io.write_string(fi.writer, ")") io.write_string(fi.writer, ")", &total_size)
total_size += ret
} }
io.flush(auto_cast w) io.flush(auto_cast w)