Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor associated pointer #5014

Merged
merged 1 commit into from
Oct 24, 2021
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
68 changes: 25 additions & 43 deletions pack.c
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,23 @@ str_associate(VALUE str, VALUE add)
static VALUE
str_associated(VALUE str)
{
return rb_ivar_lookup(str, id_associated, Qfalse);
VALUE associates = rb_ivar_lookup(str, id_associated, Qfalse);
if (!associates)
rb_raise(rb_eArgError, "no associated pointer");
return associates;
}

static VALUE
associated_pointer(VALUE associates, const char *t)
{
const VALUE *p = RARRAY_CONST_PTR(associates);
const VALUE *pend = p + RARRAY_LEN(associates);
for (; p < pend; p++) {
VALUE tmp = *p;
if (RB_TYPE_P(tmp, T_STRING) && RSTRING_PTR(tmp) == t) return tmp;
}
rb_raise(rb_eArgError, "non associated pointer");
UNREACHABLE_RETURN(Qnil);
}

static void
Expand Down Expand Up @@ -933,7 +949,7 @@ pack_unpack_internal(VALUE str, VALUE fmt, int mode)
#define hexdigits ruby_hexdigits
char *s, *send;
char *p, *pend;
VALUE ary;
VALUE ary, associates = Qfalse;
char type;
long len;
AVOID_CC_BUG long tmp_len;
Expand Down Expand Up @@ -1540,29 +1556,11 @@ pack_unpack_internal(VALUE str, VALUE fmt, int mode)

UNPACK_FETCH(&t, char *);
if (t) {
VALUE a;
const VALUE *p, *pend;

if (!(a = str_associated(str))) {
rb_raise(rb_eArgError, "no associated pointer");
}
p = RARRAY_CONST_PTR(a);
pend = p + RARRAY_LEN(a);
while (p < pend) {
if (RB_TYPE_P(*p, T_STRING) && RSTRING_PTR(*p) == t) {
if (len < RSTRING_LEN(*p)) {
tmp = rb_str_new(t, len);
str_associate(tmp, a);
}
else {
tmp = *p;
}
break;
}
p++;
}
if (p == pend) {
rb_raise(rb_eArgError, "non associated pointer");
if (!associates) associates = str_associated(str);
tmp = associated_pointer(associates, t);
if (len < RSTRING_LEN(tmp)) {
tmp = rb_str_new(t, len);
str_associate(tmp, associates);
}
}
UNPACK_PUSH(tmp);
Expand All @@ -1581,24 +1579,8 @@ pack_unpack_internal(VALUE str, VALUE fmt, int mode)

UNPACK_FETCH(&t, char *);
if (t) {
VALUE a;
const VALUE *p, *pend;

if (!(a = str_associated(str))) {
rb_raise(rb_eArgError, "no associated pointer");
}
p = RARRAY_CONST_PTR(a);
pend = p + RARRAY_LEN(a);
while (p < pend) {
if (RB_TYPE_P(*p, T_STRING) && RSTRING_PTR(*p) == t) {
tmp = *p;
break;
}
p++;
}
if (p == pend) {
rb_raise(rb_eArgError, "non associated pointer");
}
if (!associates) associates = str_associated(str);
tmp = associated_pointer(associates, t);
}
UNPACK_PUSH(tmp);
}
Expand Down