1#[derive(Clone, Copy, Debug, PartialEq, Eq)]
7pub struct Span {
8 pub start_offset: usize,
9 pub end_offset: usize,
10 pub start_line: usize,
11 pub start_col: usize,
12 pub end_line: usize,
13 pub end_col: usize,
14}
15
16#[derive(Clone, Copy, Debug, PartialEq, Eq)]
18pub struct LineCol {
19 pub line: usize,
20 pub col: usize,
21}
22
23impl Span {
24 pub fn from_offsets(source: &str, start_offset: usize, end_offset: usize) -> Span {
26 let start = line_col(source, start_offset);
27 let end = line_col(source, end_offset);
28 Span {
29 start_offset,
30 end_offset,
31 start_line: start.line,
32 start_col: start.col,
33 end_line: end.line,
34 end_col: end.col,
35 }
36 }
37}
38
39pub fn line_col(source: &str, offset: usize) -> LineCol {
43 let mut line = 1;
44 let mut col = 1;
45 for (idx, unit) in source.encode_utf16().enumerate() {
46 if idx >= offset {
47 break;
48 }
49 if unit == 0x000A {
50 line += 1;
51 col = 1;
52 } else {
53 col += 1;
54 }
55 }
56 LineCol { line, col }
57}