Re: Compile-time string hash

Discussion in 'C Programming' started by Öö Tiib, Mar 25, 2013.

  1. Öö Tiib

    Öö Tiib Guest

    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'.
    Öö Tiib, Mar 25, 2013
    #1
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Nagaraj
    Replies:
    1
    Views:
    855
    Lionel B
    Mar 1, 2007
  2. Carter
    Replies:
    2
    Views:
    502
    Carter
    Mar 4, 2009
  3. rp
    Replies:
    1
    Views:
    517
    red floyd
    Nov 10, 2011
  4. glen herrmannsfeldt

    Re: Compile-time string hash

    glen herrmannsfeldt, Mar 25, 2013, in forum: C Programming
    Replies:
    1
    Views:
    253
    glen herrmannsfeldt
    Mar 26, 2013
  5. Johann Klammer

    Re: Compile-time string hash

    Johann Klammer, Mar 25, 2013, in forum: C Programming
    Replies:
    0
    Views:
    209
    Johann Klammer
    Mar 25, 2013
Loading...

Share This Page