mirror of https://github.com/astral-sh/ruff
Avoid allocations on newline character replacement
This commit is contained in:
parent
77b4ce3f59
commit
f7fb35b5b0
|
|
@ -66,10 +66,11 @@ fn logging_f_string(
|
||||||
// If the literal text contains a '%' placeholder, bail out: mixing
|
// If the literal text contains a '%' placeholder, bail out: mixing
|
||||||
// f-string interpolation with '%' placeholders is ambiguous for our
|
// f-string interpolation with '%' placeholders is ambiguous for our
|
||||||
// automatic conversion, so don't offer a fix for this case.
|
// automatic conversion, so don't offer a fix for this case.
|
||||||
if lit.value.as_ref().contains('%') {
|
if lit.value.contains('%') {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
format_string.push_str(&lit.value.replace('\n', "\\n"));
|
|
||||||
|
push_escaped_newlines(&lit.value, &mut format_string);
|
||||||
}
|
}
|
||||||
InterpolatedStringElement::Interpolation(interpolated) => {
|
InterpolatedStringElement::Interpolation(interpolated) => {
|
||||||
if interpolated.format_spec.is_some()
|
if interpolated.format_spec.is_some()
|
||||||
|
|
@ -117,6 +118,19 @@ fn logging_f_string(
|
||||||
diagnostic.set_fix(fix);
|
diagnostic.set_fix(fix);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Push str replacing `\n` with `\\n` while writing directly into the
|
||||||
|
/// destination buffer, avoiding intermediate allocations.
|
||||||
|
#[inline]
|
||||||
|
fn push_escaped_newlines(literal: &str, destination: &mut String) {
|
||||||
|
for ch in literal.chars() {
|
||||||
|
if ch == '\n' {
|
||||||
|
destination.push_str("\\n");
|
||||||
|
} else {
|
||||||
|
destination.push(ch);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/// Returns `true` if the attribute is a reserved attribute on the `logging` module's `LogRecord`
|
/// Returns `true` if the attribute is a reserved attribute on the `logging` module's `LogRecord`
|
||||||
/// class.
|
/// class.
|
||||||
fn is_reserved_attr(attr: &str) -> bool {
|
fn is_reserved_attr(attr: &str) -> bool {
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue