Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(span): add contains_inclusive method #4491

Merged
merged 1 commit into from
Jul 27, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 39 additions & 0 deletions crates/oxc_span/src/span.rs
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,30 @@ impl Span {
self.start == SPAN.start && self.end == SPAN.end
}

/// Check if this [`Span`] contains another [`Span`].
///
/// [`Span`]s that start & end at the same position as this [`Span`] are
/// considered contained.
///
/// # Examples
///
/// ```rust
/// # use oxc_span::Span;
/// let span = Span::new(5, 10);
///
/// assert!(span.contains_inclusive(span)); // always true for itself
/// assert!(span.contains_inclusive(Span::new(5, 5)));
/// assert!(span.contains_inclusive(Span::new(6, 10)));
/// assert!(span.contains_inclusive(Span::empty(5)));
///
/// assert!(!span.contains_inclusive(Span::new(4, 10)));
/// assert!(!span.contains_inclusive(Span::empty(0)));
/// ```
#[inline]
pub const fn contains_inclusive(self, span: Span) -> bool {
self.start <= span.start && span.end <= self.end
}

/// Create a [`Span`] covering the maximum range of two [`Span`]s.
///
/// # Example
Expand Down Expand Up @@ -365,4 +389,19 @@ mod test {
assert!(Span::new(0, 1) > Span::new(0, 0));
assert!(Span::new(2, 5) > Span::new(0, 3));
}

#[test]
fn test_contains() {
let span = Span::new(5, 10);

assert!(span.contains_inclusive(span));
assert!(span.contains_inclusive(Span::new(5, 5)));
assert!(span.contains_inclusive(Span::new(10, 10)));
assert!(span.contains_inclusive(Span::new(6, 9)));

assert!(!span.contains_inclusive(Span::new(0, 0)));
assert!(!span.contains_inclusive(Span::new(4, 10)));
assert!(!span.contains_inclusive(Span::new(5, 11)));
assert!(!span.contains_inclusive(Span::new(4, 11)));
}
}
Loading