Skip to content

Commit 469ba79

Browse files
committed
FEAT: new Bincode dialect CROP command for shrinking buffers from the left side at the read buffer index position
1 parent 826714e commit 469ba79

File tree

2 files changed

+38
-1
lines changed

2 files changed

+38
-1
lines changed

src/core/u-bincode.c

+9-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
** REBOL [R3] Language Interpreter and Run-time Environment
44
**
55
** Copyright 2012 REBOL Technologies
6-
** Copyright 2012-2018 Rebol Open Source Developers
6+
** Copyright 2012-2022 Rebol Open Source Developers
77
** REBOL is a trademark of REBOL Technologies
88
**
99
** Licensed under the Apache License, Version 2.0 (the "License");
@@ -1639,6 +1639,14 @@ static REBCNT EncodedU32_Size(u32 value) {
16391639
+ mst->time.minute * 60
16401640
+ mst->time.hour * 3600);
16411641
break;
1642+
case SYM_CROP:
1643+
n = 0;
1644+
if (IS_PROTECT_SERIES(bin)) Trap0(RE_PROTECTED);
1645+
Remove_Series(VAL_SERIES(buffer_read), 0, VAL_INDEX(buffer_read));
1646+
cp = VAL_BIN_HEAD(buffer_read);
1647+
VAL_INDEX(buffer_write) = MAX(0, (REBI64)VAL_INDEX(buffer_write) - VAL_INDEX(buffer_read));
1648+
VAL_INDEX(buffer_read) = 0;
1649+
continue;
16421650
default:
16431651
Trap1(RE_INVALID_SPEC, value);
16441652
}

src/tests/units/bincode-test.r3

+29
Original file line numberDiff line numberDiff line change
@@ -484,6 +484,35 @@ is-protected-error?: func[code][
484484

485485
===end-group===
486486

487+
===start-group=== "BinCode special read/write functions"
487488

489+
--test-- "BinCode - CROP"
490+
;; CROP shrinks buffers from the left side at the position of the read buffer
491+
b: binary #{010203}
492+
--assert [1] = binary/read b [UI8 CROP]
493+
--assert all [
494+
#{0203} = b/buffer
495+
#{0203} = b/buffer-write
496+
]
497+
--assert b = binary/write b [UI8 4]
498+
--assert all [
499+
#{0403} = b/buffer
500+
#{03} = b/buffer-write
501+
]
502+
--assert [4] = binary/read b [UI8 CROP]
503+
--assert all [
504+
#{03} = b/buffer
505+
#{03} = b/buffer-write
506+
]
507+
--assert b = binary/write b [AT 2 UI8 5]
508+
--assert all [
509+
#{0305} = b/buffer
510+
#{} = b/buffer-write
511+
]
512+
--assert [3 5] = binary/read b [UI8 UI8 CROP]
513+
--assert all [
514+
empty? head b/buffer
515+
empty? head b/buffer-write
516+
]
488517

489518
~~~end-file~~~

0 commit comments

Comments
 (0)