Anon
Anon

Reputation: 2492

With GCC switch case ranges, is it possible to construct a single case that ranges [A-Za-z]?

Pretty straight forward question. GCC has Case ranges that allow for things like this:

switch (c.toLatin1()) {
default: {
    foo();
    break;
} case 'A' ... 'Z': { 
    bar();
    break;
} case 'a' ... 'z': {
    bar();
    break;
}

https://gcc.gnu.org/onlinedocs/gcc/Case-Ranges.html

The issue here however, is that bar() is redundant, and 'A' ... 'z' will end up including a bunch of unwanted characters:

enter image description here

Conceptually, is something like the following possible?

switch (c.toLatin1()) {
default: {
    foo();
    break;
} case 'A' ... 'Z' || 'a' ... 'z': { 
    bar();
    break;
}

Obviously that is pseudo-code, but you get the idea. Solutions if need be, can include text macros. I am more concerned with accidentally introducing bugs because I forget to add new code:

switch (c.toLatin1()) {
default: {
    foo();
    break;
} case 'A' ... 'Z': { 
    foo(); // I add here
    bar();
    break;
} case 'a' ... 'z': {
    bar(); // but forget to add it here too.
    break;
}

Because those two cases, are effectively one case. Thanks.

Upvotes: 3

Views: 675

Answers (1)

Paul Evans
Paul Evans

Reputation: 27577

Conceptually, is something like the following possible?

switch (c.toLatin1()) {
default: {
    foo();
    break;
} case 'A' ... 'Z' || 'a' ... 'z': { 
    bar();
    break;
}

Sure, just write:

switch (c.toLatin1()) {
default: {
    foo();
    break;
} case 'A' ... 'Z':
  case 'a' ... 'z': { 
    bar();
    break;
}

Upvotes: 6

Related Questions