C 中的“[0 ... 255] =”语法是什么?
What is this "[0 ... 255] =" syntax in C?
参考js0n.c
代码语法如下:
static void *gostruct[] =
{
[0 ... 255] = &&l_bad,
['\t'] = &&l_loop, [' '] = &&l_loop, ['\r'] = &&l_loop, ['\n'] = &&l_loop,
['"'] = &&l_qup,
[':'] = &&l_loop, [','] = &&l_loop,
['['] = &&l_up, [']'] = &&l_down, // tracking [] and {} individually would allow fuller validation but is really messy
['{'] = &&l_up, ['}'] = &&l_down,
['-'] = &&l_bare, [48 ... 57] = &&l_bare, // 0-9
[65 ... 90] = &&l_bare, // A-Z
[97 ... 122] = &&l_bare // a-z
};
........
.......
l_bad:
*vlen = cur - json; // where error'd
return 0;
........
........
谁能解释一下这里发生了什么?语法 [0 ... 255]
和
&&l_bad
在这里做什么?
...
是GCC提供的扩展
https://gcc.gnu.org/onlinedocs/gcc/Designated-Inits.html#Designated-Inits
To initialize a range of elements to the same value, write [first ...
last] = value
. This is a GNU extension. For example,
int widths[] = { [0 ... 9] = 1, [10 ... 99] = 2, [100] = 3 };
&&
是另一个扩展
https://gcc.gnu.org/onlinedocs/gcc/Labels-as-Values.html#Labels-as-Values
You can get the address of a label defined in the current function (or
a containing function) with the unary operator &&
. The value has
type void *
. This value is a constant and can be used wherever a
constant of that type is valid. For example:
void *ptr;
/* ... */
ptr = &&foo;
参考js0n.c
代码语法如下:
static void *gostruct[] =
{
[0 ... 255] = &&l_bad,
['\t'] = &&l_loop, [' '] = &&l_loop, ['\r'] = &&l_loop, ['\n'] = &&l_loop,
['"'] = &&l_qup,
[':'] = &&l_loop, [','] = &&l_loop,
['['] = &&l_up, [']'] = &&l_down, // tracking [] and {} individually would allow fuller validation but is really messy
['{'] = &&l_up, ['}'] = &&l_down,
['-'] = &&l_bare, [48 ... 57] = &&l_bare, // 0-9
[65 ... 90] = &&l_bare, // A-Z
[97 ... 122] = &&l_bare // a-z
};
........
.......
l_bad:
*vlen = cur - json; // where error'd
return 0;
........
........
谁能解释一下这里发生了什么?语法 [0 ... 255]
和
&&l_bad
在这里做什么?
...
是GCC提供的扩展
https://gcc.gnu.org/onlinedocs/gcc/Designated-Inits.html#Designated-Inits
To initialize a range of elements to the same value, write
[first ... last] = value
. This is a GNU extension. For example,int widths[] = { [0 ... 9] = 1, [10 ... 99] = 2, [100] = 3 };
&&
是另一个扩展
https://gcc.gnu.org/onlinedocs/gcc/Labels-as-Values.html#Labels-as-Values
You can get the address of a label defined in the current function (or a containing function) with the unary operator
&&
. The value has typevoid *
. This value is a constant and can be used wherever a constant of that type is valid. For example:void *ptr; /* ... */ ptr = &&foo;