Skip to content

Commit 7feb738

Browse files
committed
rustdoc: Reduce allocations in a theme function
1 parent 027a232 commit 7feb738

File tree

1 file changed

+9
-7
lines changed

1 file changed

+9
-7
lines changed

src/librustdoc/theme.rs

+9-7
Original file line numberDiff line numberDiff line change
@@ -173,15 +173,17 @@ fn build_rule(v: &[u8], positions: &[usize]) -> String {
173173
.map(|x| ::std::str::from_utf8(&v[x[0]..x[1]]).unwrap_or(""))
174174
.collect::<String>()
175175
.trim()
176-
.replace('\n', " ")
177-
.replace('/', "")
178-
.replace('\t', " ")
179-
.replace('{', "")
180-
.replace('}', "")
176+
.chars()
177+
.filter_map(|c| match c {
178+
'\n' | '\t' => Some(' '),
179+
'/' | '{' | '}' => None,
180+
c => Some(c),
181+
})
182+
.collect::<String>()
181183
.split(' ')
182184
.filter(|s| !s.is_empty())
183-
.collect::<Vec<&str>>()
184-
.join(" "),
185+
.intersperse(" ")
186+
.collect::<String>(),
185187
)
186188
.unwrap_or_else(|_| String::new())
187189
}

0 commit comments

Comments
 (0)