Refactor common::bytes::repr using common::escape

This commit is contained in:
Jeong YunWon 2023-05-03 15:01:52 +09:00
parent bbbe25336a
commit e09b23d3fb
2 changed files with 9 additions and 7 deletions

View file

@ -41,12 +41,13 @@ impl std::fmt::Display for Constant {
match self {
Constant::None => f.pad("None"),
Constant::Bool(b) => f.pad(if *b { "True" } else { "False" }),
Constant::Str(s) => {
use rustpython_common::escape::Escape;
rustpython_common::escape::UnicodeEscape::new_repr(s.as_str()).write_quoted(f)
}
Constant::Str(s) => rustpython_common::escape::UnicodeEscape::new_repr(s.as_str())
.str_repr()
.write(f),
Constant::Bytes(b) => {
f.pad(&rustpython_common::bytes::repr(b).map_err(|_err| std::fmt::Error)?)
let escape = rustpython_common::escape::AsciiEscape::new_repr(b);
let repr = escape.bytes_repr().to_string().unwrap();
f.pad(&repr)
}
Constant::Int(i) => i.fmt(f),
Constant::Tuple(tup) => {

View file

@ -509,10 +509,11 @@ impl<'a> Unparser<'a> {
if is_spec {
self.unparse_fstring_body(values, is_spec)
} else {
use rustpython_common::escape::Escape;
self.p("f")?;
let body = to_string_fmt(|f| Unparser::new(f).unparse_fstring_body(values, is_spec));
rustpython_common::escape::UnicodeEscape::new_repr(&body).write_quoted(&mut self.f)
rustpython_common::escape::UnicodeEscape::new_repr(&body)
.str_repr()
.write(&mut self.f)
}
}
}