diff options
| author | Yukihiro Matsumoto <[email protected]> | 2012-06-02 21:17:14 +0900 |
|---|---|---|
| committer | Yukihiro Matsumoto <[email protected]> | 2012-06-02 21:17:14 +0900 |
| commit | c0c712d33b180aa0be5808577af5f6ee9016e4d7 (patch) | |
| tree | 3db035652bf9caeee7a04f4cbce07e5ffa41fdf9 /src/array.c | |
| parent | 687ec8e65ce42bef9734e10fc4b245e7007b84f4 (diff) | |
| download | mruby-c0c712d33b180aa0be5808577af5f6ee9016e4d7.tar.gz mruby-c0c712d33b180aa0be5808577af5f6ee9016e4d7.zip | |
small refactoring around Array#concat
Diffstat (limited to 'src/array.c')
| -rw-r--r-- | src/array.c | 25 |
1 files changed, 16 insertions, 9 deletions
diff --git a/src/array.c b/src/array.c index 0a45f9ce3..1ca6eaf1c 100644 --- a/src/array.c +++ b/src/array.c @@ -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); } +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 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); - size_t len = a1->len + a2->len; - if (a1->capa < len) mrb_ary_expand_capa(mrb, a1, len); - memcpy(a1->buf+a1->len, a2->buf, sizeof(mrb_value)*a2->len); - mrb_write_barrier(mrb, (struct RBasic*)a1); - a1->len = len; + ary_concat(mrb, mrb_ary_ptr(self), a2->buf, a2->len); } mrb_value 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_ary_concat(mrb, self, other); + mrb_get_args(mrb, "a", &buf, &blen); + ary_concat(mrb, mrb_ary_ptr(self), buf, blen); return self; } |
