From 4b146b25333c52ca4503dfc3c4215b583e8e9963 Mon Sep 17 00:00:00 2001 From: nobu Date: Tue, 4 Feb 2014 05:07:21 +0000 Subject: 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. git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@44804 b2dd03c8-39d4-4d8f-98ff-823fe69b080e --- ChangeLog | 7 +++++++ include/ruby/intern.h | 4 ++-- pack.c | 49 +++++++++++++++++++++++++++++++++++++++++++++---- string.c | 41 ----------------------------------------- test/ruby/test_pack.rb | 2 +- 5 files changed, 55 insertions(+), 48 deletions(-) diff --git a/ChangeLog b/ChangeLog index 9da6568926..01bb586375 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,10 @@ +Tue Feb 4 14:07:20 2014 Nobuyoshi Nakada + + * 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 * string.c (rb_str_modify_expand): enable capacity and disable diff --git a/include/ruby/intern.h b/include/ruby/intern.h index 46af270bb3..710fa26980 100644 --- a/include/ruby/intern.h +++ b/include/ruby/intern.h @@ -769,8 +769,8 @@ VALUE rb_str_replace(VALUE, VALUE); 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); diff --git a/pack.c b/pack.c index 71dd6afcb3..6e515b2140 100644 --- a/pack.c +++ b/pack.c @@ -234,6 +234,45 @@ static void qpencode(VALUE,VALUE,long); 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) } 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) 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) 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) 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) { 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__"); } diff --git a/string.c b/string.c index 983c2a1166..224710869f 100644 --- a/string.c +++ b/string.c @@ -1535,47 +1535,6 @@ str_discard(VALUE str) } } -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) { diff --git a/test/ruby/test_pack.rb b/test/ruby/test_pack.rb index 3f0931bdc0..38c1981a44 100644 --- a/test/ruby/test_pack.rb +++ b/test/ruby/test_pack.rb @@ -181,7 +181,7 @@ class TestPack < Test::Unit::TestCase 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 -- cgit v1.2.3