From 90e94990efbd7fb280f5ff30d2da4401d7ce27fc Mon Sep 17 00:00:00 2001 From: Rusty Russell Date: Tue, 11 May 2010 10:23:40 +0930 Subject: [PATCH 1/1] str: add stringify() --- ccan/str/str.h | 12 ++++++++++++ ccan/str/test/run.c | 8 +++++++- 2 files changed, 19 insertions(+), 1 deletion(-) diff --git a/ccan/str/str.h b/ccan/str/str.h index f633bc75..70fe26c7 100644 --- a/ccan/str/str.h +++ b/ccan/str/str.h @@ -43,4 +43,16 @@ static inline bool strends(const char *str, const char *postfix) return streq(str + strlen(str) - strlen(postfix), postfix); } + +/** + * stringify - Turn expression into a string literal + * @expr: any C expression + * + * Example: + * #define PRINT_COND_IF_FALSE(cond) \ + * ((cond) || printf("%s is false!", stringify(cond))) + */ +#define stringify(expr) stringify_1(expr) +/* Double-indirection required to stringify expansions */ +#define stringify_1(expr) #expr #endif /* CCAN_STR_H */ diff --git a/ccan/str/test/run.c b/ccan/str/test/run.c index f35d75d6..a9dacab0 100644 --- a/ccan/str/test/run.c +++ b/ccan/str/test/run.c @@ -35,7 +35,7 @@ int main(int argc, char *argv[]) } } - plan_tests(n * n * 5); + plan_tests(n * n * 5 + 3); for (i = 0; i < n; i++) { for (j = 0; j < n; j++) { unsigned int k, identical = 0; @@ -76,5 +76,11 @@ int main(int argc, char *argv[]) } } + ok1(streq(stringify(NUM_SUBSTRINGS), + "((sizeof(substrings) / sizeof(substrings[0])) - 1)")); + ok1(streq(stringify(ARRAY_SIZE(substrings)), + "(sizeof(substrings) / sizeof(substrings[0]))")); + ok1(streq(stringify(i == 0), "i == 0")); + return exit_status(); } -- 2.39.2