@@ -269,60 +269,74 @@ yp_regexp_parse_expression(yp_regexp_parser_t *parser);
269
269
typedef enum {
270
270
YP_REGEXP_OPTION_STATE_INVALID ,
271
271
YP_REGEXP_OPTION_STATE_TOGGLEABLE ,
272
- YP_REGEXP_OPTION_STATE_ADDABLE
272
+ YP_REGEXP_OPTION_STATE_ADDABLE ,
273
+ YP_REGEXP_OPTION_STATE_ADDED ,
274
+ YP_REGEXP_OPTION_STATE_REMOVED
273
275
} yp_regexp_option_state_t ;
274
276
275
- enum {
276
- option_slots = 128
277
- };
277
+ // These are the options that are configurable on the regular expression (or
278
+ // from within a group).
279
+ #define YP_REGEXP_OPTION_STATE_SLOT_MINIMUM 'a'
280
+ #define YP_REGEXP_OPTION_STATE_SLOT_MAXIMUM 'x'
281
+ #define YP_REGEXP_OPTION_STATE_SLOTS (YP_REGEXP_OPTION_STATE_SLOT_MAXIMUM - YP_REGEXP_OPTION_STATE_SLOT_MINIMUM + 1)
282
+
278
283
// This is the set of options that are configurable on the regular expression.
279
- typedef yp_regexp_option_state_t yp_regexp_options_t [ option_slots ] ;
284
+ typedef struct { unsigned char values [ YP_REGEXP_OPTION_STATE_SLOTS ]; } yp_regexp_options_t ;
280
285
281
286
// Initialize a new set of options to their default values.
282
287
static void
283
288
yp_regexp_options_init (yp_regexp_options_t * options ) {
284
- memset (options , YP_REGEXP_OPTION_STATE_INVALID , sizeof (yp_regexp_option_state_t ) * option_slots );
285
- ( * options ) ['i' ] = YP_REGEXP_OPTION_STATE_TOGGLEABLE ;
286
- ( * options ) ['m' ] = YP_REGEXP_OPTION_STATE_TOGGLEABLE ;
287
- ( * options ) ['x' ] = YP_REGEXP_OPTION_STATE_TOGGLEABLE ;
288
- ( * options ) ['d' ] = YP_REGEXP_OPTION_STATE_ADDABLE ;
289
- ( * options ) ['a' ] = YP_REGEXP_OPTION_STATE_ADDABLE ;
290
- ( * options ) ['u' ] = YP_REGEXP_OPTION_STATE_ADDABLE ;
289
+ memset (options , YP_REGEXP_OPTION_STATE_INVALID , sizeof (uint8_t ) * YP_REGEXP_OPTION_STATE_SLOTS );
290
+ options -> values ['i' - YP_REGEXP_OPTION_STATE_SLOT_MINIMUM ] = YP_REGEXP_OPTION_STATE_TOGGLEABLE ;
291
+ options -> values ['m' - YP_REGEXP_OPTION_STATE_SLOT_MINIMUM ] = YP_REGEXP_OPTION_STATE_TOGGLEABLE ;
292
+ options -> values ['x' - YP_REGEXP_OPTION_STATE_SLOT_MINIMUM ] = YP_REGEXP_OPTION_STATE_TOGGLEABLE ;
293
+ options -> values ['d' - YP_REGEXP_OPTION_STATE_SLOT_MINIMUM ] = YP_REGEXP_OPTION_STATE_ADDABLE ;
294
+ options -> values ['a' - YP_REGEXP_OPTION_STATE_SLOT_MINIMUM ] = YP_REGEXP_OPTION_STATE_ADDABLE ;
295
+ options -> values ['u' - YP_REGEXP_OPTION_STATE_SLOT_MINIMUM ] = YP_REGEXP_OPTION_STATE_ADDABLE ;
291
296
}
292
297
293
298
// Attempt to add the given option to the set of options. Returns true if it was
294
299
// added, false if it was already present.
295
300
static bool
296
- yp_regexp_options_add (yp_regexp_options_t * options , unsigned char option ) {
297
- if (option >= option_slots ) {
298
- return false;
299
- }
300
- switch ((* options )[option ]) {
301
- case YP_REGEXP_OPTION_STATE_INVALID :
302
- return false;
303
- case YP_REGEXP_OPTION_STATE_TOGGLEABLE :
304
- case YP_REGEXP_OPTION_STATE_ADDABLE :
305
- (* options )[option ] = YP_REGEXP_OPTION_STATE_INVALID ;
306
- return true;
301
+ yp_regexp_options_add (yp_regexp_options_t * options , unsigned char key ) {
302
+ if (key >= YP_REGEXP_OPTION_STATE_SLOT_MINIMUM && key <= YP_REGEXP_OPTION_STATE_SLOT_MAXIMUM ) {
303
+ key -= YP_REGEXP_OPTION_STATE_SLOT_MINIMUM ;
304
+
305
+ switch (options -> values [key ]) {
306
+ case YP_REGEXP_OPTION_STATE_INVALID :
307
+ case YP_REGEXP_OPTION_STATE_REMOVED :
308
+ return false;
309
+ case YP_REGEXP_OPTION_STATE_TOGGLEABLE :
310
+ case YP_REGEXP_OPTION_STATE_ADDABLE :
311
+ options -> values [key ] = YP_REGEXP_OPTION_STATE_ADDED ;
312
+ return true;
313
+ case YP_REGEXP_OPTION_STATE_ADDED :
314
+ return true;
315
+ }
307
316
}
317
+
308
318
return false;
309
319
}
310
320
311
321
// Attempt to remove the given option from the set of options. Returns true if
312
322
// it was removed, false if it was already absent.
313
323
static bool
314
- yp_regexp_options_remove (yp_regexp_options_t * options , unsigned char option ) {
315
- if (option >= option_slots ) {
316
- return false;
317
- }
318
- switch ((* options )[option ]) {
319
- case YP_REGEXP_OPTION_STATE_INVALID :
320
- case YP_REGEXP_OPTION_STATE_ADDABLE :
321
- return false;
322
- case YP_REGEXP_OPTION_STATE_TOGGLEABLE :
323
- (* options )[option ] = YP_REGEXP_OPTION_STATE_INVALID ;
324
- return true;
324
+ yp_regexp_options_remove (yp_regexp_options_t * options , unsigned char key ) {
325
+ if (key >= YP_REGEXP_OPTION_STATE_SLOT_MINIMUM && key <= YP_REGEXP_OPTION_STATE_SLOT_MAXIMUM ) {
326
+ key -= YP_REGEXP_OPTION_STATE_SLOT_MINIMUM ;
327
+
328
+ switch (options -> values [key ]) {
329
+ case YP_REGEXP_OPTION_STATE_INVALID :
330
+ case YP_REGEXP_OPTION_STATE_ADDABLE :
331
+ return false;
332
+ case YP_REGEXP_OPTION_STATE_TOGGLEABLE :
333
+ case YP_REGEXP_OPTION_STATE_ADDED :
334
+ case YP_REGEXP_OPTION_STATE_REMOVED :
335
+ options -> values [key ] = YP_REGEXP_OPTION_STATE_REMOVED ;
336
+ return true;
337
+ }
325
338
}
339
+
326
340
return false;
327
341
}
328
342
0 commit comments