From 5b0e92d725288cd7462b48462c835ccc88bf5d4a Mon Sep 17 00:00:00 2001 From: Jeong YunWon Date: Wed, 3 May 2023 03:25:40 +0900 Subject: [PATCH] Refactor common::str::repr using common::escape --- ast/src/constant.rs | 5 ++++- ast/src/unparse.rs | 3 ++- 2 files changed, 6 insertions(+), 2 deletions(-) diff --git a/ast/src/constant.rs b/ast/src/constant.rs index 6099272efd..7b40fb1f39 100644 --- a/ast/src/constant.rs +++ b/ast/src/constant.rs @@ -41,7 +41,10 @@ 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) => rustpython_common::str::repr(s).fmt(f), + Constant::Str(s) => { + use rustpython_common::escape::Escape; + rustpython_common::escape::UnicodeEscape::new_repr(s.as_str()).write_quoted(f) + } Constant::Bytes(b) => { f.pad(&rustpython_common::bytes::repr(b).map_err(|_err| std::fmt::Error)?) } diff --git a/ast/src/unparse.rs b/ast/src/unparse.rs index 081c2a9241..1af7bf2e98 100644 --- a/ast/src/unparse.rs +++ b/ast/src/unparse.rs @@ -509,9 +509,10 @@ 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)); - fmt::Display::fmt(&rustpython_common::str::repr(&body), &mut self.f) + rustpython_common::escape::UnicodeEscape::new_repr(&body).write_quoted(&mut self.f) } } }