Ignore:
Timestamp:
Jul 9, 2020, 8:51:43 AM (4 years ago)
Author:
coas-nagasima
Message:

mrubyを2.1.1に更新

Location:
EcnlProtoTool/trunk/mruby-2.1.1
Files:
1 edited
1 moved

Legend:

Unmodified
Added
Removed
  • EcnlProtoTool/trunk/mruby-2.1.1/mrbgems/mruby-array-ext/src/array.c

    r331 r439  
    6666  for (i = 0; i < RARRAY_LEN(ary); ++i) {
    6767    v = RARRAY_PTR(ary)[i];
    68     if (mrb_type(v) == MRB_TT_ARRAY &&
     68    if (mrb_array_p(v) &&
    6969        RARRAY_LEN(v) > 1 &&
    7070        mrb_equal(mrb, RARRAY_PTR(v)[1], value))
     
    107107}
    108108
    109 /*
    110  *  call-seq:
    111  *     ary.to_h   ->   Hash
    112  *
    113  *  Returns the result of interpreting <i>aray</i> as an array of
    114  *  <tt>[key, value]</tt> paris.
    115  *
    116  *      [[:foo, :bar], [1, 2]].to_h
    117  *        # => {:foo => :bar, 1 => 2}
    118  *
    119  */
    120 
    121 static mrb_value
    122 mrb_ary_to_h(mrb_state *mrb, mrb_value ary)
    123 {
    124   mrb_int i;
    125   mrb_value v, hash;
    126 
    127   hash = mrb_hash_new_capa(mrb, 0);
    128 
    129   for (i = 0; i < RARRAY_LEN(ary); ++i) {
    130     v = mrb_check_array_type(mrb, RARRAY_PTR(ary)[i]);
    131 
    132     if (mrb_nil_p(v)) {
    133       mrb_raisef(mrb, E_TYPE_ERROR, "wrong element type %S at %S (expected array)",
    134         mrb_str_new_cstr(mrb,  mrb_obj_classname(mrb, ary_elt(ary, i))),
    135         mrb_fixnum_value(i)
    136       );
    137     }
    138 
    139     if (RARRAY_LEN(v) != 2) {
    140       mrb_raisef(mrb, E_ARGUMENT_ERROR, "wrong array length at %S (expected 2, was %S)",
    141         mrb_fixnum_value(i),
    142         mrb_fixnum_value(RARRAY_LEN(v))
    143       );
    144     }
    145 
    146     mrb_hash_set(mrb, hash, RARRAY_PTR(v)[0], RARRAY_PTR(v)[1]);
    147   }
    148 
    149   return hash;
    150 }
    151109
    152110/*
     
    175133{
    176134  struct RArray *a = mrb_ary_ptr(self);
    177   mrb_int i, j, k, len;
    178   mrb_value index;
     135  mrb_int i, j, k, len, alen;
    179136  mrb_value val;
    180137  mrb_value *ptr;
     
    183140  mrb_ary_modify(mrb, a);
    184141
    185   if (mrb_get_args(mrb, "o|i", &index, &len) == 1) {
     142  if (mrb_get_argc(mrb) == 1) {
     143    mrb_value index;
     144
     145    mrb_get_args(mrb, "o|i", &index, &len);
    186146    switch (mrb_type(index)) {
    187147    case MRB_TT_RANGE:
    188       if (mrb_range_beg_len(mrb, index, &i, &len, a->len, TRUE) == 1) {
     148      if (mrb_range_beg_len(mrb, index, &i, &len, ARY_LEN(a), TRUE) == MRB_RANGE_OK) {
    189149        goto delete_pos_len;
    190150      }
     
    201161  }
    202162
    203   i = mrb_fixnum(index);
     163  mrb_get_args(mrb, "ii", &i, &len);
    204164 delete_pos_len:
    205   if (i < 0) i += a->len;
    206   if (i < 0 || a->len < i) return mrb_nil_value();
     165  alen = ARY_LEN(a);
     166  if (i < 0) i += alen;
     167  if (i < 0 || alen < i) return mrb_nil_value();
    207168  if (len < 0) return mrb_nil_value();
    208   if (a->len == i) return mrb_ary_new(mrb);
    209   if (len > a->len - i) len = a->len - i;
     169  if (alen == i) return mrb_ary_new(mrb);
     170  if (len > alen - i) len = alen - i;
    210171
    211172  ary = mrb_ary_new_capa(mrb, len);
    212 
     173  ptr = ARY_PTR(a);
    213174  for (j = i, k = 0; k < len; ++j, ++k) {
    214     mrb_ary_push(mrb, ary, a->ptr[j]);
    215   }
    216 
    217   ptr = a->ptr + i;
    218   for (j = i; j < a->len - len; ++j) {
     175    mrb_ary_push(mrb, ary, ptr[j]);
     176  }
     177
     178  ptr += i;
     179  for (j = i; j < alen - len; ++j) {
    219180    *ptr = *(ptr+len);
    220181    ++ptr;
    221182  }
    222183
    223   mrb_ary_resize(mrb, self, a->len - len);
     184  mrb_ary_resize(mrb, self, alen - len);
    224185  return ary;
    225186}
     
    234195  mrb_define_method(mrb, a, "rassoc", mrb_ary_rassoc, MRB_ARGS_REQ(1));
    235196  mrb_define_method(mrb, a, "values_at", mrb_ary_values_at, MRB_ARGS_ANY());
    236   mrb_define_method(mrb, a, "to_h",   mrb_ary_to_h, MRB_ARGS_REQ(0));
    237   mrb_define_method(mrb, a, "slice!", mrb_ary_slice_bang,   MRB_ARGS_ANY());
     197  mrb_define_method(mrb, a, "slice!", mrb_ary_slice_bang,   MRB_ARGS_ARG(1,1));
    238198}
    239199
Note: See TracChangeset for help on using the changeset viewer.