Maybe I don't get it, but this whole case block looks like it's scanning the same string 6 times in the worst case. Shouldn't that be possible with only one iteration?