diff options
author | Titus Wormer <tituswormer@gmail.com> | 2022-07-22 18:46:33 +0200 |
---|---|---|
committer | Titus Wormer <tituswormer@gmail.com> | 2022-07-22 18:57:19 +0200 |
commit | bac358ee5c341729e50630f2569a69b4d580ce47 (patch) | |
tree | 9ea5f311dcad46e54dfaa55a2985c75925ff6c83 /src/util/edit_map.rs | |
parent | 0525454e33ed6bcd7b43da1c0969c1d592e743d9 (diff) | |
download | markdown-rs-bac358ee5c341729e50630f2569a69b4d580ce47.tar.gz markdown-rs-bac358ee5c341729e50630f2569a69b4d580ce47.tar.bz2 markdown-rs-bac358ee5c341729e50630f2569a69b4d580ce47.zip |
Refactor to use a single shared edit map
Diffstat (limited to '')
-rw-r--r-- | src/util/edit_map.rs | 15 |
1 files changed, 6 insertions, 9 deletions
diff --git a/src/util/edit_map.rs b/src/util/edit_map.rs index 3bcef48..4d9f557 100644 --- a/src/util/edit_map.rs +++ b/src/util/edit_map.rs @@ -57,8 +57,6 @@ fn shift_links(events: &mut [Event], jumps: &[(usize, usize, usize)]) { /// links in check. #[derive(Debug)] pub struct EditMap { - /// Whether this map was consumed already. - consumed: bool, /// Record of changes. map: Vec<(usize, usize, Vec<Event>)>, } @@ -66,10 +64,7 @@ pub struct EditMap { impl EditMap { /// Create a new edit map. pub fn new() -> EditMap { - EditMap { - consumed: false, - map: vec![], - } + EditMap { map: vec![] } } /// Create an edit: a remove and/or add at a certain place. pub fn add(&mut self, index: usize, remove: usize, add: Vec<Event>) { @@ -84,8 +79,9 @@ impl EditMap { self.map .sort_unstable_by(|a, b| a.0.partial_cmp(&b.0).unwrap()); - assert!(!self.consumed, "cannot consume after consuming"); - self.consumed = true; + if self.map.is_empty() { + return; + } // Calculate jumps: where items in the current list move to. let mut jumps = Vec::with_capacity(self.map.len()); @@ -118,12 +114,13 @@ impl EditMap { while let Some(mut slice) = vecs.pop() { events.append(&mut slice); } + + self.map.truncate(0); } } /// Create an edit. fn add_impl(edit_map: &mut EditMap, at: usize, remove: usize, mut add: Vec<Event>, before: bool) { - assert!(!edit_map.consumed, "cannot add after consuming"); let mut index = 0; if remove == 0 && add.is_empty() { |