Skip to content

Commit

Permalink
POSIX.xs: Use macro to reduce complexity
Browse files Browse the repository at this point in the history
This #defines a macro and uses it to populate a structure, so that
strings don't have to be typed twice.
  • Loading branch information
khwilliamson committed May 9, 2021
1 parent eb5c02e commit 2e8d032
Showing 1 changed file with 30 additions and 24 deletions.
54 changes: 30 additions & 24 deletions ext/POSIX/POSIX.xs
Expand Up @@ -1578,26 +1578,32 @@ struct lconv_offset {
size_t offset;
};

/* Create e.g.,
{"thousands_sep", STRUCT_OFFSET(struct lconv, thousands_sep)},
*/
#define LCONV_ENTRY(name) \
{STRINGIFY(name), STRUCT_OFFSET(struct lconv, name)}

static const struct lconv_offset lconv_strings[] = {
#ifdef USE_LOCALE_NUMERIC
{"decimal_point", STRUCT_OFFSET(struct lconv, decimal_point)},
{"thousands_sep", STRUCT_OFFSET(struct lconv, thousands_sep)},
LCONV_ENTRY(decimal_point),
LCONV_ENTRY(thousands_sep),
# ifndef NO_LOCALECONV_GROUPING
{"grouping", STRUCT_OFFSET(struct lconv, grouping)},
LCONV_ENTRY(grouping),
# endif
#endif
#ifdef USE_LOCALE_MONETARY
{"int_curr_symbol", STRUCT_OFFSET(struct lconv, int_curr_symbol)},
{"currency_symbol", STRUCT_OFFSET(struct lconv, currency_symbol)},
{"mon_decimal_point", STRUCT_OFFSET(struct lconv, mon_decimal_point)},
LCONV_ENTRY(int_curr_symbol),
LCONV_ENTRY(currency_symbol),
LCONV_ENTRY(mon_decimal_point),
# ifndef NO_LOCALECONV_MON_THOUSANDS_SEP
{"mon_thousands_sep", STRUCT_OFFSET(struct lconv, mon_thousands_sep)},
LCONV_ENTRY(mon_thousands_sep),
# endif
# ifndef NO_LOCALECONV_MON_GROUPING
{"mon_grouping", STRUCT_OFFSET(struct lconv, mon_grouping)},
LCONV_ENTRY(mon_grouping),
# endif
{"positive_sign", STRUCT_OFFSET(struct lconv, positive_sign)},
{"negative_sign", STRUCT_OFFSET(struct lconv, negative_sign)},
LCONV_ENTRY(positive_sign),
LCONV_ENTRY(negative_sign),
#endif
{NULL, 0}
};
Expand All @@ -1619,21 +1625,21 @@ static const struct lconv_offset lconv_strings[] = {

static const struct lconv_offset lconv_integers[] = {
#ifdef USE_LOCALE_MONETARY
{"int_frac_digits", STRUCT_OFFSET(struct lconv, int_frac_digits)},
{"frac_digits", STRUCT_OFFSET(struct lconv, frac_digits)},
{"p_cs_precedes", STRUCT_OFFSET(struct lconv, p_cs_precedes)},
{"p_sep_by_space", STRUCT_OFFSET(struct lconv, p_sep_by_space)},
{"n_cs_precedes", STRUCT_OFFSET(struct lconv, n_cs_precedes)},
{"n_sep_by_space", STRUCT_OFFSET(struct lconv, n_sep_by_space)},
{"p_sign_posn", STRUCT_OFFSET(struct lconv, p_sign_posn)},
{"n_sign_posn", STRUCT_OFFSET(struct lconv, n_sign_posn)},
LCONV_ENTRY(int_frac_digits),
LCONV_ENTRY(frac_digits),
LCONV_ENTRY(p_cs_precedes),
LCONV_ENTRY(p_sep_by_space),
LCONV_ENTRY(n_cs_precedes),
LCONV_ENTRY(n_sep_by_space),
LCONV_ENTRY(p_sign_posn),
LCONV_ENTRY(n_sign_posn),
#ifdef HAS_LC_MONETARY_2008
{"int_p_cs_precedes", STRUCT_OFFSET(struct lconv, int_p_cs_precedes)},
{"int_p_sep_by_space", STRUCT_OFFSET(struct lconv, int_p_sep_by_space)},
{"int_n_cs_precedes", STRUCT_OFFSET(struct lconv, int_n_cs_precedes)},
{"int_n_sep_by_space", STRUCT_OFFSET(struct lconv, int_n_sep_by_space)},
{"int_p_sign_posn", STRUCT_OFFSET(struct lconv, int_p_sign_posn)},
{"int_n_sign_posn", STRUCT_OFFSET(struct lconv, int_n_sign_posn)},
LCONV_ENTRY(int_p_cs_precedes),
LCONV_ENTRY(int_p_sep_by_space),
LCONV_ENTRY(int_n_cs_precedes),
LCONV_ENTRY(int_n_sep_by_space),
LCONV_ENTRY(int_p_sign_posn),
LCONV_ENTRY(int_n_sign_posn),
#endif
#endif
{NULL, 0}
Expand Down

0 comments on commit 2e8d032

Please sign in to comment.