use crate::{arc, ns, objc};
#[doc(alias = "NSRange")]
#[derive(Debug, PartialEq, Eq, Clone, Copy)]
#[repr(C)]
pub struct Range {
pub loc: ns::UInteger,
pub len: ns::UInteger,
}
impl Range {
#[inline]
pub const fn new(loc: ns::UInteger, len: ns::UInteger) -> Self {
Self { loc, len }
}
#[doc(alias = "NSMaxRange")]
#[inline]
pub const fn max(&self) -> ns::UInteger {
self.loc + self.len
}
#[doc(alias = "NSIntersectionRange")]
#[inline]
pub fn intersection(a: Self, b: Self) -> Self {
unsafe { NSIntersectionRange(a, b) }
}
#[doc(alias = "NSUnionRange")]
#[inline]
pub fn union(a: Self, b: Self) -> Self {
unsafe { NSUnionRange(a, b) }
}
#[doc(alias = "NSLocationInRange")]
#[inline]
pub const fn loc_in_range(location: ns::UInteger, range: &Self) -> bool {
location >= range.loc && (location - range.loc) < range.len
}
#[inline]
pub const fn contains(&self, location: ns::UInteger) -> bool {
Range::loc_in_range(location, self)
}
}
impl From<std::ops::Range<usize>> for Range {
#[inline]
fn from(value: std::ops::Range<usize>) -> Self {
Self {
loc: value.start,
len: value.len(),
}
}
}
impl ns::Value {
#[objc::msg_send(valueWithRange:)]
pub fn with_range(range: ns::Range) -> arc::R<Self>;
}
#[link(name = "Foundation", kind = "framework")]
unsafe extern "C" {
fn NSIntersectionRange(a: Range, b: Range) -> Range;
fn NSUnionRange(a: Range, b: Range) -> Range;
}