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
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
//! Warning: the `super::str_impl` module depends heavily on the implementation in this crate for
//! safety! All of the cuts must be performed either immediately before or after an ascii codepoint,
//! so the resulting slices are valid str if they began as valid str.
use super::Read;
use super::{Identifier, Text};
use memchr::{memchr2_iter, memchr3_iter};
use std::str::{from_utf8, from_utf8_unchecked};

use crate::error::{Error, ErrorCode};
use crate::parse::BibtexParse;
use crate::validate::IDENTIFIER_ALLOWED;

/// Ignore junk characters between entries.
///
/// Returns (updated_pos, true) if an entry was found; otherwise (input.len(), false) if hit EOF.
pub fn next_entry_or_eof(input: &[u8], mut pos: usize) -> (usize, bool) {
    while pos < input.len() {
        pos += 1;
        match input[pos - 1] {
            b'@' => return (pos, true),
            b'%' => {
                while pos < input.len() && input[pos] != b'\n' {
                    pos += 1;
                }
                if pos == input.len() {
                    return (pos, false);
                } else {
                    // found \n, skip it
                    pos += 1
                }
            }
            _ => {}
        }
    }
    (input.len(), false)
}

/// Ignore whitespace and comments within entries.
///
/// Note that this follows the same convention as the built-in `u8::is_ascii_whitespace`
/// and in particular unlike biber does not consider U+000B VERTICAL TAB to be whitespace.
pub fn comment(input: &[u8], mut pos: usize) -> usize {
    while pos < input.len() {
        match input[pos] {
            // ASCII whitespace
            b'\t' | b'\n' | b'\x0C' | b'\r' | b' ' => pos += 1,
            // begin comment
            b'%' => {
                pos += 1;
                while pos < input.len() && input[pos] != b'\n' {
                    pos += 1;
                }
                if pos == input.len() {
                    return pos;
                } else {
                    // found \n, skip it
                    pos += 1
                }
            }
            _ => return pos,
        }
    }
    input.len()
}

/// Consume until we hit a disallowed character, and then perform UTF-8 validation.
pub fn identifier(input: &[u8], start: usize) -> Result<(usize, Identifier<&str>), Error> {
    let mut end = start;

    while end < input.len() && IDENTIFIER_ALLOWED[input[end] as usize] {
        end += 1
    }

    if end == start {
        return Err(Error::syntax(ErrorCode::Empty));
    }

    let s = from_utf8(&input[start..end])?;
    Ok((end, Identifier(s)))
}

/// Consume a non-empty sequence of digits [0-9]+.
///
/// Since ascii digits are valid UTF-8, we can skip the validation step.
pub fn number(input: &[u8], start: usize) -> Result<(usize, &str), Error> {
    let mut end = start;

    while end < input.len() && input[end].is_ascii_digit() {
        end += 1
    }

    if end == start {
        return Err(Error::syntax(ErrorCode::Empty));
    }

    // SAFETY: we only parsed ascii digits so this is guaranteed to be
    // valid utf8.
    Ok((end, unsafe { from_utf8_unchecked(&input[start..end]) }))
}

/// Consume a string with balanced brackets, until the string becomes unbalanced.
pub fn balanced(input: &[u8], start: usize) -> Result<(usize, &[u8]), Error> {
    let mut bracket_depth = 0;

    for offset in memchr2_iter(b'{', b'}', &input[start..]) {
        let end = start + offset;
        if input[end] == b'{' {
            bracket_depth += 1
        } else {
            // found the closing bracket
            if bracket_depth == 0 {
                return Ok((end, &input[start..end]));
            }
            bracket_depth -= 1;
        }
    }

    // we did not find find the closing bracket
    Err(Error::syntax(ErrorCode::UnterminatedTextToken))
}

