azalea_brigadier/context/
string_range.rs

1use std::cmp;
2
3#[derive(Debug, Clone, PartialEq, Eq, Hash, Default, Copy)]
4pub struct StringRange {
5    start: usize,
6    end: usize,
7}
8
9impl StringRange {
10    pub fn new(start: usize, end: usize) -> Self {
11        Self { start, end }
12    }
13
14    pub fn at(pos: usize) -> Self {
15        Self::new(pos, pos)
16    }
17
18    pub fn between(start: usize, end: usize) -> Self {
19        Self::new(start, end)
20    }
21
22    pub fn encompassing(a: &Self, b: &Self) -> Self {
23        Self::new(cmp::min(a.start, b.start), cmp::max(a.end, b.end))
24    }
25
26    pub fn start(&self) -> usize {
27        self.start
28    }
29
30    pub fn end(&self) -> usize {
31        self.end
32    }
33
34    pub fn get<'a>(&self, reader: &'a str) -> &'a str {
35        &reader[self.start..self.end]
36    }
37
38    pub fn is_empty(&self) -> bool {
39        self.start == self.end
40    }
41
42    pub fn length(&self) -> usize {
43        self.end - self.start
44    }
45}