Strictly speaking you can't assume that the various characters are in range, portably. The only characters actually guaranteed to be located in a continuous range in the symbol table are '0'
to '9'
. That's mostly a nitpick though, since 99.9% of all systems are ASCII or UTF.
What's more serious is that this look-up is slow. You have numerous branches which the CPU must execute. If you have to call this function repeatedly from a loop, it will be performance-heavy. Instead, you can replace all of this with a look-up table.
For boolean checks, you should be using bool
.
A fixed function might look like this:
#include <stdbool.h>
bool isdelimit (char ch)
{
ch &= 0x7F; // ensure 7 bit
const bool DELIMIT[128] =
{
['\n'] = true,
['\t'] = true,
[' '] = true,
['\0'] = true,
// ...
};
return DELIMIT[ch];
}
The table DELIMIT
will per default initialize all items to false
save for those that you explicitly initialize. By using the character value as the search key, the algorithm turns branchless and efficient.
The above trick with using designated initializers means that you only need to type out those delimiters you are interested in, rather than typing out a big table of 127 values.
Also check out the rarely used but 100% standard C functions strpbrk
and strcspn
(string.h) that can be used for this very purpose too.