Project

General

Profile

Bug #10568 ยป ruby-2.1-rb-str-associated.patch

bkutil (Balazs Kutil), 12/17/2014 04:26 PM

View differences:

pack.c
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;
}
/*
* call-seq:
* arr.pack ( aTemplateString ) -> aBinaryString
......
}
if (associates) {
rb_str_associate(res, associates);
str_associate(res, associates);
}
OBJ_INFECT(res, fmt);
switch (enc_info) {
......
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);
......
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;
......
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);
......
{
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__");
}
test/ruby/test_pack.rb
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
    (1-1/1)