Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to stringify a string which contains a comma?

I want to pass a version string in the compile command:

$ g++ -Wall -D VERSION="2013-12-03 02:15:21, commit cb060df" -o main main.cpp

Inside my code, I have the following:

#define TOSTR_(x) #x
#define STRINGIFY(x) TOSTR_(x)
#define VERSION_STR STRINGIFY(VERSION)

This doesn't work, because the VERSION macro has a comma in it, so it looks like I'm passing two arguments to TOSTR() (apparently, the VERSION macro only gets expanded after it's passed to STRINGIFY() as one unique argument).

The following approach I found in here also doesn't work:

#define CONCAT(...) __VA_ARGS__
#define TOSTR_(x) #x
#define STRINGIFY(x) TOSTR_(CONCAT(x))
#define VERSION_STR STRINGIFY(VERSION)

bacause this appears to be the same as

#define VERSIONSTR "CONCAT(2013-12-03 02:15:21, commit cb060df)"

that is, the macro CONCAT() doesn't get expanded.

Note 1: I would rather not pass a C string in the command line, because the version string is actually dynamically generated, and it might contain some quotes. This means that just writing g++ -D VERSION=\""$(GENERATED_STRING)"\" instead of stringifying the passed argument wouldn't work.

Note 2: I would be extremely happy if someone found a way of doing this without any preprocessor macros at all.

like image 484
fonini Avatar asked Dec 04 '13 19:12

fonini


2 Answers

You can write the TOSTR_ macro to take variable arguments:

#define TOSTR_(x...) #x
#define STRINGIFY(x) TOSTR_(x)
#define VERSION_STR STRINGIFY(VERSION)

This code has been tested and works on Apple LLVM version 5.0.

like image 172
godel9 Avatar answered Sep 30 '22 13:09

godel9


__VA_ARGS__ must be in each call:

#define _STRINGIZE(...) #__VA_ARGS__
#define STRINGIZE(...) _STRINGIZE(__VA_ARGS__)
like image 43
Joel Teply Avatar answered Sep 30 '22 13:09

Joel Teply