1
2
Fork 0
mirror of https://github.com/mat-1/azalea.git synced 2025-08-02 06:16:04 +00:00
azalea/azalea-brigadier/src/context/string_range.rs

45 lines
873 B
Rust

use std::cmp;
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct StringRange {
start: usize,
end: usize,
}
impl StringRange {
pub fn new(start: usize, end: usize) -> Self {
Self { start, end }
}
pub fn at(pos: usize) -> Self {
Self::new(pos, pos)
}
pub fn between(start: usize, end: usize) -> Self {
Self::new(start, end)
}
pub fn encompassing(a: &Self, b: &Self) -> Self {
Self::new(cmp::min(a.start, b.start), cmp::max(a.end, b.end))
}
pub fn start(&self) -> usize {
self.start
}
pub fn end(&self) -> usize {
self.end
}
pub fn get(&self, reader: &str) -> &str {
&reader[self.start..self.end]
}
pub fn is_empty(&self) -> bool {
self.start == self.end
}
pub fn length(&self) -> usize {
self.end - self.start
}
}