print lists as strings when possible
@@ -1024,31 +1024,7 @@ fn print_char(&mut self, is_quoted: bool, c: char)
}
}
- fn print_string(
- &mut self,
- iter: &mut HCPreOrderIterator,
- mut max_depth: usize,
- h: usize,
- n: usize,
- ) {
- iter.stack().pop();
- iter.stack().pop();
-
- if self.check_max_depth(&mut max_depth) {
- self.state_stack.push(TokenOrRedirect::Atom(clause_name!("...")));
- return;
- }
-
- let mut heap_pstr_iter =
- self.machine_st.heap_pstr_iter(Addr::PStrLocation(h, n));
-
- let buf = heap_pstr_iter.to_string();
- let end_addr = heap_pstr_iter.focus();
-
- let at_cdr = self.at_cdr(",");
-
- if !at_cdr && Addr::EmptyList == end_addr {
- if !self.machine_st.flags.double_quotes.is_codes() {
+ fn print_proper_string(&mut self, buf: String, max_depth: usize) {
self.push_char('"');
let buf =
@@ -1073,7 +1049,42 @@ fn print_string(
self.append_str(&buf);
self.push_char('"');
+ }
+
+ fn print_list_like(
+ &mut self,
+ iter: &mut HCPreOrderIterator,
+ addr: Addr,
+ mut max_depth: usize,
+ ) {
+ let a1 = iter.stack().pop();
+ let a2 = iter.stack().pop();
+
+ if self.check_max_depth(&mut max_depth) {
+ self.state_stack.push(TokenOrRedirect::Atom(clause_name!("...")));
+ return;
+ }
+
+ let mut heap_pstr_iter =
+ self.machine_st.heap_pstr_iter(addr);
+
+ let buf = heap_pstr_iter.to_string();
+ if buf.is_empty() {
+ iter.stack().push(a2.unwrap());
+ iter.stack().push(a1.unwrap());
+
+ self.push_list(iter, max_depth);
+ return;
+ }
+
+ let end_addr = heap_pstr_iter.focus();
+
+ let at_cdr = self.at_cdr(",");
+
+ if !at_cdr && Addr::EmptyList == end_addr {
+ if !self.machine_st.flags.double_quotes.is_codes() {
+ self.print_proper_string(buf, max_depth);
return;
}
}
@@ -1332,13 +1343,13 @@ fn handle_heap_term(
self.append_str(&format!("{}", u));
}
&HeapCellValue::Addr(Addr::PStrLocation(h, n)) => {
- self.print_string(iter, max_depth, h, n);
+ self.print_list_like(iter, Addr::PStrLocation(h, n), max_depth);
}
- &HeapCellValue::Addr(Addr::Lis(_)) => {
+ &HeapCellValue::Addr(Addr::Lis(l)) => {
if self.ignore_ops {
self.format_struct(iter, max_depth, 2, clause_name!("."));
} else {
- self.push_list(iter, max_depth);
+ self.print_list_like(iter, Addr::Lis(l), max_depth); //self.push_list(iter, max_depth);
}
}
&HeapCellValue::Addr(addr) => {
