1

Using DDMathStringTokenizer, while something like 2 + 4 tokenizes fine into 3 tokens (with the second being the + operator and the first and third being numbers), if I pass over a + alone, it fails to return it as a token.

This does not hold true for every other operator I've tried, such as / * - etc.

I can probably force this somehow but how can I get DDMathStringTokenizer to tokenize the + correctly?

To reproduce the issue: The following will return an array with no objects. If you change the string value to another operator character it will return a valid array.

NSError *error = nil;
NSString *string = @"+";
DDMathOperatorSet *opSet = [DDMathOperatorSet defaultOperatorSet];
DDMathStringTokenizer *tokenizer = [[DDMathStringTokenizer alloc] initWithString:string operatorSet:opSet error:&error];
NSLog(@"Tokens:%@", [tokenizer allObjects]);
jscs
  • 63,694
  • 13
  • 151
  • 195
joshd
  • 1,626
  • 14
  • 17
  • I suspect that since it doesn't know if it's an add or a positive it might just return nil? But then that should also happen with `-`. – joshd Jun 13 '15 at 19:36
  • Probably better to write this to the DDMathParser issue tracker: https://github.com/davedelong/DDMathParser/issues – Sven Jun 14 '15 at 13:13

1 Answers1

0

This is the tokenizer trying to be too smart. Since the + character is the first token, it's assumed to be a unary operator (no left-hand-side). It then gets dropped on the floor, because unary + is a worthless operator: it doesn't affect evaluation, so it gets omitted from the token stream.

I believe this is better on the 1.1 branch, but I haven't had time to finish integrating changes in to master.

Dave DeLong
  • 242,470
  • 58
  • 448
  • 498