cranelift_isle/
files.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
#![allow(missing_docs)]

use std::ops::Index;
use std::path::{Path, PathBuf};

#[derive(Default, Clone, PartialEq, Eq, Debug)]
pub struct Files {
    /// Arena of filenames from the input source.
    ///
    /// Indexed via `Pos::file`.
    pub file_names: Vec<String>,

    /// Arena of file source texts.
    ///
    /// Indexed via `Pos::file`.
    pub file_texts: Vec<String>,

    /// Arena of file line maps.
    ///
    /// Indexed via `Pos::file`.
    pub file_line_maps: Vec<LineMap>,
}

#[derive(Default, Clone, PartialEq, Eq, Debug)]
pub struct LineMap {
    /// Mapping from line number to starting byte position.
    line_ends: Vec<usize>,
}

impl Index<usize> for LineMap {
    type Output = usize;

    fn index(&self, index: usize) -> &Self::Output {
        &self.line_ends[index]
    }
}

impl LineMap {
    pub fn from_str(text: &str) -> Self {
        let line_ends = text.match_indices('\n').map(|(i, _)| i + 1).collect();
        Self { line_ends }
    }

    /// Get the line on which `pos` occurs
    pub fn line(&self, pos: usize) -> usize {
        self.line_ends.partition_point(|&end| end <= pos)
    }

    /// Get the starting byte position of `line`.
    pub fn get(&self, line: usize) -> Option<&usize> {
        self.line_ends.get(line)
    }
}

impl Files {
    pub fn from_paths<P: AsRef<Path>>(
        paths: impl IntoIterator<Item = P>,
    ) -> Result<Self, (PathBuf, std::io::Error)> {
        let mut file_names = Vec::new();
        let mut file_texts = Vec::new();
        let mut file_line_maps = Vec::new();

        for path in paths {
            let path = path.as_ref();
            let contents =
                std::fs::read_to_string(path).map_err(|err| (path.to_path_buf(), err))?;
            let name = path.display().to_string();

            file_line_maps.push(LineMap::from_str(&contents));
            file_names.push(name);
            file_texts.push(contents);
        }

        Ok(Self {
            file_names,
            file_texts,
            file_line_maps,
        })
    }

    pub fn from_names_and_contents(files: impl IntoIterator<Item = (String, String)>) -> Self {
        let mut file_names = Vec::new();
        let mut file_texts = Vec::new();
        let mut file_line_maps = Vec::new();

        for (name, contents) in files {
            file_line_maps.push(LineMap::from_str(&contents));
            file_names.push(name);
            file_texts.push(contents);
        }

        Self {
            file_names,
            file_texts,
            file_line_maps,
        }
    }

    pub fn file_name(&self, file: usize) -> Option<&str> {
        self.file_names.get(file).map(|x| x.as_str())
    }

    pub fn file_text(&self, file: usize) -> Option<&str> {
        self.file_texts.get(file).map(|x| x.as_str())
    }

    pub fn file_line_map(&self, file: usize) -> Option<&LineMap> {
        self.file_line_maps.get(file)
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn line_map() {
        let line_map = LineMap::from_str("");
        assert_eq!(line_map.line_ends, &[]);
        assert_eq!(line_map.line(0), 0);
        assert_eq!(line_map.line(100), 0);

        let line_map = LineMap::from_str("line 0");
        assert_eq!(line_map.line_ends, &[]);
        assert_eq!(line_map.line(0), 0);
        assert_eq!(line_map.line(100), 0);

        let line_map = LineMap::from_str("line 0\nline 1");
        assert_eq!(line_map.line_ends, &[7]);
        assert_eq!(line_map.line(0), 0);
        assert_eq!(line_map.line(100), 1);
    }
}