1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
//! The various iterators this crate provides.
//!
//! These iterators are not a very stable interface and you really should
//! avoid considering them to be concrete types.  A lot of the iterators in
//! this crate use `impl Iterator` for this reason but restrictions in the
//! language don't allow this to be used in all places on the versions of
//! rust this crate wants to compile for.
use std::marker::PhantomData;
use std::ops::{Index, Range};

use crate::{Change, ChangeTag, DiffOp, DiffTag};

/// Iterator for [`DiffOp::iter_changes`].
pub struct ChangesIter<'lookup, Old: ?Sized, New: ?Sized, T> {
    old: &'lookup Old,
    new: &'lookup New,
    old_range: Range<usize>,
    new_range: Range<usize>,
    old_index: usize,
    new_index: usize,
    old_i: usize,
    new_i: usize,
    tag: DiffTag,
    _marker: PhantomData<T>,
}

impl<'lookup, Old, New, T> ChangesIter<'lookup, Old, New, T>
where
    Old: Index<usize, Output = T> + ?Sized,
    New: Index<usize, Output = T> + ?Sized,
{
    pub(crate) fn new(old: &'lookup Old, new: &'lookup New, op: DiffOp) -> Self {
        let (tag, old_range, new_range) = op.as_tag_tuple();
        let old_index = old_range.start;
        let new_index = new_range.start;
        let old_i = old_range.start;
        let new_i = new_range.start;
        ChangesIter {
            old,
            new,
            old_range,
            new_range,
            old_index,
            new_index,
            old_i,
            new_i,
            tag,
            _marker: PhantomData,
        }
    }
}

impl<'lookup, Old, New, T> Iterator for ChangesIter<'lookup, Old, New, T>
where
    Old: Index<usize, Output = T> + ?Sized,
    New: Index<usize, Output = T> + ?Sized,
    T: Clone,
{
    type Item = Change<T>;

    fn next(&mut self) -> Option<Self::Item> {
        match self.tag {
            DiffTag::Equal => {
                if self.old_i < self.old_range.end {
                    let value = self.old[self.old_i].clone();
                    self.old_i += 1;
                    self.old_index += 1;
                    self.new_index += 1;
                    Some(Change {
                        tag: ChangeTag::Equal,
                        old_index: Some(self.old_index - 1),
                        new_index: Some(self.new_index - 1),
                        value,
                    })
                } else {
                    None
                }
            }
            DiffTag::Delete => {
                if self.old_i < self.old_range.end {
                    let value = self.old[self.old_i].clone();
                    self.old_i += 1;
                    self.old_index += 1;
                    Some(Change {
                        tag: ChangeTag::Delete,
                        old_index: Some(self.old_index - 1),
                        new_index: None,
                        value,
                    })
                } else {
                    None
                }
            }
            DiffTag::Insert => {
                if self.new_i < self.new_range.end {
                    let value = self.new[self.new_i].clone();
                    self.new_i += 1;
                    self.new_index += 1;
                    Some(Change {
                        tag: ChangeTag::Insert,
                        old_index: None,
                        new_index: Some(self.new_index - 1),
                        value,
                    })
                } else {
                    None
                }
            }
            DiffTag::Replace => {
                if self.old_i < self.old_range.end {
                    let value = self.old[self.old_i].clone();
                    self.old_i += 1;
                    self.old_index += 1;
                    Some(Change {
                        tag: ChangeTag::Delete,
                        old_index: Some(self.old_index - 1),
                        new_index: None,
                        value,
                    })
                } else if self.new_i < self.new_range.end {
                    let value = self.new[self.new_i].clone();
                    self.new_i += 1;
                    self.new_index += 1;
                    Some(Change {
                        tag: ChangeTag::Insert,
                        old_index: None,
                        new_index: Some(self.new_index - 1),
                        value,
                    })
                } else {
                    None
                }
            }
        }
    }
}

#[cfg(feature = "text")]
mod text {
    use super::*;

    /// Iterator for [`TextDiff::iter_all_changes`](crate::TextDiff::iter_all_changes).
    pub struct AllChangesIter<'slf, 'data, T: ?Sized> {
        old: &'slf [&'data T],
        new: &'slf [&'data T],
        ops: &'slf [DiffOp],
        current_iter: Option<ChangesIter<'slf, [&'data T], [&'data T], &'data T>>,
    }

    impl<'slf, 'data, T> AllChangesIter<'slf, 'data, T>
    where
        T: 'data + ?Sized + PartialEq,
    {
        pub(crate) fn new(
            old: &'slf [&'data T],
            new: &'slf [&'data T],
            ops: &'slf [DiffOp],
        ) -> Self {
            AllChangesIter {
                old,
                new,
                ops,
                current_iter: None,
            }
        }
    }

    impl<'slf, 'data, T> Iterator for AllChangesIter<'slf, 'data, T>
    where
        T: PartialEq + 'data + ?Sized,
        'data: 'slf,
    {
        type Item = Change<&'data T>;

        fn next(&mut self) -> Option<Self::Item> {
            loop {
                if let Some(ref mut iter) = self.current_iter {
                    if let Some(rv) = iter.next() {
                        return Some(rv);
                    }
                    self.current_iter.take();
                }
                if let Some((&first, rest)) = self.ops.split_first() {
                    self.current_iter = Some(ChangesIter::new(self.old, self.new, first));
                    self.ops = rest;
                } else {
                    return None;
                }
            }
        }
    }
}

#[cfg(feature = "text")]
pub use self::text::*;