Skip to content
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 21 additions & 2 deletions compiler/noirc_printable_type/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
}

Expand Down Expand Up @@ -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::<FieldElement>::FmtString(template.to_string(), vec![]);
assert_eq!(display.to_string(), template);
assert_eq!(display.to_string(), expected);
}

#[test]
Expand Down