mirror of
https://github.com/astral-sh/ruff.git
synced 2025-08-03 02:12:22 +00:00
Pre-allocate output contents during autofix application (#2340)
This commit is contained in:
parent
74e3cdfd7c
commit
7a83b65fbe
2 changed files with 10 additions and 2 deletions
|
@ -27,7 +27,7 @@ fn apply_fixes<'a>(
|
|||
fixes: impl Iterator<Item = &'a Fix>,
|
||||
locator: &'a Locator<'a>,
|
||||
) -> (String, usize) {
|
||||
let mut output = String::new();
|
||||
let mut output = String::with_capacity(locator.len());
|
||||
let mut last_pos: Location = Location::new(1, 0);
|
||||
let mut applied: BTreeSet<&Fix> = BTreeSet::default();
|
||||
let mut num_fixed: usize = 0;
|
||||
|
@ -68,7 +68,7 @@ fn apply_fixes<'a>(
|
|||
|
||||
/// Apply a single fix.
|
||||
pub(crate) fn apply_fix(fix: &Fix, locator: &Locator) -> String {
|
||||
let mut output = String::new();
|
||||
let mut output = String::with_capacity(locator.len());
|
||||
|
||||
// Add all contents from `last_pos` to `fix.location`.
|
||||
let slice = locator.slice_source_code_range(&Range::new(Location::new(1, 0), fix.location));
|
||||
|
|
|
@ -139,6 +139,14 @@ impl<'a> Locator<'a> {
|
|||
&self.contents[inner_end..outer_end],
|
||||
)
|
||||
}
|
||||
|
||||
pub fn len(&self) -> usize {
|
||||
self.contents.len()
|
||||
}
|
||||
|
||||
pub fn is_empty(&self) -> bool {
|
||||
self.contents.is_empty()
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue