How to stringify a string which contains a comma?
Asked Answered
S

2

7

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.

Swindell answered 4/12, 2013 at 19:57 Comment(0)
A
8

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.

Amu answered 4/12, 2013 at 20:7 Comment(1)
For the record: I'm getting the warning "ISO C++ does not permit named variadic macros" (emphasis mine). To get rid of it, just chage #define TOSTR_(x...) #x to #define TOSTR_(...) #__VA_ARGS__.Swindell
S
4

__VA_ARGS__ must be in each call:

#define _STRINGIZE(...) #__VA_ARGS__
#define STRINGIZE(...) _STRINGIZE(__VA_ARGS__)
Statius answered 1/4, 2018 at 22:12 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.