I'm currently working on a code project that requires me to replace certain strings with hashes of those strings. Seeing as these strings will not change at runtime, it would be advantageous, efficiency wise, to have the c preprocessor run my hash function on each string that I declare to be hashed at compile time.
Is there any way to get the C preprocessor to run my hash function at compile time?
I know this doesn't work the way I described above, but just to get an idea of where I'm going, here's some pseudocode that uses a macro. Imagine that instead of simply expanding the macro, the preprocessor ran the hash function and expanded it to the return value of that hash function:
#include <iostream>
#include <string>
#define U64_HASH(inputString) getU64HashCode(inputString)
//my hash function
unsigned long long getU64HashCode (string inputString)
{
/*code*/
}
int main()
{
cout << U64_HASH("thanks for helping me") << endl;
return 0;
}
Again, ideally the cout << U64_HASH("thanks for helping me") << endl;
would expand to cout << 12223622566970860302 << endl;
I wrote a header file generator, and that works fine for this project.
Final Solution
I have decided to use John Purdy's perl script for this project, as it is simply awesome,and allows me to feed the output I want directly to my compiler. Thanks very much, John.
One way to accomplish this is to put all your strings into a header file, and name them:
// StringHeader.h
#define helloWorld "Hello World"
#define error_invalid_input "Error: Invalid Input"
#define this_could_get_tedious "this could get tedious"
Then you can use those strings:
#include "StringHeader.h"
std::cout << this_could_get_tedious << std::endl;
Then you can run a program on your StringHeader.h
to hash each string, and generate a replacement header file:
// Generated StringHeader.h
#define helloWorld 097148937421
#define error_invalid_input 014782672317
#define this_could_get_tedious 894792738384
That looks very manual and tedious, at first, but there's ways to automate it.
For example, you could write something to parse your source code, looking for "quoted strings". Then it could name each string, write it to a single StringHeader.h
, and replace the inline quoted string with the new named string constant. As an additional step when you create the file, you could hash each string - or you could has the file in one go after you've created it. This could let you create a hashed and non-hashed version of the file (which could be nice to create a non-hashed Debug version, and a hashed Release version).
If you do try that, your initial parser to look for strings will have to handle edge cases (comments, #include lines, duplicated strings, etc).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With