From f9bad2d81d9bf3812a8b78fa9015d8524810030b Mon Sep 17 00:00:00 2001 From: Antonio Scandurra Date: Mon, 4 Jul 2022 15:37:40 +0200 Subject: [PATCH] Replace \r and \r\n with \n when indexing text into the rope --- crates/text/src/rope.rs | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/crates/text/src/rope.rs b/crates/text/src/rope.rs index 116700624983af08d92e3e50675a8f148750e902..8c5dc260d635906fe914b17e8bf68549a93d2a3b 100644 --- a/crates/text/src/rope.rs +++ b/crates/text/src/rope.rs @@ -58,11 +58,19 @@ impl Rope { pub fn push(&mut self, text: &str) { let mut new_chunks = SmallVec::<[_; 16]>::new(); let mut new_chunk = ArrayString::new(); - for ch in text.chars() { + let mut chars = text.chars().peekable(); + while let Some(mut ch) = chars.next() { if new_chunk.len() + ch.len_utf8() > 2 * CHUNK_BASE { new_chunks.push(Chunk(new_chunk)); new_chunk = ArrayString::new(); } + + if ch == '\r' { + ch = '\n'; + if chars.peek().copied() == Some('\n') { + chars.next(); + } + } new_chunk.push(ch); } if !new_chunk.is_empty() {