Skip to content

Commit 3a30769

Browse files
committed
FEAT: Bincode - added padding command PAD
1 parent fa46cb4 commit 3a30769

File tree

2 files changed

+32
-0
lines changed

2 files changed

+32
-0
lines changed

src/core/u-bincode.c

+27
Original file line numberDiff line numberDiff line change
@@ -506,6 +506,13 @@ static REBCNT EncodedU32_Size(u32 value) {
506506
continue;
507507
}
508508
goto error;
509+
case SYM_PAD:
510+
if (IS_INTEGER(next)) {
511+
i = count % VAL_INT32(next);
512+
count += (i > 0) ? VAL_INT32(next) - i : 0;
513+
continue;
514+
}
515+
goto error;
509516
case SYM_BYTES:
510517
if (IS_BINARY(next)) {
511518
count += VAL_LEN(next);
@@ -902,6 +909,14 @@ static REBCNT EncodedU32_Size(u32 value) {
902909
n = 0;
903910
break;
904911

912+
case SYM_PAD:
913+
n = VAL_INDEX(buffer_write) % VAL_INT32(next);
914+
if (n > 0) {
915+
n = VAL_INT32(next) - n;
916+
memset(cp, 0, n);
917+
}
918+
break;
919+
905920
case SYM_ENCODEDU32:
906921
ASSERT_U32_RANGE(next);
907922
ulong = VAL_UNT32(next);
@@ -1476,6 +1491,18 @@ static REBCNT EncodedU32_Size(u32 value) {
14761491
VAL_INDEX(buffer_read) = i; //TODO: range test
14771492
cp = BIN_DATA(bin) + VAL_INDEX(buffer_read);
14781493
continue;
1494+
case SYM_PAD:
1495+
next = ++value;
1496+
if (IS_GET_WORD(next)) next = Get_Var(next);
1497+
if (!IS_INTEGER(next)) Trap1(RE_INVALID_SPEC, value);
1498+
i = VAL_INDEX(buffer_read) % VAL_INT32(next);
1499+
if (i > 0) {
1500+
i = VAL_INT32(next) - i;
1501+
ASSERT_INDEX_RANGE(buffer_read, i, value);
1502+
VAL_INDEX(buffer_read) += i;
1503+
cp = BIN_DATA(bin) + VAL_INDEX(buffer_read);
1504+
}
1505+
continue;
14791506
case SYM_SKIPBITS:
14801507
next = ++value;
14811508
if (IS_GET_WORD(next)) next = Get_Var(next);

src/tests/units/bincode-test.r3

+5
Original file line numberDiff line numberDiff line change
@@ -230,6 +230,11 @@ is-protected-error?: func[code][
230230
--assert is-range-error? [binary/read b [AT 1 SKIP 5 UI8]]
231231
--assert is-range-error? [binary/read b [AT 1 SKIP -1 UI8]]
232232

233+
--test-- "BinCode - PAD"
234+
binary/write b: #{} [UI8 255 PAD 4 UI8 255]
235+
--assert b = #{FF000000FF}
236+
--assert [255 255] = binary/read b [UI8 PAD 4 UI8]
237+
233238
--test-- "BinCode - LENGTH?"
234239
;LENGTH? returns number of bytes remaining in the buffer
235240
b: binary #{01020304}

0 commit comments

Comments
 (0)