Skip to content

Instantly share code, notes, and snippets.

@itod
Created March 23, 2013 20:08
Show Gist options
  • Save itod/5229175 to your computer and use it in GitHub Desktop.
Save itod/5229175 to your computer and use it in GitHub Desktop.
Use ParseKit's `PKTokenizer` class to tokenize HLA-style hex numbers like `$00FF_FFFF` and binary numbers like `%0001_0101`
NSString *s = @"$00FF_FFFF %0001_0101";
PKTokenizer *t = [PKTokenizer tokenizerWithString:s];
// add support for HLA-style hex numbers like $00FF_FFFF
[t.numberState addPrefix:@"$" forRadix:16];
[t.numberState addGroupingSeparator:'_' forRadix:16];
[t setTokenizerState:t.numberState from:'$' to:'$'];
// add support for HLA-style binary numbers like %0001_0101
[t.numberState addPrefix:@"%" forRadix:2];
[t.numberState addGroupingSeparator:'_' forRadix:2];
[t setTokenizerState:t.numberState from:'%' to:'%'];
for (PKToken *tok in t) {
NSLog(@"(%@) (%.1f) : %@", tok.stringValue, tok.floatValue, [tok debugDescription]];
}
// Prints:
// ($00FF_FFFF) (16777215.0) : <Number «16777215»>
// (%0001_0101) (21.0) : <Number «21»>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment