1
0
Fork 0
mirror of https://github.com/ruby/ruby.git synced 2022-11-09 12:17:21 -05:00

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
This commit is contained in:
nobu 2014-02-04 05:07:21 +00:00
parent bebc52a4a7
commit 4b146b2533
5 changed files with 55 additions and 48 deletions

View file

@ -1,3 +1,10 @@
Tue Feb 4 14:07:20 2014 Nobuyoshi Nakada <nobu@ruby-lang.org>
* 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@ruby-lang.org>
* string.c (rb_str_modify_expand): enable capacity and disable

View file

@ -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);

49
pack.c
View file

@ -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__");
}

View file

@ -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)
{

View file

@ -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