forked from Wilfred/difftastic
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlines.rs
341 lines (294 loc) · 9 KB
/
lines.rs
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
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
//! Manipulate lines of text and groups of lines.
// The `from_offset*` methods on NewlinePositions are sensible names,
// and the docs clippy cites:
// https://rust-lang.github.io/api-guidelines/naming.html#ad-hoc-conversions-follow-as_-to_-into_-conventions-c-conv
// don't actually have an opinion on `from_foo` names.
#![allow(clippy::wrong_self_convention)]
use crate::positions::SingleLineSpan;
use std::ops::Sub;
use std::{cmp::Ordering, fmt};
/// A distinct number type for line numbers, to prevent confusion with
/// other numerical data.
///
/// Zero-indexed internally.
#[derive(Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub struct LineNumber(pub u32);
impl LineNumber {
pub fn one_indexed(self) -> u32 {
self.0 + 1
}
pub fn as_usize(self) -> usize {
self.0 as usize
}
}
impl fmt::Debug for LineNumber {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.write_fmt(format_args!(
"LineNumber: {} (raw: {})",
self.one_indexed(),
self.0
))
}
}
impl From<u32> for LineNumber {
fn from(number: u32) -> Self {
Self(number)
}
}
pub fn format_line_num(line_num: LineNumber) -> String {
format!("{} ", line_num.one_indexed())
}
/// A position in a single line of a string.
#[derive(Debug, PartialEq, Clone, Copy)]
struct LinePosition {
/// Both zero-indexed.
pub line: LineNumber,
column: usize,
}
/// A struct for efficiently converting absolute string positions to
/// line-relative positions.
#[derive(Debug)]
pub struct NewlinePositions {
/// A vector of the start and end positions of all the lines in
/// `s`. Positions include the newline character itself.
positions: Vec<(usize, usize)>,
}
impl From<&str> for NewlinePositions {
fn from(s: &str) -> Self {
let mut line_start = 0;
let mut positions = vec![];
for line in s.split('\n') {
let line_end = line_start + line.len() + "\n".len();
// TODO: this assumes lines terminate with \n, not \r\n.
positions.push((line_start, line_end - 1));
line_start = line_end;
}
NewlinePositions { positions }
}
}
impl NewlinePositions {
fn from_offset(&self, offset: usize) -> usize {
let idx = self.positions.binary_search_by(|(line_start, line_end)| {
if *line_end < offset {
return Ordering::Less;
}
if *line_start > offset {
return Ordering::Greater;
}
Ordering::Equal
});
idx.expect("line should be present")
}
/// Convert to single-line spans. If the original span crosses a
/// newline, the vec will contain multiple items.
pub fn from_offsets(&self, region_start: usize, region_end: usize) -> Vec<SingleLineSpan> {
assert!(region_start <= region_end);
let first_idx = self.from_offset(region_start);
let last_idx = self.from_offset(region_end);
let mut res = vec![];
for idx in first_idx..=last_idx {
let (line_start, line_end) = self.positions[idx];
res.push(SingleLineSpan {
line: (idx as u32).into(),
start_col: if line_start > region_start {
0
} else {
region_start - line_start
} as u32,
end_col: if region_end < line_end {
region_end - line_start
} else {
line_end - line_start
} as u32,
});
}
res
}
pub fn from_offsets_relative_to(
&self,
start: SingleLineSpan,
region_start: usize,
region_end: usize,
) -> Vec<SingleLineSpan> {
assert!(region_start <= region_end);
let mut res = vec![];
for pos in self.from_offsets(region_start, region_end) {
if pos.line.0 == 0 {
res.push(SingleLineSpan {
line: start.line,
start_col: start.start_col + pos.start_col,
end_col: start.start_col + pos.end_col,
});
} else {
res.push(SingleLineSpan {
line: (start.line.0 + pos.line.0).into(),
start_col: pos.start_col,
end_col: pos.end_col,
});
}
}
res
}
}
/// Return the length of `s` in codepoints. This is important when
/// finding character boundaries for slicing without errors.
pub fn codepoint_len(s: &str) -> usize {
s.chars().count()
}
/// Return the length of `s` in bytes.
///
/// This is a trivial wrapper to make it clear when we want bytes not
/// codepoints.
pub fn byte_len(s: &str) -> usize {
s.len()
}
pub trait MaxLine {
fn max_line(&self) -> LineNumber;
}
impl<S: AsRef<str>> MaxLine for S {
fn max_line(&self) -> LineNumber {
(self
.as_ref()
.trim_end() // Remove extra trailing whitespaces.
.split('\n') // Split by `\n` to calculate lines.
.count() as u32)
.sub(1) // Sub 1 to make zero-indexed LineNumber
.into()
}
}
/// Split `s` on \n or \r\n. Always returns a non-empty vec. Each line
/// in the vec does not include the trailing newline.
///
/// This differs from `str::lines`, which considers `""` to be zero
/// lines and `"foo\n"` to be one line.
pub fn split_on_newlines(s: &str) -> Vec<&str> {
s.split('\n')
.map(|l| {
if let Some(l) = l.strip_suffix('\r') {
l
} else {
l
}
})
.collect()
}
pub fn is_all_whitespace(s: &str) -> bool {
s.chars().all(|c| c.is_whitespace())
}
#[cfg(test)]
mod tests {
use super::*;
use pretty_assertions::assert_eq;
#[test]
fn from_offsets_first_line() {
let newline_positions: NewlinePositions = "foo".into();
let line_spans = newline_positions.from_offsets(1, 3);
assert_eq!(
line_spans,
vec![SingleLineSpan {
line: 0.into(),
start_col: 1,
end_col: 3
}]
);
}
#[test]
fn from_offsets_first_char() {
let newline_positions: NewlinePositions = "foo".into();
let line_spans = newline_positions.from_offsets(0, 0);
assert_eq!(
line_spans,
vec![SingleLineSpan {
line: 0.into(),
start_col: 0,
end_col: 0
}]
);
}
#[test]
fn from_offsets_split_over_multiple_lines() {
let newline_positions: NewlinePositions = "foo\nbar\nbaz\naaaaaaaaaaa".into();
let line_spans = newline_positions.from_offsets(5, 10);
assert_eq!(
line_spans,
vec![
SingleLineSpan {
line: 1.into(),
start_col: 1,
end_col: 3
},
SingleLineSpan {
line: 2.into(),
start_col: 0,
end_col: 2
}
]
);
}
#[test]
fn str_max_line() {
let line: String = "foo\nbar".into();
assert_eq!(line.max_line().0, 1);
}
#[test]
fn empty_str_max_line() {
let line: String = "".into();
assert_eq!(line.max_line().0, 0);
}
#[test]
fn str_max_line_trailing_newline() {
let line: String = "foo\nbar\n".into();
assert_eq!(line.max_line().0, 1);
}
#[test]
fn str_max_line_extra_trailing_newline() {
let line: String = "foo\nbar\n\n".into();
assert_eq!(line.max_line().0, 1);
}
#[test]
fn from_offsets_relative_to() {
let newline_positions: NewlinePositions = "foo\nbar".into();
let pos = SingleLineSpan {
line: 1.into(),
start_col: 1,
end_col: 1,
};
let line_spans = newline_positions.from_offsets_relative_to(pos, 1, 2);
assert_eq!(
line_spans,
vec![SingleLineSpan {
line: 1.into(),
start_col: 2,
end_col: 3
}]
);
}
#[test]
fn codepoint_len_non_ascii() {
assert_eq!(codepoint_len("ƒoo"), 3);
}
#[test]
fn test_split_line_empty() {
assert_eq!(split_on_newlines(""), vec![""]);
}
#[test]
fn test_split_line_single() {
assert_eq!(split_on_newlines("foo"), vec!["foo"]);
}
#[test]
fn test_split_line_with_newline() {
assert_eq!(split_on_newlines("foo\nbar"), vec!["foo", "bar"]);
}
#[test]
fn test_split_line_with_crlf() {
assert_eq!(split_on_newlines("foo\r\nbar"), vec!["foo", "bar"]);
}
#[test]
fn test_split_line_with_trailing_newline() {
assert_eq!(split_on_newlines("foo\nbar\n"), vec!["foo", "bar", ""]);
}
#[test]
fn test_is_all_whiteapce() {
assert!(is_all_whitespace(" \n\t"));
}
}