OXYGENHUB
# fd280228bdc2 oxygen / crates / oxy-core / src / lines.rs
5.0 KiB · Rust · last changed A new file's lines share its content 1 month ago source blame history raw
1
//! The lines of an insert (spec §7.4) held as one buffer and where each
2
//! line ends, shared by the change and the file graph it builds: a tree of
3
//! forty million lines is then a few allocations per insert, not one per
4
//! line, and a graph's atoms point into their insert's buffer rather than
5
//! copying it.
6
7
use std::sync::Arc;
8
9
#[derive(Default, PartialEq, Eq, Hash)]
10
struct Inner {
11
    /// Shared with the file it was read from, when there was one.
12
    buf: Arc<Vec<u8>>,
13
    /// The end offset of each line in `buf`.
14
    ends: Vec<u64>,
15
}
16
17
/// An insert's lines. Cloning is cheap: the buffer is shared.
18
#[derive(Clone, Default, PartialEq, Eq, Hash)]
19
pub struct Lines(Arc<Inner>);
20
21
impl Lines {
22
    /// Lines from their bytes, each keeping its terminator; a final
23
    /// unterminated line is its own line (the rule of
24
    /// [`crate::diff::split_lines`]).
25
    pub fn split(content: &[u8]) -> Lines {
26
        Lines::split_shared(Arc::new(content.to_vec()))
27
    }
28
29
    /// [`Lines::split`] of a buffer held elsewhere too (a file's content),
30
    /// shared rather than copied.
31
    pub fn split_shared(buf: Arc<Vec<u8>>) -> Lines {
32
        let content = &buf[..];
33
        let mut ends = Vec::new();
34
        for (i, &c) in content.iter().enumerate() {
35
            if c == b'\n' {
36
                ends.push(i as u64 + 1);
37
            }
38
        }
39
        if ends.last().copied().unwrap_or(0) as usize != content.len() {
40
            ends.push(content.len() as u64);
41
        }
42
        Lines(Arc::new(Inner { buf, ends }))
43
    }
44
45
    pub fn len(&self) -> usize {
46
        self.0.ends.len()
47
    }
48
49
    pub fn is_empty(&self) -> bool {
50
        self.0.ends.is_empty()
51
    }
52
53
    /// Line `i`, with its terminator.
54
    pub fn get(&self, i: usize) -> Option<&[u8]> {
55
        let end = *self.0.ends.get(i)? as usize;
56
        let start = if i == 0 {
57
            0
58
        } else {
59
            self.0.ends[i - 1] as usize
60
        };
61
        Some(&self.0.buf[start..end])
62
    }
63
64
    pub fn iter(&self) -> impl ExactSizeIterator<Item = &[u8]> + Clone + '_ {
65
        (0..self.len()).map(move |i| self.get(i).unwrap())
66
    }
67
68
    /// Every line, joined: the bytes they were split from.
69
    pub fn bytes(&self) -> &[u8] {
70
        &self.0.buf
71
    }
72
73
    /// Lines `range` as lines of their own.
74
    pub fn slice(&self, range: std::ops::Range<usize>) -> Lines {
75
        self.iter().skip(range.start).take(range.len()).collect()
76
    }
77
78
    /// The lines as owned vectors, for code that edits them one by one.
79
    pub fn to_vec(&self) -> Vec<Vec<u8>> {
80
        self.iter().map(<[u8]>::to_vec).collect()
81
    }
82
}
83
84
impl std::ops::Index<usize> for Lines {
85
    type Output = [u8];
86
    fn index(&self, i: usize) -> &[u8] {
87
        self.get(i).expect("line index out of range")
88
    }
89
}
90
91
impl<'a> FromIterator<&'a [u8]> for Lines {
92
    fn from_iter<I: IntoIterator<Item = &'a [u8]>>(iter: I) -> Lines {
93
        let (mut buf, mut ends) = (Vec::new(), Vec::new());
94
        for l in iter {
95
            buf.extend_from_slice(l);
96
            ends.push(buf.len() as u64);
97
        }
98
        Lines(Arc::new(Inner {
99
            buf: Arc::new(buf),
100
            ends,
101
        }))
102
    }
103
}
104
105
impl FromIterator<Vec<u8>> for Lines {
106
    fn from_iter<I: IntoIterator<Item = Vec<u8>>>(iter: I) -> Lines {
107
        let (mut buf, mut ends) = (Vec::new(), Vec::new());
108
        for l in iter {
109
            buf.extend_from_slice(&l);
110
            ends.push(buf.len() as u64);
111
        }
112
        Lines(Arc::new(Inner {
113
            buf: Arc::new(buf),
114
            ends,
115
        }))
116
    }
117
}
118
119
impl From<Vec<Vec<u8>>> for Lines {
120
    fn from(v: Vec<Vec<u8>>) -> Lines {
121
        v.into_iter().collect()
122
    }
123
}
124
125
impl std::fmt::Debug for Lines {
126
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
127
        f.debug_list()
128
            .entries(self.iter().map(String::from_utf8_lossy))
129
            .finish()
130
    }
131
}
132
133
/// One line of a [`Lines`], held by a file graph's atom.
134
#[derive(Clone, Debug, PartialEq, Eq)]
135
pub struct LineRef {
136
    lines: Lines,
137
    index: u32,
138
}
139
140
impl LineRef {
141
    pub fn new(lines: Lines, index: u32) -> LineRef {
142
        LineRef { lines, index }
143
    }
144
145
    pub fn bytes(&self) -> &[u8] {
146
        &self.lines[self.index as usize]
147
    }
148
}
149
150
#[cfg(test)]
151
mod tests {
152
    use super::*;
153
154
    #[test]
155
    fn lines_split_index_and_join_as_the_line_rule_says() {
156
        for text in [&b""[..], b"a\n", b"a\nb", b"a\n\nb\n", b"\n", b"no end"] {
157
            let l = Lines::split(text);
158
            let want = crate::diff::split_lines(text);
159
            assert_eq!(l.len(), want.len(), "{text:?}");
160
            assert_eq!(l.iter().collect::<Vec<_>>(), want);
161
            assert_eq!(l.bytes(), text);
162
            let v: Lines = want.iter().copied().collect();
163
            assert_eq!(v, l);
164
            assert_eq!(Lines::from(l.to_vec()), l);
165
        }
166
        let l = Lines::split(b"a\nb\nc\n");
167
        assert_eq!(&l[1], b"b\n");
168
        assert_eq!(
169
            l.slice(1..3).to_vec(),
170
            vec![b"b\n".to_vec(), b"c\n".to_vec()]
171
        );
172
        assert_eq!(LineRef::new(l.clone(), 2).bytes(), b"c\n");
173
    }
174
}