Skip to content

Commit fd17902

Browse files
committed
FEAT: BinCode - match binary!
1 parent 75e81a3 commit fd17902

File tree

2 files changed

+26
-1
lines changed

2 files changed

+26
-1
lines changed

src/core/u-bincode.c

+14-1
Original file line numberDiff line numberDiff line change
@@ -208,7 +208,7 @@ static REBCNT EncodedU32_Size(u32 value) {
208208
// /write "Write data into output buffer"
209209
// data [binary! block!] "Data dialect"
210210
// /read "Read data from the input buffer"
211-
// code [word! block! integer!] "Input encoding"
211+
// code [word! block! integer! binary!] "Input encoding"
212212
// /into "Put READ results in out block, instead of creating a new block"
213213
// out [block!] "Target block for results, when /into is used"
214214
// /with "Additional input argument"
@@ -1589,6 +1589,19 @@ static REBCNT EncodedU32_Size(u32 value) {
15891589
//puts("-- found set-word?");
15901590
DS_PUSH(value);
15911591
continue;
1592+
case REB_BINARY:
1593+
// do binary comparison, return TRUE and skip if data matches
1594+
// or return FALSE and keep position where it is
1595+
VAL_SET(temp, REB_LOGIC);
1596+
n = Val_Series_Len(value);
1597+
if ((cp + n) > ep || NZ(memcmp(cp, VAL_BIN_DATA(value), n))) {
1598+
VAL_LOGIC(temp) = FALSE;
1599+
n = 0; // no advance
1600+
}
1601+
else {
1602+
VAL_LOGIC(temp) = TRUE;
1603+
}
1604+
break;
15921605
}
15931606
// Set prior set-words:
15941607
while (DSP > ssp) {

src/tests/units/bincode-test.r3

+12
Original file line numberDiff line numberDiff line change
@@ -440,6 +440,18 @@ is-protected-error?: func[code][
440440
;- used for fixed size octal numbers (used for example in TAR files)
441441
--assert 8 = binary/read/with #{3130} 'OCTAL-BYTES 2
442442

443+
--test-- "BinCode - match binary!"
444+
b: binary #{0badCafe}
445+
--assert binary/read b #{0bad}
446+
--assert binary/read b #{Cafe}
447+
--assert tail? b/buffer
448+
--assert [#[true] #[false] #[true]] = binary/read b [
449+
ATz 0 ; reset position to head
450+
#{0bad} ; true and advance
451+
#{F00D} ; false, no advance
452+
#{Cafe} ; true and advance
453+
]
454+
--assert [#[true] #{CAFE}] = binary/read b [ATz 0 #{0bad} bytes 2]
443455

444456
===end-group===
445457

0 commit comments

Comments
 (0)