Advertisement
Guest User

Untitled

a guest
Oct 16th, 2019
65
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.66 KB | None | 0 0
  1. // generate character-based static lookup tables
  2. // that can be used for character classification,
  3. // e.g. like in cctype functions (isdigit, isalpha ...)
  4.  
  5. struct CharLookupTable
  6. {
  7. bool data[256];
  8.  
  9. template <unsigned int N>
  10. constexpr CharLookupTable(const char (&str)[N])
  11. : data{}
  12. {
  13. for (unsigned int i = 0; i < (N - 1); ++i)
  14. data[static_cast<unsigned char>(str[i])] = true;
  15. }
  16. };
  17.  
  18. constexpr CharLookupTable digits{"0123456789"};
  19. constexpr CharLookupTable hexdigits{"0123456789abcdefABCDEF"};
  20.  
  21. bool isdigit(char c)
  22. {
  23. return digits.data[static_cast<unsigned char>(c)];
  24. }
  25.  
  26. bool ishexdigit(char c)
  27. {
  28. return hexdigits.data[static_cast<unsigned char>(c)];
  29. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement