Skip to content

Instantly share code, notes, and snippets.

@mattt
Last active January 22, 2020 23:32
Show Gist options
  • Star 33 You must be signed in to star a gist
  • Fork 3 You must be signed in to fork a gist
  • Save mattt/8402537 to your computer and use it in GitHub Desktop.
Save mattt/8402537 to your computer and use it in GitHub Desktop.
Re-declaration of existing `NSRange` functions and implementation of new functions to match conventions of comparable Foundation and Core Foundation types.
NS_INLINE NSRange NSRangeMake(NSUInteger loc, NSUInteger len) {
return NSMakeRange(loc, len);
}
NS_INLINE NSUInteger NSRangeMax(NSRange range) {
return NSMaxRange(range);
}
NS_INLINE BOOL NSRangeContainsLocation(NSUInteger loc, NSRange range) {
return NSLocationInRange(loc, range);
}
NS_INLINE BOOL NSRangeEqualToRange(NSRange range1, NSRange range2) {
return NSEqualRanges(range1, range2);
}
NS_INLINE NSRange NSRangeZero() {
return NSRangeMake(0, 0);
}
NS_INLINE NSRange NSRangeUnion(NSRange range1, NSRange range2) {
return NSUnionRange(range1, range2);
}
NS_INLINE NSRange NSRangeIntersection(NSRange range1, NSRange range2) {
return NSIntersectionRange(range1, range2);
}
NS_INLINE NSDictionary * NSRangeCreateDictionaryRepresentation(NSRange range) {
return @{@"location": @(range.location), @"length": @(range.length)};
}
NS_INLINE BOOL NSRangeMakeFromDictionaryRepresentation(NSDictionary *representation, NSRangePointer range) {
if (!representation[@"location"] || !representation[@"length"]) {
return NO;
}
NSRange r;
r.location = [representation[@"location"] unsignedIntegerValue];
r.length = [representation[@"length"] unsignedIntegerValue];
range = &r;
return YES;
}
@harlanhaskins
Copy link

typedef NSRangeZero (NSRange){0, 0}

or

typedef NSRangeZero NSRangeMake(0, 0)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment