static mrb_value mrb_ary_aget(mrb_state *mrb, mrb_value self) { struct RArray *a = mrb_ary_ptr(self); mrb_int i, len; mrb_value index; if (mrb_get_args(mrb, "o|i", &index, &len) == 1) { switch (mrb_type(index)) { /* a[n..m] */ case MRB_TT_RANGE: if (mrb_range_beg_len(mrb, index, &i, &len, a->len)) { return ary_subseq(mrb, a, i, len); } else { return mrb_nil_value(); } case MRB_TT_FIXNUM: return mrb_ary_ref(mrb, self, mrb_fixnum(index)); default: return mrb_ary_ref(mrb, self, aget_index(mrb, index)); } } i = aget_index(mrb, index); if (i < 0) i += a->len; if (i < 0 || a->len < i) return mrb_nil_value(); if (len < 0) return mrb_nil_value(); if (a->len == i) return mrb_ary_new(mrb); if (len > a->len - i) len = a->len - i; return ary_subseq(mrb, a, i, len); }
static VALUE pair_after_open_tokens(VALUE pair) { int before_len, after_len; VALUE around_open = pair_get_around_open_tokens(pair); before_len = pair_get_before_open_len(pair); after_len = pair_get_after_open_len(pair); return ary_subseq(around_open, before_len+1, after_len); return ary_subseq(around_open, 0, before_len); }
static VALUE pair_after_close_tokens(VALUE pair) { int before_len, after_len; VALUE around_close = pair_get_around_close_tokens(pair); if (around_close == Qnil) return Qnil; before_len = pair_get_before_close_len(pair); after_len = pair_get_after_close_len(pair); return ary_subseq(around_close, before_len+1, after_len); return ary_subseq(around_close, 0, before_len); }
mrb_value mrb_ary_last(mrb_state *mrb, mrb_value self) { struct RArray *a = mrb_ary_ptr(self); int size; mrb_value *vals; int len; mrb_get_args(mrb, "*", &vals, &len); if (len > 1) { mrb_raise(mrb, E_ARGUMENT_ERROR, "wrong number of arguments"); } if (len == 0) return (a->len > 0)? a->ptr[a->len - 1]: mrb_nil_value(); /* len == 1 */ size = mrb_fixnum(*vals); if (size < 0) { mrb_raise(mrb, E_ARGUMENT_ERROR, "negative array size"); } if (size > a->len) size = a->len; if ((a->flags & MRB_ARY_SHARED) || size > ARY_DEFAULT_LEN) { return ary_subseq(mrb, a, a->len - size, size); } return mrb_ary_new_from_values(mrb, size, a->ptr + a->len - size); }
mrb_value mrb_ary_aget(mrb_state *mrb, mrb_value self) { struct RArray *a = mrb_ary_ptr(self); mrb_int index, len; mrb_value *argv; int size; mrb_get_args(mrb, "i*", &index, &argv, &size); switch(size) { case 0: return mrb_ary_ref(mrb, self, index); case 1: if (mrb_type(argv[0]) != MRB_TT_FIXNUM) { mrb_raise(mrb, E_TYPE_ERROR, "expected Fixnum"); } if (index < 0) index += a->len; if (index < 0 || a->len < (int)index) return mrb_nil_value(); len = mrb_fixnum(argv[0]); if (len < 0) return mrb_nil_value(); if (a->len == (int)index) return mrb_ary_new(mrb); if ((int)len > a->len - index) len = a->len - index; return ary_subseq(mrb, a, index, len); default: mrb_raise(mrb, E_ARGUMENT_ERROR, "wrong number of arguments"); } return mrb_nil_value(); /* dummy to avoid warning : not reach here */ }
mrb_value mrb_ary_first(mrb_state *mrb, mrb_value self) { struct RArray *a = mrb_ary_ptr(self); int size; if (mrb_get_args(mrb, "|i", &size) == 0) { return (a->len > 0)? a->ptr[0]: mrb_nil_value(); } if (size > a->len) size = a->len; if (a->flags & MRB_ARY_SHARED) { return ary_subseq(mrb, a, 0, size); } return mrb_ary_new_from_values(mrb, size, a->ptr); }
static mrb_value mrb_ary_last(mrb_state *mrb, mrb_value self) { struct RArray *a = mrb_ary_ptr(self); mrb_int size; if (mrb_get_args(mrb, "|i", &size) == 0) return (a->len > 0)? a->ptr[a->len - 1]: mrb_nil_value(); if (size < 0) { mrb_raise(mrb, E_ARGUMENT_ERROR, "negative array size"); } if (size > a->len) size = a->len; if (ARY_SHARED_P(a) || size > ARY_DEFAULT_LEN) { return ary_subseq(mrb, a, a->len - size, size); } return mrb_ary_new_from_values(mrb, size, a->ptr + a->len - size); }
static mrb_value mrb_ary_first(mrb_state *mrb, mrb_value self) { struct RArray *a = mrb_ary_ptr(self); mrb_int size, alen = ARY_LEN(a); if (mrb_get_args(mrb, "|i", &size) == 0) { return (alen > 0)? ARY_PTR(a)[0]: mrb_nil_value(); } if (size < 0) { mrb_raise(mrb, E_ARGUMENT_ERROR, "negative array size"); } if (size > alen) size = alen; if (ARY_SHARED_P(a)) { return ary_subseq(mrb, a, 0, size); } return mrb_ary_new_from_values(mrb, size, ARY_PTR(a)); }
static mrb_value mrb_ary_last(mrb_state *mrb, mrb_value self) { struct RArray *a = mrb_ary_ptr(self); mrb_int n, size, alen; n = mrb_get_args(mrb, "|i", &size); alen = ARY_LEN(a); if (n == 0) { return (alen > 0) ? ARY_PTR(a)[alen - 1]: mrb_nil_value(); } if (size < 0) { mrb_raise(mrb, E_ARGUMENT_ERROR, "negative array size"); } if (size > alen) size = alen; if (ARY_SHARED_P(a) || size > ARY_DEFAULT_LEN) { return ary_subseq(mrb, a, alen - size, size); } return mrb_ary_new_from_values(mrb, size, ARY_PTR(a) + alen - size); }