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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
|
use crate::error::*;
#[derive(Debug, Clone)]
pub struct LineRange {
lower: usize,
upper: usize,
}
impl Default for LineRange {
fn default() -> LineRange {
LineRange {
lower: usize::min_value(),
upper: usize::max_value(),
}
}
}
impl LineRange {
pub fn new(from: usize, to: usize) -> Self {
LineRange {
lower: from,
upper: to,
}
}
pub fn from(range_raw: &str) -> Result<LineRange> {
LineRange::parse_range(range_raw)
}
fn parse_range(range_raw: &str) -> Result<LineRange> {
let mut new_range = LineRange::default();
if range_raw.bytes().next().ok_or("Empty line range")? == b':' {
new_range.upper = range_raw[1..].parse()?;
return Ok(new_range);
} else if range_raw.bytes().last().ok_or("Empty line range")? == b':' {
new_range.lower = range_raw[..range_raw.len() - 1].parse()?;
return Ok(new_range);
}
let line_numbers: Vec<&str> = range_raw.split(':').collect();
match line_numbers.len() {
1 => {
new_range.lower = line_numbers[0].parse()?;
new_range.upper = new_range.lower;
Ok(new_range)
}
2 => {
new_range.lower = line_numbers[0].parse()?;
let first_byte = line_numbers[1].bytes().next();
new_range.upper = if first_byte == Some(b'+') {
let more_lines = &line_numbers[1][1..]
.parse()
.map_err(|_| "Invalid character after +")?;
new_range.lower.saturating_add(*more_lines)
} else if first_byte == Some(b'-') {
// this will prevent values like "-+5" even though "+5" is valid integer
if line_numbers[1][1..].bytes().next() == Some(b'+') {
return Err("Invalid character after -".into());
}
let prior_lines = &line_numbers[1][1..]
.parse()
.map_err(|_| "Invalid character after -")?;
let prev_lower = new_range.lower;
new_range.lower = new_range.lower.saturating_sub(*prior_lines);
prev_lower
} else {
line_numbers[1].parse()?
};
Ok(new_range)
}
_ => Err(
"Line range contained more than one ':' character. Expected format: 'N' or 'N:M'"
.into(),
),
}
}
pub(crate) fn is_inside(&self, line: usize) -> bool {
line >= self.lower && line <= self.upper
}
}
#[test]
fn test_parse_full() {
let range = LineRange::from("40:50").expect("Shouldn't fail on test!");
assert_eq!(40, range.lower);
assert_eq!(50, range.upper);
}
#[test]
fn test_parse_partial_min() {
let range = LineRange::from(":50").expect("Shouldn't fail on test!");
assert_eq!(usize::min_value(), range.lower);
assert_eq!(50, range.upper);
}
#[test]
fn test_parse_partial_max() {
let range = LineRange::from("40:").expect("Shouldn't fail on test!");
assert_eq!(40, range.lower);
assert_eq!(usize::max_value(), range.upper);
}
#[test]
fn test_parse_single() {
let range = LineRange::from("40").expect("Shouldn't fail on test!");
assert_eq!(40, range.lower);
assert_eq!(40, range.upper);
}
#[test]
fn test_parse_fail() {
let range = LineRange::from("40:50:80");
assert!(range.is_err());
let range = LineRange::from("40::80");
assert!(range.is_err());
let range = LineRange::from(":40:");
assert!(range.is_err());
}
#[test]
fn test_parse_plus() {
let range = LineRange::from("40:+10").expect("Shouldn't fail on test!");
assert_eq!(40, range.lower);
assert_eq!(50, range.upper);
}
#[test]
fn test_parse_plus_overflow() {
let range = LineRange::from(&format!("{}:+1", usize::MAX)).expect("Shouldn't fail on test!");
assert_eq!(usize::MAX, range.lower);
assert_eq!(usize::MAX, range.upper);
}
#[test]
fn test_parse_plus_fail() {
let range = LineRange::from("40:+z");
assert!(range.is_err());
let range = LineRange::from("40:+-10");
assert!(range.is_err());
let range = LineRange::from("40:+");
assert!(range.is_err());
}
#[test]
fn test_parse_minus_success() {
let range = LineRange::from("40:-10").expect("Shouldn't fail on test!");
assert_eq!(30, range.lower);
assert_eq!(40, range.upper);
}
#[test]
fn test_parse_minus_edge_cases_success() {
let range = LineRange::from("5:-4").expect("Shouldn't fail on test!");
assert_eq!(1, range.lower);
assert_eq!(5, range.upper);
let range = LineRange::from("5:-5").expect("Shouldn't fail on test!");
assert_eq!(0, range.lower);
assert_eq!(5, range.upper);
let range = LineRange::from("5:-100").expect("Shouldn't fail on test!");
assert_eq!(0, range.lower);
assert_eq!(5, range.upper);
}
#[test]
fn test_parse_minus_fail() {
let range = LineRange::from("40:-z");
assert!(range.is_err());
let range = LineRange::from("40:-+10");
assert!(range.is_err());
let range = LineRange::from("40:-");
assert!(range.is_err());
}
#[derive(Copy, Clone, Debug, PartialEq, Eq)]
pub enum RangeCheckResult {
// Within one of the given ranges
InRange,
// Before the first range or within two ranges
BeforeOrBetweenRanges,
// Line number is outside of all ranges and larger than the last range.
AfterLastRange,
}
#[derive(Debug, Clone)]
pub struct LineRanges {
ranges: Vec<LineRange>,
largest_upper_bound: usize,
}
impl LineRanges {
pub fn none() -> LineRanges {
LineRanges::from(vec![])
}
pub fn all() -> LineRanges {
LineRanges::from(vec![LineRange::default()])
}
pub fn from(ranges: Vec<LineRange>) -> LineRanges {
let largest_upper_bound = ranges
.iter()
.map(|r| r.upper)
.max()
.unwrap_or(usize::max_value());
LineRanges {
ranges,
largest_upper_bound,
}
}
pub(crate) fn check(&self, line: usize) -> RangeCheckResult {
if self.ranges.iter().any(|r| r.is_inside(line)) {
RangeCheckResult::InRange
} else if line < self.largest_upper_bound {
RangeCheckResult::BeforeOrBetweenRanges
} else {
RangeCheckResult::AfterLastRange
}
}
}
impl Default for LineRanges {
fn default() -> Self {
Self::all()
}
}
#[derive(Debug, Clone)]
pub struct HighlightedLineRanges(pub LineRanges);
impl Default for HighlightedLineRanges {
fn default() -> Self {
HighlightedLineRanges(LineRanges::none())
}
}
#[cfg(test)]
fn ranges(rs: &[&str]) -> LineRanges {
LineRanges::from(rs.iter().map(|r| LineRange::from(r).unwrap()).collect())
}
#[test]
fn test_ranges_simple() {
let ranges = ranges(&["3:8"]);
assert_eq!(RangeCheckResult::BeforeOrBetweenRanges, ranges.check(2));
assert_eq!(RangeCheckResult::InRange, ranges.check(5));
assert_eq!(RangeCheckResult::AfterLastRange, ranges.check(9));
}
#[test]
fn test_ranges_advanced() {
let
|