tags:

views:

92

answers:

4
static const int class[UCHAR_MAX] =

{ [(unsigned char)'a'] = LOWER, /*macro value classifying the characters*/
  [(unsigned char)'b'] = LOWER,
.
.
.
}

This is just an idea. Is it a bad one?

+2  A: 

Designated initializers are in C99, not C89. They also exist as a GCC extension for C89, but will not be portable.

Other than that, the use of lookup tables is a common way to handle classification of a small number of objects quickly.

Edit: One correction though: The size of the array should be UCHAR_MAX+1

interjay
+1  A: 

Unfortunately, that is not portable in C89/90.

$ gcc -std=c89 -pedantic test.c -o test
test.c:4: warning: ISO C90 forbids specifying subobject to initialize
test.c:5: warning: ISO C90 forbids specifying subobject to initialize
Matt B.
Ah, I got things mixed up.
Questionable
+1  A: 

BTW, GCC's designated initializer extensions allow for

static const int class[] = {
    [0 ... UCHAR_MAX] = UNCLASSIFIED,
    [(unsigned)'0' ... (unsigned)'9'] = DIGIT,
    [(unsigned)'A' ... (unsigned)'Z'] = UPPER,
    [(unsigned)'a' ... (unsigned)'z'] = LOWER,
 };

initializers applying to ranges of indices, with later initializations overriding earlier ones.

Very non-standard, though; this isn't in C89/C90 nor C99.

ephemient
Yes, that is pretty neat!
Questionable
A: 

Aside from using int rather than unsigned char for the type (and thereby wasting 768 bytes), I consider this a very good idea/implementation. Keep in mind that it depends on C99 features, so it won't work with old C89/C90 compilers.

On the other hand, simple conditionals should be the same speed and much smaller in code size, but they can only represent certain natural classes efficiently.

#define is_ascii_letter(x) (((unsigned)(x)|32)-97<26)
#define is_digit(x) ((unsigned)(x)-'0'<10)

etc.

R..