Skip to content

Commit ed8711c

Browse files
committed
Use VecDeque to make prepend O(m+n)
Signed-off-by: xizheyin <[email protected]>
1 parent a980cd4 commit ed8711c

File tree

1 file changed

+11
-8
lines changed

1 file changed

+11
-8
lines changed

compiler/rustc_errors/src/styled_buffer.rs

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
// Code for creating styled buffers
22

3+
use std::collections::VecDeque;
4+
35
use crate::snippet::{Style, StyledString};
46

57
#[derive(Debug)]
@@ -106,16 +108,17 @@ impl StyledBuffer {
106108
/// adding lines if needed
107109
pub(crate) fn prepend(&mut self, line: usize, string: &str, style: Style) {
108110
self.ensure_lines(line);
109-
let string_len = string.chars().count();
110-
111-
if !self.lines[line].is_empty() {
112-
// Push the old content over to make room for new content
113-
for _ in 0..string_len {
114-
self.lines[line].insert(0, StyledChar::SPACE);
115-
}
111+
if string.is_empty() {
112+
return;
116113
}
117114

118-
self.puts(line, 0, string, style);
115+
let row = &mut self.lines[line];
116+
117+
// collect the prefix into a VecDeque,
118+
// then extend the old content to the end of the deque
119+
let mut dq: VecDeque<_> = string.chars().map(|ch| StyledChar::new(ch, style)).collect();
120+
dq.extend(row.drain(..));
121+
*row = dq.into_iter().collect();
119122
}
120123

121124
/// For given `line` inserts `string` with `style` after old content of that line,

0 commit comments

Comments
 (0)