would the [' '] be converted to index 32 and ['a'] to index 97?
Yes.
Well, technically it depends on the
execution character set, so I am assuming your dev environment assumes the binary will use ASCII, UTF-8, ISO-8859-1/15, Windows-1252, or one of the compatible character sets, as they usually do. If you use a non-Western locale, it might not be true.
But then what are WHITESPACE, LETTER | LOWERCASE etc? Are the #defines you defined?
They could be preprocessor macros, yes, or enums, or both.
Since the array contains 8-bit values, a typical choice would be to use "magic cookies", i.e. bit masks:
enum {
WHITESPACE = 1 << 0,
LETTER = 1 << 1,
DIGIT = 1 << 2,
LOWERCASE = 1 << 3,
UPPERCASE = 1 << 4,
/* Still 3 bits unused! */
};
This means that if you wanted to implement your own
isspace(),
isdigit(),
islower(),
isupper(),
isalpha() and
isalnum() functions, initialize that array properly, then
#if defined(__GNUC__) || defined(__clang__)
#define STATIC_INLINE __attribute__((__const__, __unused__)) static inline
#else
#define STATIC_INLINE static inline
#endif
STATIC_INLINE int my_isspace(const unsigned char c) { return typemap[c] & WHITESPACE; }
STATIC_INLINE int my_isdigit(const unsigned char c) { return typemap[c] & DIGIT; }
STATIC_INLINE int my_islower(const unsigned char c) { return typemap[c] & LOWERCASE; }
STATIC_INLINE int my_isupper(const unsigned char c) { return typemap[c] & UPPERCASE; }
STATIC_INLINE int my_isalpha(const unsigned char c) { return typemap[c] & LETTER; }
STATIC_INLINE int my_isalnum(const unsigned char c) { return typemap[c] & (LETTER | DIGIT); }
would do the trick. In C, zero is false, and nonzero is true, so the tests simply return the specified bit(s) of the type map for the given character. If the bit(s) are (all) zero, then the result is false; otherwise the result is true. If you want a 0/1 result, use
!!(typemap[c] & ...).
(
! just is logical not:
!x is true if and only if
x is zero; otherwise it is false and has integer value 1. Thus,
!!x 0 if and only if
x is 0, otherwise it is 1.)