Skip to main content

rustty/vt/
params.rs

1//! Borrowed views over CSI parameter bytes.
2
3/// A borrowed view over all CSI parameter bytes.
4///
5/// Semicolons separate [`Param`] values, while colons separate [`Subparam`]
6/// values inside one parameter. Empty values are preserved by the iterators.
7#[derive(Clone, Copy, Debug, PartialEq, Eq)]
8pub struct Params<'a> {
9    raw: &'a [u8],
10}
11
12impl<'a> Params<'a> {
13    pub(crate) const fn new(raw: &'a [u8]) -> Self {
14        Self { raw }
15    }
16
17    /// Returns the original parameter bytes without private markers.
18    #[must_use]
19    pub const fn as_bytes(self) -> &'a [u8] {
20        self.raw
21    }
22
23    /// Returns `true` when there are no parameter bytes.
24    #[must_use]
25    pub const fn is_empty(self) -> bool {
26        self.raw.is_empty()
27    }
28
29    /// Returns the number of semicolon-delimited parameters.
30    #[must_use]
31    pub fn len(self) -> usize {
32        if self.raw.is_empty() {
33            return 0;
34        }
35        let mut count = 1;
36        for &byte in self.raw {
37            if byte == b';' {
38                count += 1;
39            }
40        }
41        count
42    }
43
44    /// Returns one parameter by its zero-based index.
45    #[must_use]
46    pub fn get(self, index: usize) -> Option<Param<'a>> {
47        self.iter().nth(index)
48    }
49
50    /// Iterates over semicolon-delimited parameters.
51    #[must_use]
52    pub const fn iter(self) -> ParamIter<'a> {
53        ParamIter {
54            remaining: self.raw,
55            done: self.raw.is_empty(),
56        }
57    }
58}
59
60/// Iterates over [`Params`] without allocating.
61#[derive(Clone, Debug)]
62pub struct ParamIter<'a> {
63    remaining: &'a [u8],
64    done: bool,
65}
66
67impl<'a> Iterator for ParamIter<'a> {
68    type Item = Param<'a>;
69
70    fn next(&mut self) -> Option<Self::Item> {
71        if self.done {
72            return None;
73        }
74
75        if let Some(separator) = self.remaining.iter().position(|&byte| byte == b';') {
76            let (value, rest) = self.remaining.split_at(separator);
77            self.remaining = &rest[1..];
78            Some(Param::new(value))
79        } else {
80            self.done = true;
81            Some(Param::new(self.remaining))
82        }
83    }
84}
85
86/// One semicolon-delimited CSI parameter.
87#[derive(Clone, Copy, Debug, PartialEq, Eq)]
88pub struct Param<'a> {
89    raw: &'a [u8],
90}
91
92impl<'a> Param<'a> {
93    const fn new(raw: &'a [u8]) -> Self {
94        Self { raw }
95    }
96
97    /// Returns the original parameter bytes.
98    #[must_use]
99    pub const fn as_bytes(self) -> &'a [u8] {
100        self.raw
101    }
102
103    /// Returns `true` when this parameter is empty.
104    #[must_use]
105    pub const fn is_empty(self) -> bool {
106        self.raw.is_empty()
107    }
108
109    /// Returns the number of colon-delimited subparameters.
110    #[must_use]
111    pub fn len(self) -> usize {
112        let mut count = 1;
113        for &byte in self.raw {
114            if byte == b':' {
115                count += 1;
116            }
117        }
118        count
119    }
120
121    /// Returns one colon-delimited subparameter by its zero-based index.
122    #[must_use]
123    pub fn get(self, index: usize) -> Option<Subparam<'a>> {
124        self.subparams().nth(index)
125    }
126
127    /// Iterates over colon-delimited subparameters.
128    #[must_use]
129    pub const fn subparams(self) -> SubparamIter<'a> {
130        SubparamIter {
131            remaining: self.raw,
132            done: false,
133        }
134    }
135
136    /// Parses a single numeric subparameter.
137    ///
138    /// This returns `None` for an empty value, malformed bytes, or a number
139    /// that does not fit in `u64`. Use [`Param::get`] to inspect all
140    /// colon-separated values.
141    #[must_use]
142    pub fn value(self) -> Option<u64> {
143        self.get(0).and_then(Subparam::value)
144    }
145}
146
147/// Iterates over [`Param`] subparameters without allocating.
148#[derive(Clone, Debug)]
149pub struct SubparamIter<'a> {
150    remaining: &'a [u8],
151    done: bool,
152}
153
154impl<'a> Iterator for SubparamIter<'a> {
155    type Item = Subparam<'a>;
156
157    fn next(&mut self) -> Option<Self::Item> {
158        if self.done {
159            return None;
160        }
161
162        if let Some(separator) = self.remaining.iter().position(|&byte| byte == b':') {
163            let (value, rest) = self.remaining.split_at(separator);
164            self.remaining = &rest[1..];
165            Some(Subparam::new(value))
166        } else {
167            self.done = true;
168            Some(Subparam::new(self.remaining))
169        }
170    }
171}
172
173/// One colon-delimited CSI subparameter.
174#[derive(Clone, Copy, Debug, PartialEq, Eq)]
175pub struct Subparam<'a> {
176    raw: &'a [u8],
177}
178
179impl<'a> Subparam<'a> {
180    const fn new(raw: &'a [u8]) -> Self {
181        Self { raw }
182    }
183
184    /// Returns the original subparameter bytes.
185    #[must_use]
186    pub const fn as_bytes(self) -> &'a [u8] {
187        self.raw
188    }
189
190    /// Returns `true` when this subparameter is empty.
191    #[must_use]
192    pub const fn is_empty(self) -> bool {
193        self.raw.is_empty()
194    }
195
196    /// Parses the subparameter as an unsigned decimal integer.
197    ///
198    /// Empty values and values with non-decimal bytes return `None`. The raw
199    /// bytes remain available through [`Subparam::as_bytes`].
200    #[must_use]
201    pub fn value(self) -> Option<u64> {
202        if self.raw.is_empty() {
203            return None;
204        }
205
206        let mut value = 0_u64;
207        for &byte in self.raw {
208            if !byte.is_ascii_digit() {
209                return None;
210            }
211            value = value.checked_mul(10)?.checked_add(u64::from(byte - b'0'))?;
212        }
213        Some(value)
214    }
215}