Fix printf bug #177

This commit is contained in:
gingerBill
2018-01-28 09:13:29 +00:00
parent 53b670b889
commit bee4cb57f2
+9 -8
View File
@@ -316,7 +316,7 @@ _parse_int :: proc(s: string, offset: int) -> (result: int, new_offset: int, ok:
new_offset = offset; new_offset = offset;
n := len(s[new_offset..]); n := len(s[new_offset..]);
for new_offset < n { for new_offset <= n {
c := rune(s[new_offset]); c := rune(s[new_offset]);
if !is_digit(c) do break; if !is_digit(c) do break;
new_offset += 1; new_offset += 1;
@@ -1034,7 +1034,7 @@ sbprintf :: proc(b: ^String_Buffer, fmt: string, args: ...any) -> string {
was_prev_index := false; was_prev_index := false;
for i := 0; i < end; /**/ { loop: for i := 0; i < end; /**/ {
fi = Fmt_Info{buf = b, good_arg_index = true}; fi = Fmt_Info{buf = b, good_arg_index = true};
prev_i := i; prev_i := i;
@@ -1045,7 +1045,7 @@ sbprintf :: proc(b: ^String_Buffer, fmt: string, args: ...any) -> string {
write_string(b, fmt[prev_i..i]); write_string(b, fmt[prev_i..i]);
} }
if i >= end { if i >= end {
break; break loop;
} }
// Process a "verb" // Process a "verb"
@@ -1125,19 +1125,20 @@ sbprintf :: proc(b: ^String_Buffer, fmt: string, args: ...any) -> string {
if i >= end { if i >= end {
write_string(b, "%!(NO VERB)"); write_string(b, "%!(NO VERB)");
break; break loop;
} }
verb, w := utf8.decode_rune_from_string(fmt[i..]); verb, w := utf8.decode_rune_from_string(fmt[i..]);
i += w; i += w;
if verb == '%' { switch {
case verb == '%':
write_byte(b, '%'); write_byte(b, '%');
} else if !fi.good_arg_index { case !fi.good_arg_index:
write_string(b, "%!(BAD ARGUMENT NUMBER)"); write_string(b, "%!(BAD ARGUMENT NUMBER)");
} else if arg_index >= len(args) { case arg_index >= len(args):
write_string(b, "%!(MISSING ARGUMENT)"); write_string(b, "%!(MISSING ARGUMENT)");
} else { case:
fmt_arg(&fi, args[arg_index], verb); fmt_arg(&fi, args[arg_index], verb);
arg_index += 1; arg_index += 1;
} }