-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Port configurable repr quote from Ruff and refactoring #4951
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
Co-Authored-By: Charlie Marsh <charlie.r.marsh@gmail.com>
6251537
to
d21066d
Compare
|
||
let quote = if dquoted { '"' } else { '\'' }; | ||
// if we don't need to escape anything we can just copy | ||
let unchanged = out_len == in_len; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Due to line 406, this test seems always failing. It must have been always using slow path
pub fn with_forced_quote(source: &'a str, quote: Quote) -> Self { | ||
let layout = EscapeLayout { quote, len: None }; | ||
Self { source, layout } | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Python requires length calculation due to allocating string object, but formatters doesn't.
will this be helpful for formatter?
let escape = if source.len() < TOO_LONG_TO_FORMAT {
UnicodeEscape::output_layout(source, Quote::Double)
} else {
UnicodeEscape::with_forced_quote(source, Quote::Double)
};
73f2689
to
c8ee020
Compare
Logic was ported, but kept the default
repr
behaviors to use single quote.I think the term
repr
- not representation or debug - meansrepr()
in Python.Instead of it, I added other configurable methods for each str and bytes.
Rather than the name repr, I found
unicode_escape
codec is doing the same thing.I will try to organize them more under 'escape' later.
I realized str and bytes are doing almost same thing but have so much different interface.
Unrelated note: I am thinking the new crate name for these stuffs
rustpython-literal
including str/bytes/float repr