Skip to content

Commit 4e3eefa

Browse files
committed
FEAT: when value of string type is used to be written, it is converted to UTF-8 automatically
FEAT: added UI16LEBYTES command
1 parent 4a4418b commit 4e3eefa

File tree

2 files changed

+23
-1
lines changed

2 files changed

+23
-1
lines changed

src/core/u-bincode.c

+20-1
Original file line numberDiff line numberDiff line change
@@ -420,6 +420,9 @@ static REBCNT EncodedU32_Size(u32 value) {
420420
Do_Path(&next, NULL);
421421
next = DS_POP; // volatile stack reference
422422
}
423+
if (IS_STRING(next)) {
424+
Set_Binary(next, Encode_UTF8_Value(next, VAL_LEN(next), 0));
425+
}
423426

424427
switch (VAL_WORD_CANON(data)) {
425428
case SYM_SI8:
@@ -475,7 +478,6 @@ static REBCNT EncodedU32_Size(u32 value) {
475478
}
476479
goto error;
477480
case SYM_BYTES:
478-
if (IS_GET_WORD(next)) next = Get_Var(next);
479481
if (IS_BINARY(next)) {
480482
count += VAL_LEN(next);
481483
continue;
@@ -489,6 +491,7 @@ static REBCNT EncodedU32_Size(u32 value) {
489491
else goto error;
490492
break;
491493
case SYM_UI16BYTES:
494+
case SYM_UI16LEBYTES:
492495
if (IS_BINARY(next)) {
493496
ASSERT_UIBYTES_RANGE(next, 0xFFFF);
494497
count += (2 + VAL_LEN(next));
@@ -640,6 +643,9 @@ static REBCNT EncodedU32_Size(u32 value) {
640643
Do_Path(&next, NULL);
641644
next = DS_POP; // volatile stack reference
642645
}
646+
if (IS_STRING(next)) {
647+
Set_Binary(next, Encode_UTF8_Value(next, VAL_LEN(next), 0));
648+
}
643649

644650
switch (VAL_WORD_CANON(data)) {
645651
case SYM_UI8:
@@ -776,6 +782,19 @@ static REBCNT EncodedU32_Size(u32 value) {
776782
memcpy(cp, VAL_BIN_AT(next), n);
777783
VAL_INDEX(buffer_write)+=2; //for the length byte;
778784
break;
785+
case SYM_UI16LEBYTES:
786+
n = VAL_LEN(next);
787+
bp = (REBYTE*)&n;
788+
#ifdef ENDIAN_LITTLE
789+
memcpy(cp, bp, 2);
790+
#else
791+
cp[0] = bp[1]; cp[1] = bp[0];
792+
#endif
793+
cp+=2;
794+
memcpy(cp, VAL_BIN_AT(next), n);
795+
VAL_INDEX(buffer_write)+=2; //for the length byte;
796+
break;
797+
779798
case SYM_UI24BYTES:
780799
n = VAL_LEN(next);
781800
bp = (REBYTE*)&n;

src/tests/units/bincode-test.r3

+3
Original file line numberDiff line numberDiff line change
@@ -147,6 +147,9 @@ is-protected-error?: func[code][
147147
--test-- "BinCode - UI16BYTES"
148148
--assert object? binary/write b [UI16BYTES #{cafe}]
149149
--assert #{0002CAFE} = binary/read b 'bytes
150+
--test-- "BinCode - UI16LEBYTES"
151+
--assert object? binary/write b [UI16LEBYTES #{cafe}]
152+
--assert #{0200CAFE} = binary/read b 'bytes
150153
--test-- "BinCode - UI24BYTES"
151154
--assert object? binary/write b [UI24BYTES #{cafe}]
152155
--assert #{000002CAFE} = binary/read b 'bytes

0 commit comments

Comments
 (0)