Skip to content

Instantly share code, notes, and snippets.

@mooware
Created September 28, 2019 01:49
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mooware/e22968bbcf70b1fbbc30623c18390e1a to your computer and use it in GitHub Desktop.
Save mooware/e22968bbcf70b1fbbc30623c18390e1a to your computer and use it in GitHub Desktop.
Constexpr generation of character lookup tables
// generate character-based static lookup tables
// that can be used for character classification,
// e.g. like in cctype functions (isdigit, isalpha ...)
struct CharLookupTable
{
bool data[256];
template <unsigned int N>
constexpr CharLookupTable(const char (&str)[N])
: data{}
{
for (unsigned int i = 0; i < (N - 1); ++i)
data[static_cast<unsigned char>(str[i])] = true;
}
};
constexpr CharLookupTable digits{"0123456789"};
constexpr CharLookupTable hexdigits{"0123456789abcdefABCDEF"};
bool isdigit(char c)
{
return digits.data[static_cast<unsigned char>(c)];
}
bool ishexdigit(char c)
{
return hexdigits.data[static_cast<unsigned char>(c)];
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment