Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Only define operator characters in one place

  • Loading branch information...
commit f0a3f95a33f2f67c8e96a53146698d59bde5e3b0 1 parent 3d57a41
@davedelong authored
View
13 DDMathParser/DDMathStringTokenizer.m
@@ -10,6 +10,7 @@
#import "DDMathStringTokenizer.h"
#import "DDMathParserMacros.h"
#import "DDMathStringToken.h"
+#import "_DDOperatorInfo.h"
#define DD_IS_DIGIT(_c) ((_c) >= '0' && (_c) <= '9')
#define DD_IS_WHITESPACE(_c) ([[NSCharacterSet whitespaceAndNewlineCharacterSet] characterIsMember:(_c)])
@@ -55,14 +56,10 @@ + (NSCharacterSet *)_operatorCharacterSet {
static dispatch_once_t onceToken;
static NSCharacterSet *_operatorSet = nil;
dispatch_once(&onceToken, ^{
- // \u2228 is ∨
- // \u2227 is ∧
- // \u00AC is ¬
- // \u2264 is ≤
- // \u2265 is ≥
- // \u00f7 is ÷
- // \u00d7 is ×
- _operatorSet = DD_RETAIN([NSCharacterSet characterSetWithCharactersInString:@"+-*/&|!%^~()<>,=\u2228\u2227\u00ac\u2264\u2265\u00f7\u00d7"]);
+ NSArray *allOperators = [_DDOperatorInfo allOperators];
+ NSArray *operatorTokens = [allOperators valueForKey:@"token"];
+ NSString *operatorString = [operatorTokens componentsJoinedByString:@""];
+ _operatorSet = DD_RETAIN([NSCharacterSet characterSetWithCharactersInString:operatorString]);
});
return _operatorSet;
}
View
7 DDMathParser/_DDOperatorInfo.m
@@ -89,10 +89,12 @@ + (NSArray *)_buildOperators {
NSInteger precedence = 0;
[operators addObject:[self infoForOperator:DDOperatorLogicalOr arity:DDOperatorArityBinary precedence:precedence token:@"||" function:@"l_or" associativity:DDOperatorAssociativityLeft]];
+ // \u2228 is ∨
[operators addObject:[self infoForOperator:DDOperatorLogicalOr arity:DDOperatorArityBinary precedence:precedence token:@"\u2228" function:@"l_or" associativity:DDOperatorAssociativityLeft]];
precedence++;
[operators addObject:[self infoForOperator:DDOperatorLogicalAnd arity:DDOperatorArityBinary precedence:precedence token:@"&&" function:@"l_and" associativity:DDOperatorAssociativityLeft]];
+ // \u2227 is ∧
[operators addObject:[self infoForOperator:DDOperatorLogicalAnd arity:DDOperatorArityBinary precedence:precedence token:@"\u2227" function:@"l_and" associativity:DDOperatorAssociativityLeft]];
precedence++;
@@ -109,14 +111,17 @@ + (NSArray *)_buildOperators {
precedence++;
[operators addObject:[self infoForOperator:DDOperatorLogicalLessThanOrEqual arity:DDOperatorArityBinary precedence:precedence token:@"<=" function:@"l_ltoe" associativity:DDOperatorAssociativityLeft]];
+ // \u2264 is ≤
[operators addObject:[self infoForOperator:DDOperatorLogicalLessThanOrEqual arity:DDOperatorArityBinary precedence:precedence token:@"\u2264" function:@"l_ltoe" associativity:DDOperatorAssociativityLeft]];
precedence++;
[operators addObject:[self infoForOperator:DDOperatorLogicalGreaterThanOrEqual arity:DDOperatorArityBinary precedence:precedence token:@">=" function:@"l_gtoe" associativity:DDOperatorAssociativityLeft]];
+ // \u2265 is ≥
[operators addObject:[self infoForOperator:DDOperatorLogicalGreaterThanOrEqual arity:DDOperatorArityBinary precedence:precedence token:@"\u2265" function:@"l_gtoe" associativity:DDOperatorAssociativityLeft]];
precedence++;
[operators addObject:[self infoForOperator:DDOperatorLogicalNot arity:DDOperatorArityUnary precedence:precedence token:@"!" function:@"l_not" associativity:DDOperatorAssociativityRight]];
+ // \u00AC is ¬
[operators addObject:[self infoForOperator:DDOperatorLogicalNot arity:DDOperatorArityUnary precedence:precedence token:@"\u00ac" function:@"l_not" associativity:DDOperatorAssociativityRight]];
precedence++;
@@ -142,8 +147,10 @@ + (NSArray *)_buildOperators {
// multiplication and division have the same precedence
[operators addObject:[self infoForOperator:DDOperatorMultiply arity:DDOperatorArityBinary precedence:precedence token:@"*" function:@"multiply" associativity:DDOperatorAssociativityLeft]];
+ // \u00d7 is ×
[operators addObject:[self infoForOperator:DDOperatorMultiply arity:DDOperatorArityBinary precedence:precedence token:@"\u00d7" function:@"multiply" associativity:DDOperatorAssociativityLeft]];
[operators addObject:[self infoForOperator:DDOperatorDivide arity:DDOperatorArityBinary precedence:precedence token:@"/" function:@"divide" associativity:DDOperatorAssociativityLeft]];
+ // \u00f7 is ÷
[operators addObject:[self infoForOperator:DDOperatorDivide arity:DDOperatorArityBinary precedence:precedence token:@"\u00f7" function:@"divide" associativity:DDOperatorAssociativityLeft]];
precedence++;
Please sign in to comment.
Something went wrong with that request. Please try again.