Refactor associated pointer
This commit is contained in:
parent
8d6e9b6658
commit
b69c38e62e
Notes:
git
2021-10-24 17:51:03 +09:00
68
pack.c
68
pack.c
@ -135,7 +135,23 @@ str_associate(VALUE str, VALUE add)
|
|||||||
static VALUE
|
static VALUE
|
||||||
str_associated(VALUE str)
|
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
|
static void
|
||||||
@ -933,7 +949,7 @@ pack_unpack_internal(VALUE str, VALUE fmt, int mode)
|
|||||||
#define hexdigits ruby_hexdigits
|
#define hexdigits ruby_hexdigits
|
||||||
char *s, *send;
|
char *s, *send;
|
||||||
char *p, *pend;
|
char *p, *pend;
|
||||||
VALUE ary;
|
VALUE ary, associates = Qfalse;
|
||||||
char type;
|
char type;
|
||||||
long len;
|
long len;
|
||||||
AVOID_CC_BUG long tmp_len;
|
AVOID_CC_BUG long tmp_len;
|
||||||
@ -1540,29 +1556,11 @@ pack_unpack_internal(VALUE str, VALUE fmt, int mode)
|
|||||||
|
|
||||||
UNPACK_FETCH(&t, char *);
|
UNPACK_FETCH(&t, char *);
|
||||||
if (t) {
|
if (t) {
|
||||||
VALUE a;
|
if (!associates) associates = str_associated(str);
|
||||||
const VALUE *p, *pend;
|
tmp = associated_pointer(associates, t);
|
||||||
|
if (len < RSTRING_LEN(tmp)) {
|
||||||
if (!(a = str_associated(str))) {
|
tmp = rb_str_new(t, len);
|
||||||
rb_raise(rb_eArgError, "no associated pointer");
|
str_associate(tmp, associates);
|
||||||
}
|
|
||||||
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");
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
UNPACK_PUSH(tmp);
|
UNPACK_PUSH(tmp);
|
||||||
@ -1581,24 +1579,8 @@ pack_unpack_internal(VALUE str, VALUE fmt, int mode)
|
|||||||
|
|
||||||
UNPACK_FETCH(&t, char *);
|
UNPACK_FETCH(&t, char *);
|
||||||
if (t) {
|
if (t) {
|
||||||
VALUE a;
|
if (!associates) associates = str_associated(str);
|
||||||
const VALUE *p, *pend;
|
tmp = associated_pointer(associates, t);
|
||||||
|
|
||||||
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");
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
UNPACK_PUSH(tmp);
|
UNPACK_PUSH(tmp);
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user