How to stringify a string which contains a comma?

╄→尐↘猪︶ㄣ 提交于 2020-01-22 16:00:29

问题


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.


回答1:


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.




回答2:


__VA_ARGS__ must be in each call:

#define _STRINGIZE(...) #__VA_ARGS__
#define STRINGIZE(...) _STRINGIZE(__VA_ARGS__)


来源:https://stackoverflow.com/questions/20384709/how-to-stringify-a-string-which-contains-a-comma

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!