diff options
| -rw-r--r-- | include/mruby/array.h | 15 | ||||
| -rw-r--r-- | mrbgems/mruby-array-ext/src/array.c | 9 | ||||
| -rw-r--r-- | src/array.c | 30 |
3 files changed, 17 insertions, 37 deletions
diff --git a/include/mruby/array.h b/include/mruby/array.h index 16f78f773..2221cab8b 100644 --- a/include/mruby/array.h +++ b/include/mruby/array.h @@ -177,20 +177,6 @@ MRB_API void mrb_ary_push(mrb_state *mrb, mrb_value array, mrb_value value); MRB_API mrb_value mrb_ary_pop(mrb_state *mrb, mrb_value ary); /* - * Returns a reference to an element of the array on the given index. - * - * Equivalent to: - * - * ary[n] - * - * @param mrb The mruby state reference. - * @param ary The target array. - * @param n The array index being referenced - * @return The referenced value. - */ -MRB_API mrb_value mrb_ary_ref(mrb_state *mrb, mrb_value ary, mrb_int n); - -/* * Sets a value on an array at the given index * * Equivalent to: @@ -243,6 +229,7 @@ MRB_API mrb_value mrb_ary_unshift(mrb_state *mrb, mrb_value self, mrb_value item * @param offset The element position (negative counts from the tail). */ MRB_API mrb_value mrb_ary_entry(mrb_value ary, mrb_int offset); +#define mrb_ary_ref(mrb, ary, n) mrb_ary_entry(ary, n) /* * Replace subsequence of an array. diff --git a/mrbgems/mruby-array-ext/src/array.c b/mrbgems/mruby-array-ext/src/array.c index d6ec50672..d97778642 100644 --- a/mrbgems/mruby-array-ext/src/array.c +++ b/mrbgems/mruby-array-ext/src/array.c @@ -95,6 +95,12 @@ mrb_ary_at(mrb_state *mrb, mrb_value ary) } static mrb_value +ary_ref(mrb_state *mrb, mrb_value ary, mrb_int n) +{ + return mrb_ary_entry(ary, n); +} + +static mrb_value mrb_ary_values_at(mrb_state *mrb, mrb_value self) { mrb_int argc; @@ -102,10 +108,9 @@ mrb_ary_values_at(mrb_state *mrb, mrb_value self) mrb_get_args(mrb, "*", &argv, &argc); - return mrb_get_values_at(mrb, self, RARRAY_LEN(self), argc, argv, mrb_ary_ref); + return mrb_get_values_at(mrb, self, RARRAY_LEN(self), argc, argv, ary_ref); } - /* * call-seq: * ary.slice!(index) -> obj or nil diff --git a/src/array.c b/src/array.c index a49c2399c..6a3034962 100644 --- a/src/array.c +++ b/src/array.c @@ -694,19 +694,6 @@ mrb_ary_unshift_m(mrb_state *mrb, mrb_value self) return self; } -MRB_API mrb_value -mrb_ary_ref(mrb_state *mrb, mrb_value ary, mrb_int n) -{ - struct RArray *a = mrb_ary_ptr(ary); - mrb_int len = ARY_LEN(a); - - /* range check */ - if (n < 0) n += len; - if (n < 0 || len <= n) return mrb_nil_value(); - - return ARY_PTR(a)[n]; -} - MRB_API void mrb_ary_set(mrb_state *mrb, mrb_value ary, mrb_int n, mrb_value val) { @@ -1191,15 +1178,16 @@ mrb_ary_empty_p(mrb_state *mrb, mrb_value self) } MRB_API mrb_value -mrb_ary_entry(mrb_value ary, mrb_int offset) +mrb_ary_entry(mrb_value ary, mrb_int n) { - if (offset < 0) { - offset += RARRAY_LEN(ary); - } - if (offset < 0 || RARRAY_LEN(ary) <= offset) { - return mrb_nil_value(); - } - return RARRAY_PTR(ary)[offset]; + struct RArray *a = mrb_ary_ptr(ary); + mrb_int len = ARY_LEN(a); + + /* range check */ + if (n < 0) n += len; + if (n < 0 || len <= n) return mrb_nil_value(); + + return ARY_PTR(a)[n]; } static mrb_value |
