Skip to content

Commit

Permalink
FEAT: when value of string type is used to be written, it is converte…
Browse files Browse the repository at this point in the history
…d to UTF-8 automatically

FEAT: added UI16LEBYTES command
  • Loading branch information
Oldes committed May 27, 2019
1 parent 4a4418b commit 4e3eefa
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 1 deletion.
21 changes: 20 additions & 1 deletion src/core/u-bincode.c
Original file line number Diff line number Diff line change
Expand Up @@ -420,6 +420,9 @@ static REBCNT EncodedU32_Size(u32 value) {
Do_Path(&next, NULL);
next = DS_POP; // volatile stack reference
}
if (IS_STRING(next)) {
Set_Binary(next, Encode_UTF8_Value(next, VAL_LEN(next), 0));
}

switch (VAL_WORD_CANON(data)) {
case SYM_SI8:
Expand Down Expand Up @@ -475,7 +478,6 @@ static REBCNT EncodedU32_Size(u32 value) {
}
goto error;
case SYM_BYTES:
if (IS_GET_WORD(next)) next = Get_Var(next);
if (IS_BINARY(next)) {
count += VAL_LEN(next);
continue;
Expand All @@ -489,6 +491,7 @@ static REBCNT EncodedU32_Size(u32 value) {
else goto error;
break;
case SYM_UI16BYTES:
case SYM_UI16LEBYTES:
if (IS_BINARY(next)) {
ASSERT_UIBYTES_RANGE(next, 0xFFFF);
count += (2 + VAL_LEN(next));
Expand Down Expand Up @@ -640,6 +643,9 @@ static REBCNT EncodedU32_Size(u32 value) {
Do_Path(&next, NULL);
next = DS_POP; // volatile stack reference
}
if (IS_STRING(next)) {
Set_Binary(next, Encode_UTF8_Value(next, VAL_LEN(next), 0));
}

switch (VAL_WORD_CANON(data)) {
case SYM_UI8:
Expand Down Expand Up @@ -776,6 +782,19 @@ static REBCNT EncodedU32_Size(u32 value) {
memcpy(cp, VAL_BIN_AT(next), n);
VAL_INDEX(buffer_write)+=2; //for the length byte;
break;
case SYM_UI16LEBYTES:
n = VAL_LEN(next);
bp = (REBYTE*)&n;
#ifdef ENDIAN_LITTLE
memcpy(cp, bp, 2);
#else
cp[0] = bp[1]; cp[1] = bp[0];
#endif
cp+=2;
memcpy(cp, VAL_BIN_AT(next), n);
VAL_INDEX(buffer_write)+=2; //for the length byte;
break;

case SYM_UI24BYTES:
n = VAL_LEN(next);
bp = (REBYTE*)&n;
Expand Down
3 changes: 3 additions & 0 deletions src/tests/units/bincode-test.r3
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,9 @@ is-protected-error?: func[code][
--test-- "BinCode - UI16BYTES"
--assert object? binary/write b [UI16BYTES #{cafe}]
--assert #{0002CAFE} = binary/read b 'bytes
--test-- "BinCode - UI16LEBYTES"
--assert object? binary/write b [UI16LEBYTES #{cafe}]
--assert #{0200CAFE} = binary/read b 'bytes
--test-- "BinCode - UI24BYTES"
--assert object? binary/write b [UI24BYTES #{cafe}]
--assert #{000002CAFE} = binary/read b 'bytes
Expand Down

0 comments on commit 4e3eefa

Please sign in to comment.