case (case '0' ... '9':) 内的三个点是否有效的 C 语言 switch 语法?
我在 DRBD 软件(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;
}
我从来没有见过这个之前 C 中的“三点”构造(case '0' ... '9':
)。它是有效的标准 C 语言吗?或者这是某种预处理器的魔法?这是怎么回事?
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
这是非标准语言扩展。
可能是海湾合作委员会: http://gcc.gnu.org/onlinedocs/gcc-4.1 .2/gcc/Case-Ranges.html。
That's a non-standard language extension.
Probably GCC: http://gcc.gnu.org/onlinedocs/gcc-4.1.2/gcc/Case-Ranges.html.
正如其他人所说,这是一个特定于编译器的扩展。使用正确的选项调用编译器(例如,
gcc -std=c99 -pedantic
),它应该向您发出警告。我还将指出,除了另一个编译器可能不会实现它之外,它的使用也有潜在的危险。
'a' ... 'z'
表示 26 个小写字母 - 但 C 标准不保证它们的值是连续的。例如,在EBCDIC
中,字母之间有标点符号。另一方面,我怀疑 gcc 或 Sun C 是否支持使用字母不连续的字符集的系统。 (它们采用 ASCII 及其所有派生形式,包括 Latin-1、Windows-1252 和 Unicode。)
另一方面,它排除带重音的字母。 (根据
DRBD
的使用方式,这可能是问题,也可能不是问题。)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. InEBCDIC
, 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.)不,这是 GCC 的扩展< /a>.
No, this is an extension of GCC.
这不是标准 C,而是 Sun C 编译器中的扩展。
参考:2.7 Switch 语句中的大小写范围 在 Oracle 网站上。
更新:显然,不仅仅是 Oracle! :-)
This is not standard C, but is an extension found in the Sun C compiler.
Refer to: 2.7 Case Ranges in Switch Statements at Oracle's web site.
UPDATE: Apparently, not just Oracle! :-)