pack.c: support `@' directive (absolute position).

This commit is contained in:
Yukihiro "Matz" Matsumoto
2021-06-12 16:14:01 +09:00
parent 952e7efdd7
commit 96ad60b9aa
+19 -1
View File
@@ -40,6 +40,7 @@ enum pack_dir {
PACK_DIR_QENC, /* M */
PACK_DIR_NUL, /* x */
PACK_DIR_BACK, /* X */
PACK_DIR_ABS, /* @ */
PACK_DIR_INVALID
};
@@ -1218,13 +1219,17 @@ alias:
dir = PACK_DIR_BACK;
type = PACK_TYPE_NONE;
break;
case '@':
dir = PACK_DIR_ABS;
type = PACK_TYPE_NONE;
break;
case 'Z':
dir = PACK_DIR_STR;
type = PACK_TYPE_STRING;
flags |= PACK_FLAG_WIDTH | PACK_FLAG_COUNT2 | PACK_FLAG_Z;
break;
case 'p': case 'P':
case '%': case '@':
case '%':
mrb_raisef(mrb, E_ARGUMENT_ERROR, "%c is not supported", (char)t);
break;
default:
@@ -1309,6 +1314,14 @@ mrb_pack_pack(mrb_state *mrb, mrb_value ary)
ridx -= count;
continue;
}
else if (dir == PACK_DIR_ABS) {
count -= ridx;
if (count > 0) goto grow;
count = -count;
check_x(mrb, ridx, count, '@');
ridx -= count;
continue;
}
if ((flags & PACK_FLAG_WIDTH) && aidx >= RARRAY_LEN(ary)) {
mrb_raise(mrb, E_ARGUMENT_ERROR, "too few arguments");
@@ -1427,6 +1440,11 @@ pack_unpack(mrb_state *mrb, mrb_value str, int single)
srcidx -= count;
continue;
}
else if (dir == PACK_DIR_ABS) {
check_x(mrb, srclen, count, '@');
srcidx = count;
continue;
}
if (flags & PACK_FLAG_COUNT2) {
sptr = (const unsigned char *)RSTRING_PTR(str) + srcidx;