Skip to content

Latest commit

 

History

History
69 lines (52 loc) · 4.7 KB

NSRange.md

File metadata and controls

69 lines (52 loc) · 4.7 KB

NSRange

Structure for representing a range with location and length in mulle-objc.

Structure Definition

typedef struct _NSRange {
   NSUInteger location;  // Starting index
   NSUInteger length;    // Number of items
} NSRange;

Functions

Range Creation

Range Operations

Range Manipulation

Usage Example

// 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}

Important Notes

  1. Range Validation

    • Check for empty ranges
    • Validate indices
    • Handle overflow
    • Consider bounds
  2. Performance

    • Pass by value
    • Avoid copying
    • Cache results
    • Use stack allocation
  3. Best Practices

    • Check for overflow
    • Validate input
    • Document assumptions
    • Test edge cases
    • Consider zero length