Skip to content

Commit 4b146b2

Browse files
committed
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
1 parent bebc52a commit 4b146b2

File tree

5 files changed

+55
-48
lines changed

5 files changed

+55
-48
lines changed

ChangeLog

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,10 @@
1+
Tue Feb 4 14:07:20 2014 Nobuyoshi Nakada <[email protected]>
2+
3+
* pack.c (str_associate, str_associated): keep associated objects
4+
in an instance variables, instead of in the internal structure.
5+
6+
* string.c (rb_str_associate, rb_str_associated): deprecate.
7+
18
Tue Feb 4 12:55:31 2014 Nobuyoshi Nakada <[email protected]>
29

310
* string.c (rb_str_modify_expand): enable capacity and disable

include/ruby/intern.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -769,8 +769,8 @@ VALUE rb_str_replace(VALUE, VALUE);
769769
VALUE rb_str_inspect(VALUE);
770770
VALUE rb_str_dump(VALUE);
771771
VALUE rb_str_split(VALUE, const char*);
772-
void rb_str_associate(VALUE, VALUE);
773-
VALUE rb_str_associated(VALUE);
772+
DEPRECATED(void rb_str_associate(VALUE, VALUE));
773+
DEPRECATED(VALUE rb_str_associated(VALUE));
774774
void rb_str_setter(VALUE, ID, VALUE*);
775775
VALUE rb_str_intern(VALUE);
776776
VALUE rb_sym_to_s(VALUE);

pack.c

Lines changed: 45 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -234,6 +234,45 @@ static void qpencode(VALUE,VALUE,long);
234234

235235
static unsigned long utf8_to_uv(const char*,long*);
236236

237+
static ID id_associated;
238+
239+
static void
240+
str_associate(VALUE str, VALUE add)
241+
{
242+
VALUE assoc;
243+
244+
assoc = rb_attr_get(str, id_associated);
245+
if (RB_TYPE_P(assoc, T_ARRAY)) {
246+
/* already associated */
247+
rb_ary_concat(assoc, add);
248+
}
249+
else {
250+
rb_ivar_set(str, id_associated, add);
251+
}
252+
}
253+
254+
static VALUE
255+
str_associated(VALUE str)
256+
{
257+
VALUE assoc = rb_attr_get(str, id_associated);
258+
if (NIL_P(assoc)) assoc = Qfalse;
259+
return assoc;
260+
}
261+
262+
void
263+
rb_str_associate(VALUE str, VALUE add)
264+
{
265+
rb_warn("rb_str_associate() is only for internal use and deprecated; do not use");
266+
str_associate(str, add);
267+
}
268+
269+
VALUE
270+
rb_str_associated(VALUE str)
271+
{
272+
rb_warn("rb_str_associated() is only for internal use and deprecated; do not use");
273+
return str_associated(str);
274+
}
275+
237276
/*
238277
* call-seq:
239278
* arr.pack ( aTemplateString ) -> aBinaryString
@@ -921,7 +960,7 @@ pack_pack(VALUE ary, VALUE fmt)
921960
}
922961

923962
if (associates) {
924-
rb_str_associate(res, associates);
963+
str_associate(res, associates);
925964
}
926965
OBJ_INFECT(res, fmt);
927966
switch (enc_info) {
@@ -1801,7 +1840,7 @@ pack_unpack(VALUE str, VALUE fmt)
18011840
VALUE a;
18021841
const VALUE *p, *pend;
18031842

1804-
if (!(a = rb_str_associated(str))) {
1843+
if (!(a = str_associated(str))) {
18051844
rb_raise(rb_eArgError, "no associated pointer");
18061845
}
18071846
p = RARRAY_CONST_PTR(a);
@@ -1810,7 +1849,7 @@ pack_unpack(VALUE str, VALUE fmt)
18101849
if (RB_TYPE_P(*p, T_STRING) && RSTRING_PTR(*p) == t) {
18111850
if (len < RSTRING_LEN(*p)) {
18121851
tmp = rb_tainted_str_new(t, len);
1813-
rb_str_associate(tmp, a);
1852+
str_associate(tmp, a);
18141853
}
18151854
else {
18161855
tmp = *p;
@@ -1844,7 +1883,7 @@ pack_unpack(VALUE str, VALUE fmt)
18441883
VALUE a;
18451884
const VALUE *p, *pend;
18461885

1847-
if (!(a = rb_str_associated(str))) {
1886+
if (!(a = str_associated(str))) {
18481887
rb_raise(rb_eArgError, "no associated pointer");
18491888
}
18501889
p = RARRAY_CONST_PTR(a);
@@ -2006,4 +2045,6 @@ Init_pack(void)
20062045
{
20072046
rb_define_method(rb_cArray, "pack", pack_pack, 1);
20082047
rb_define_method(rb_cString, "unpack", pack_unpack, 1);
2048+
2049+
id_associated = rb_intern_const("__pack_associated__");
20092050
}

string.c

Lines changed: 0 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -1535,47 +1535,6 @@ str_discard(VALUE str)
15351535
}
15361536
}
15371537

1538-
void
1539-
rb_str_associate(VALUE str, VALUE add)
1540-
{
1541-
/* sanity check */
1542-
rb_check_frozen(str);
1543-
if (STR_ASSOC_P(str)) {
1544-
/* already associated */
1545-
rb_ary_concat(RSTRING(str)->as.heap.aux.shared, add);
1546-
}
1547-
else {
1548-
if (STR_SHARED_P(str)) {
1549-
VALUE assoc = RSTRING(str)->as.heap.aux.shared;
1550-
str_make_independent(str);
1551-
if (STR_ASSOC_P(assoc)) {
1552-
assoc = RSTRING(assoc)->as.heap.aux.shared;
1553-
rb_ary_concat(assoc, add);
1554-
add = assoc;
1555-
}
1556-
}
1557-
else if (STR_EMBED_P(str)) {
1558-
str_make_independent(str);
1559-
}
1560-
else if (RSTRING(str)->as.heap.aux.capa != RSTRING_LEN(str)) {
1561-
RESIZE_CAPA(str, RSTRING_LEN(str));
1562-
}
1563-
FL_SET(str, STR_ASSOC);
1564-
RBASIC_CLEAR_CLASS(add);
1565-
RB_OBJ_WRITE(str, &RSTRING(str)->as.heap.aux.shared, add);
1566-
}
1567-
}
1568-
1569-
VALUE
1570-
rb_str_associated(VALUE str)
1571-
{
1572-
if (STR_SHARED_P(str)) str = RSTRING(str)->as.heap.aux.shared;
1573-
if (STR_ASSOC_P(str)) {
1574-
return RSTRING(str)->as.heap.aux.shared;
1575-
}
1576-
return Qfalse;
1577-
}
1578-
15791538
void
15801539
rb_must_asciicompat(VALUE str)
15811540
{

test/ruby/test_pack.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -181,7 +181,7 @@ def test_pack_p
181181
assert_equal a[0], a.pack("p").unpack("p")[0]
182182
assert_equal a, a.pack("p").freeze.unpack("p*")
183183
assert_raise(ArgumentError) { (a.pack("p") + "").unpack("p*") }
184-
assert_raise(ArgumentError) { (a.pack("p") << "d").unpack("p*") }
184+
assert_equal a, (a.pack("p") << "d").unpack("p*")
185185
end
186186

187187
def test_format_string_modified

0 commit comments

Comments
 (0)