Line data Source code
1 : //! Byte spans in the script text.
2 :
3 : /// A span of bytes in the script text.
4 : #[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Hash)]
5 : pub struct TextSpan {
6 : /// The byte offset of the start
7 : pub offset: u32,
8 : /// The length in bytes
9 : pub length: u32,
10 : }
11 :
12 : impl TextSpan {
13 917865 : pub const fn new(offset: u32, length: u32) -> Self {
14 917865 : Self { offset, length }
15 917865 : }
16 :
17 : /// The end offset (exclusive)
18 543444 : pub const fn end(&self) -> u32 {
19 543444 : self.offset + self.length
20 543444 : }
21 :
22 : /// The smallest span that covers both spans
23 11772 : pub fn cover(self, other: TextSpan) -> TextSpan {
24 11772 : let begin = self.offset.min(other.offset);
25 11772 : let end = self.end().max(other.end());
26 11772 : TextSpan::new(begin, end - begin)
27 11772 : }
28 : }
29 :
30 : impl From<TextSpan> for dashql_buffers::parser::TextSpan {
31 0 : fn from(span: TextSpan) -> Self {
32 0 : dashql_buffers::parser::TextSpan::new(span.offset, span.length)
33 0 : }
34 : }
|