Reputation: 2492
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:
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
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