Last active
October 8, 2018 15:37
-
-
Save MichaelEvanchik/2dbd10033bc818f794363622c63dd44f to your computer and use it in GitHub Desktop.
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
#include "NSUFRange.h" | |
#import <Foundation/NSValue.h> | |
#import <Foundation/NSObjCRuntime.h> | |
@class NSString; | |
typedef struct _NSUFRange { | |
float location; | |
float length; | |
} | |
NSUFRange; | |
typedef NSUFRange *NSUFRangePointer; | |
NS_INLINE NSUFRange NSUFMakeRange(float loc, float len) { | |
NSUFRange r; | |
r.location = loc; | |
r.length = len; | |
return r; | |
} | |
NS_INLINE float NSUFMaxRange(NSUFRange range) { | |
return (range.location + range.length); | |
} | |
NS_INLINE BOOL NSUFLocationInRange(float loc, NSUFRange range) { | |
return (!(loc < range.location) && (loc - range.location) < range.length) ? YES : NO; | |
} | |
NS_INLINE BOOL NSUFEqualRanges(NSUFRange range1, NSUFRange range2) { | |
return (range1.location == range2.location && range1.length == range2.length); | |
} | |
Categories | |
------------------ | |
//not sure why NSNumber has to be used, but i had to, no big deal | |
@implementation NSNumber (Utilities) | |
-(NSNumber*)isInRange:(float)lowerRange HighRange:(float)highestRange ValueToEvaluate:(float)valueToEvaluate | |
{ | |
NSNumber *intNumber; | |
NSUFRange validRange = NSUFMakeRange(lowerRange, highestRange); | |
BOOL supportsRule = NSUFLocationInRange(valueToEvaluate, validRange); | |
if ( supportsRule ) | |
{ | |
intNumber = [NSNumber numberWithInteger: 1]; | |
return intNumber; | |
} | |
intNumber = [NSNumber numberWithInteger: 0]; | |
return intNumber; | |
} | |
@end | |
-(NSString *)substring:(NSInteger)from to:(NSInteger)to | |
{ | |
NSString *rightPart = [self substringFromIndex:from]; | |
return [rightPart substringToIndex:to-from]; | |
} | |
-(int)indexOf:(NSString*)text | |
{ | |
NSRange range = [self rangeOfString:text]; | |
if ( range.length > 0 ) { | |
return range.location; | |
} else { | |
return -1; | |
} | |
} | |
-(BOOL)validForXpathForVersion:(AXLRequestWindowController*)controller applicableFor:(NSString*)ApplicableFor fulllVersionFor:(NSArray*)FullVersionFor | |
{ | |
//if this rule is specicific for this full version, this superceeds all | |
if(controller.delegate.fullVersion.stringValue == [FullVersionFor description]) | |
{ | |
return YES; | |
} | |
else | |
{ | |
NSArray *aryApplicableFor = [ApplicableFor componentsSeparatedByString: @","]; | |
//TODO: make sure all entries are valid versions | |
CGFloat floatVersion = (CGFloat)[controller.delegate.version.stringValue floatValue]; | |
CGFloat rangeHoldNegative = 0; | |
CGFloat rangeHoldPositive = 0; | |
CGFloat loopFloatVersion = 0; | |
NSString *lastApplicable = nil; | |
NSString *loopVersion = nil; | |
NSNumber *supportsRule; | |
NSNumber *IsSupported; | |
IsSupported = [NSNumber numberWithInteger: 1]; | |
for (NSString *applicable in aryApplicableFor) | |
{ | |
loopVersion = nil; | |
CGFloat loopFloatVersion = 0; | |
if( ([applicable indexOf:@"+"] > -1) ) | |
{ | |
loopVersion = [applicable substring:[applicable indexOf:@"+"] + 1 to:[applicable length]]; | |
} | |
if( ([applicable indexOf:@"-"] > -1) ) | |
{ | |
loopVersion = [applicable substring:[applicable indexOf:@"-"] + 1 to:[applicable length]]; | |
} | |
loopFloatVersion = (CGFloat)[loopVersion floatValue]; | |
//first check if its a single entry | |
if([aryApplicableFor count] == 1) | |
{ | |
if([applicable isEqualToString:@"all"] || !applicable.length) | |
{ | |
return YES; | |
} | |
else | |
{ | |
if( ([applicable indexOf:@"+"] > -1) ) | |
{ | |
if( [supportsRule isInRange:loopFloatVersion HighRange:99.0 ValueToEvaluate:floatVersion] == IsSupported) | |
return YES; | |
} | |
else if( ([applicable indexOf:@"-"] > -1) ) | |
{ | |
if( [supportsRule isInRange:0 HighRange:loopFloatVersion ValueToEvaluate:floatVersion] == IsSupported) | |
return YES; | |
} | |
else if ([applicable isEqualToString:controller.delegate.version.stringValue]) | |
return YES; | |
} | |
} | |
else | |
{ | |
if( ([applicable indexOf:@"+"] < 0) && ([applicable indexOf:@"-"] < 0) ) | |
{ | |
if(applicable == controller.delegate.fullVersion.stringValue) | |
return YES; | |
if(rangeHoldNegative > 0) | |
{ | |
rangeHoldNegative = 0; | |
if( [supportsRule isInRange:0 HighRange:rangeHoldNegative ValueToEvaluate:floatVersion] == IsSupported) | |
return YES; | |
} | |
if(rangeHoldPositive > 0) | |
{ | |
rangeHoldPositive = 0; | |
if( [supportsRule isInRange:rangeHoldPositive HighRange:99.0 ValueToEvaluate:floatVersion] == IsSupported) | |
return YES; | |
} | |
} | |
if( ([applicable indexOf:@"+"] > -1) ) | |
{ | |
rangeHoldPositive = (CGFloat)[loopVersion floatValue]; | |
if(rangeHoldNegative > 0) | |
{ | |
rangeHoldNegative = 0; | |
if( [supportsRule isInRange:0 HighRange:rangeHoldNegative ValueToEvaluate:floatVersion] == IsSupported) | |
return YES; | |
} | |
} | |
if( ([applicable indexOf:@"-"] > -1) ) | |
{ | |
rangeHoldNegative = (CGFloat)[loopVersion floatValue]; | |
if(rangeHoldNegative > 0 && rangeHoldPositive > 0) | |
{ | |
rangeHoldNegative = 0; | |
rangeHoldPositive = 0; | |
if( [supportsRule isInRange:rangeHoldPositive HighRange:rangeHoldNegative ValueToEvaluate:floatVersion] == IsSupported) | |
return YES; | |
} | |
if(rangeHoldNegative > 0 && rangeHoldPositive == 0) | |
{ | |
rangeHoldNegative = 0; | |
if( [supportsRule isInRange:0 HighRange:rangeHoldNegative ValueToEvaluate:floatVersion] == supportsRule) | |
return YES; | |
} | |
} | |
}//end on if one count in array or more then one | |
lastApplicable = applicable; | |
}//end on for | |
if(rangeHoldNegative > 0) | |
{ | |
loopVersion = [lastApplicable substring:[lastApplicable indexOf:@"-"] + 1 to:[lastApplicable length]]; | |
loopFloatVersion = (CGFloat)[loopVersion floatValue]; | |
rangeHoldNegative = 0; | |
if( [supportsRule isInRange:0 HighRange:loopFloatVersion ValueToEvaluate:floatVersion] == supportsRule) | |
return YES; | |
} | |
if(rangeHoldPositive > 0) | |
{ | |
loopVersion = [lastApplicable substring:[lastApplicable indexOf:@"+"] + 1 to:[lastApplicable length]]; | |
loopFloatVersion = (CGFloat)[loopVersion floatValue]; | |
rangeHoldPositive = 0; | |
if( [supportsRule isInRange:loopFloatVersion HighRange:99.0 ValueToEvaluate:floatVersion] == supportsRule) | |
return YES; | |
} | |
}// end if specific FullVersion | |
return NO; | |
}//end function |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment