[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: meino.cramer@...
- Date: Tue, 11 Mar 2014 19:17:21 +0100
Shmuel Zeigerman <email@example.com> [14-03-11 19:04]:
> On 11/03/2014 19:25, firstname.lastname@example.org wrote:
> >(using Lua 5.2.2 under Linux)
> >from a serial line I receive A LOT of lines
> >consisting of data in ASCII format.
> >The first one to three characters decide, how
> >I have to interpret the data.
> >Since data are transmitted FAST I want to make
> >the recognition code which branches into the
> >different data processing parts as efficient
> >as possible. The code will run on a embedded
> >system, so I will not have the memory for huge
> >look-up table or such...
> >And I will do it in pure Lua since this is the
> >reason why I do this in pure Lua! ;)
> >What is the "best" solution?
> >('best' in "" since this also a product of the
> >point of view ;)
> For simplicity sake, let it always be 3 characters to decide.
> Define as many 3-character identifiers as you need.
> Then it is a simple if-else:
> local prefix, info = line:sub(1,3), line:sub(4)
> if prefix == "abc" then
> -- do something
> elseif prefix == "123" then
> -- do something else
> elseif ...
> (Strings equality test is very cheap in Lua.)
Problem here is, that there is no ifxed length of
The code, which tells me about the kind of data
after that code is either one byte or two bytes or
three bytes long. ASCII data are following directly
So if I alway cut 3 characters I will include data
in some cases, which creates "unknown codes"...