Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Opposite of C preprocessor "stringification"

When using C preprocessor one can stringify macro argument like this:

#define TO_STRING(x) "a string with " #x 

and so when used, the result is as follows:

TO_STRING(test) will expand to: "a string with test"

Is there any way to do the opposite? Get a string literal as an input argument and produce a C identifier? For example:

TO_IDENTIFIER("some_identifier") would expand to: some_identifier

Thank you for your answers.

EDIT: For those wondering what do I need it for:

I wanted to refer to nodes in a scene graph of my 3D engine by string identifiers but at the same time avoid comparing strings in tight loops. So I figured I'll write a simple tool that will run in pre-build step of compilation and search for predefined string - for example ID("something"). Then for every such token it would calculate CRC32 of the string between the parenthesis and generate a header file with #defines containing those numerical identifiers. For example for the string "something" it would be:

#define __CRC32ID_something 0x09DA31FB

Then, generated header file would be included by each cpp file using ID(x) macros. The ID("something") would of course expand to __CRC32ID_something, so in effect what the compiler would see are simple integer identifiers instead of human friendly strings. Of course now I'll simply settle for ID(something) but I thought that using quotes would make more sense - a programmer who doesn't know how the ID macro works can think that something without quotes is a C identifier when in reality such identifier doesn't exist at all.

like image 733
MasterM Avatar asked Aug 08 '11 12:08

MasterM


People also ask

What is stringification?

When a macro parameter is used with a leading ' # ', the preprocessor replaces it with the literal text of the actual argument, converted to a string constant. Unlike normal parameter replacement, the argument is not macro-expanded first. This is called stringification .

What is Stringizing operator in C?

Stringizing operator (#) The number-sign or "stringizing" operator (#) converts macro parameters to string literals without expanding the parameter definition. It's used only with macros that take arguments.

What does ## mean in macro?

The double-number-sign or token-pasting operator (##), which is sometimes called the merging or combining operator, is used in both object-like and function-like macros. It permits separate tokens to be joined into a single token, and therefore, can't be the first or last token in the macro definition.


2 Answers

No, you can't unstringify something.

like image 187
Puppy Avatar answered Oct 05 '22 20:10

Puppy


//unstringify test  enum fruits{apple,pear};  #define IF_WS_COMPARE_SET_ENUM(x) if(ws.compare(L#x)==0)f_ret=x;  fruits enum_from_string(wstring ws) {  fruits f_ret;  IF_WS_COMPARE_SET_ENUM(apple)  IF_WS_COMPARE_SET_ENUM(pear)  return f_ret; }  void main() {  fruits f;  f=enum_from_string(L"apple");  f=enum_from_string(L"pear"); } 
like image 29
Alexander Barabas Avatar answered Oct 05 '22 19:10

Alexander Barabas