Skip to main content

c0/
scan.rs

1//! Scanning primitives. Each takes a buffer, a position and a stop, and
2//! returns the position after the construct it skips; the fallible ones
3//! fail on malformed input. They are the bricks the readers are built
4//! from and are exposed for tools that index C0DATA themselves.
5
6use crate::*;
7
8/// Skip layout bytes.
9#[inline]
10pub fn layout(buf: &[u8], mut pos: usize, stop: usize) -> usize {
11    while pos < stop && is_layout(buf[pos]) {
12        pos += 1;
13    }
14    pos
15}
16
17/// Skip a plain run of bytes >= 0x20 (a label, key, payload or plain value
18/// with its layout). Returns the position of the next control code or
19/// `stop`.
20#[inline]
21pub fn plain(buf: &[u8], mut pos: usize, stop: usize) -> usize {
22    while pos < stop && buf[pos] >= 0x20 {
23        pos += 1;
24    }
25    pos
26}
27
28/// Skip a text (STX … ETX) starting at the STX at `pos`. Returns the
29/// position after the ETX. Fails on an unclosed text, a bad escape, a
30/// nested STX, or framing inside the text.
31pub fn text(buf: &[u8], pos: usize, stop: usize) -> Result<usize> {
32    let start = pos;
33    let mut pos = pos + 1; // STX
34    while pos < stop {
35        match buf[pos] {
36            DLE => {
37                pos += 1;
38                if pos >= stop {
39                    return Err(Error::end("DLE at end of text", pos));
40                }
41                if !(buf[pos] < 0x20 && is_text_escaped(buf[pos])) {
42                    return Err(Error::syntax(
43                        "DLE before a byte that is not one of the seven",
44                        pos,
45                    ));
46                }
47                pos += 1;
48            }
49            ETX => return Ok(pos + 1),
50            STX => return Err(Error::syntax("STX inside text (text does not nest)", pos)),
51            ETB | EOT | SYN | EM => return Err(Error::syntax("framing inside open text", pos)),
52            _ => pos += 1,
53        }
54    }
55    Err(Error::end(
56        format!("text opened at {start} is not closed"),
57        pos,
58    ))
59}
60
61/// Skip a nested level starting at the DLE at `pos`. Returns the position
62/// after the matching CAN.
63pub fn level(buf: &[u8], pos: usize, stop: usize) -> Result<usize> {
64    let start = pos;
65    let mut pos = pos + 1; // DLE
66    let mut depth = 1;
67    while pos < stop {
68        let byte = buf[pos];
69        if byte >= 0x20 {
70            pos += 1;
71        } else if byte == STX {
72            pos = text(buf, pos, stop)?;
73        } else if byte == DLE {
74            depth += 1;
75            pos += 1;
76        } else if byte == CAN {
77            depth -= 1;
78            pos += 1;
79            if depth == 0 {
80                return Ok(pos);
81            }
82        } else if byte == ETB || byte == EOT || byte == SYN || byte == EM {
83            return Err(Error::syntax("framing inside open level", pos));
84        } else {
85            pos += 1;
86        }
87    }
88    Err(Error::end(
89        format!("level opened at {start} is not closed"),
90        pos,
91    ))
92}
93
94/// From `pos` (just after a US, RS or a key's US), find the end of one
95/// unit's value: the next control code at this depth other than the ones
96/// that belong to the value (STX…ETX, DLE…CAN, ENQ, BEL note). Layout at
97/// the edges is included in the returned range.
98pub fn unit(buf: &[u8], mut pos: usize, stop: usize) -> Result<usize> {
99    while pos < stop {
100        let byte = buf[pos];
101        if byte >= 0x20 || is_layout(byte) {
102            pos += 1;
103        } else if byte == STX {
104            pos = text(buf, pos, stop)?;
105        } else if byte == DLE {
106            pos = level(buf, pos, stop)?;
107        } else if byte == ENQ {
108            pos += 1;
109        } else if byte == BEL {
110            // a note or a dead level stays inside the unit; a dead
111            // record-level element (BEL RS / SOH / GS / FS) ends it
112            let p2 = layout(buf, pos + 1, stop);
113            if p2 < stop && buf[p2] < 0x20 && buf[p2] != STX && buf[p2] != DLE {
114                return Ok(pos);
115            }
116            pos = comment(buf, pos, stop)?;
117        } else {
118            return Ok(pos);
119        }
120    }
121    Ok(pos)
122}
123
124/// Skip a comment starting at the BEL at `pos`: a note (text or plain to
125/// the next code) or a dead element (the next code's whole element).
126/// Returns the position after it.
127pub fn comment(buf: &[u8], pos: usize, stop: usize) -> Result<usize> {
128    let pos = layout(buf, pos + 1, stop); // BEL
129    if pos >= stop {
130        return Err(Error::end("BEL at end of input", pos));
131    }
132    let byte = buf[pos];
133    if byte >= 0x20 {
134        Ok(plain(buf, pos, stop))
135    } else if byte == STX {
136        text(buf, pos, stop)
137    } else if byte == DLE {
138        level(buf, pos, stop)
139    } else if byte == RS {
140        record_end(buf, pos + 1, stop)
141    } else if byte == SOH {
142        // header row or keyed unit
143        let p2 = layout(buf, pos + 1, stop);
144        if p2 < stop && buf[p2] == RS {
145            record_end(buf, p2 + 1, stop)
146        } else {
147            keyed_unit_end(buf, pos + 1, stop)
148        }
149    } else if byte == GS {
150        // one group line: GS repetition is an empty group, not depth
151        Ok(plain(buf, pos + 1, stop))
152    } else if byte == SUB {
153        // a dead group line with its depth prefix: BEL SUB… GS name
154        let mut p2 = pos;
155        while p2 < stop && buf[p2] == SUB {
156            p2 = layout(buf, p2 + 1, stop);
157        }
158        if p2 < stop && buf[p2] == GS {
159            Ok(plain(buf, p2 + 1, stop))
160        } else {
161            Err(Error::syntax(
162                "BEL before a code that is not an element",
163                pos,
164            ))
165        }
166    } else if byte == FS {
167        Ok(plain(buf, pos + 1, stop))
168    } else {
169        Err(Error::syntax(
170            "BEL before a code that is not an element",
171            pos,
172        ))
173    }
174}
175
176/// From just after an RS, find the end of the record: the next
177/// record-level code at this depth. Returns that position.
178pub fn record_end(buf: &[u8], mut pos: usize, stop: usize) -> Result<usize> {
179    while pos < stop {
180        let byte = buf[pos];
181        if byte >= 0x20 || is_layout(byte) || byte == US || byte == ENQ {
182            pos += 1;
183        } else if byte == SUB {
184            // a sub-unit (a SUB run then US) stays inside the record; a SUB
185            // prefix before a line code ends this record first
186            let mut p2 = pos;
187            while p2 < stop && buf[p2] == SUB {
188                p2 = layout(buf, p2 + 1, stop);
189            }
190            if !(p2 < stop && buf[p2] == US) {
191                return Ok(pos);
192            }
193            pos = p2;
194        } else if byte == SOH {
195            // a header row (SOH RS) ends the record; a keyed unit is inside it
196            let p2 = layout(buf, pos + 1, stop);
197            if p2 < stop && buf[p2] == RS {
198                return Ok(pos);
199            }
200            pos += 1;
201        } else if byte == STX {
202            pos = text(buf, pos, stop)?;
203        } else if byte == DLE {
204            pos = level(buf, pos, stop)?;
205        } else if byte == BEL {
206            // a note inside the record, or a dead element; a dead
207            // record-level element (BEL RS / BEL GS / BEL SUB… GS …) ends
208            // this record first.
209            let p2 = layout(buf, pos + 1, stop);
210            if p2 < stop && matches!(buf[p2], RS | GS | FS | SUB) {
211                return Ok(pos);
212            }
213            if p2 < stop && buf[p2] == SOH {
214                let p3 = layout(buf, p2 + 1, stop);
215                if p3 < stop && buf[p3] == RS {
216                    return Ok(pos);
217                }
218            }
219            pos = comment(buf, pos, stop)?;
220        } else {
221            return Ok(pos);
222        }
223    }
224    Ok(pos)
225}
226
227/// From just after an SOH (not a header row), skip the key, US and value
228/// of one keyed unit. Returns the position after the value.
229pub fn keyed_unit_end(buf: &[u8], pos: usize, stop: usize) -> Result<usize> {
230    let pos = plain(buf, layout(buf, pos, stop), stop);
231    if !(pos < stop && buf[pos] == US) {
232        return Err(Error::syntax("key without a value (no US)", pos));
233    }
234    unit(buf, pos + 1, stop)
235}