diff options
author | Kevin Newton <[email protected]> | 2023-06-26 09:51:24 -0400 |
---|---|---|
committer | git <[email protected]> | 2023-06-26 14:03:33 +0000 |
commit | ec59b95cfd8f9ff59353f20ab9edbf4786e858fb (patch) | |
tree | d0a52fb04c59d69035fa7ba9fb1c394d4c80fc32 /yarp/regexp.c | |
parent | 515bd4214497b3af02f6eef51b496ad9a0cf6b3b (diff) |
[ruby/yarp] Use smaller regexp options
https://github.com/ruby/yarp/commit/4deb7c3ae0
Diffstat (limited to 'yarp/regexp.c')
-rw-r--r-- | yarp/regexp.c | 82 |
1 files changed, 48 insertions, 34 deletions
diff --git a/yarp/regexp.c b/yarp/regexp.c index 144bd34755..1d4d1ac1af 100644 --- a/yarp/regexp.c +++ b/yarp/regexp.c @@ -269,60 +269,74 @@ yp_regexp_parse_expression(yp_regexp_parser_t *parser); typedef enum { YP_REGEXP_OPTION_STATE_INVALID, YP_REGEXP_OPTION_STATE_TOGGLEABLE, - YP_REGEXP_OPTION_STATE_ADDABLE + YP_REGEXP_OPTION_STATE_ADDABLE, + YP_REGEXP_OPTION_STATE_ADDED, + YP_REGEXP_OPTION_STATE_REMOVED } yp_regexp_option_state_t; -enum { - option_slots = 128 -}; +// These are the options that are configurable on the regular expression (or +// from within a group). +#define YP_REGEXP_OPTION_STATE_SLOT_MINIMUM 'a' +#define YP_REGEXP_OPTION_STATE_SLOT_MAXIMUM 'x' +#define YP_REGEXP_OPTION_STATE_SLOTS (YP_REGEXP_OPTION_STATE_SLOT_MAXIMUM - YP_REGEXP_OPTION_STATE_SLOT_MINIMUM + 1) + // This is the set of options that are configurable on the regular expression. -typedef yp_regexp_option_state_t yp_regexp_options_t[option_slots]; +typedef struct { unsigned char values[YP_REGEXP_OPTION_STATE_SLOTS]; } yp_regexp_options_t; // Initialize a new set of options to their default values. static void yp_regexp_options_init(yp_regexp_options_t *options) { - memset(options, YP_REGEXP_OPTION_STATE_INVALID, sizeof(yp_regexp_option_state_t) * option_slots); - (*options)['i'] = YP_REGEXP_OPTION_STATE_TOGGLEABLE; - (*options)['m'] = YP_REGEXP_OPTION_STATE_TOGGLEABLE; - (*options)['x'] = YP_REGEXP_OPTION_STATE_TOGGLEABLE; - (*options)['d'] = YP_REGEXP_OPTION_STATE_ADDABLE; - (*options)['a'] = YP_REGEXP_OPTION_STATE_ADDABLE; - (*options)['u'] = YP_REGEXP_OPTION_STATE_ADDABLE; + memset(options, YP_REGEXP_OPTION_STATE_INVALID, sizeof(uint8_t) * YP_REGEXP_OPTION_STATE_SLOTS); + options->values['i' - YP_REGEXP_OPTION_STATE_SLOT_MINIMUM] = YP_REGEXP_OPTION_STATE_TOGGLEABLE; + options->values['m' - YP_REGEXP_OPTION_STATE_SLOT_MINIMUM] = YP_REGEXP_OPTION_STATE_TOGGLEABLE; + options->values['x' - YP_REGEXP_OPTION_STATE_SLOT_MINIMUM] = YP_REGEXP_OPTION_STATE_TOGGLEABLE; + options->values['d' - YP_REGEXP_OPTION_STATE_SLOT_MINIMUM] = YP_REGEXP_OPTION_STATE_ADDABLE; + options->values['a' - YP_REGEXP_OPTION_STATE_SLOT_MINIMUM] = YP_REGEXP_OPTION_STATE_ADDABLE; + options->values['u' - YP_REGEXP_OPTION_STATE_SLOT_MINIMUM] = YP_REGEXP_OPTION_STATE_ADDABLE; } // Attempt to add the given option to the set of options. Returns true if it was // added, false if it was already present. static bool -yp_regexp_options_add(yp_regexp_options_t *options, unsigned char option) { - if (option >= option_slots) { - return false; - } - switch ((*options)[option]) { - case YP_REGEXP_OPTION_STATE_INVALID: - return false; - case YP_REGEXP_OPTION_STATE_TOGGLEABLE: - case YP_REGEXP_OPTION_STATE_ADDABLE: - (*options)[option] = YP_REGEXP_OPTION_STATE_INVALID; - return true; +yp_regexp_options_add(yp_regexp_options_t *options, unsigned char key) { + if (key >= YP_REGEXP_OPTION_STATE_SLOT_MINIMUM && key <= YP_REGEXP_OPTION_STATE_SLOT_MAXIMUM) { + key -= YP_REGEXP_OPTION_STATE_SLOT_MINIMUM; + + switch (options->values[key]) { + case YP_REGEXP_OPTION_STATE_INVALID: + case YP_REGEXP_OPTION_STATE_REMOVED: + return false; + case YP_REGEXP_OPTION_STATE_TOGGLEABLE: + case YP_REGEXP_OPTION_STATE_ADDABLE: + options->values[key] = YP_REGEXP_OPTION_STATE_ADDED; + return true; + case YP_REGEXP_OPTION_STATE_ADDED: + return true; + } } + return false; } // Attempt to remove the given option from the set of options. Returns true if // it was removed, false if it was already absent. static bool -yp_regexp_options_remove(yp_regexp_options_t *options, unsigned char option) { - if (option >= option_slots) { - return false; - } - switch ((*options)[option]) { - case YP_REGEXP_OPTION_STATE_INVALID: - case YP_REGEXP_OPTION_STATE_ADDABLE: - return false; - case YP_REGEXP_OPTION_STATE_TOGGLEABLE: - (*options)[option] = YP_REGEXP_OPTION_STATE_INVALID; - return true; +yp_regexp_options_remove(yp_regexp_options_t *options, unsigned char key) { + if (key >= YP_REGEXP_OPTION_STATE_SLOT_MINIMUM && key <= YP_REGEXP_OPTION_STATE_SLOT_MAXIMUM) { + key -= YP_REGEXP_OPTION_STATE_SLOT_MINIMUM; + + switch (options->values[key]) { + case YP_REGEXP_OPTION_STATE_INVALID: + case YP_REGEXP_OPTION_STATE_ADDABLE: + return false; + case YP_REGEXP_OPTION_STATE_TOGGLEABLE: + case YP_REGEXP_OPTION_STATE_ADDED: + case YP_REGEXP_OPTION_STATE_REMOVED: + options->values[key] = YP_REGEXP_OPTION_STATE_REMOVED; + return true; + } } + return false; } |