/// Consume a string with balanced brackets, terminating when we hit a top-level byte 'until'.
///
///SAFETY: for the string version, `until` must be valid ASCII.
pub fn protected(until: u8) -> impl FnMut(&[u8], usize) -> Result<(usize, &[u8]), Error> {
    move |input: &[u8], start: usize| {
        let mut bracket_depth = 0;

        for offset in memchr3_iter(until, b'{', b'}', &input[start..]) {
            let end = start + offset;
            match input[end] {
                b if b == until => {
                    if bracket_depth == 0 {
                        return Ok((end, &input[start..end]));
                    }
                }
                b'{' => bracket_depth += 1,
                _ => {
                    if bracket_depth == 0 {
                        return Err(Error::syntax(ErrorCode::UnexpectedClosingBracket));
                    }
                    bracket_depth -= 1;
                }
            }
        }

        // we did not find an unprotected `"`
        Err(Error::syntax(ErrorCode::UnterminatedTextToken))
    }
}

super::create_input_impl::read_impl!([u8], SliceReader, Bytes, std::convert::identity);

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_next_entry_or_eof() {
        assert_eq!(next_entry_or_eof(b"junk", 0), (4, false));
        assert_eq!(next_entry_or_eof(b"junk", 2), (4, false));
        assert_eq!(next_entry_or_eof(b"", 0), (0, false));
        assert_eq!(next_entry_or_eof(b"  @art", 2), (3, true));
        assert_eq!(next_entry_or_eof(b"%@@\n@a", 0), (5, true));
        assert_eq!(next_entry_or_eof(b"\nignored @a", 0), (10, true));
        assert_eq!(next_entry_or_eof(b"%@a", 0), (3, false));
    }

    #[test]
    fn test_comment() {
        assert_eq!(comment(b"%   a\n ab", 0), 7);
        assert_eq!(comment(b"%   a\n ab", 1), 4);
        assert_eq!(comment(b"  %\na", 1), 4);
        // all valid whitespace chars
        assert_eq!(comment(b"\x09\x0a\x0c\x0d\x20b", 0), 5);
        assert_eq!(comment(b"\x09\x0a\x0c\x0d\x20b", 2), 5);
        // comments ignore everything, including invalid utf-8
        assert_eq!(comment(b"%\xa8!\xfd!\x7f!\nc", 0), 8);
        // we follow whatwg convention and do not consider U+000B VERTICAL TAB
        // to be ascii whitespace, unlike biber
        assert_eq!(comment(b"\x0b", 0), 0);
        assert_eq!(comment(b"", 0), 0);
    }

    #[test]
    fn test_protected() {
        assert_eq!(protected(b'"')(b"cap\"rest", 0), Ok((3, &b"cap"[..])));
        assert_eq!(protected(b'"')(b"cap\"rest", 1), Ok((3, &b"ap"[..])));
        assert_eq!(protected(b'"')(b"a{\"}\"rest", 0), Ok((4, &b"a{\"}"[..])));
        assert_eq!(
            protected(b'"')(b"a{{\"} \"}\"rest", 0),
            Ok((8, &b"a{{\"} \"}"[..]))
        );
        // did not find unprotected
        assert_eq!(
            protected(b'"')(b"{\"", 0),
            Err(Error::syntax(ErrorCode::UnterminatedTextToken))
        );
        // unexpected closing
        assert_eq!(
            protected(b'"')(b"}\"", 0),
            Err(Error::syntax(ErrorCode::UnexpectedClosingBracket))
        );
    }

    #[test]
    fn test_balanced() {
        assert_eq!(balanced(b"url}abc", 0), Ok((3, &b"url"[..])));
        assert_eq!(balanced("u{}rl}🍄c".as_bytes(), 0), Ok((5, &b"u{}rl"[..])));
        assert_eq!(balanced(b"u{{}}rl}abc", 1), Ok((7, &b"{{}}rl"[..])));

        assert_eq!(
            balanced(b"none", 0),
            Err(Error::syntax(ErrorCode::UnterminatedTextToken))
        );
        assert_eq!(
            balanced(b"{no}e", 0),
            Err(Error::syntax(ErrorCode::UnterminatedTextToken))
        );
    }

    use proptest::prelude::*;
    proptest! {
        #[test]
        fn no_panic(s in "\\PC*") {
            let _ = number(s.as_bytes(), 0);
        }
    }
}