Created
September 28, 2019 01:49
-
-
Save mooware/e22968bbcf70b1fbbc30623c18390e1a to your computer and use it in GitHub Desktop.
Constexpr generation of character lookup tables
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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