sonic_rs/
reader.rs

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
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
use std::{marker::PhantomData, ptr::NonNull};

use crate::{
    error::invalid_utf8,
    input::JsonSlice,
    util::{private::Sealed, utf8::from_utf8},
    JsonInput, Result,
};

pub(crate) struct Position {
    pub line: usize,
    pub column: usize,
}

impl Position {
    pub(crate) fn from_index(mut i: usize, data: &[u8]) -> Self {
        // i must not exceed the length of data
        i = i.min(data.len());
        let mut position = Position { line: 1, column: 0 };
        for ch in &data[..i] {
            match *ch {
                b'\n' => {
                    position.line += 1;
                    position.column = 0;
                }
                _ => {
                    position.column += 1;
                }
            }
        }
        position
    }
}

/// Trait is used by the deserializer for iterating over input. And it is sealed and cannot be
/// implemented for types outside of sonic_rs.
#[doc(hidden)]
pub trait Reader<'de>: Sealed {
    fn remain(&self) -> usize;
    fn eat(&mut self, n: usize);
    fn backward(&mut self, n: usize);
    fn peek_n(&mut self, n: usize) -> Option<&'de [u8]>;
    fn peek(&mut self) -> Option<u8>;
    fn index(&self) -> usize;
    fn at(&self, index: usize) -> u8;
    fn set_index(&mut self, index: usize);
    fn next_n(&mut self, n: usize) -> Option<&'de [u8]>;

    #[inline(always)]
    fn next(&mut self) -> Option<u8> {
        self.peek().inspect(|_| {
            self.eat(1);
        })
    }
    fn cur_ptr(&mut self) -> *mut u8;

    /// # Safety
    /// cur must be a valid pointer in the slice
    unsafe fn set_ptr(&mut self, cur: *mut u8);
    fn slice_unchecked(&self, start: usize, end: usize) -> &'de [u8];

    fn as_u8_slice(&self) -> &'de [u8];

    fn check_utf8_final(&self) -> Result<()>;

    fn next_invalid_utf8(&self) -> usize;

    fn check_invalid_utf8(&mut self);

    fn slice_ref(&self, subset: &'de [u8]) -> JsonSlice<'de>;
}

/// JSON input source that reads from a string/bytes-like JSON input.
///
/// Support most common types: &str, &[u8], &FastStr, &Bytes and &String
///
/// # Examples
/// ```
/// use bytes::Bytes;
/// use faststr::FastStr;
/// use serde::de::Deserialize;
/// use sonic_rs::{Deserializer, Read};
///
/// let mut de = Deserializer::new(Read::from(r#"123"#));
/// let num: i32 = Deserialize::deserialize(&mut de).unwrap();
/// assert_eq!(num, 123);
///
/// let mut de = Deserializer::new(Read::from(r#"123"#.as_bytes()));
/// let num: i32 = Deserialize::deserialize(&mut de).unwrap();
/// assert_eq!(num, 123);
///
/// let f = FastStr::new("123");
/// let mut de = Deserializer::new(Read::from(&f));
/// let num: i32 = Deserialize::deserialize(&mut de).unwrap();
/// assert_eq!(num, 123);
/// ```
pub struct Read<'a> {
    // pin the input JSON, because `slice` will reference it
    input: Box<JsonSlice<'a>>,
    slice: &'a [u8],
    pub(crate) index: usize,
    // next invalid utf8 position, if not found, will be usize::MAX
    next_invalid_utf8: usize,
}

impl<'a> Read<'a> {
    /// Make a `Read` from string/bytes-like JSON input.
    pub fn from<I: JsonInput<'a>>(input: I) -> Self {
        let need = input.need_utf8_valid();
        Self::new_in(input, need)
    }

    pub(crate) fn new(slice: &'a [u8], need_validate: bool) -> Self {
        Self::new_in(slice, need_validate)
    }

    pub(crate) fn new_in<I: JsonInput<'a>>(input: I, need_validate: bool) -> Self {
        let input = Box::new(input.to_json_slice());
        // #safety: we pinned the input json
        let slice = unsafe { &*((*input).as_ref() as *const [u8]) };
        // validate the utf-8 at first for slice
        let next_invalid_utf8 = match from_utf8(slice) {
            Err(e) if need_validate => e.offset(),
            _ => usize::MAX,
        };

        Self {
            input,
            slice,
            index: 0,
            next_invalid_utf8,
        }
    }
}

