]> git.ozlabs.org Git - ccan/commitdiff
str: add stringify()
authorRusty Russell <rusty@rustcorp.com.au>
Tue, 11 May 2010 00:53:40 +0000 (10:23 +0930)
committerRusty Russell <rusty@rustcorp.com.au>
Tue, 11 May 2010 00:53:40 +0000 (10:23 +0930)
ccan/str/str.h
ccan/str/test/run.c

index f633bc757e1c15655b7a06cfcd21a08922bb4e2a..70fe26c74fad3905b3acf2efd8ac0b792ba766a5 100644 (file)
@@ -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 */
index f35d75d6ea4dc53e8ccb01349dbd519ef6b32739..a9dacab051e9f28e7601df2778842529ba35e3fa 100644 (file)
@@ -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();
 }