microsoft/qdk

Public

mirrored from https://github.com/microsoft/qdkAvailable

CodeCommitsIssuesPull requestsActionsInsightsSecurity
v1.19.0

Branches

Tags

  • No tags available.
0Branches0Tags
Go to file
Add file
Code

Clone

HTTPS

Download ZIP

source/compiler/qsc_data_structures/src/span.rs

120lines · modeblame

a5f0bf3fSarah Marshall3 years ago1// Copyright (c) Microsoft Corporation.
2// Licensed under the MIT License.
3
4use miette::SourceSpan;
5use std::{
6fmt::{self, Display, Formatter},
e0c4dd33Ian Davis2 years ago7ops::{Add, Index, Sub},
a5f0bf3fSarah Marshall3 years ago8};
9
10/// A region between two offsets in an array. Spans are the half-open interval `[lo, hi)`.
11#[derive(Clone, Copy, Debug, Default, Eq, Hash, Ord, PartialEq, PartialOrd)]
12pub struct Span {
13/// The smallest offset contained in the span.
4986fd3eStefan J. Wernli3 years ago14pub lo: u32,
a5f0bf3fSarah Marshall3 years ago15/// The next offset after the largest offset contained in the span.
4986fd3eStefan J. Wernli3 years ago16pub hi: u32,
a5f0bf3fSarah Marshall3 years ago17}
18
f20c06a3orpuente-MS2 years ago19impl Span {
00152589orpuente-MS2 years ago20/// Returns true if the position is within the span. Meaning it is in the
21/// right open interval `[self.lo, self.hi)`.
f20c06a3orpuente-MS2 years ago22#[must_use]
23pub fn contains(&self, offset: u32) -> bool {
24(self.lo..self.hi).contains(&offset)
25}
26
00152589orpuente-MS2 years ago27/// Returns true if the position is in the closed interval `[self.lo, self.hi]`.
28#[must_use]
29pub fn touches(&self, offset: u32) -> bool {
30(self.lo..=self.hi).contains(&offset)
31}
32
33/// Intersect `other` with `self` and returns a new `Span` or `None`
34/// if the spans have no overlap.
f20c06a3orpuente-MS2 years ago35#[must_use]
36pub fn intersection(&self, other: &Self) -> Option<Self> {
37let lo = self.lo.max(other.lo);
38let hi = self.hi.min(other.hi);
39
40if lo <= hi {
41Some(Self { lo, hi })
42} else {
43None
44}
45}
46}
47
4986fd3eStefan J. Wernli3 years ago48impl Add<u32> for Span {
508fb904Sarah Marshall3 years ago49type Output = Self;
50
4986fd3eStefan J. Wernli3 years ago51fn add(self, rhs: u32) -> Self::Output {
508fb904Sarah Marshall3 years ago52Self {
53lo: self.lo + rhs,
54hi: self.hi + rhs,
55}
56}
57}
58
e0c4dd33Ian Davis2 years ago59impl Sub<u32> for Span {
60type Output = Self;
61
62fn sub(self, rhs: u32) -> Self::Output {
63Self {
64lo: self.lo - rhs,
65hi: self.hi - rhs,
66}
67}
68}
69
a5f0bf3fSarah Marshall3 years ago70impl Display for Span {
71fn fmt(&self, f: &mut Formatter) -> fmt::Result {
72write!(f, "[{}-{}]", self.lo, self.hi)?;
73Ok(())
74}
75}
76
77impl Index<Span> for str {
78type Output = str;
79
80fn index(&self, index: Span) -> &Self::Output {
4986fd3eStefan J. Wernli3 years ago81&self[(index.lo as usize)..(index.hi as usize)]
a5f0bf3fSarah Marshall3 years ago82}
83}
84
85impl Index<&Span> for str {
86type Output = str;
87
88fn index(&self, index: &Span) -> &Self::Output {
4986fd3eStefan J. Wernli3 years ago89&self[(index.lo as usize)..(index.hi as usize)]
a5f0bf3fSarah Marshall3 years ago90}
91}
92
93impl Index<Span> for String {
94type Output = str;
95
96fn index(&self, index: Span) -> &Self::Output {
4986fd3eStefan J. Wernli3 years ago97&self[(index.lo as usize)..(index.hi as usize)]
a5f0bf3fSarah Marshall3 years ago98}
99}
100
101impl From<Span> for SourceSpan {
102fn from(value: Span) -> Self {
4986fd3eStefan J. Wernli3 years ago103Self::from((value.lo as usize)..(value.hi as usize))
a5f0bf3fSarah Marshall3 years ago104}
105}
b74867ceStefan J. Wernli2 years ago106
107pub trait WithSpan {
108#[must_use]
109fn with_span(self, span: Span) -> Self;
110}
893003e6Stefan J. Wernli2 years ago111
112impl<T> WithSpan for Box<T>
113where
114T: WithSpan,
115{
116fn with_span(mut self, span: Span) -> Self {
117*self = (*self).with_span(span);
118self
119}
120}