@@ -30,8 +30,9 @@ RE_State *RES_new(const char *str) {
30
30
}
31
31
32
32
static RE_Token Token_new (void ) {
33
- RE_Token token ;
34
- token .t = UNKNOWN ;
33
+ RE_Token token = {
34
+ .t = UNKNOWN
35
+ };
35
36
memset (& token .u , 0 , 256 );
36
37
return token ;
37
38
}
@@ -76,7 +77,8 @@ void Re_drop(RE_Node *n) {
76
77
77
78
static RE_Branch * Branch_resize (RE_Branch * branch ) {
78
79
branch -> size = branch -> size * 2 + 4 ;
79
- branch -> p = (RE_Piece * * )RE_realloc (branch -> p , branch -> size , sizeof (RE_Piece * ));
80
+ branch -> p = (RE_Piece * * )RE_realloc (branch -> p , branch -> size ,
81
+ sizeof (RE_Piece * ));
80
82
return branch ;
81
83
}
82
84
@@ -90,26 +92,27 @@ static RE_Node *Node_new(void) {
90
92
91
93
static RE_Node * Node_resize (RE_Node * node ) {
92
94
node -> size = node -> size * 2 + 4 ;
93
- node -> b = (RE_Branch * * )RE_realloc (node -> b , node -> size , sizeof (RE_Branch * ));
95
+ node -> b = (RE_Branch * * )RE_realloc (node -> b , node -> size ,
96
+ sizeof (RE_Branch * ));
94
97
return node ;
95
98
}
96
99
97
- void fill_by_range (int begin , int end , bool * ch , bool fill ) {
100
+ inline void fill_by_range (int begin , int end , bool * ch , bool fill ) {
98
101
for (int i = begin ; i <= end ; ++ i ) {
99
102
ch [i ] = fill ;
100
103
}
101
104
}
102
- #
103
105
104
- static void fill_by_string (char * s , bool * ch , bool fill ) {
105
- while (* s ) {
106
+ static inline void fill_by_string (char * s , bool * ch , bool fill ) {
107
+ while (* s != '\0' ) {
106
108
ch [(int )* s ] = fill ;
107
109
s ++ ;
108
110
}
109
111
}
110
112
111
- #define fill_by_char (c , ch , fill ) \
112
- ch[(int)c] = fill
113
+ static inline void fill_by_char (int c , bool * ch , bool fill ) {
114
+ ch [c ] = fill ;
115
+ }
113
116
114
117
static RE_Token get_token_escaped (RE_State * st ) {
115
118
RE_Token res = Token_new ();
@@ -185,9 +188,12 @@ static RE_Token get_token_escaped(RE_State *st) {
185
188
return res ;
186
189
}
187
190
188
- #define cmp_class (s , class_name , shift ) \
189
- (!strncmp(s, class_name, strlen(class_name)) && \
190
- (shift = strlen(class_name)) > 0)
191
+ static inline bool cmp_class (const char * s , const char * class_name ,
192
+ int * shift ) {
193
+ int len = strlen (class_name );
194
+ return (strncmp (s , class_name , len ) == 0 &&
195
+ (* shift = len ) > 0 );
196
+ }
191
197
192
198
// charset is complex and I am lazy
193
199
static RE_Token get_token_charset (RE_State * st ) {
@@ -220,7 +226,7 @@ static RE_Token get_token_charset(RE_State *st) {
220
226
break ;
221
227
case '-' :
222
228
if (st -> str_read_pos [1 ] != ']' ) {
223
- fill_by_range (st -> str_read_pos [-1 ], st -> str_read_pos [1 ], \
229
+ fill_by_range (st -> str_read_pos [-1 ], st -> str_read_pos [1 ],
224
230
res .u .ch , fill );
225
231
st -> str_read_pos ++ ;
226
232
} else { // ']' can be the last character in bracket
@@ -237,42 +243,42 @@ static RE_Token get_token_charset(RE_State *st) {
237
243
// print, punct, space, upper, word, xdigit
238
244
st -> str_read_pos += 2 ;
239
245
int shift = 0 ;
240
- if (cmp_class (st -> str_read_pos , "ascii" , shift )) {
246
+ if (cmp_class (st -> str_read_pos , "ascii" , & shift )) {
241
247
fill_by_range (0 , 255 , res .u .ch , fill );
242
- } else if (cmp_class (st -> str_read_pos , "alnum" , shift )) {
248
+ } else if (cmp_class (st -> str_read_pos , "alnum" , & shift )) {
243
249
fill_by_range ((int )'a' , (int )'z' , res .u .ch , fill );
244
250
fill_by_range ((int )'A' , (int )'Z' , res .u .ch , fill );
245
251
fill_by_range ((int )'0' , (int )'9' , res .u .ch , fill );
246
- } else if (cmp_class (st -> str_read_pos , "alpha" , shift )) {
252
+ } else if (cmp_class (st -> str_read_pos , "alpha" , & shift )) {
247
253
fill_by_range ((int )'a' , (int )'z' , res .u .ch , fill );
248
254
fill_by_range ((int )'A' , (int )'Z' , res .u .ch , fill );
249
- } else if (cmp_class (st -> str_read_pos , "blank" , shift )) {
255
+ } else if (cmp_class (st -> str_read_pos , "blank" , & shift )) {
250
256
res .u .ch [(int )' ' ] = fill ;
251
257
res .u .ch [(int )'\t' ] = fill ;
252
- } else if (cmp_class (st -> str_read_pos , "cntrl" , shift )) {
258
+ } else if (cmp_class (st -> str_read_pos , "cntrl" , & shift )) {
253
259
fill_by_range ((int )'\x00' , (int )'\x1F' , res .u .ch , fill );
254
260
res .u .ch [(int )'\x7F' ] = fill ;
255
- } else if (cmp_class (st -> str_read_pos , "digit" , shift )) {
261
+ } else if (cmp_class (st -> str_read_pos , "digit" , & shift )) {
256
262
fill_by_range ((int )'0' , (int )'9' , res .u .ch , fill );
257
- } else if (cmp_class (st -> str_read_pos , "graph" , shift )) {
263
+ } else if (cmp_class (st -> str_read_pos , "graph" , & shift )) {
258
264
fill_by_range ((int )'\x21' , (int )'\x7E' , res .u .ch , fill );
259
- } else if (cmp_class (st -> str_read_pos , "lower" , shift )) {
265
+ } else if (cmp_class (st -> str_read_pos , "lower" , & shift )) {
260
266
fill_by_range ((int )'a' , (int )'z' , res .u .ch , fill );
261
- } else if (cmp_class (st -> str_read_pos , "print" , shift )) {
267
+ } else if (cmp_class (st -> str_read_pos , "print" , & shift )) {
262
268
fill_by_range ((int )'\x20' , (int )'\x7E' , res .u .ch , fill );
263
- } else if (cmp_class (st -> str_read_pos , "punct" , shift )) {
264
- fill_by_string ("][!\"#$%&'()*+,./:;<=>?@\\^_`{|}~-" , res .u .ch , \
269
+ } else if (cmp_class (st -> str_read_pos , "punct" , & shift )) {
270
+ fill_by_string ("][!\"#$%&'()*+,./:;<=>?@\\^_`{|}~-" , res .u .ch ,
265
271
fill );
266
- } else if (cmp_class (st -> str_read_pos , "space" , shift )) {
272
+ } else if (cmp_class (st -> str_read_pos , "space" , & shift )) {
267
273
fill_by_string (" \t\r\n\v\f" , res .u .ch , fill );
268
- } else if (cmp_class (st -> str_read_pos , "upper" , shift )) {
274
+ } else if (cmp_class (st -> str_read_pos , "upper" , & shift )) {
269
275
fill_by_range ((int )'A' , (int )'Z' , res .u .ch , fill );
270
- } else if (cmp_class (st -> str_read_pos , "word" , shift )) {
276
+ } else if (cmp_class (st -> str_read_pos , "word" , & shift )) {
271
277
fill_by_range ((int )'a' , (int )'z' , res .u .ch , fill );
272
278
fill_by_range ((int )'A' , (int )'Z' , res .u .ch , fill );
273
279
fill_by_range ((int )'0' , (int )'9' , res .u .ch , fill );
274
280
res .u .ch [(int )'-' ] = fill ;
275
- } else if (cmp_class (st -> str_read_pos , "xdigit" , shift )) {
281
+ } else if (cmp_class (st -> str_read_pos , "xdigit" , & shift )) {
276
282
fill_by_range ((int )'a' , (int )'f' , res .u .ch , fill );
277
283
fill_by_range ((int )'A' , (int )'F' , res .u .ch , fill );
278
284
fill_by_range ((int )'0' , (int )'9' , res .u .ch , fill );
@@ -410,9 +416,10 @@ static RE_Token get_token(RE_State *st) {
410
416
return res ;
411
417
}
412
418
413
- #define unget_token (st , t ) \
414
- st->unget = t; \
419
+ static inline void unget_token (RE_State * st , RE_Token t ) {
420
+ st -> unget = t ;
415
421
st -> has_unget = true;
422
+ }
416
423
417
424
static RE_Atom * parse_atom (RE_State * st ) {
418
425
RE_Atom * res = alloc (sizeof (RE_Atom ));
@@ -474,8 +481,8 @@ static RE_Branch *parse_branch(RE_State *st) {
474
481
RE_Token peek = get_token (st );
475
482
476
483
// jyi's dialect: the outer ')' can be omited
477
- if (peek .t == END \
478
- || (peek .t == META && peek .u .metachar == ')' ) \
484
+ if (peek .t == END
485
+ || (peek .t == META && peek .u .metachar == ')' )
479
486
|| (peek .t == META && peek .u .metachar == '|' )) {
480
487
unget_token (st , peek );
481
488
break ;
0 commit comments