Commit c0c712d3 authored by Yukihiro Matsumoto's avatar Yukihiro Matsumoto

small refactoring around Array#concat

parent 687ec8e6
...@@ -163,26 +163,33 @@ mrb_ary_s_create(mrb_state *mrb, mrb_value self) ...@@ -163,26 +163,33 @@ mrb_ary_s_create(mrb_state *mrb, mrb_value self)
return mrb_ary_new_from_values(mrb, (size_t)len, vals); return mrb_ary_new_from_values(mrb, (size_t)len, vals);
} }
static void
ary_concat(mrb_state *mrb, struct RArray *a, mrb_value *buf, size_t blen)
{
size_t len = a->len + blen;
if (a->capa < len) mrb_ary_expand_capa(mrb, a, len);
memcpy(a->buf+a->len, buf, sizeof(mrb_value)*blen);
mrb_write_barrier(mrb, (struct RBasic*)a);
a->len = len;
}
void void
mrb_ary_concat(mrb_state *mrb, mrb_value self, mrb_value other) mrb_ary_concat(mrb_state *mrb, mrb_value self, mrb_value other)
{ {
struct RArray *a1 = mrb_ary_ptr(self);
struct RArray *a2 = mrb_ary_ptr(other); struct RArray *a2 = mrb_ary_ptr(other);
size_t len = a1->len + a2->len;
if (a1->capa < len) mrb_ary_expand_capa(mrb, a1, len); ary_concat(mrb, mrb_ary_ptr(self), a2->buf, a2->len);
memcpy(a1->buf+a1->len, a2->buf, sizeof(mrb_value)*a2->len);
mrb_write_barrier(mrb, (struct RBasic*)a1);
a1->len = len;
} }
mrb_value mrb_value
mrb_ary_concat_m(mrb_state *mrb, mrb_value self) mrb_ary_concat_m(mrb_state *mrb, mrb_value self)
{ {
mrb_value other; mrb_value *buf;
int blen;
mrb_get_args(mrb, "A", &other); mrb_get_args(mrb, "a", &buf, &blen);
mrb_ary_concat(mrb, self, other); ary_concat(mrb, mrb_ary_ptr(self), buf, blen);
return self; return self;
} }
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment