From 9275161d0fbc9dbbca547b8de78441d934fbfb1b Mon Sep 17 00:00:00 2001 From: yuyi Date: Sun, 19 Mar 2023 15:52:45 +0800 Subject: [PATCH] strconv: fix v_sprintf with '%%' (#17708) --- vlib/strconv/format_test.v | 6 ++++++ vlib/strconv/vprintf.c.v | 6 ++++++ 2 files changed, 12 insertions(+) diff --git a/vlib/strconv/format_test.v b/vlib/strconv/format_test.v index 150a6d8973..16af688b53 100644 --- a/vlib/strconv/format_test.v +++ b/vlib/strconv/format_test.v @@ -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' +} diff --git a/vlib/strconv/vprintf.c.v b/vlib/strconv/vprintf.c.v index 382d44db98..475da651c6 100644 --- a/vlib/strconv/vprintf.c.v +++ b/vlib/strconv/vprintf.c.v @@ -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++