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

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

View file

@ -41,12 +41,13 @@ impl std::fmt::Display for Constant {
match self { match self {
Constant::None => f.pad("None"), Constant::None => f.pad("None"),
Constant::Bool(b) => f.pad(if *b { "True" } else { "False" }), Constant::Bool(b) => f.pad(if *b { "True" } else { "False" }),
Constant::Str(s) => { Constant::Str(s) => rustpython_common::escape::UnicodeEscape::new_repr(s.as_str())
use rustpython_common::escape::Escape; .str_repr()
rustpython_common::escape::UnicodeEscape::new_repr(s.as_str()).write_quoted(f) .write(f),
}
Constant::Bytes(b) => { 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::Int(i) => i.fmt(f),
Constant::Tuple(tup) => { Constant::Tuple(tup) => {

View file

@ -509,10 +509,11 @@ impl<'a> Unparser<'a> {
if is_spec { if is_spec {
self.unparse_fstring_body(values, is_spec) self.unparse_fstring_body(values, is_spec)
} else { } else {
use rustpython_common::escape::Escape;
self.p("f")?; self.p("f")?;
let body = to_string_fmt(|f| Unparser::new(f).unparse_fstring_body(values, is_spec)); 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)
} }
} }
} }