spacepaste

  1.  
  2. #define STR_TYPE_EMPTY (1 << 0)
  3. #define STR_TYPE_BINARY (1 << 1)
  4. #define STR_TYPE_ALPHABETIC (1 << 2)
  5. #define STR_TYPE_ALPHANUMERIC (1 << 3)
  6. #define STR_TYPE_ASCII (1 << 4)
  7. #define STR_TYPE_UNICODE (1 << 5)
  8. #define STR_TYPE_HEXADECIMAL (1 << 6)
  9. #define STR_TYPE_UPPERCASE (1 << 7)
  10. #define STR_TYPE_LOWERCASE (1 << 8)
  11. #define STR_TYPE_SPACE (1 << 9)
  12. #define STR_TYPE_PUNCTUATION (1 << 10)
  13. #define STR_TYPE_PRINTABLE (1 << 11)
  14. #define STR_TYPE_GRAPH (1 << 12)
  15. #define STR_TYPE_DIGIT (1 << 13)
  16. #define STR_TYPE_CONTROL (1 << 14)
  17. #define STR_TYPE_BLANK (1 << 15)
  18. #define STR_TYPE_ADD_TYPE(STR, STR_TYPE) (STR).type |= STR_TYPE;
  19. #define STR_TYPE_REMOVE_TYPE(STR, STR_TYPE) (STR).type &= ~STR_TYPE;
  20. // uses ctypes.h, unknows if safe or portable
  21. #include <ctype.h>
  22. #define range_BINARY(ch) (ch=='0'||ch=='1')
  23. #define range_UNICODE(ch) (ch==ch) /* to be improved */
  24. #define range_ALPHABETIC(ch) isalpha((unsigned char) ch)
  25. #define range_ALPHANUMERIC(ch) isalnum((unsigned char) ch)
  26. #define range_ASCII(ch) isascii((unsigned char) ch)
  27. #define range_HEXADECIMAL(ch) isxdigit((unsigned char) ch)
  28. #define range_UPPERCASE(ch) isupper((unsigned char) ch)
  29. #define range_LOWERCASE(ch) islower((unsigned char) ch)
  30. #define range_SPACE(ch) isspace((unsigned char) ch)
  31. #define range_PUNCTUATION(ch) ispunct((unsigned char) ch)
  32. #define range_PRINTABLE(ch) isprint((unsigned char) ch)
  33. #define range_GRAPH(ch) isgraph((unsigned char) ch)
  34. #define range_DIGIT(ch) isxdigit((unsigned char) ch)
  35. #define range_CONTROL(ch) iscntrl((unsigned char) ch)
  36. #define range_BLANK(ch) isblank((unsigned char) ch)
  37. // this is not consistant throughout strings and is currently only the representation of the last character added
  38. // if a flag gets disabled it should not be allowed to become re-enabled unless on the event of an undo
  39. #define AUTOTYPE_TEST_(AUTOSTR, AUTOCH, AUTORANGE, AUTOTYPE) { \
  40. \
  41. /* AUTORANGE */ \
  42. if(range_##AUTORANGE(AUTOCH)) { \
  43. if ((AUTOSTR).type == STR_TYPE_EMPTY) { \
  44. STR_TYPE_REMOVE_TYPE((AUTOSTR), STR_TYPE_EMPTY) \
  45. } \
  46. STR_TYPE_ADD_TYPE((AUTOSTR), STR_TYPE_##AUTOTYPE) \
  47. } \
  48. else { \
  49. STR_TYPE_REMOVE_TYPE((AUTOSTR), STR_TYPE_##AUTOTYPE) \
  50. } \
  51. }
  52. #define AUTOTYPE_TEST(AUTOSTR, AUTOCH, AUTOTYPE) AUTOTYPE_TEST_(AUTOSTR, AUTOCH, AUTOTYPE, AUTOTYPE)
  53. #define AUTOTYPE(str, ch) { \
  54. AUTOTYPE_TEST((str), (ch), BINARY) \
  55. AUTOTYPE_TEST((str), (ch), ALPHABETIC) \
  56. AUTOTYPE_TEST((str), (ch), ALPHANUMERIC) \
  57. AUTOTYPE_TEST((str), (ch), ASCII) \
  58. AUTOTYPE_TEST((str), (ch), UNICODE) \
  59. AUTOTYPE_TEST((str), (ch), HEXADECIMAL) \
  60. AUTOTYPE_TEST((str), (ch), UPPERCASE) \
  61. AUTOTYPE_TEST((str), (ch), LOWERCASE) \
  62. AUTOTYPE_TEST((str), (ch), SPACE) \
  63. AUTOTYPE_TEST((str), (ch), PUNCTUATION) \
  64. AUTOTYPE_TEST((str), (ch), PRINTABLE) \
  65. AUTOTYPE_TEST((str), (ch), GRAPH) \
  66. AUTOTYPE_TEST((str), (ch), DIGIT) \
  67. AUTOTYPE_TEST((str), (ch), CONTROL) \
  68. AUTOTYPE_TEST((str), (ch), BLANK) \
  69. }
  70. #define TYPECHECK(str, type_) { \
  71. if ((str).type & type_) puts(#type_); \
  72. }
  73. #define TYPEINFO(str) { \
  74. printf("(%s).type = %d\n", #str, (str).type); \
  75. TYPECHECK((str), STR_TYPE_EMPTY) \
  76. TYPECHECK((str), STR_TYPE_BINARY) \
  77. TYPECHECK((str), STR_TYPE_ALPHABETIC) \
  78. TYPECHECK((str), STR_TYPE_ALPHANUMERIC) \
  79. TYPECHECK((str), STR_TYPE_ASCII) \
  80. TYPECHECK((str), STR_TYPE_UNICODE) \
  81. TYPECHECK((str), STR_TYPE_HEXADECIMAL) \
  82. TYPECHECK((str), STR_TYPE_UPPERCASE) \
  83. TYPECHECK((str), STR_TYPE_LOWERCASE) \
  84. TYPECHECK((str), STR_TYPE_PUNCTUATION) \
  85. TYPECHECK((str), STR_TYPE_PRINTABLE) \
  86. TYPECHECK((str), STR_TYPE_GRAPH) \
  87. TYPECHECK((str), STR_TYPE_DIGIT) \
  88. TYPECHECK((str), STR_TYPE_CONTROL) \
  89. TYPECHECK((str), STR_TYPE_BLANK) \
  90. }
  91.