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
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
//! Borrows a slice of bytes of the input document.
//!
//! Choose this implementation if:
//!
//! 1. You already have the data loaded in-memory and can borrow it while
//! using the engine.
//!
//! ## Performance characteristics
//!
//! This type of input is the fastest to process for the engine,
//! since there is no additional overhead from loading anything to memory.
//! It is on par with [`OwnedBytes`](`super::OwnedBytes`), but doesn't take ownership
//! of the bytes.

use super::{
    align_to,
    error::Infallible,
    padding::{EndPaddedInput, PaddedBlock, TwoSidesPaddedInput},
    Input, InputBlockIterator, SliceSeekable, MAX_BLOCK_SIZE,
};
use crate::{debug, result::InputRecorder};
use rsonpath_syntax::str::JsonString;

/// Input wrapping a borrowed [`[u8]`] buffer.
pub struct BorrowedBytes<'a> {
    middle_bytes: &'a [u8],
    first_block: PaddedBlock,
    last_block: PaddedBlock,
}

/// Iterator over blocks of [`BorrowedBytes`] of size exactly `N`.
pub struct BorrowedBytesBlockIterator<'r, I, R, const N: usize> {
    input: I,
    idx: usize,
    recorder: &'r R,
}

impl<'a> BorrowedBytes<'a> {
    /// Create a new instance of [`BorrowedBytes`] wrapping the given buffer.
    ///
    /// The input will be automatically padded internally, incurring at most
    /// two times [`MAX_BLOCK_SIZE`] of memory overhead.
    #[must_use]
    #[inline(always)]
    pub fn new(bytes: &'a [u8]) -> Self {
        let (first, middle, last) = align_to::<MAX_BLOCK_SIZE>(bytes);
        let first_block = PaddedBlock::pad_first_block(first);
        let last_block = PaddedBlock::pad_last_block(last);

        Self {
            middle_bytes: middle,
            first_block,
            last_block,
        }
    }

    pub(super) fn as_padded_input(&self) -> TwoSidesPaddedInput {
        TwoSidesPaddedInput::new(&self.first_block, self.middle_bytes, &self.last_block)
    }
}

impl<'a> From<&'a [u8]> for BorrowedBytes<'a> {
    #[inline(always)]
    fn from(value: &'a [u8]) -> Self {
        BorrowedBytes::new(value)
    }
}

impl<'a> From<&'a str> for BorrowedBytes<'a> {
    #[inline(always)]
    fn from(value: &'a str) -> Self {
        BorrowedBytes::new(value.as_bytes())
    }
}

impl<'a, 'r, I, R, const N: usize> BorrowedBytesBlockIterator<'r, I, R, N>
where
    R: InputRecorder<&'a [u8]>,
{
    #[must_use]
    #[inline(always)]
    pub(super) fn new(input: I, recorder: &'r R) -> Self {
        Self {
            idx: 0,
            input,
            recorder,
        }
    }
}

impl<'a> Input for BorrowedBytes<'a> {
    type BlockIterator<'b, 'r, R, const N: usize> = BorrowedBytesBlockIterator<'r, TwoSidesPaddedInput<'b>, R, N>
    where Self: 'b,
          R: InputRecorder<&'b [u8]> + 'r;

    type Error = Infallible;
    type Block<'b, const N: usize> = &'b [u8] where Self: 'b;

    #[inline(always)]
    fn leading_padding_len(&self) -> usize {
        self.first_block.padding_len()
    }

    #[inline(always)]
    fn trailing_padding_len(&self) -> usize {
        self.last_block.padding_len()
    }

    #[inline(always)]
    fn len_hint(&self) -> Option<usize> {
        Some(self.middle_bytes.len() + self.first_block.len() + self.last_block.len())
    }

    #[inline(always)]
    fn iter_blocks<'b, 'r, R, const N: usize>(&'b self, recorder: &'r R) -> Self::BlockIterator<'b, 'r, R, N>
    where
        R: InputRecorder<&'b [u8]>,
    {
        let padded_input = TwoSidesPaddedInput::new(&self.first_block, self.middle_bytes, &self.last_block);

        Self::BlockIterator {
            idx: 0,
            input: padded_input,
            recorder,
        }
    }

