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 892601 : pub const fn new(offset: u32, length: u32) -> Self {
14 892601 : Self { offset, length }
15 892601 : }
16 :
17 : /// The end offset (exclusive)
18 525913 : pub const fn end(&self) -> u32 {
19 525913 : self.offset + self.length
20 525913 : }
21 :
22 : /// The smallest span that covers both spans
23 11517 : pub fn cover(self, other: TextSpan) -> TextSpan {
24 11517 : let begin = self.offset.min(other.offset);
25 11517 : let end = self.end().max(other.end());
26 11517 : TextSpan::new(begin, end - begin)
27 11517 : }
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 : }
|