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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
use memchr::{memchr, memrchr};
use crate::reader::Cursor;
#[derive(Debug, Clone, Eq, PartialEq)]
pub struct Span<'a> {
content: &'a str,
start_cursor: Cursor,
end_cursor: Cursor,
}
impl<'a> Span<'a> {
pub(in crate::reader) fn new(
content: &'a str,
start_cursor: Cursor,
end_cursor: Cursor,
) -> Span {
Span {
content,
start_cursor,
end_cursor,
}
}
pub fn whole_content(&self) -> &'a str {
self.content
}
pub fn content(&self) -> &'a str {
&self.content[self.start_cursor.byte_offset()..self.end_cursor.byte_offset()]
}
pub fn content_before(&self) -> &'a str {
&self.content[..self.start_cursor.byte_offset()]
}
pub fn content_after(&self) -> &'a str {
&self.content[self.end_cursor.byte_offset()..]
}
pub fn start_cursor(&self) -> &Cursor {
&self.start_cursor
}
pub fn end_cursor(&self) -> &Cursor {
&self.end_cursor
}
pub fn len(&self) -> usize {
self.end_cursor.byte_offset() - self.start_cursor.byte_offset()
}
pub fn is_empty(&self) -> bool {
self.len() == 0
}
pub fn char_length(&self) -> usize {
self.end_cursor.char_offset() - self.start_cursor.char_offset()
}
pub fn lines(&self) -> &'a str {
let start_index = match memrchr(b'\n', self.content_before().as_bytes()) {
Some(v) => v + 1,
None => 0,
};
let end_index = match memchr(b'\n', self.content_after().as_bytes()) {
Some(v) => v + self.end_cursor.byte_offset(),
None => self.content.len(),
};
&self.content[start_index..end_index]
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_lines_single_line() {
let text = "This\nis\nthe\ntest";
let span = Span::new(
text,
Cursor::new(1, 0, 0, 0),
Cursor::new(1, 0, 0, 0),
);
assert_eq!(span.lines(), "This", "The lines is incorrect");
let text = "This\nis\nthe\ntest";
let span = Span::new(
text,
Cursor::new(4, 0, 0, 0),
Cursor::new(4, 0, 0, 0),
);
assert_eq!(span.lines(), "This", "The lines is incorrect");
let text = "This\nis\nthe\ntest";
let span = Span::new(
text,
Cursor::new(5, 0, 0, 0),
Cursor::new(5, 0, 0, 0),
);
assert_eq!(span.lines(), "is", "The lines is incorrect");
}
#[test]
fn test_lines_multiline() {
let text = "This\nis\nthe\ntest";
let span = Span::new(
text,
Cursor::new(5, 0, 0, 0),
Cursor::new(08, 0, 0, 0),
);
assert_eq!(span.lines(), "is\nthe", "The lines is incorrect");
}
}