Last active
August 29, 2015 14:05
-
-
Save cennydavidsson/5272fb5c3f5dcb557d63 to your computer and use it in GitHub Desktop.
An old snippet from the code of my game Solution.
This file contains hidden or 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
| + (NSCharacterSet *)validCharacters { | |
| return [NSCharacterSet characterSetWithCharactersInString:@"1234567890+-*()"]; | |
| } | |
| + (BOOL)isStringValid:(NSString *)string { | |
| if (!string) return NO; | |
| NSCharacterSet *inputSet = [NSCharacterSet characterSetWithCharactersInString:string]; | |
| if (string.length > 1) { | |
| if ([[NSCharacterSet decimalDigitCharacterSet] isSupersetOfSet:inputSet]) return YES; | |
| }else { | |
| if ([[CDExpression validCharacters] isSupersetOfSet:inputSet]) return YES; | |
| } | |
| return NO; | |
| } | |
| - (BOOL)canStringBeAddedToEquation:(NSString *)string { | |
| if (![CDExpression isStringValid:string]) return NO; | |
| NSString *lastString = ![self.Expression lastObject] ? @"" : [self.Expression lastObject]; | |
| NSCharacterSet *lastSet = [NSCharacterSet characterSetWithCharactersInString:lastString]; | |
| NSCharacterSet *stringSet = [NSCharacterSet characterSetWithCharactersInString:string]; | |
| if (!lastString || [[NSCharacterSet characterSetWithCharactersInString:@"+*-x("] isSupersetOfSet:lastSet]) { | |
| if ([[NSCharacterSet decimalDigitCharacterSet] isSupersetOfSet:stringSet]) { | |
| return YES; | |
| }else if ([string isEqualToString:@"("]) { | |
| return YES; | |
| } | |
| } | |
| if ([string isEqualToString:@")"]) { | |
| if (self.parenthesesStack > 0) { | |
| return YES; | |
| } | |
| } | |
| if ([[NSCharacterSet decimalDigitCharacterSet] isSupersetOfSet:lastSet] && ![lastString isEqualToString:@""]) { | |
| if ([[NSCharacterSet characterSetWithCharactersInString:@"*-+x"] isSupersetOfSet:stringSet]) { | |
| return YES; | |
| } | |
| } | |
| if ([lastString isEqualToString:@")"]) { | |
| if ([[NSCharacterSet characterSetWithCharactersInString:@"*-+x"] isSupersetOfSet:stringSet]) { | |
| return YES; | |
| } | |
| } | |
| return NO; | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment