I’m currently reading “The C Programming Language 2nd edition” and I’m not clear about this exercise:
Functions like isupper can be implemented to save space or to save time. Explore both possibilities.
- How can I implement this function?
- How should I write two versions, one to save time and one to
save space (some pseudo code would be nice)?
I would appreciate some advice about this.
Original answer
One version uses an array initialized with appropriate values, one byte per character in the code set (plus 1 to allow for EOF, which may also be passed to the classification functions):
Note that the ‘bits’ can be used by all the various functions like
isupper(),islower(),isalpha(), etc with appropriate values for the mask. And if you make the ‘bits’ array changeable at runtime, you can adapt to different (single-byte) code sets.This takes space – the array.
The other version makes assumptions about the contiguity of upper-case characters, and also about the limited set of valid upper-case characters (fine for ASCII, not so good for ISO 8859-1 or its relatives):
This can (almost) be implemented in a macro; it is hard to avoid evaluating the character twice, which is not actually permitted in the standard. Using non-standard (GNU) extensions, it can be implemented as a macro that evaluates the character argument just once. To extend this to ISO 8859-1 would require a second condition, along the lines of:
Repeat that as a macro very often and the ‘space saving’ rapidly becomes a cost as the bit masking has a fixed size.
Given the requirements of modern code sets, the mapping version is almost invariably used in practice; it can adapt at run-time to the current code set, etc, which the range-based versions cannot.
Extended answer
Ignoring issues of namespaces for symbols in headers, you have a series of twelve classification macros:
isalpha()isupper()islower()isalnum()isgraph()isprint()iscntrl()isdigit()isblank()isspace()ispunct()isxdigit()The distinction between
isspace()andisblank()is:isspace()— space (' '), form feed ('\f'), new-line ('\n'), carriage return ('\r'), horizontal tab ('\t'), and vertical tab ('\v').isblank()— space (' '), and horizontal tab ('\t').There are definitions for these sets of characters in the C standard, and guidelines for the C locale.
For example (in the C locale), either
islower()orisupper()is true ifisalpha()is true, but that need not be the true in other locales.I think the necessary bits are:
DIGIT_MASKXDIGT_MASKALPHA_MASKLOWER_MASKUPPER_MASKPUNCT_MASKSPACE_MASKPRINT_MASKCNTRL_MASKBLANK_MASKFrom these ten masks, you can create the other two:
Superficially, you can also use
ALPHA_MASK = UPPER_MASK | LOWER_MASK, but in some locales, there are alphabetic characters that are neither upper-case nor lower-case.So, we can define masks as follows:
The data for the character set; the data shown is for the first half of ISO 8859-1, but is the same for the first half of all the 8859-x code sets. I’m using C99 designated initializers as a documentary aid, even though the entries are all in order:
As already noted, the names here are actually in the namespace reserved for users, so if you looked in a
<ctype.h>header you’d find more cryptic names and they’d probably all start with one or two underscores.