impl<'a> Reader<'a> for Read<'a> {
    #[inline(always)]
    fn remain(&self) -> usize {
        self.slice.len() - self.index
    }

    #[inline(always)]
    fn slice_ref(&self, subset: &'a [u8]) -> JsonSlice<'a> {
        self.input.slice_ref(subset)
    }

    #[inline(always)]
    fn peek_n(&mut self, n: usize) -> Option<&'a [u8]> {
        let end = self.index + n;
        (end <= self.slice.len()).then(|| {
            let ptr = self.slice[self.index..].as_ptr();
            unsafe { std::slice::from_raw_parts(ptr, n) }
        })
    }

    #[inline(always)]
    fn set_index(&mut self, index: usize) {
        self.index = index
    }

    #[inline(always)]
    fn peek(&mut self) -> Option<u8> {
        if self.index < self.slice.len() {
            Some(self.slice[self.index])
        } else {
            None
        }
    }

    #[inline(always)]
    fn at(&self, index: usize) -> u8 {
        self.slice[index]
    }

    #[inline(always)]
    fn next_n(&mut self, n: usize) -> Option<&'a [u8]> {
        let new_index = self.index + n;
        if new_index <= self.slice.len() {
            let ret = &self.slice[self.index..new_index];
            self.index = new_index;
            Some(ret)
        } else {
            None
        }
    }

    #[inline(always)]
    fn cur_ptr(&mut self) -> *mut u8 {
        panic!("should only used in PaddedSliceRead");
    }

    #[inline(always)]
    unsafe fn set_ptr(&mut self, _cur: *mut u8) {
        panic!("should only used in PaddedSliceRead");
    }

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

    #[inline(always)]
    fn eat(&mut self, n: usize) {
        self.index += n;
    }

    #[inline(always)]
    fn backward(&mut self, n: usize) {
        self.index -= n;
    }

    #[inline(always)]
    fn slice_unchecked(&self, start: usize, end: usize) -> &'a [u8] {
        &self.slice[start..end]
    }

    #[inline(always)]
    fn as_u8_slice(&self) -> &'a [u8] {
        self.slice
    }

    #[inline(always)]
    fn check_utf8_final(&self) -> Result<()> {
        if self.next_invalid_utf8 == usize::MAX {
            Ok(())
        } else {
            Err(invalid_utf8(self.slice, self.next_invalid_utf8))
        }
    }

    fn check_invalid_utf8(&mut self) {
        self.next_invalid_utf8 = match from_utf8(&self.slice[self.index..]) {
            Ok(_) => usize::MAX,
            Err(e) => self.index + e.offset(),
        };
    }

    fn next_invalid_utf8(&self) -> usize {
        self.next_invalid_utf8
    }
}

pub(crate) struct PaddedSliceRead<'a> {
    base: NonNull<u8>,
    cur: NonNull<u8>,
    len: usize,
    _life: PhantomData<&'a mut [u8]>,
}

impl<'a> PaddedSliceRead<'a> {
    const PADDING_SIZE: usize = 64;
    pub fn new(slice: &'a mut [u8]) -> Self {
        let base = unsafe { NonNull::new_unchecked(slice.as_mut_ptr()) };
        Self {
            base,
            cur: base,
            len: slice.len() - Self::PADDING_SIZE,
            _life: PhantomData,
        }
    }
}

