Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
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
30 changes: 29 additions & 1 deletion compiler/noirc_evaluator/src/ssa/interpreter/value.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ use crate::ssa::ir::{
function::FunctionId,
instruction::Intrinsic,
integer::IntegerConstant,
is_printable_byte,
types::{CompositeType, NumericType, Type},
value::ValueId,
};
Expand Down Expand Up @@ -434,7 +435,34 @@ impl std::fmt::Display for ArrayValue {
let rc = self.rc.borrow();

let is_slice = if self.is_slice { "&" } else { "" };
write!(f, "rc{rc} {is_slice}[")?;
write!(f, "rc{rc} {is_slice}")?;

// Check if the array could be shown as a string literal
if self.element_types.len() == 1
&& matches!(self.element_types[0], Type::Numeric(NumericType::Unsigned { bit_size: 8 }))
{
let printable = self.elements.borrow().iter().all(|value| {
matches!(value, Value::Numeric(NumericValue::U8(byte)) if is_printable_byte(*byte))
});
if printable {
let bytes = self
.elements
.borrow()
.iter()
.map(|value| {
let Value::Numeric(NumericValue::U8(byte)) = value else {
panic!("Expected U8 value in array, found {value}");
};
*byte
})
.collect::<Vec<_>>();
let string = String::from_utf8(bytes).unwrap();
write!(f, "b{string:?}")?;
return Ok(());
}
}

write!(f, "[")?;

let length = self.elements.borrow().len() / self.element_types.len();
if length == 0 {
Expand Down
1 change: 1 addition & 0 deletions compiler/noirc_evaluator/src/ssa/ir/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,4 @@ pub(crate) mod post_order;
pub(crate) mod printer;
pub mod types;
pub mod value;
pub use printer::is_printable_byte;
15 changes: 9 additions & 6 deletions compiler/noirc_evaluator/src/ssa/ir/printer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -434,12 +434,7 @@ fn try_byte_array_to_string(elements: &Vector<ValueId>, dfg: &DataFlowGraph) ->
return None;
}
let byte: u8 = element as u8;
const FORM_FEED: u8 = 12; // This is the ASCII code for '\f', which isn't a valid escape sequence in strings
if byte != FORM_FEED
&& (byte.is_ascii_alphanumeric()
|| byte.is_ascii_punctuation()
|| byte.is_ascii_whitespace())
{
if is_printable_byte(byte) {
string.push(byte as char);
} else {
return None;
Expand All @@ -448,6 +443,14 @@ fn try_byte_array_to_string(elements: &Vector<ValueId>, dfg: &DataFlowGraph) ->
Some(string)
}

pub fn is_printable_byte(byte: u8) -> bool {
const FORM_FEED: u8 = 12; // This is the ASCII code for '\f', which isn't a valid escape sequence in strings
byte != FORM_FEED
&& (byte.is_ascii_alphanumeric()
|| byte.is_ascii_punctuation()
|| byte.is_ascii_whitespace())
}

fn result_types(dfg: &DataFlowGraph, results: &[ValueId]) -> String {
let types = vecmap(results, |result| dfg.type_of_value(*result).to_string());
if types.is_empty() {
Expand Down
Loading