1
0
mirror of https://github.com/vlang/v.git synced 2023-08-10 21:13:21 +03:00

strconv: fix v_sprintf with '%%' (#17708)

This commit is contained in:
yuyi 2023-03-19 15:52:45 +08:00 committed by GitHub
parent 3793bf1c99
commit 9275161d0f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 12 additions and 0 deletions

View File

@ -113,3 +113,9 @@ fn test_sprintf_does_not_double_free_on_g() {
x := 3.141516
assert strconv.v_sprintf('aaa %G', x) == 'aaa 3.141516'
}
fn test_sprintf_with_escape() {
n := 69
s := strconv.v_sprintf('%d is 100%% awesome', n)
assert s == '69 is 100% awesome'
}

View File

@ -75,6 +75,12 @@ pub fn v_sprintf(str string, pt ...voidptr) string {
i++
continue
}
if ch == `%` && status == .field_char {
status = .norm_char
res.write_u8(ch)
i++
continue
}
if ch == `%` && status == .norm_char {
status = .field_char
i++