impl<'a> Reader<'a> for PaddedSliceRead<'a> {
    #[inline(always)]
    fn as_u8_slice(&self) -> &'a [u8] {
        unsafe { std::slice::from_raw_parts(self.base.as_ptr(), self.len) }
    }

    #[inline(always)]
    fn slice_ref(&self, subset: &'a [u8]) -> JsonSlice<'a> {
        subset.into()
    }

    #[inline(always)]
    fn remain(&self) -> usize {
        let remain = self.len as isize - self.index() as isize;
        std::cmp::max(remain, 0) as usize
    }

    #[inline(always)]
    fn peek_n(&mut self, n: usize) -> Option<&'a [u8]> {
        unsafe { Some(std::slice::from_raw_parts(self.cur.as_ptr(), n)) }
    }

    #[inline(always)]
    fn set_index(&mut self, index: usize) {
        unsafe { self.cur = NonNull::new_unchecked(self.base.as_ptr().add(index)) }
    }

    #[inline(always)]
    fn peek(&mut self) -> Option<u8> {
        unsafe { Some(*self.cur.as_ptr()) }
    }

    #[inline(always)]
    fn at(&self, index: usize) -> u8 {
        unsafe { *(self.base.as_ptr().add(index)) }
    }

    #[inline(always)]
    fn next_n(&mut self, n: usize) -> Option<&'a [u8]> {
        unsafe {
            let ptr = self.cur.as_ptr();
            self.cur = NonNull::new_unchecked(ptr.add(n));
            Some(std::slice::from_raw_parts(ptr, n))
        }
    }

    #[inline(always)]
    fn index(&self) -> usize {
        unsafe { self.cur.as_ptr().offset_from(self.base.as_ptr()) as usize }
    }

    fn eat(&mut self, n: usize) {
        unsafe {
            self.cur = NonNull::new_unchecked(self.cur.as_ptr().add(n));
        }
    }

    #[inline(always)]
    fn cur_ptr(&mut self) -> *mut u8 {
        self.cur.as_ptr()
    }

    #[inline(always)]
    unsafe fn set_ptr(&mut self, cur: *mut u8) {
        self.cur = NonNull::new_unchecked(cur);
    }

    #[inline(always)]
    fn backward(&mut self, n: usize) {
        unsafe {
            self.cur = NonNull::new_unchecked(self.cur.as_ptr().sub(n));
        }
    }

    #[inline(always)]
    fn slice_unchecked(&self, start: usize, end: usize) -> &'a [u8] {
        unsafe {
            let ptr = self.base.as_ptr().add(start);
            let n = end - start;
            std::slice::from_raw_parts(ptr, n)
        }
    }

    #[inline(always)]
    fn check_invalid_utf8(&mut self) {
        /* need to nothing here */
    }

    #[inline(always)]
    fn next_invalid_utf8(&self) -> usize {
        usize::MAX
    }

    #[inline(always)]
    fn check_utf8_final(&self) -> Result<()> {
        Ok(())
    }
}

#[cfg(test)]
mod test {
    use bytes::Bytes;
    use faststr::FastStr;

    use super::*;
    use crate::{Deserialize, Deserializer};
    fn test_peek() {
        let data = b"1234567890";
        let mut reader = Read::new(data, false);
        assert_eq!(reader.peek(), Some(b'1'));
        assert_eq!(reader.peek_n(4).unwrap(), &b"1234"[..]);
    }

    fn test_next() {
        let data = b"1234567890";
        let mut reader = Read::new(data, false);
        assert_eq!(reader.next(), Some(b'1'));
        assert_eq!(reader.peek(), Some(b'2'));
        assert_eq!(reader.next_n(4).unwrap(), &b"2345"[..]);
        assert_eq!(reader.peek(), Some(b'6'));
    }

    fn test_index() {
        let data = b"1234567890";
        let mut reader = Read::new(data, false);
        assert_eq!(reader.index(), 0);

        reader.next().unwrap();
        assert_eq!(reader.index(), 1);

        reader.next_n(4).unwrap();
        assert_eq!(reader.index(), 5);
    }

    #[test]
    fn test_reader() {
        test_peek();
        test_next();
        test_index();
    }

    macro_rules! test_deserialize_reader {
        ($json:expr) => {
            let mut de = Deserializer::new(Read::from($json));
            let num: i32 = Deserialize::deserialize(&mut de).unwrap();
            assert_eq!(num, 123);
        };
    }

    #[test]
    fn test_deserialize() {
        let b = Bytes::from(r#"123"#);
        let f = FastStr::from(r#"123"#);
        let s = String::from(r#"123"#);
        test_deserialize_reader!(r#"123"#);
        test_deserialize_reader!(r#"123"#.as_bytes());
        test_deserialize_reader!(&b);
        test_deserialize_reader!(&f);
        test_deserialize_reader!(&s);
    }
}