Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Are triple dots inside a case (case '0' ... '9':) valid C language switch syntax? [duplicate]

I noticed this in open source code files for DRBD software (user/drbdtool_common.c)

const char* shell_escape(const char* s)
{
    /* ugly static buffer. so what. */
    static char buffer[1024];
    char *c = buffer;

    if (s == NULL)
        return s;

    while (*s) {
        if (buffer + sizeof(buffer) < c+2)
            break;

        switch(*s) {
        /* set of 'clean' characters */
        case '%': case '+': case '-': case '.': case '/':
        case '0' ... '9':
        case ':': case '=': case '@':
        case 'A' ... 'Z':
        case '_':
        case 'a' ... 'z':
            break;
        /* escape everything else */
        default:
            *c++ = '\\';
        }
        *c++ = *s++;
    }
    *c = '\0';
    return buffer;
}

I have never seen this "triple dot" construction (case '0' ... '9':) in C before. Is it a valid standard C language? Or is that some kind of preprocessor magic? What's going on here?

like image 493
Gart Avatar asked Aug 12 '11 17:08

Gart


People also ask

What is the syntax of switch case?

Switch Case Syntaxswitch( expression ) { case value-1: Block-1; Break; case value-2: Block-2; Break; case value-n: Block-n; Break; default: Block-1; Break; } Statement-x; The expression can be integer expression or a character expression.

Can switch case have duplicates?

If two cases in a 'switch' statement are identical, the second case will never be executed. This most likely indicates a copy-paste error where the first case was copied and then not properly adjusted.

Is switch a case statement in C?

In C++, the switch statement is used for executing one condition from multiple conditions. It is similar to an if-else-if ladder. Switch statement consists of conditional based cases and a default case. In a switch statement, the “case value” can be of “char” and “int” type.

Can switch case have multiple conditions in C?

The switch case in the C language is used when we have more than one option for the single variable that we need to execute. The switch finds the best match for the switch expression and executes the statement accordingly. If we don't find any match at all for the switch expression, the default statement is executed.


2 Answers

That's a non-standard language extension.

Probably GCC: http://gcc.gnu.org/onlinedocs/gcc-4.1.2/gcc/Case-Ranges.html.

like image 66
Oliver Charlesworth Avatar answered Oct 10 '22 19:10

Oliver Charlesworth


As others have said, this is a compiler-specific extension. Invoke the compiler with the right options (say, gcc -std=c99 -pedantic), and it should warn you about it.

I'll also point out that its use is potentially dangerous, apart from the fact that another compiler might not implement it. 'a' ... 'z' denotes the 26 lowercase letters -- but the C Standard doesn't guarantee that their values are contiguous. In EBCDIC, for example, there are punctuation characters among the letters.

On the other hand, I doubt that either gcc or Sun C supports systems that use a character set in which the letters aren't contiguous. (They are in ASCII and all its derivatives, including Latin-1, Windows-1252, and Unicode.)

On the other other hand, it excludes accented letters. (Depending on how DRBD is used, that may or may not be an issue.)

like image 44
Keith Thompson Avatar answered Oct 10 '22 18:10

Keith Thompson