    #[inline]
    fn seek_backward(&self, from: usize, needle: u8) -> Option<usize> {
        return if from >= MAX_BLOCK_SIZE && from < self.middle_bytes.len() + MAX_BLOCK_SIZE {
            match self.middle_bytes.seek_backward(from - MAX_BLOCK_SIZE, needle) {
                Some(x) => Some(x + MAX_BLOCK_SIZE),
                None => handle_first(&self.first_block, needle),
            }
        } else {
            self.as_padded_input().seek_backward(from, needle)
        };

        #[cold]
        #[inline(never)]
        fn handle_first(first_block: &PaddedBlock, needle: u8) -> Option<usize> {
            first_block.bytes().seek_backward(first_block.len() - 1, needle)
        }
    }

    #[inline]
    fn seek_forward<const N: usize>(&self, from: usize, needles: [u8; N]) -> Result<Option<(usize, u8)>, Infallible> {
        return Ok(
            if from >= MAX_BLOCK_SIZE && from < self.middle_bytes.len() + MAX_BLOCK_SIZE {
                match self.middle_bytes.seek_forward(from - MAX_BLOCK_SIZE, needles) {
                    Some((x, y)) => Some((x + MAX_BLOCK_SIZE, y)),
                    None => handle_last(&self.last_block, MAX_BLOCK_SIZE + self.middle_bytes.len(), needles),
                }
            } else {
                self.as_padded_input().seek_forward(from, needles)
            },
        );

        #[cold]
        #[inline(never)]
        fn handle_last<const N: usize>(
            last_block: &PaddedBlock,
            offset: usize,
            needles: [u8; N],
        ) -> Option<(usize, u8)> {
            last_block
                .bytes()
                .seek_forward(0, needles)
                .map(|(x, y)| (x + offset, y))
        }
    }

    #[inline]
    fn seek_non_whitespace_forward(&self, from: usize) -> Result<Option<(usize, u8)>, Infallible> {
        return Ok(
            // The hot path is when we start and end within the middle section.
            // We use the regular slice path for that scenario, and fall back to the very expensive
            // TwoSidesPaddedInput with all bells and whistles only when that doesn't work.
            if from >= MAX_BLOCK_SIZE && from < self.middle_bytes.len() + MAX_BLOCK_SIZE {
                match self.middle_bytes.seek_non_whitespace_forward(from - MAX_BLOCK_SIZE) {
                    Some((x, y)) => Some((x + MAX_BLOCK_SIZE, y)),
                    None => handle_last(&self.last_block, MAX_BLOCK_SIZE + self.middle_bytes.len()),
                }
            } else {
                self.as_padded_input().seek_non_whitespace_forward(from)
            },
        );

        #[cold]
        #[inline(never)]
        fn handle_last(last_block: &PaddedBlock, offset: usize) -> Option<(usize, u8)> {
            last_block
                .bytes()
                .seek_non_whitespace_forward(0)
                .map(|(x, y)| (x + offset, y))
        }
    }

    #[inline]
    fn seek_non_whitespace_backward(&self, from: usize) -> Option<(usize, u8)> {
        return if from >= MAX_BLOCK_SIZE && from < self.middle_bytes.len() + MAX_BLOCK_SIZE {
            match self.middle_bytes.seek_non_whitespace_backward(from - MAX_BLOCK_SIZE) {
                Some((x, y)) => Some((x + MAX_BLOCK_SIZE, y)),
                None => handle_first(&self.first_block),
            }
        } else {
            self.as_padded_input().seek_non_whitespace_backward(from)
        };

        #[cold]
        #[inline(never)]
        fn handle_first(first_block: &PaddedBlock) -> Option<(usize, u8)> {
            first_block.bytes().seek_non_whitespace_backward(first_block.len() - 1)
        }
    }

