3

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.

Anon
  • 2,267
  • 3
  • 34
  • 51
  • Notice that `'A'-'Z'` is not guaranty to be contiguous, as for example with EBCDIC. – Jarod42 Jul 17 '19 at 17:17
  • @Jarod42 don't you mean, `'A' ... 'Z'` ? Also, can you expand upon EBCDIC? If it is a platform thing, could I `#define` a text macro which would guarantee contiguous behavior? – Anon Jul 17 '19 at 17:20
  • Range A..Z in [EBCDIC](https://fr.wikipedia.org/wiki/Extended_Binary_Coded_Decimal_Interchange_Code) contains éè. – Jarod42 Jul 17 '19 at 17:27

1 Answers1

6

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;
}
Paul Evans
  • 27,315
  • 3
  • 37
  • 54
  • Addendum to this, I went ahead and defined a text macro: `#define case_Letter case 'a' ... 'z': case 'A' ... 'Z':` which works nicely. – Anon Jul 17 '19 at 17:25