Skip to content

Commit

Permalink
Extracted repeatedly defined IDs
Browse files Browse the repository at this point in the history
  • Loading branch information
nobu committed Jul 27, 2021
1 parent 1b33190 commit 19a1f46
Show file tree
Hide file tree
Showing 2 changed files with 87 additions and 43 deletions.
80 changes: 53 additions & 27 deletions enum.c
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,19 @@
VALUE rb_mEnumerable;

static ID id_next;
static ID id__alone;
static ID id__separator;
static ID id_chunk_categorize;
static ID id_chunk_enumerable;
static ID id_sliceafter_enum;
static ID id_sliceafter_pat;
static ID id_sliceafter_pred;
static ID id_slicebefore_enumerable;
static ID id_slicebefore_sep_pat;
static ID id_slicebefore_sep_pred;
static ID id_slicewhen_enum;
static ID id_slicewhen_inverted;
static ID id_slicewhen_pred;

#define id_div idDiv
#define id_each idEach
Expand Down Expand Up @@ -3188,8 +3201,8 @@ chunk_ii(RB_BLOCK_CALL_FUNC_ARGLIST(i, _argp))
{
struct chunk_arg *argp = MEMO_FOR(struct chunk_arg, _argp);
VALUE v, s;
VALUE alone = ID2SYM(rb_intern("_alone"));
VALUE separator = ID2SYM(rb_intern("_separator"));
VALUE alone = ID2SYM(id__alone);
VALUE separator = ID2SYM(id__separator);

ENUM_WANT_SVALUE();

Expand Down Expand Up @@ -3241,8 +3254,8 @@ chunk_i(RB_BLOCK_CALL_FUNC_ARGLIST(yielder, enumerator))
VALUE arg;
struct chunk_arg *memo = NEW_MEMO_FOR(struct chunk_arg, arg);

enumerable = rb_ivar_get(enumerator, rb_intern("chunk_enumerable"));
memo->categorize = rb_ivar_get(enumerator, rb_intern("chunk_categorize"));
enumerable = rb_ivar_get(enumerator, id_chunk_enumerable);
memo->categorize = rb_ivar_get(enumerator, id_chunk_categorize);
memo->prev_value = Qnil;
memo->prev_elts = Qnil;
memo->yielder = yielder;
Expand Down Expand Up @@ -3352,8 +3365,8 @@ enum_chunk(VALUE enumerable)
RETURN_SIZED_ENUMERATOR(enumerable, 0, 0, enum_size);

enumerator = rb_obj_alloc(rb_cEnumerator);
rb_ivar_set(enumerator, rb_intern("chunk_enumerable"), enumerable);
rb_ivar_set(enumerator, rb_intern("chunk_categorize"), rb_block_proc());
rb_ivar_set(enumerator, id_chunk_enumerable, enumerable);
rb_ivar_set(enumerator, id_chunk_categorize, rb_block_proc());
rb_block_call(enumerator, idInitialize, 0, 0, chunk_i, enumerator);
return enumerator;
}
Expand Down Expand Up @@ -3400,9 +3413,9 @@ slicebefore_i(RB_BLOCK_CALL_FUNC_ARGLIST(yielder, enumerator))
VALUE arg;
struct slicebefore_arg *memo = NEW_MEMO_FOR(struct slicebefore_arg, arg);

enumerable = rb_ivar_get(enumerator, rb_intern("slicebefore_enumerable"));
memo->sep_pred = rb_attr_get(enumerator, rb_intern("slicebefore_sep_pred"));
memo->sep_pat = NIL_P(memo->sep_pred) ? rb_ivar_get(enumerator, rb_intern("slicebefore_sep_pat")) : Qnil;
enumerable = rb_ivar_get(enumerator, id_slicebefore_enumerable);
memo->sep_pred = rb_attr_get(enumerator, id_slicebefore_sep_pred);
memo->sep_pat = NIL_P(memo->sep_pred) ? rb_ivar_get(enumerator, id_slicebefore_sep_pat) : Qnil;
memo->prev_elts = Qnil;
memo->yielder = yielder;

