Skip to content

Commit 4deb7c3

Browse files
committed
Use smaller regexp options
1 parent df3d32c commit 4deb7c3

File tree

1 file changed

+48
-34
lines changed

1 file changed

+48
-34
lines changed

src/regexp.c

Lines changed: 48 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -269,60 +269,74 @@ yp_regexp_parse_expression(yp_regexp_parser_t *parser);
269269
typedef enum {
270270
YP_REGEXP_OPTION_STATE_INVALID,
271271
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
273275
} yp_regexp_option_state_t;
274276

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+
278283
// 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;
280285

281286
// Initialize a new set of options to their default values.
282287
static void
283288
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;
291296
}
292297

293298
// Attempt to add the given option to the set of options. Returns true if it was
294299
// added, false if it was already present.
295300
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+
}
307316
}
317+
308318
return false;
309319
}
310320

311321
// Attempt to remove the given option from the set of options. Returns true if
312322
// it was removed, false if it was already absent.
313323
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+
}
325338
}
339+
326340
return false;
327341
}
328342

0 commit comments

Comments
 (0)