/// TODO. /// /// See [Apple's documentation](https://developer.apple.com/documentation/foundation/nsrange?language=objc). #[repr(C)] // PartialEq is same as NSEqualRanges #[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Hash, PartialOrd, Ord)] pubstruct NSRange { /// The lower bound of the range (inclusive). pub location: NSUInteger, /// The number of items in the range, starting from `location`. pub length: NSUInteger,
}
impl NSRange { /// Create a new range with the given values. /// /// # Examples /// /// ``` /// use objc2_foundation::NSRange; /// assert_eq!(NSRange::new(3, 2), NSRange::from(3..5)); /// ``` #[inline] #[doc(alias = "NSMakeRange")] pubconstfn new(location: usize, length: usize) -> Self { // Equivalent to NSMakeRange Self { location, length }
}
/// Returns `true` if the range contains no items. /// /// # Examples /// /// ``` /// use objc2_foundation::NSRange; /// /// assert!(!NSRange::from(3..5).is_empty()); /// assert!( NSRange::from(3..3).is_empty()); /// ``` #[inline] pubfn is_empty(&self) -> bool { self.length == 0
}
/// Returns `true` if the index is within the range. /// /// # Examples /// /// ``` /// use objc2_foundation::NSRange; /// /// assert!(!NSRange::from(3..5).contains(2)); /// assert!( NSRange::from(3..5).contains(3)); /// assert!( NSRange::from(3..5).contains(4)); /// assert!(!NSRange::from(3..5).contains(5)); /// /// assert!(!NSRange::from(3..3).contains(3)); /// ``` #[inline] #[doc(alias = "NSLocationInRange")] pubfn contains(&self, index: usize) -> bool { // Same as NSLocationInRange iflet Some(len) = index.checked_sub(self.location) {
len < self.length
} else { // index < self.location false
}
}
/// Returns the upper bound of the range (exclusive). #[inline] #[doc(alias = "NSMaxRange")] pubfn end(&self) -> usize { self.location
.checked_add(self.length)
.expect("NSRange too large")
}
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung und die Messung sind noch experimentell.