Expand Down Expand Up @@ -3566,15 +3579,15 @@ enum_slice_before(int argc, VALUE *argv, VALUE enumerable)
if (argc != 0)
rb_error_arity(argc, 0, 0);
enumerator = rb_obj_alloc(rb_cEnumerator);
rb_ivar_set(enumerator, rb_intern("slicebefore_sep_pred"), rb_block_proc());
rb_ivar_set(enumerator, id_slicebefore_sep_pred, rb_block_proc());
}
else {
VALUE sep_pat;
rb_scan_args(argc, argv, "1", &sep_pat);
enumerator = rb_obj_alloc(rb_cEnumerator);
rb_ivar_set(enumerator, rb_intern("slicebefore_sep_pat"), sep_pat);
rb_ivar_set(enumerator, id_slicebefore_sep_pat, sep_pat);
}
rb_ivar_set(enumerator, rb_intern("slicebefore_enumerable"), enumerable);
rb_ivar_set(enumerator, id_slicebefore_enumerable, enumerable);
rb_block_call(enumerator, idInitialize, 0, 0, slicebefore_i, enumerator);
return enumerator;
}
Expand Down Expand Up @@ -3630,9 +3643,9 @@ sliceafter_i(RB_BLOCK_CALL_FUNC_ARGLIST(yielder, enumerator))
VALUE arg;
struct sliceafter_arg *memo = NEW_MEMO_FOR(struct sliceafter_arg, arg);

enumerable = rb_ivar_get(enumerator, rb_intern("sliceafter_enum"));
memo->pat = rb_ivar_get(enumerator, rb_intern("sliceafter_pat"));
memo->pred = rb_attr_get(enumerator, rb_intern("sliceafter_pred"));
enumerable = rb_ivar_get(enumerator, id_sliceafter_enum);
memo->pat = rb_ivar_get(enumerator, id_sliceafter_pat);
memo->pred = rb_attr_get(enumerator, id_sliceafter_pred);
memo->prev_elts = Qnil;
memo->yielder = yielder;

Expand Down Expand Up @@ -3695,9 +3708,9 @@ enum_slice_after(int argc, VALUE *argv, VALUE enumerable)
}

enumerator = rb_obj_alloc(rb_cEnumerator);
rb_ivar_set(enumerator, rb_intern("sliceafter_enum"), enumerable);
rb_ivar_set(enumerator, rb_intern("sliceafter_pat"), pat);
rb_ivar_set(enumerator, rb_intern("sliceafter_pred"), pred);
rb_ivar_set(enumerator, id_sliceafter_enum, enumerable);
rb_ivar_set(enumerator, id_sliceafter_pat, pat);
rb_ivar_set(enumerator, id_sliceafter_pred, pred);

rb_block_call(enumerator, idInitialize, 0, 0, sliceafter_i, enumerator);
return enumerator;
Expand Down Expand Up @@ -3760,12 +3773,12 @@ slicewhen_i(RB_BLOCK_CALL_FUNC_ARGLIST(yielder, enumerator))
struct slicewhen_arg *memo =
NEW_PARTIAL_MEMO_FOR(struct slicewhen_arg, arg, inverted);

enumerable = rb_ivar_get(enumerator, rb_intern("slicewhen_enum"));
memo->pred = rb_attr_get(enumerator, rb_intern("slicewhen_pred"));
enumerable = rb_ivar_get(enumerator, id_slicewhen_enum);
memo->pred = rb_attr_get(enumerator, id_slicewhen_pred);
memo->prev_elt = Qundef;
memo->prev_elts = Qnil;
memo->yielder = yielder;
memo->inverted = RTEST(rb_attr_get(enumerator, rb_intern("slicewhen_inverted")));
memo->inverted = RTEST(rb_attr_get(enumerator, id_slicewhen_inverted));

rb_block_call(enumerable, id_each, 0, 0, slicewhen_ii, arg);
memo = MEMO_FOR(struct slicewhen_arg, arg);
Expand Down Expand Up @@ -3845,9 +3858,9 @@ enum_slice_when(VALUE enumerable)
pred = rb_block_proc();

enumerator = rb_obj_alloc(rb_cEnumerator);
rb_ivar_set(enumerator, rb_intern("slicewhen_enum"), enumerable);
rb_ivar_set(enumerator, rb_intern("slicewhen_pred"), pred);
rb_ivar_set(enumerator, rb_intern("slicewhen_inverted"), Qfalse);
rb_ivar_set(enumerator, id_slicewhen_enum, enumerable);
rb_ivar_set(enumerator, id_slicewhen_pred, pred);
rb_ivar_set(enumerator, id_slicewhen_inverted, Qfalse);

