Re: Compile-time string hash
On Monday, 25 March 2013 11:10:15 UTC+2, David Brown wrote:
> I am looking for ideas or suggestions for a problem I'm facing. I am
> trying to find an efficient way to make hashes from strings to use in a
> very simple command-line interpreter.
Tokenizer generated with lexers like FLEX does not suit you?
> This is for a small
> microcontroller, so I am looking for compact code and compile-time
> evaluation. Typically this will mean macros in C - it is quite likely
> that C++ templates would give a more elegant solution, but I don't have
> access to C++ on all targets for this code.
Do not expect wonders from preprocessor ... it is not
Turing-complete like templates of C++. ;)
> This works well enough, but I would like to
> try to make something that can work with different lengths, and that
> uses a string rather than a list of characters - so that it will work
> with hash("log") and hash("start").
If it has to be small and efficient then I would do like that:
* Write a function for run-time hash.
* Have string and function pair for all the valid commands in some file.
* Add function for "command not found" case.
* Write generator that replaces strings with hashes, sorts the pairs by
hashes into array and generates code for that array.
* Write C function 'execute_command_line' does binary search in that
generated array by hash and executes related function pointer or
"command not found" function.
* Upon receiving command line just pass it to your 'execute_command_line'.
|All times are GMT. The time now is 07:57 PM.|
Powered by vBulletin®. Copyright ©2000 - 2014, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.