[Date Prev][Date Next][Thread Prev][Thread Next]
- Subject: Re: Most efficient way to recognize a line by its first (up to) three characters
- From: Tim Hill <drtimhill@...>
- Date: Tue, 11 Mar 2014 12:19:46 -0700
On Mar 11, 2014, at 11:59 AM, Luiz Henrique de Figueiredo <email@example.com> wrote:
>> Suppose I have one character code like this
>> A;0000;0000;0000 ("0"s is data)
> Now that you've given us a sample of the input, the solution is easier:
> ... read line
> local prefix,info=line:match("(.-);(.*)$")
> local h=handlers[p[prefix] or badprefix
> and register handlers for "A", "AB", "ABC", etc.
Looking at his example, if the data after the prefix and delimiter is really numeric, then you could actually get away with registering “A;0” .. to “A;9” etc redundantly for the single character case, and “AB;” for the two character case(s). This would let you skip the RE match at the expanse of a somewhat larger lookup table, which I’m guessing would be faster overall. Of course, the handlers might need to know that one or two characters had been eaten, but that’s pretty trivial.