From d6c3a68d45e2e51e475e15663b888338e192dfdb Mon Sep 17 00:00:00 2001 From: Ary Borenszweig Date: Mon, 9 Dec 2024 14:43:13 -0300 Subject: [PATCH 1/2] fix: println("{{}}") was printing "{{}}" instead of "{}" --- compiler/noirc_printable_type/src/lib.rs | 23 +++++++++++++++++++++-- 1 file changed, 21 insertions(+), 2 deletions(-) diff --git a/compiler/noirc_printable_type/src/lib.rs b/compiler/noirc_printable_type/src/lib.rs index d46b37c4ea..c452daeffb 100644 --- a/compiler/noirc_printable_type/src/lib.rs +++ b/compiler/noirc_printable_type/src/lib.rs @@ -278,6 +278,20 @@ fn write_template_replacing_interpolations( let mut last_index = 0; // How far we've written from the template let mut char_indices = template.char_indices().peekable(); while let Some((char_index, char)) = char_indices.next() { + // If we see a '}' it must be "}}" because the ones for interpolation are handled + // when we see '{' + if char == '}' { + // Write what we've seen so far in the template, including this '}' + write!(fmt, "{}", &template[last_index..=char_index])?; + + // Skip the second '}' + let (_, closing_curly) = char_indices.next().unwrap(); + assert_eq!(closing_curly, '}'); + + last_index = char_indices.peek().map(|(index, _)| *index).unwrap_or(template.len()); + continue; + } + // Keep going forward until we find a '{' if char != '{' { continue; @@ -290,7 +304,11 @@ fn write_template_replacing_interpolations( // If it's '{{', write '{' and keep going if char_indices.peek().map(|(_, char)| char) == Some(&'{') { write!(fmt, "{{")?; - (last_index, _) = char_indices.next().unwrap(); + + // Skip the second '{' + char_indices.next().unwrap(); + + last_index = char_indices.peek().map(|(index, _)| *index).unwrap_or(template.len()); continue; } @@ -428,9 +446,10 @@ mod tests { #[test] fn printable_value_display_to_string_with_curly_escapes() { let template = "hello {{world}} {{{{double_escape}}}}"; + let expected = "hello {world} {{double_escape}}"; let display = PrintableValueDisplay::::FmtString(template.to_string(), vec![]); - assert_eq!(display.to_string(), template); + assert_eq!(display.to_string(), expected); } #[test] From d41f66475d60a9d721ecff11f00593e4bda02e3b Mon Sep 17 00:00:00 2001 From: Ary Borenszweig Date: Mon, 9 Dec 2024 15:17:23 -0300 Subject: [PATCH 2/2] Fix test --- compiler/noirc_printable_type/src/lib.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/compiler/noirc_printable_type/src/lib.rs b/compiler/noirc_printable_type/src/lib.rs index c452daeffb..6ae187da27 100644 --- a/compiler/noirc_printable_type/src/lib.rs +++ b/compiler/noirc_printable_type/src/lib.rs @@ -460,7 +460,7 @@ mod tests { (PrintableValue::String("TWO".to_string()), PrintableType::String { length: 3 }), (PrintableValue::String("THREE".to_string()), PrintableType::String { length: 5 }), ]; - let expected = "hello ONE {{no}} TWO {{not_again}} THREE world"; + let expected = "hello ONE {no} TWO {not_again} THREE world"; let display = PrintableValueDisplay::::FmtString(template.to_string(), values); assert_eq!(display.to_string(), expected);