gausplat_loader/function/
decode.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
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
//! Functions for decoding process.

pub use super::{NULL, SPACE};
pub use crate::error::Error;

use std::io::Read;

/// Decoding function.
pub trait Decoder: Sized {
    /// Error during decoding.
    type Err;

    /// Decoding from the `reader`.
    fn decode(reader: &mut impl Read) -> Result<Self, Self::Err>;
}

/// Decoding function with an initial state.
pub trait DecoderWith<T>
where
    Self: Sized,
{
    /// Error during decoding.
    type Err;

    /// Decoding from the `reader` with the `init` state.
    fn decode_with(
        reader: &mut impl Read,
        init: T,
    ) -> Result<Self, Self::Err>;
}

/// Discarding `n` bytes.
#[inline]
pub fn advance(
    reader: &mut impl Read,
    n: usize,
) -> Result<(), Error> {
    // Using a cache size of 128 bytes.
    const CACHE_SIZE: usize = 1 << CACHE_SIZE_LEVEL;
    const CACHE_SIZE_LEVEL: usize = 7;
    const CACHE_SIZE_MASK: usize = CACHE_SIZE - 1;
    let cache = &mut [0; CACHE_SIZE];

    (0..n >> CACHE_SIZE_LEVEL)
        .try_for_each(|_| reader.read_exact(cache))
        .and_then(|_| reader.read_exact(&mut cache[..n & CACHE_SIZE_MASK]))
        .map_err(Into::into)
}

/// Checking if the byte is [`NULL[0]`](NULL).
#[inline]
pub const fn is_null(byte: u8) -> bool {
    byte == NULL[0]
}

/// Checking if the byte is [`SPACE[0]`](SPACE).
#[inline]
pub const fn is_space(byte: u8) -> bool {
    byte == SPACE[0]
}

/// Reading `n` bytes.
#[inline]
pub fn read_bytes(
    reader: &mut impl Read,
    n: usize,
) -> Result<Vec<u8>, Error> {
    let mut bytes = vec![0; n];
    reader.read_exact(&mut bytes)?;
    Ok(bytes)
}

/// Reading a byte after all delimiter bytes.
#[inline]
pub fn read_byte_after(
    reader: &mut impl Read,
    delimiter: impl Fn(u8) -> bool,
) -> Result<u8, Error> {
    loop {
        let byte = &mut [0; 1];
        reader.read_exact(byte)?;
        if !delimiter(byte[0]) {
            return Ok(byte[0]);
        }
    }
}

/// Reading all bytes before the delimiter.
#[inline]
pub fn read_bytes_before(
    reader: &mut impl Read,
    delimiter: impl Fn(u8) -> bool,
    capacity: usize,
) -> Result<Vec<u8>, Error> {
    let mut bytes = Vec::with_capacity(capacity);
    loop {
        let byte = &mut [0; 1];
        reader.read_exact(byte)?;
        if delimiter(byte[0]) {
            return Ok(bytes);
        }
        bytes.push(byte[0]);
    }
}

/// Reading `N` bytes.
#[inline]
pub fn read_bytes_const<const N: usize>(
    reader: &mut impl Read
) -> Result<[u8; N], Error> {
    let mut bytes = [0; N];
    reader.read_exact(&mut bytes)?;
    Ok(bytes)
}

/// Reading all bytes before the delimiters.
#[inline]
pub fn read_bytes_before_many_const<const N: usize>(
    reader: &mut impl Read,
    delimiters: &[u8; N],
    capacity: usize,
) -> Result<Vec<u8>, Error> {
    debug_assert_ne!(0, N);

    let mut bytes = Vec::with_capacity(capacity);
    let mut ring = [0; N];
    let mut pos = 0;
    loop {
        let byte = &mut [0; 1];
        reader.read_exact(byte)?;
        bytes.push(byte[0]);
        ring[pos % N] = byte[0];
        pos += 1;
        if pos >= N && (0..N).all(|idx| ring[(idx + pos) % N] == delimiters[idx]) {
            bytes.truncate(pos - N);
            return Ok(bytes);
        }
    }
}

/// Reading all bytes before the CRLF, LF.
#[inline]
pub fn read_bytes_before_newline(
    reader: &mut impl Read,
    capacity: usize,
) -> Result<Vec<u8>, Error> {
    let mut bytes = Vec::with_capacity(capacity);
    loop {
        let byte = &mut [0; 1];
        reader.read_exact(byte)?;
        if byte[0] == b'\n' {
            return Ok(bytes);
        }
        if byte[0] == b'\r' {
            reader.read_exact(byte)?;
            if byte[0] == b'\n' {
                return Ok(bytes);
            }
            bytes.push(b'\r');
        }
        bytes.push(byte[0]);
    }
}

