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:
Then you can use those strings:
Then you can run a program on your
StringHeader.hto hash each string, and generate a replacement header file: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).