Last active
January 22, 2020 23:32
-
-
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.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | |
} |
You're right. It should be defined as a constant instead, like CGRectZero, etc.
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
Isn't that
NSRange
uninitialized?