Skip to content
This repository
Browse code

don't hardcode recognizable operator patterns; recognize operators of…

… any length
  • Loading branch information...
commit 2a6ed34d921024ee59e725738b1975dc38ac218c 1 parent f0a3f95
Dave DeLong authored
8  DDMathParser/DDMathStringToken.m
@@ -102,10 +102,10 @@ - (void)resolveToOperator:(DDOperator)operator {
102 102
     operatorInfo = nil;
103 103
     
104 104
     NSArray *matching = [_DDOperatorInfo infosForOperator:operator];
105  
-    if ([matching count] != 1) { return; }
106  
-    
107  
-    ambiguous = NO;
108  
-    operatorInfo = DD_RETAIN([matching objectAtIndex:0]);
  105
+    if ([matching count] > 0) {
  106
+        ambiguous = NO;
  107
+        operatorInfo = DD_RETAIN([matching objectAtIndex:0]);
  108
+    }
109 109
 }
110 110
 
111 111
 @end
34  DDMathParser/DDMathStringTokenizer.m
@@ -442,25 +442,25 @@ - (DDMathStringToken *)_parseOperatorWithError:(NSError **)error {
442 442
     unichar character = [self _nextCharacter];
443 443
     
444 444
     NSCharacterSet *operatorCharacters = [[self class] _operatorCharacterSet];
445  
-    if ([operatorCharacters characterIsMember:character]) {
446  
-        unichar peekNext = [self _peekNextCharacter];
447  
-        if (character == '<' || character == '>' || character == '*' || character == '&' || character == '|' || character == '=') {
448  
-            // <, >, *, &, |, =
449  
-            if (peekNext == character) {
450  
-                // <<, >>, **, &&, ||, ==
451  
-                _characterIndex++;
452  
-                length++;
453  
-            }
454  
-        }
455  
-        
456  
-        if ((character == '<' || character == '>' || character == '!') && peekNext == '=' && length == 1) {
457  
-            // <=, >=, !=
458  
-            _characterIndex++;
459  
-            length++;
  445
+    
  446
+    NSString *lastGood = nil;
  447
+    NSUInteger lastGoodLength = length;
  448
+    
  449
+    while ([operatorCharacters characterIsMember:character]) {
  450
+        NSString *tmp = [NSString stringWithCharacters:(_characters+start) length:length];
  451
+        NSArray *operators = [_DDOperatorInfo infosForOperatorToken:tmp];
  452
+        if ([operators count] > 0) {
  453
+            lastGood = tmp;
  454
+            lastGoodLength = length;
460 455
         }
  456
+        character = [self _nextCharacter];
  457
+        length++;
  458
+    }
  459
+    
  460
+    if (length > 0 && lastGood != nil) {
  461
+        _characterIndex = start+lastGoodLength;
461 462
         
462  
-        NSString *rawToken = [NSString stringWithCharacters:(_characters + start) length:length];
463  
-        return [DDMathStringToken mathStringTokenWithToken:rawToken type:DDTokenTypeOperator];
  463
+        return [DDMathStringToken mathStringTokenWithToken:lastGood type:DDTokenTypeOperator];
464 464
     }
465 465
     
466 466
     _characterIndex = start;

0 notes on commit 2a6ed34

Please sign in to comment.
Something went wrong with that request. Please try again.