/// Reading until the delimiters.
#[inline]
pub fn read_bytes_until_many_const<const N: usize>(
    reader: &mut impl Read,
    delimiters: &[u8; N],
) -> Result<(), Error> {
    debug_assert_ne!(0, N);

    let mut ring = [0; N];
    let mut pos = 0;
    loop {
        let byte = &mut [0; 1];
        reader.read_exact(byte)?;
        ring[pos % N] = byte[0];
        pos += 1;
        if pos >= N && (0..N).all(|idx| ring[(idx + pos) % N] == delimiters[idx]) {
            return Ok(());
        }
    }
}

/// Reading exact one CRLF or LF.
#[inline]
pub fn read_newline(reader: &mut impl Read) -> Result<Box<[u8]>, Error> {
    let mut byte = [0; 1];
    reader.read_exact(&mut byte)?;
    if byte[0] == b'\n' {
        return Ok([b'\n'].into());
    }
    if byte[0] == b'\r' {
        reader.read_exact(&mut byte)?;
        if byte[0] == b'\n' {
            return Ok([b'\r', b'\n'].into());
        }
    }
    Err(Error::MissingSymbol("<newline> (CRLF or LF)".into()))
}

#[cfg(test)]
mod tests {
    use std::io;

    struct InvalidRead;

    impl io::Read for InvalidRead {
        fn read(
            &mut self,
            _: &mut [u8],
        ) -> io::Result<usize> {
            Err(io::Error::new(io::ErrorKind::InvalidData, "cannot read"))
        }
    }

    #[test]
    fn advance() {
        use super::*;
        use std::io::Cursor;

        let reader = &mut Cursor::new(b"\x01\x02\0\0\x04\0\x50\0");

        advance(reader, 4).unwrap();
        let output = read_bytes_const(reader).unwrap();
        let target = [0x04, 0x00, 0x50, 0x00];
        assert_eq!(output, target);

        advance(reader, 4).unwrap_err();
    }

    #[test]
    fn read_bytes() {
        use super::*;
        use std::io::Cursor;

        let source =
            &include_bytes!("../../examples/data/hello-world/ascii+binary.dat")[..];
        let reader = &mut Cursor::new(source);

        let target = &source[0..24];
        let output = read_bytes(reader, 24).unwrap();
        assert_eq!(output, target);

        let target = &source[24..40];
        let output = read_bytes(reader, 16).unwrap();
        assert_eq!(output, target);

        read_bytes(reader, 2).unwrap_err();

        let target = std::io::ErrorKind::UnexpectedEof;
        let output = reader.read_exact(&mut [0; 1]).unwrap_err().kind();
        assert_eq!(output, target);

        read_bytes(reader, 1).unwrap_err();
    }

    #[test]
    fn read_byte_after() {
        use super::*;
        use std::io::Cursor;

        let source =
            &include_bytes!("../../examples/data/hello-world/ascii+space.txt")[..];
        let reader = &mut Cursor::new(source);

        let target = b',';
        let output = read_byte_after(reader, |b| b" Helo".contains(&b)).unwrap();
        assert_eq!(output, target);

        read_byte_after(&mut Cursor::new(&[][..]), is_space).unwrap_err();

        read_byte_after(&mut InvalidRead, is_null).unwrap_err();
    }

    #[test]
    fn read_bytes_before() {
        use super::*;

        let source =
            &include_bytes!("../../examples/data/hello-world/ascii+binary.dat")[..];
        let reader = &mut std::io::Cursor::new(source);

        advance(reader, 8).unwrap();
        let target = b"Hello, World!";
        let output = read_bytes_before(reader, |b| b == 0, 16).unwrap();
        assert_eq!(output, target);

        read_bytes_before(reader, |b| b == 0, 16).unwrap_err();

        read_bytes_before(&mut InvalidRead, is_null, 0).unwrap_err();
    }

    #[test]
    fn read_bytes_const() {
        use super::*;
        use std::io::Cursor;

        let source =
            &include_bytes!("../../examples/data/hello-world/ascii+binary.dat")[..];
        let reader = &mut Cursor::new(source);

        let target = [0xd5, 0xda, 0x34, 0x01, 0x60, 0xcc, 0xd5, 0x07];
        let output = read_bytes_const(reader).unwrap();
        assert_eq!(output, target);

        read_bytes_const::<512>(reader).unwrap_err();
    }

    #[test]
    fn read_bytes_after_and_before() {
        use super::*;

        let source =
            &include_bytes!("../../examples/data/hello-world/ascii+space.txt")[..];
        let reader = &mut std::io::Cursor::new(source);

        let target = b'H';
        let output = read_byte_after(reader, is_space).unwrap();
        assert_eq!(output, target);

        let target = b"ello, World!";
        let output = read_bytes_before(reader, |b| b == b'\n', 16).unwrap();
        assert_eq!(output, target);

        let target = b'B';
        let output = read_byte_after(reader, is_space).unwrap();
        assert_eq!(output, target);

        let target = b"onjour, le monde";
        let output = read_bytes_before(reader, |b| b == b'!', 16).unwrap();
        assert_eq!(output, target);

        let target = b'\n';
        let output = read_byte_after(reader, is_space).unwrap();
        assert_eq!(output, target);

        read_byte_after(reader, is_space).unwrap_err();
    }

