Skip to content

Commit e98856b

Browse files
committed
Only allocate new string if there are DOS backlines
1 parent 4a54b26 commit e98856b

File tree

1 file changed

+7
-1
lines changed

1 file changed

+7
-1
lines changed

src/librustdoc/html/highlight.rs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -408,7 +408,13 @@ pub(super) fn write_code(
408408
line_info: Option<LineInfo>,
409409
) {
410410
// This replace allows to fix how the code source with DOS backline characters is displayed.
411-
let src = src.replace("\r\n", "\n");
411+
let src =
412+
// The first "\r\n" should be fairly close to the beginning of the string relatively
413+
// to its overall length, and most strings handled by rustdoc likely don't have
414+
// DOS backlines anyway.
415+
// Checking for the single ASCII character '\r' is much more efficient than checking for
416+
// the whole string "\r\n".
417+
if src.contains('\r') { src.replace("\r\n", "\n").into() } else { Cow::Borrowed(src) };
412418
let mut token_handler = TokenHandler {
413419
out,
414420
closing_tags: Vec::new(),

0 commit comments

Comments
 (0)