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
/// Writing input to bytes.
///
/// This is useful to capture a part of the input,
/// without allocating when the input is a slice.
pub trait Write {
/// Type of bytes to write to.
type Bytes: core::convert::AsRef<[u8]> + Default;
/// Write input to `bytes` until `stop` yields true.
///
/// The `stop` function gets the previously read bytes and the current byte.
///
/// This function does not return a new [`Self::Bytes`] to avoid allocations.
///
/// ~~~
/// fn test<L: hifijson::Write>(lexer: &mut L) {
/// let mut bytes = L::Bytes::default();
/// lexer.write_until(&mut bytes, |c| c == b' ');
/// assert_eq!(bytes.as_ref(), b"Hello");
/// lexer.write_until(&mut bytes, |_| false);
/// assert_eq!(bytes.as_ref(), b" World");
/// }
/// let s = b"Hello World";
/// test(&mut hifijson::SliceLexer::new(s));
/// test(&mut hifijson::IterLexer::new(s.iter().copied().map(Ok::<_, ()>)));
/// ~~~
fn write_until(&mut self, bytes: &mut Self::Bytes, stop: impl FnMut(u8) -> bool);
}
impl<'a> Write for crate::SliceLexer<'a> {
type Bytes = &'a [u8];
fn write_until(&mut self, bytes: &mut Self::Bytes, stop: impl FnMut(u8) -> bool) {
let pos = self.slice.iter().copied().position(stop);
let pos = pos.unwrap_or(self.slice.len());
*bytes = &self.slice[..pos];
self.slice = &self.slice[pos..]
}
}
#[cfg(feature = "alloc")]
impl<E, I: Iterator<Item = Result<u8, E>>> Write for crate::IterLexer<E, I> {
type Bytes = alloc::vec::Vec<u8>;
fn write_until(&mut self, bytes: &mut Self::Bytes, mut stop: impl FnMut(u8) -> bool) {
use crate::Read;
bytes.clear();
while let Some(c) = self.peek_next() {
if stop(c) {
return;
} else {
self.take_next();
bytes.push(c)
}
}
}
}