rb_block_call(enumerator, idInitialize, 0, 0, slicewhen_i, enumerator);
return enumerator;
Expand Down Expand Up @@ -3911,9 +3924,9 @@ enum_chunk_while(VALUE enumerable)
pred = rb_block_proc();

enumerator = rb_obj_alloc(rb_cEnumerator);
rb_ivar_set(enumerator, rb_intern("slicewhen_enum"), enumerable);
rb_ivar_set(enumerator, rb_intern("slicewhen_pred"), pred);
rb_ivar_set(enumerator, rb_intern("slicewhen_inverted"), Qtrue);
rb_ivar_set(enumerator, id_slicewhen_enum, enumerable);
rb_ivar_set(enumerator, id_slicewhen_pred, pred);
rb_ivar_set(enumerator, id_slicewhen_inverted, Qtrue);

rb_block_call(enumerator, idInitialize, 0, 0, slicewhen_i, enumerator);
return enumerator;
Expand Down Expand Up @@ -4469,5 +4482,18 @@ Init_Enumerable(void)
rb_define_method(rb_mEnumerable, "uniq", enum_uniq, 0);
rb_define_method(rb_mEnumerable, "compact", enum_compact, 0);

id__alone = rb_intern_const("_alone");
id__separator = rb_intern_const("_separator");
id_chunk_categorize = rb_intern_const("chunk_categorize");
id_chunk_enumerable = rb_intern_const("chunk_enumerable");
id_next = rb_intern_const("next");
id_sliceafter_enum = rb_intern_const("sliceafter_enum");
id_sliceafter_pat = rb_intern_const("sliceafter_pat");
id_sliceafter_pred = rb_intern_const("sliceafter_pred");
id_slicebefore_enumerable = rb_intern_const("slicebefore_enumerable");
id_slicebefore_sep_pat = rb_intern_const("slicebefore_sep_pat");
id_slicebefore_sep_pred = rb_intern_const("slicebefore_sep_pred");
id_slicewhen_enum = rb_intern_const("slicewhen_enum");
id_slicewhen_inverted = rb_intern_const("slicewhen_inverted");
id_slicewhen_pred = rb_intern_const("slicewhen_pred");
}
50 changes: 34 additions & 16 deletions transcode.c
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,15 @@ static VALUE rb_eConverterNotFoundError;

VALUE rb_cEncodingConverter;

static ID id_destination_encoding;
static ID id_destination_encoding_name;
static ID id_error_bytes;
static ID id_error_char;
static ID id_incomplete_input;
static ID id_readagain_bytes;
static ID id_source_encoding;
static ID id_source_encoding_name;

static VALUE sym_invalid, sym_undef, sym_replace, sym_fallback;
static VALUE sym_xml, sym_text, sym_attr;
static VALUE sym_universal_newline;
Expand Down Expand Up @@ -2068,9 +2077,9 @@ make_econv_exception(rb_econv_t *ec)
}

exc = rb_exc_new3(rb_eInvalidByteSequenceError, mesg);
rb_ivar_set(exc, rb_intern("error_bytes"), bytes);
rb_ivar_set(exc, rb_intern("readagain_bytes"), bytes2);
rb_ivar_set(exc, rb_intern("incomplete_input"), ec->last_error.result == econv_incomplete_input ? Qtrue : Qfalse);
rb_ivar_set(exc, id_error_bytes, bytes);
rb_ivar_set(exc, id_readagain_bytes, bytes2);
rb_ivar_set(exc, id_incomplete_input, ec->last_error.result == econv_incomplete_input ? Qtrue : Qfalse);
goto set_encs;
}
if (ec->last_error.result == econv_undefined_conversion) {
Expand Down Expand Up @@ -2119,20 +2128,20 @@ make_econv_exception(rb_econv_t *ec)
idx = rb_enc_find_index(ec->last_error.source_encoding);
if (0 <= idx)
rb_enc_associate_index(bytes, idx);
rb_ivar_set(exc, rb_intern("error_char"), bytes);
rb_ivar_set(exc, id_error_char, bytes);
goto set_encs;
}
return Qnil;