    #[inline(always)]
    fn is_member_match(&self, from: usize, to: usize, member: &JsonString) -> Result<bool, Self::Error> {
        debug_assert!(from < to);
        // The hot path is when we're checking fully within the middle section.
        // This has to be as fast as possible, so the "cold" path referring to the TwoSidesPaddedInput
        // impl is explicitly marked with #[cold].
        if from > MAX_BLOCK_SIZE && to < self.middle_bytes.len() + MAX_BLOCK_SIZE {
            // This is the hot path -- do the bounds check and memcmp.
            let bytes = self.middle_bytes;
            let from = from - MAX_BLOCK_SIZE;
            let to = to - MAX_BLOCK_SIZE;
            let slice = &bytes[from..to];
            Ok(member.quoted().as_bytes() == slice && (from == 0 || bytes[from - 1] != b'\\'))
        } else {
            // This is a very expensive, cold path.
            Ok(self.as_padded_input().is_member_match(from, to, member))
        }
    }
}

impl<'a, 'r, R, const N: usize> InputBlockIterator<'a, N>
    for BorrowedBytesBlockIterator<'r, TwoSidesPaddedInput<'a>, R, N>
where
    R: InputRecorder<&'a [u8]> + 'r,
{
    type Block = &'a [u8];
    type Error = Infallible;

    #[inline(always)]
    fn next(&mut self) -> Result<Option<Self::Block>, Self::Error> {
        debug!("next!");
        return if self.idx >= MAX_BLOCK_SIZE && self.idx < self.input.middle().len() + MAX_BLOCK_SIZE {
            let start = self.idx - MAX_BLOCK_SIZE;
            // SAFETY: Bounds check above.
            // self.idx >= MBS => start >= 0, and self.idx < middle.len + MBS => self.idx < middle.len
            // By construction, middle has length divisible by N.
            let block = unsafe { self.input.middle().get_unchecked(start..start + N) };
            self.recorder.record_block_start(block);
            self.idx += N;
            Ok(Some(block))
        } else {
            Ok(cold_path(self))
        };

        #[cold]
        fn cold_path<'a, 'r, R, const N: usize>(
            iter: &mut BorrowedBytesBlockIterator<'r, TwoSidesPaddedInput<'a>, R, N>,
        ) -> Option<&'a [u8]>
        where
            R: InputRecorder<&'a [u8]>,
        {
            let block = iter.input.try_slice(iter.idx, N);

            if let Some(b) = block {
                iter.recorder.record_block_start(b);
                iter.idx += N;
            }

            block
        }
    }

    #[inline(always)]
    fn offset(&mut self, count: isize) {
        assert!(count >= 0);
        debug!("offsetting input iter by {count}");
        self.idx += count as usize * N;
    }

    #[inline(always)]
    fn get_offset(&self) -> usize {
        debug!("getting input iter {}", self.idx);
        self.idx
    }
}

impl<'a, 'r, R, const N: usize> InputBlockIterator<'a, N> for BorrowedBytesBlockIterator<'r, EndPaddedInput<'a>, R, N>
where
    R: InputRecorder<&'a [u8]> + 'r,
{
    type Block = &'a [u8];
    type Error = Infallible;

    #[inline(always)]
    fn next(&mut self) -> Result<Option<Self::Block>, Self::Error> {
        debug!("next!");
        return if self.idx < self.input.middle().len() {
            let start = self.idx;
            // SAFETY: Bounds check above.
            // self.idx >= MBS => start >= 0, and self.idx < middle.len + MBS => self.idx < middle.len
            // By construction, middle has length divisible by N.
            let block = unsafe { self.input.middle().get_unchecked(start..start + N) };
            self.recorder.record_block_start(block);
            self.idx += N;
            Ok(Some(block))
        } else {
            Ok(cold_path(self))
        };

        #[cold]
        fn cold_path<'a, 'r, R, const N: usize>(
            iter: &mut BorrowedBytesBlockIterator<'r, EndPaddedInput<'a>, R, N>,
        ) -> Option<&'a [u8]>
        where
            R: InputRecorder<&'a [u8]>,
        {
            let block = iter.input.try_slice(iter.idx, N);

            if let Some(b) = block {
                iter.recorder.record_block_start(b);
                iter.idx += N;
            }

            block
        }
    }

    #[inline(always)]
    fn offset(&mut self, count: isize) {
        assert!(count >= 0);
        debug!("offsetting input iter by {count}");
        self.idx += count as usize * N;
    }

    #[inline(always)]
    fn get_offset(&self) -> usize {
        debug!("getting input iter {}", self.idx);
        self.idx
    }
}