From d9feed6703e6db86334497e90e9751a974545055 Mon Sep 17 00:00:00 2001 From: "Yukihiro \"Matz\" Matsumoto" Date: Tue, 1 Apr 2014 14:31:39 +0900 Subject: move Array#inspect implementation to mrblib/array.rb --- mrblib/array.rb | 11 +++++++++++ mrblib/hash.rb | 1 + src/array.c | 61 --------------------------------------------------------- 3 files changed, 12 insertions(+), 61 deletions(-) diff --git a/mrblib/array.rb b/mrblib/array.rb index a7f172879..718553e83 100644 --- a/mrblib/array.rb +++ b/mrblib/array.rb @@ -84,6 +84,17 @@ class Array self end + ## + # Private method for Array creation. + # + # ISO 15.2.12.5.31 (x) + def inspect + return "[]" if self.size == 0 + "["+self.map{|x|x.inspect}.join(", ")+"]" + end + # ISO 15.2.12.5.32 (x) + alias to_s inspect + ## # Delete element with index +key+ def delete(key, &block) diff --git a/mrblib/hash.rb b/mrblib/hash.rb index 74d3d0534..d15fa434e 100644 --- a/mrblib/hash.rb +++ b/mrblib/hash.rb @@ -142,6 +142,7 @@ class Hash k.inspect + "=>" + v.inspect }.join(", ")+"}" end + # ISO 15.2.13.4.31 (x) alias to_s inspect # 1.8/1.9 Hash#reject! returns Hash; ISO says nothing. diff --git a/src/array.c b/src/array.c index 5dba7c691..42e36feb3 100644 --- a/src/array.c +++ b/src/array.c @@ -983,65 +983,6 @@ mrb_ary_entry(mrb_value ary, mrb_int offset) return ary_elt(ary, offset); } -static mrb_value -inspect_ary(mrb_state *mrb, mrb_value ary, mrb_value list) -{ - mrb_int i; - mrb_value s, arystr; - char head[] = { '[' }; - char sep[] = { ',', ' ' }; - char tail[] = { ']' }; - - /* check recursive */ - for (i=0; i 0) { - mrb_str_buf_cat(mrb, arystr, sep, sizeof(sep)); - } - if (mrb_array_p(RARRAY_PTR(ary)[i])) { - s = inspect_ary(mrb, RARRAY_PTR(ary)[i], list); - } - else { - s = mrb_inspect(mrb, RARRAY_PTR(ary)[i]); - } - mrb_str_buf_cat(mrb, arystr, RSTRING_PTR(s), RSTRING_LEN(s)); - mrb_gc_arena_restore(mrb, ai); - } - - mrb_str_buf_cat(mrb, arystr, tail, sizeof(tail)); - mrb_ary_pop(mrb, list); - - return arystr; -} - -/* 15.2.12.5.31 (x) */ -/* - * call-seq: - * ary.to_s -> string - * ary.inspect -> string - * - * Creates a string representation of +self+. - */ - -static mrb_value -mrb_ary_inspect(mrb_state *mrb, mrb_value ary) -{ - if (RARRAY_LEN(ary) == 0) return mrb_str_new_lit(mrb, "[]"); - return inspect_ary(mrb, ary, mrb_ary_new(mrb)); -} - static mrb_value join_ary(mrb_state *mrb, mrb_value ary, mrb_value sep, mrb_value list) { @@ -1228,8 +1169,6 @@ mrb_init_array(mrb_state *mrb) mrb_define_method(mrb, a, "slice", mrb_ary_aget, MRB_ARGS_ANY()); /* 15.2.12.5.29 */ mrb_define_method(mrb, a, "unshift", mrb_ary_unshift_m, MRB_ARGS_ANY()); /* 15.2.12.5.30 */ - mrb_define_method(mrb, a, "inspect", mrb_ary_inspect, MRB_ARGS_NONE()); /* 15.2.12.5.31 (x) */ - mrb_define_alias(mrb, a, "to_s", "inspect"); /* 15.2.12.5.32 (x) */ mrb_define_method(mrb, a, "==", mrb_ary_equal, MRB_ARGS_REQ(1)); /* 15.2.12.5.33 (x) */ mrb_define_method(mrb, a, "eql?", mrb_ary_eql, MRB_ARGS_REQ(1)); /* 15.2.12.5.34 (x) */ mrb_define_method(mrb, a, "<=>", mrb_ary_cmp, MRB_ARGS_REQ(1)); /* 15.2.12.5.36 (x) */ -- cgit v1.2.3