Structure for representing a range with location and length in mulle-objc.
typedef struct _NSRange {
NSUInteger location; // Starting index
NSUInteger length; // Number of items
} NSRange;
NSMakeRange
- Create range structNSEmptyRange
- Create empty range
NSEqualRanges
- Compare rangesNSLocationInRange
- Check if location in rangeNSMaxRange
- Get maximum location
NSUnionRange
- Combine rangesNSIntersectionRange
- Intersect ranges
// Create range
NSRange range = NSMakeRange(0, 10);
// Check if location is in range
BOOL contains = NSLocationInRange(5, range); // YES
// Get maximum location
NSUInteger max = NSMaxRange(range); // 10
// Combine ranges
NSRange range1 = NSMakeRange(0, 5);
NSRange range2 = NSMakeRange(3, 5);
NSRange union = NSUnionRange(range1, range2); // {0, 8}
// Intersect ranges
NSRange intersection = NSIntersectionRange(range1, range2); // {3, 2}
-
Range Validation
- Check for empty ranges
- Validate indices
- Handle overflow
- Consider bounds
-
Performance
- Pass by value
- Avoid copying
- Cache results
- Use stack allocation
-
Best Practices
- Check for overflow
- Validate input
- Document assumptions
- Test edge cases
- Consider zero length