aboutsummaryrefslogtreecommitdiff
path: root/azalea-brigadier/src/context/string_range.rs
diff options
context:
space:
mode:
authormat <27899617+mat-1@users.noreply.github.com>2022-04-20 01:34:12 +0000
committerGitHub <noreply@github.com>2022-04-20 01:34:12 +0000
commit5fd87615cf1514c7f9a0358988964768ded3f06e (patch)
tree001c3c760fdae8fe7b72cacb1f87d3703cc4e82c /azalea-brigadier/src/context/string_range.rs
parentd09762f5d38ab1200fb08ca3b1178813b4e47081 (diff)
parentbe194c1ca136100fd8f53ed068d82c9f7ae32870 (diff)
downloadazalea-drasl-5fd87615cf1514c7f9a0358988964768ded3f06e.tar.xz
Merge pull request #1 from mat-1/brigadier
azalea-brigadier
Diffstat (limited to 'azalea-brigadier/src/context/string_range.rs')
-rw-r--r--azalea-brigadier/src/context/string_range.rs45
1 files changed, 45 insertions, 0 deletions
diff --git a/azalea-brigadier/src/context/string_range.rs b/azalea-brigadier/src/context/string_range.rs
new file mode 100644
index 00000000..8ca88624
--- /dev/null
+++ b/azalea-brigadier/src/context/string_range.rs
@@ -0,0 +1,45 @@
+use std::cmp;
+
+#[derive(Debug, Clone, PartialEq, Eq, Hash, Default)]
+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<'a>(&self, reader: &'a str) -> &'a 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
+ }
+}