Changeset 331 for EcnlProtoTool/trunk/mruby-1.3.0/mrbgems/mruby-array-ext
- Timestamp:
- Jan 21, 2018, 12:10:09 AM (6 years ago)
- Location:
- EcnlProtoTool/trunk/mruby-1.3.0
- Files:
-
- 3 edited
- 1 moved
Legend:
- Unmodified
- Added
- Removed
-
EcnlProtoTool/trunk/mruby-1.3.0/mrbgems/mruby-array-ext/mrblib/array.rb
r321 r331 1 1 class Array 2 ## 3 # call-seq: 4 # Array.try_convert(obj) -> array or nil 5 # 6 # Tries to convert +obj+ into an array, using +to_ary+ method. 7 # converted array or +nil+ if +obj+ cannot be converted for any reason. 8 # This method can be used to check if an argument is an array. 9 # 10 # Array.try_convert([1]) #=> [1] 11 # Array.try_convert("1") #=> nil 12 # 13 # if tmp = Array.try_convert(arg) 14 # # the argument is an array 15 # elsif tmp = String.try_convert(arg) 16 # # the argument is a string 17 # end 18 # 19 def self.try_convert(obj) 20 if obj.respond_to?(:to_ary) 21 obj.to_ary 22 else 23 nil 24 end 25 end 26 2 27 ## 3 28 # call-seq: … … 251 276 # 252 277 # Alternatively, if a block is given it will only be executed when an 253 # invalid +index+ is referenced. Negative values of +index+ count from the 254 # end of the array. 278 # invalid +index+ is referenced. 279 # 280 # Negative values of +index+ count from the end of the array. 255 281 # 256 282 # a = [ 11, 22, 33, 44 ] … … 710 736 nil 711 737 end 738 739 ## 740 # call-seq: 741 # ary.to_ary -> ary 742 # 743 # Returns +self+. 744 # 745 def to_ary 746 self 747 end 748 749 ## 750 # call-seq: 751 # ary.dig(idx, ...) -> object 752 # 753 # Extracts the nested value specified by the sequence of <i>idx</i> 754 # objects by calling +dig+ at each step, returning +nil+ if any 755 # intermediate step is +nil+. 756 # 757 def dig(idx,*args) 758 n = self[idx] 759 if args.size > 0 760 n&.dig(*args) 761 else 762 n 763 end 764 end 712 765 end -
EcnlProtoTool/trunk/mruby-1.3.0/mrbgems/mruby-array-ext/src/array.c
r321 r331 1 #include "mruby.h"2 #include "mruby/value.h"3 #include "mruby/array.h"4 #include "mruby/range.h"5 #include "mruby/hash.h"1 #include <mruby.h> 2 #include <mruby/value.h> 3 #include <mruby/array.h> 4 #include <mruby/range.h> 5 #include <mruby/hash.h> 6 6 7 7 /* … … 132 132 if (mrb_nil_p(v)) { 133 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, RARRAY_PTR(ary)[i])),134 mrb_str_new_cstr(mrb, mrb_obj_classname(mrb, ary_elt(ary, i))), 135 135 mrb_fixnum_value(i) 136 136 ); … … 150 150 } 151 151 152 /* 153 * call-seq: 154 * ary.slice!(index) -> obj or nil 155 * ary.slice!(start, length) -> new_ary or nil 156 * ary.slice!(range) -> new_ary or nil 157 * 158 * Deletes the element(s) given by an +index+ (optionally up to +length+ 159 * elements) or by a +range+. 160 * 161 * Returns the deleted object (or objects), or +nil+ if the +index+ is out of 162 * range. 163 * 164 * a = [ "a", "b", "c" ] 165 * a.slice!(1) #=> "b" 166 * a #=> ["a", "c"] 167 * a.slice!(-1) #=> "c" 168 * a #=> ["a"] 169 * a.slice!(100) #=> nil 170 * a #=> ["a"] 171 */ 172 173 static mrb_value 174 mrb_ary_slice_bang(mrb_state *mrb, mrb_value self) 175 { 176 struct RArray *a = mrb_ary_ptr(self); 177 mrb_int i, j, k, len; 178 mrb_value index; 179 mrb_value val; 180 mrb_value *ptr; 181 mrb_value ary; 182 183 mrb_ary_modify(mrb, a); 184 185 if (mrb_get_args(mrb, "o|i", &index, &len) == 1) { 186 switch (mrb_type(index)) { 187 case MRB_TT_RANGE: 188 if (mrb_range_beg_len(mrb, index, &i, &len, a->len, TRUE) == 1) { 189 goto delete_pos_len; 190 } 191 else { 192 return mrb_nil_value(); 193 } 194 case MRB_TT_FIXNUM: 195 val = mrb_funcall(mrb, self, "delete_at", 1, index); 196 return val; 197 default: 198 val = mrb_funcall(mrb, self, "delete_at", 1, index); 199 return val; 200 } 201 } 202 203 i = mrb_fixnum(index); 204 delete_pos_len: 205 if (i < 0) i += a->len; 206 if (i < 0 || a->len < i) return mrb_nil_value(); 207 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; 210 211 ary = mrb_ary_new_capa(mrb, len); 212 213 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) { 219 *ptr = *(ptr+len); 220 ++ptr; 221 } 222 223 mrb_ary_resize(mrb, self, a->len - len); 224 return ary; 225 } 226 152 227 void 153 228 mrb_mruby_array_ext_gem_init(mrb_state* mrb) … … 160 235 mrb_define_method(mrb, a, "values_at", mrb_ary_values_at, MRB_ARGS_ANY()); 161 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()); 162 238 } 163 239 -
EcnlProtoTool/trunk/mruby-1.3.0/mrbgems/mruby-array-ext/test/array.rb
r321 r331 1 1 ## 2 2 # Array(Ext) Test 3 4 assert("Array.try_convert") do 5 assert_nil Array.try_convert(0) 6 assert_nil Array.try_convert(nil) 7 assert_equal [], Array.try_convert([]) 8 assert_equal [1,2,3], Array.try_convert([1,2,3]) 9 end 3 10 4 11 assert("Array#assoc") do … … 295 302 end 296 303 304 assert('Array#to_h (Modified)') do 305 class A 306 def to_ary 307 $a.clear 308 nil 309 end 310 end 311 $a = [A.new] 312 assert_raise(TypeError) { $a.to_h } 313 end 314 297 315 assert("Array#index (block)") do 298 316 assert_nil (1..10).to_a.index { |i| i % 5 == 0 and i % 7 == 0 } 299 317 assert_equal 34, (1..100).to_a.index { |i| i % 5 == 0 and i % 7 == 0 } 300 318 end 319 320 assert("Array#to_ary") do 321 assert_equal [], [].to_ary 322 assert_equal [1,2,3], [1,2,3].to_ary 323 end 324 325 assert("Array#dig") do 326 h = [[[1]], 0] 327 assert_equal(1, h.dig(0, 0, 0)) 328 assert_nil(h.dig(2, 0)) 329 assert_raise(TypeError) {h.dig(:a)} 330 end 331 332 assert("Array#slice!") do 333 a = [1, 2, 3] 334 b = a.slice!(0) 335 c = [1, 2, 3, 4, 5] 336 d = c.slice!(0, 2) 337 e = [1, 2, 3, 4, 5] 338 f = e.slice!(1..3) 339 g = [1, 2, 3] 340 h = g.slice!(-1) 341 i = [1, 2, 3] 342 j = i.slice!(0, -1) 343 344 assert_equal(a, [2, 3]) 345 assert_equal(b, 1) 346 assert_equal(c, [3, 4, 5]) 347 assert_equal(d, [1, 2]) 348 assert_equal(e, [1, 5]) 349 assert_equal(f, [2, 3, 4]) 350 assert_equal(g, [1, 2]) 351 assert_equal(h, 3) 352 assert_equal(i, [1, 2, 3]) 353 assert_equal(j, nil) 354 end
Note:
See TracChangeset
for help on using the changeset viewer.