    #[test]
    fn read_bytes_before_many_const() {
        use super::*;

        let source =
            &include_bytes!("../../examples/data/hello-world/ascii+space.txt")[..];
        let reader = &mut std::io::Cursor::new(source);

        advance(reader, 12).unwrap();
        let target = b"Hello";
        let output = &read_bytes_before_many_const(reader, b", ", 16).unwrap();
        assert_eq!(output, target);

        advance(reader, 19).unwrap();
        let target = b"Bonjour, le";
        let output = &read_bytes_before_many_const(reader, b" monde", 20).unwrap();
        assert_eq!(output, target);

        read_bytes_before_many_const(reader, b" monde", 20).unwrap_err();
        read_bytes_before_many_const(reader, b" monde", 20).unwrap_err();

        read_bytes_before_many_const(&mut InvalidRead, b" ", 0).unwrap_err();
    }

    #[test]
    fn read_bytes_before_newline() {
        use super::*;

        let source =
            &include_bytes!("../../examples/data/hello-world/utf8+newline.txt")[..];
        let reader = &mut std::io::Cursor::new(source);

        let target = b"Hello, World!";
        let output = read_bytes_before_newline(reader, 16).unwrap();
        assert_eq!(output, target);

        let target = "\u{4f60}\u{597d}\u{ff0c}".as_bytes();
        let output = read_bytes_before_newline(reader, 8).unwrap();
        assert_eq!(output, target);

        let target = "\u{4e16}\u{754c}\u{ff01} ".as_bytes();
        let output = read_bytes_before_newline(reader, 8).unwrap();
        assert_eq!(output, target);

        // NOTE: In some viewers, a carriage return (CR) is displayed as a newline.
        // However, it is not considered a newline in this function.
        let target = b"";
        let output = read_bytes_before_newline(reader, 4).unwrap();
        assert_eq!(output, target);
        let output = read_bytes_before_newline(reader, 4).unwrap();
        assert_eq!(output, target);

        read_bytes_before_newline(reader, 20).unwrap_err();

        let target = std::io::ErrorKind::UnexpectedEof;
        let output = reader.read_exact(&mut [0; 1]).unwrap_err().kind();
        assert_eq!(output, target);

        read_bytes_before_newline(reader, 4).unwrap_err();

        read_bytes_before_newline(&mut InvalidRead, 0).unwrap_err();
    }

    #[test]
    fn read_bytes_until_many_const() {
        use super::*;

        let source =
            &include_bytes!("../../examples/data/hello-world/ascii+space.txt")[..];
        let reader = &mut std::io::Cursor::new(source);

        let target = b"World";
        read_bytes_until_many_const(reader, b", ").unwrap();
        let output = read_bytes(reader, target.len()).unwrap();
        assert_eq!(output, target);

        let target = b"le monde";
        read_bytes_until_many_const(reader, b"Bonjour, ").unwrap();
        let output = read_bytes(reader, target.len()).unwrap();
        assert_eq!(output, target);

        read_bytes_until_many_const(reader, b"Bonjour").unwrap_err();
        read_bytes_until_many_const(reader, b"Bonjour").unwrap_err();

        read_bytes_until_many_const(&mut InvalidRead, b" ").unwrap_err();
    }

    #[test]
    fn read_newline() {
        use super::*;

        let source = b"\nHi!";
        let reader = &mut std::io::Cursor::new(source);
        let target = (*b"\n").into();
        let output = read_newline(reader).unwrap();
        assert_eq!(output, target);

        let source = b"\r\nHi!";
        let reader = &mut std::io::Cursor::new(source);
        let target = (*b"\r\n").into();
        let output = read_newline(reader).unwrap();
        assert_eq!(output, target);

        let source = b"\rHi!";
        let reader = &mut std::io::Cursor::new(source);
        read_newline(reader).unwrap_err();

        let source = b"\n\nHi!";
        let reader = &mut std::io::Cursor::new(source);
        let target = (*b"\n").into();
        let output = read_newline(reader).unwrap();
        assert_eq!(output, target);

        let source = b"H";
        let reader = &mut std::io::Cursor::new(source);
        read_newline(reader).unwrap_err();

        let source = b"\r";
        let reader = &mut std::io::Cursor::new(source);
        read_newline(reader).unwrap_err();

        let source = &b""[..];
        let reader = &mut std::io::Cursor::new(source);
        read_newline(reader).unwrap_err();

        read_newline(&mut InvalidRead).unwrap_err();
    }
}