Merge branch 'master' of github.com:mruby/mruby

This commit is contained in:
Yukihiro "Matz" Matsumoto
2014-07-09 07:17:40 +09:00
2 changed files with 23 additions and 4 deletions
+1
View File
@@ -54,6 +54,7 @@ mrb_value mrb_ary_entry(mrb_value ary, mrb_int offset);
mrb_value mrb_ary_shift(mrb_state *mrb, mrb_value self);
mrb_value mrb_ary_clear(mrb_state *mrb, mrb_value self);
mrb_value mrb_ary_join(mrb_state *mrb, mrb_value ary, mrb_value sep);
mrb_value mrb_ary_resize(mrb_state *mrb, mrb_value ary, mrb_int len);
static inline mrb_int
mrb_ary_len(mrb_state *mrb, mrb_value ary)
+22 -4
View File
@@ -200,10 +200,6 @@ ary_expand_capa(mrb_state *mrb, struct RArray *a, mrb_int len)
if (capa > a->aux.capa) {
mrb_value *expanded_ptr = (mrb_value *)mrb_realloc(mrb, a->ptr, sizeof(mrb_value)*capa);
if (!expanded_ptr) {
mrb_raise(mrb, E_RUNTIME_ERROR, "out of memory");
}
a->aux.capa = capa;
a->ptr = expanded_ptr;
}
@@ -231,6 +227,28 @@ ary_shrink_capa(mrb_state *mrb, struct RArray *a)
}
}
mrb_value
mrb_ary_resize(mrb_state *mrb, mrb_value ary, mrb_int new_len)
{
mrb_int old_len;
struct RArray *a = mrb_ary_ptr(ary);
ary_modify(mrb, a);
old_len = RARRAY_LEN(ary);
if (old_len != new_len) {
a->len = new_len;
if (new_len < old_len) {
ary_shrink_capa(mrb, a);
}
else {
ary_expand_capa(mrb, a, new_len);
ary_fill_with_nil(a->ptr + old_len, new_len - old_len);
}
}
return ary;
}
static mrb_value
mrb_ary_s_create(mrb_state *mrb, mrb_value self)
{