enhanced_magic_string/utils/
get_locator.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
use super::char_string::CharString;

pub fn get_locator(code: &CharString) -> impl Fn(usize) -> Loc {
  let lines = code.split('\n');
  let mut line_offsets = vec![];
  let mut pos = 0;

  for line in lines {
    line_offsets.push(pos);
    pos += line.len() + 1;
  }

  move |pos| {
    // binary search
    let mut left = 0;
    let mut right = line_offsets.len();

    while left < right {
      let mid = (left + right) >> 1;

      if pos < line_offsets[mid] {
        right = mid;
      } else {
        left = mid + 1;
      }
    }

    let line = left - 1;
    let column = pos - line_offsets[line];

    Loc { line, column }
  }
}

#[derive(Debug, Clone)]
pub struct Loc {
  pub line: usize,
  pub column: usize,
}