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
#[cfg(feature = "compile-with-external-structures")]
use crate::containers::ExternalSharedByteList;
#[cfg(feature = "compile-with-external-structures")]
type SharedByteList = ExternalSharedByteList;
#[cfg(not(feature = "compile-with-external-structures"))]
type SharedByteList<'a> = &'a [u8];
#[cfg(feature = "compile-with-external-structures")]
use crate::containers::ExternalList;
#[cfg(feature = "compile-with-external-structures")]
type List<T> = ExternalList<T>;
#[cfg(not(feature = "compile-with-external-structures"))]
type List<T> = Vec<T>;
#[cfg(feature = "compile-with-external-structures")]
use crate::containers::ExternalStringPtr;
#[cfg(feature = "compile-with-external-structures")]
type StringPtr = ExternalStringPtr;
#[cfg(not(feature = "compile-with-external-structures"))]
type StringPtr = String;
use crate::source::SourceLine;
use crate::source::{decode_input, CustomDecoder, DecodedInput, InputError};
#[derive(Debug, Default)]
#[repr(C)]
pub struct Input {
pub(crate) decoded: DecodedInput,
decoder: CustomDecoder,
}
impl Input {
pub fn new<Name>(name: Name, decoder: CustomDecoder) -> Self
where
Name: Into<StringPtr>,
{
Self {
decoded: DecodedInput {
name: name.into(),
..Default::default()
},
decoder,
}
}
pub fn set_bytes(&mut self, bytes: List<u8>) {
self.decoded.set_bytes(bytes)
}
pub(crate) fn byte_at(&self, idx: usize) -> Option<u8> {
if let Some(c) = self.decoded.bytes.get(idx) {
Some(*c)
} else {
None
}
}
pub(crate) fn unchecked_byte_at(&self, idx: usize) -> u8 {
self.decoded.bytes[idx]
}
pub(crate) fn substr_at(&self, start: usize, end: usize) -> Option<&[u8]> {
self.decoded.substr_at(start, end)
}
pub fn line_col_for_pos(&self, pos: usize) -> Option<(usize, usize)> {
self.decoded.line_col_for_pos(pos)
}
pub(crate) fn len(&self) -> usize {
self.decoded.len()
}
pub(crate) fn line_at(&self, idx: usize) -> &SourceLine {
&self.decoded.line_at(idx)
}
pub(crate) fn lines_count(&self) -> usize {
self.decoded.lines.len()
}
pub(crate) fn set_encoding(&mut self, encoding: &str) -> Result<(), InputError> {
let new_input = decode_input(
std::mem::take(&mut self.decoded.bytes),
StringPtr::from(encoding),
self.decoder.take(),
)
.to_result()?;
self.set_bytes(new_input);
Ok(())
}
pub fn as_shared_bytes(&self) -> SharedByteList {
self.decoded.as_shared_bytes()
}
pub fn into_bytes(self) -> List<u8> {
self.decoded.into_bytes()
}
}