Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > Re: Compile-time string hash

Thread Tools

Re: Compile-time string hash

Öö Tiib
Posts: n/a
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'.

Reply With Quote

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
hash of hash of hash of hash in c++ rp C++ 1 11-10-2011 04:45 PM
Hash#select returns an array but Hash#reject returns a hash... Srijayanth Sridhar Ruby 19 07-02-2008 12:49 PM
make a hash object to string object as look like a hash ? Pokkai Dokkai Ruby 8 11-18-2007 05:23 AM
convert string to hash of hash? dt Perl Misc 1 03-02-2007 04:57 AM
Hash String to String/Int Alex Banks Perl Misc 2 05-22-2005 04:30 PM