Use a better algorithm for keeping track of the recognition of named entities

I ran into a serious performance problem when parsing a 5 MB document that contained a lot of invalid named entities due to unencoded ampersands. Switching to a lookup table-based algorithm made my test case run in 8 seconds instead of 140 seconds.