Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #define is_string_literal(X)
- ...
- ...
- is_string_literal("hello") == true;
- const char * p = "hello";
- is_string_literal(p) == false;
- bool is_string_literal(const char * s);
- is_string_literal("hello") == true;
- const char * p = "hello";
- is_string_literal(p) == false;
- #define is_literal_(x) is_literal_f(#x, sizeof(#x) - 1)
- #define is_literal(x) is_literal_(x)
- bool is_literal_f(const char *s, size_t l)
- {
- const char *e = s + l;
- if(s[0] == 'L') s++;
- if(s[0] != '"') return false;
- for(; s != e; s = strchr(s + 1, '"'))
- {
- if(s == NULL) return false;
- s++;
- while(isspace(*s)) s++;
- if(*s != '"') return false;
- }
- return true;
- }
- const char *p = "string";
- // should is_literal(p) be true or false?
- enum string_type { LITERAL, ARRAY, POINTER };
- void string_func(/*const? */char *c, enum string_type t);
- #define string_func(s)
- (string_func)(s, is_literal(s) ? LITERAL :
- sizeof(s) == sizeof(char *) ? POINTER : ARRAY)
- const char x[] = "Hello, World!";
- #define is_string_literal(X) _is_string_literal("" X)
- bool _is_string_literal (const char *str) { return true; } // practically not needed
- #define is_string_literal(s)
- (memcmp(#s, """, 1) == 0)
- template<size_t SIZE> // for "abcd" or const char a[] = "abcd"
- bool is_string_literal (const char (&arr)[SIZE]) { return true; }
- template<size_t SIZE> // for char a[] = "abcd"
- bool is_string_literal (char (&arr)[SIZE]) { return false; }
- template<typename CHAR_TYPE> // for *p (char* or const char*)
- bool is_string_literal(CHAR_TYPE *&p) { return false; }
- #define is_string_literal(X) _is_string_literal("" X)
- bool _is_string_literal (const char *str) { return true; }
Add Comment
Please, Sign In to add comment