ruby-changes:32725
From: nobu <ko1@a...>
Date: Tue, 4 Feb 2014 14:07:26 +0900 (JST)
Subject: [ruby-changes:32725] nobu:r44804 (trunk): pack.c: use ivar for associated objects
nobu 2014-02-04 14:07:21 +0900 (Tue, 04 Feb 2014) New Revision: 44804 http://svn.ruby-lang.org/cgi-bin/viewvc.cgi?view=rev&revision=44804 Log: pack.c: use ivar for associated objects * pack.c (str_associate, str_associated): keep associated objects in an instance variables, instead of in the internal structure. * string.c (rb_str_associate, rb_str_associated): deprecate. Modified files: trunk/ChangeLog trunk/include/ruby/intern.h trunk/pack.c trunk/string.c trunk/test/ruby/test_pack.rb Index: include/ruby/intern.h =================================================================== --- include/ruby/intern.h (revision 44803) +++ include/ruby/intern.h (revision 44804) @@ -769,8 +769,8 @@ VALUE rb_str_replace(VALUE, VALUE); https://github.com/ruby/ruby/blob/trunk/include/ruby/intern.h#L769 VALUE rb_str_inspect(VALUE); VALUE rb_str_dump(VALUE); VALUE rb_str_split(VALUE, const char*); -void rb_str_associate(VALUE, VALUE); -VALUE rb_str_associated(VALUE); +DEPRECATED(void rb_str_associate(VALUE, VALUE)); +DEPRECATED(VALUE rb_str_associated(VALUE)); void rb_str_setter(VALUE, ID, VALUE*); VALUE rb_str_intern(VALUE); VALUE rb_sym_to_s(VALUE); Index: ChangeLog =================================================================== --- ChangeLog (revision 44803) +++ ChangeLog (revision 44804) @@ -1,3 +1,10 @@ https://github.com/ruby/ruby/blob/trunk/ChangeLog#L1 +Tue Feb 4 14:07:20 2014 Nobuyoshi Nakada <nobu@r...> + + * pack.c (str_associate, str_associated): keep associated objects + in an instance variables, instead of in the internal structure. + + * string.c (rb_str_associate, rb_str_associated): deprecate. + Tue Feb 4 12:55:31 2014 Nobuyoshi Nakada <nobu@r...> * string.c (rb_str_modify_expand): enable capacity and disable Index: string.c =================================================================== --- string.c (revision 44803) +++ string.c (revision 44804) @@ -1536,47 +1536,6 @@ str_discard(VALUE str) https://github.com/ruby/ruby/blob/trunk/string.c#L1536 } void -rb_str_associate(VALUE str, VALUE add) -{ - /* sanity check */ - rb_check_frozen(str); - if (STR_ASSOC_P(str)) { - /* already associated */ - rb_ary_concat(RSTRING(str)->as.heap.aux.shared, add); - } - else { - if (STR_SHARED_P(str)) { - VALUE assoc = RSTRING(str)->as.heap.aux.shared; - str_make_independent(str); - if (STR_ASSOC_P(assoc)) { - assoc = RSTRING(assoc)->as.heap.aux.shared; - rb_ary_concat(assoc, add); - add = assoc; - } - } - else if (STR_EMBED_P(str)) { - str_make_independent(str); - } - else if (RSTRING(str)->as.heap.aux.capa != RSTRING_LEN(str)) { - RESIZE_CAPA(str, RSTRING_LEN(str)); - } - FL_SET(str, STR_ASSOC); - RBASIC_CLEAR_CLASS(add); - RB_OBJ_WRITE(str, &RSTRING(str)->as.heap.aux.shared, add); - } -} - -VALUE -rb_str_associated(VALUE str) -{ - if (STR_SHARED_P(str)) str = RSTRING(str)->as.heap.aux.shared; - if (STR_ASSOC_P(str)) { - return RSTRING(str)->as.heap.aux.shared; - } - return Qfalse; -} - -void rb_must_asciicompat(VALUE str) { rb_encoding *enc = rb_enc_get(str); Index: pack.c =================================================================== --- pack.c (revision 44803) +++ pack.c (revision 44804) @@ -234,6 +234,45 @@ static void qpencode(VALUE,VALUE,long); https://github.com/ruby/ruby/blob/trunk/pack.c#L234 static unsigned long utf8_to_uv(const char*,long*); +static ID id_associated; + +static void +str_associate(VALUE str, VALUE add) +{ + VALUE assoc; + + assoc = rb_attr_get(str, id_associated); + if (RB_TYPE_P(assoc, T_ARRAY)) { + /* already associated */ + rb_ary_concat(assoc, add); + } + else { + rb_ivar_set(str, id_associated, add); + } +} + +static VALUE +str_associated(VALUE str) +{ + VALUE assoc = rb_attr_get(str, id_associated); + if (NIL_P(assoc)) assoc = Qfalse; + return assoc; +} + +void +rb_str_associate(VALUE str, VALUE add) +{ + rb_warn("rb_str_associate() is only for internal use and deprecated; do not use"); + str_associate(str, add); +} + +VALUE +rb_str_associated(VALUE str) +{ + rb_warn("rb_str_associated() is only for internal use and deprecated; do not use"); + return str_associated(str); +} + /* * call-seq: * arr.pack ( aTemplateString ) -> aBinaryString @@ -921,7 +960,7 @@ pack_pack(VALUE ary, VALUE fmt) https://github.com/ruby/ruby/blob/trunk/pack.c#L960 } if (associates) { - rb_str_associate(res, associates); + str_associate(res, associates); } OBJ_INFECT(res, fmt); switch (enc_info) { @@ -1801,7 +1840,7 @@ pack_unpack(VALUE str, VALUE fmt) https://github.com/ruby/ruby/blob/trunk/pack.c#L1840 VALUE a; const VALUE *p, *pend; - if (!(a = rb_str_associated(str))) { + if (!(a = str_associated(str))) { rb_raise(rb_eArgError, "no associated pointer"); } p = RARRAY_CONST_PTR(a); @@ -1810,7 +1849,7 @@ pack_unpack(VALUE str, VALUE fmt) https://github.com/ruby/ruby/blob/trunk/pack.c#L1849 if (RB_TYPE_P(*p, T_STRING) && RSTRING_PTR(*p) == t) { if (len < RSTRING_LEN(*p)) { tmp = rb_tainted_str_new(t, len); - rb_str_associate(tmp, a); + str_associate(tmp, a); } else { tmp = *p; @@ -1844,7 +1883,7 @@ pack_unpack(VALUE str, VALUE fmt) https://github.com/ruby/ruby/blob/trunk/pack.c#L1883 VALUE a; const VALUE *p, *pend; - if (!(a = rb_str_associated(str))) { + if (!(a = str_associated(str))) { rb_raise(rb_eArgError, "no associated pointer"); } p = RARRAY_CONST_PTR(a); @@ -2006,4 +2045,6 @@ Init_pack(void) https://github.com/ruby/ruby/blob/trunk/pack.c#L2045 { rb_define_method(rb_cArray, "pack", pack_pack, 1); rb_define_method(rb_cString, "unpack", pack_unpack, 1); + + id_associated = rb_intern_const("__pack_associated__"); } Index: test/ruby/test_pack.rb =================================================================== --- test/ruby/test_pack.rb (revision 44803) +++ test/ruby/test_pack.rb (revision 44804) @@ -181,7 +181,7 @@ class TestPack < Test::Unit::TestCase https://github.com/ruby/ruby/blob/trunk/test/ruby/test_pack.rb#L181 assert_equal a[0], a.pack("p").unpack("p")[0] assert_equal a, a.pack("p").freeze.unpack("p*") assert_raise(ArgumentError) { (a.pack("p") + "").unpack("p*") } - assert_raise(ArgumentError) { (a.pack("p") << "d").unpack("p*") } + assert_equal a, (a.pack("p") << "d").unpack("p*") end def test_format_string_modified -- ML: ruby-changes@q... Info: http://www.atdot.net/~ko1/quickml/