Guest User

Untitled

a guest
Jul 15th, 2018
85
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.69 KB | None | 0 0
  1. #define is_string_literal(X)
  2. ...
  3. ...
  4.  
  5. is_string_literal("hello") == true;
  6. const char * p = "hello";
  7. is_string_literal(p) == false;
  8.  
  9. bool is_string_literal(const char * s);
  10.  
  11. is_string_literal("hello") == true;
  12. const char * p = "hello";
  13. is_string_literal(p) == false;
  14.  
  15. #define is_literal_(x) is_literal_f(#x, sizeof(#x) - 1)
  16. #define is_literal(x) is_literal_(x)
  17.  
  18. bool is_literal_f(const char *s, size_t l)
  19. {
  20. const char *e = s + l;
  21. if(s[0] == 'L') s++;
  22. if(s[0] != '"') return false;
  23. for(; s != e; s = strchr(s + 1, '"'))
  24. {
  25. if(s == NULL) return false;
  26. s++;
  27. while(isspace(*s)) s++;
  28. if(*s != '"') return false;
  29. }
  30. return true;
  31. }
  32.  
  33. const char *p = "string";
  34. // should is_literal(p) be true or false?
  35.  
  36. enum string_type { LITERAL, ARRAY, POINTER };
  37.  
  38. void string_func(/*const? */char *c, enum string_type t);
  39.  
  40. #define string_func(s)
  41. (string_func)(s, is_literal(s) ? LITERAL :
  42. sizeof(s) == sizeof(char *) ? POINTER : ARRAY)
  43.  
  44. const char x[] = "Hello, World!";
  45.  
  46. #define is_string_literal(X) _is_string_literal("" X)
  47. bool _is_string_literal (const char *str) { return true; } // practically not needed
  48.  
  49. #define is_string_literal(s)
  50. (memcmp(#s, """, 1) == 0)
  51.  
  52. template<size_t SIZE> // for "abcd" or const char a[] = "abcd"
  53. bool is_string_literal (const char (&arr)[SIZE]) { return true; }
  54.  
  55. template<size_t SIZE> // for char a[] = "abcd"
  56. bool is_string_literal (char (&arr)[SIZE]) { return false; }
  57.  
  58. template<typename CHAR_TYPE> // for *p (char* or const char*)
  59. bool is_string_literal(CHAR_TYPE *&p) { return false; }
  60.  
  61. #define is_string_literal(X) _is_string_literal("" X)
  62. bool _is_string_literal (const char *str) { return true; }
Add Comment
Please, Sign In to add comment