Skip to content
This repository has been archived by the owner on Nov 24, 2023. It is now read-only.

Support CRLF linebreaks during replace #177

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 30 additions & 1 deletion src/replace.rs
Original file line number Diff line number Diff line change
Expand Up @@ -59,9 +59,27 @@ impl Data {
return Vec::new();
}

// Both LF and CRLF are reported as single span unit so we need to skip
// any CRs encountered in the text when adapting the span information
// to the original `[u8]` data.
let mut crs = 0;
self.parts.iter().fold(Vec::new(), |mut acc, d| {
match d.data {
State::Initial => acc.extend_from_slice(&self.original[d.start..=d.end]),
State::Initial => {
let start = d.start + crs;
let mut count = d.end - d.start + 1;
let segment = self.original[start..].iter().take_while(|&&b| {
if count == 0 { return false; }

if b == b'\r' {
crs += 1;
} else {
count -= 1;
}
true
});
acc.extend(segment)
},
State::Replaced(ref d) | State::Inserted(ref d) => acc.extend_from_slice(&d),
};
acc
Expand Down Expand Up @@ -248,6 +266,17 @@ mod tests {
assert_eq!("lorem\nlol\nlol", str(&d.to_vec()));
}

#[test]
fn replace_multiple_lines_with_crlf() {
let mut d = Data::new(b"lorem\r\nipsum\r\ndolor");

d.replace_range(6, 10, b"lol").unwrap();
assert_eq!("lorem\r\nlol\r\ndolor", str(&d.to_vec()));

d.replace_range(12, 16, b"lol").unwrap();
assert_eq!("lorem\r\nlol\r\nlol", str(&d.to_vec()));
}

#[test]
fn replace_multiple_lines_with_insert_only() {
let mut d = Data::new(b"foo!");
Expand Down