set_encs:
rb_ivar_set(exc, rb_intern("source_encoding_name"), rb_str_new2(ec->last_error.source_encoding));
rb_ivar_set(exc, rb_intern("destination_encoding_name"), rb_str_new2(ec->last_error.destination_encoding));
rb_ivar_set(exc, id_source_encoding_name, rb_str_new2(ec->last_error.source_encoding));
rb_ivar_set(exc, id_destination_encoding_name, rb_str_new2(ec->last_error.destination_encoding));
int idx = rb_enc_find_index(ec->last_error.source_encoding);
if (0 <= idx)
rb_ivar_set(exc, rb_intern("source_encoding"), rb_enc_from_encoding(rb_enc_from_index(idx)));
rb_ivar_set(exc, id_source_encoding, rb_enc_from_encoding(rb_enc_from_index(idx)));
idx = rb_enc_find_index(ec->last_error.destination_encoding);
if (0 <= idx)
rb_ivar_set(exc, rb_intern("destination_encoding"), rb_enc_from_encoding(rb_enc_from_index(idx)));
rb_ivar_set(exc, id_destination_encoding, rb_enc_from_encoding(rb_enc_from_index(idx)));
return exc;
}

Expand Down Expand Up @@ -4262,7 +4271,7 @@ rb_econv_check_error(rb_econv_t *ec)
static VALUE
ecerr_source_encoding_name(VALUE self)
{
return rb_attr_get(self, rb_intern("source_encoding_name"));
return rb_attr_get(self, id_source_encoding_name);
}

/*
Expand All @@ -4288,7 +4297,7 @@ ecerr_source_encoding_name(VALUE self)
static VALUE
ecerr_source_encoding(VALUE self)
{
return rb_attr_get(self, rb_intern("source_encoding"));
return rb_attr_get(self, id_source_encoding);
}

/*
Expand All @@ -4300,7 +4309,7 @@ ecerr_source_encoding(VALUE self)
static VALUE
ecerr_destination_encoding_name(VALUE self)
{
return rb_attr_get(self, rb_intern("destination_encoding_name"));
return rb_attr_get(self, id_destination_encoding_name);
}

/*
Expand All @@ -4312,7 +4321,7 @@ ecerr_destination_encoding_name(VALUE self)
static VALUE
ecerr_destination_encoding(VALUE self)
{
return rb_attr_get(self, rb_intern("destination_encoding"));
return rb_attr_get(self, id_destination_encoding);
}

/*
Expand All @@ -4333,7 +4342,7 @@ ecerr_destination_encoding(VALUE self)
static VALUE
ecerr_error_char(VALUE self)
{
return rb_attr_get(self, rb_intern("error_char"));
return rb_attr_get(self, id_error_char);
}

/*
Expand All @@ -4354,7 +4363,7 @@ ecerr_error_char(VALUE self)
static VALUE
ecerr_error_bytes(VALUE self)
{
return rb_attr_get(self, rb_intern("error_bytes"));
return rb_attr_get(self, id_error_bytes);
}

/*
Expand All @@ -4366,7 +4375,7 @@ ecerr_error_bytes(VALUE self)
static VALUE
ecerr_readagain_bytes(VALUE self)
{
return rb_attr_get(self, rb_intern("readagain_bytes"));
return rb_attr_get(self, id_readagain_bytes);
}

/*
Expand Down Expand Up @@ -4396,7 +4405,7 @@ ecerr_readagain_bytes(VALUE self)
static VALUE
ecerr_incomplete_input(VALUE self)
{
return rb_attr_get(self, rb_intern("incomplete_input"));
return rb_attr_get(self, id_incomplete_input);
}

/*
Expand Down Expand Up @@ -4426,6 +4435,15 @@ Init_transcode(void)
{
transcoder_table = st_init_strcasetable();

id_destination_encoding = rb_intern_const("destination_encoding");
id_destination_encoding_name = rb_intern_const("destination_encoding_name");
id_error_bytes = rb_intern_const("error_bytes");
id_error_char = rb_intern_const("error_char");
id_incomplete_input = rb_intern_const("incomplete_input");
id_readagain_bytes = rb_intern_const("readagain_bytes");
id_source_encoding = rb_intern_const("source_encoding");
id_source_encoding_name = rb_intern_const("source_encoding_name");

sym_invalid = ID2SYM(rb_intern_const("invalid"));
sym_undef = ID2SYM(rb_intern_const("undef"));
sym_replace = ID2SYM(rb_intern_const("replace"));
Expand Down

0 comments on commit 19a1f46

Please sign in to comment.