Skip to content

Commit

Permalink
fix: utf16be encoding
Browse files Browse the repository at this point in the history
  • Loading branch information
jetjinser committed Dec 10, 2024
1 parent b830031 commit 8802a11
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 11 deletions.
12 changes: 6 additions & 6 deletions encoding/decoding_test.mbt
Original file line number Diff line number Diff line change
Expand Up @@ -188,14 +188,14 @@ test "lossy decoding UTF16BE encoded data with UTF8" {
inspect!(
buf.to_bytes(),
content=
#|b"\x00\xd1\x00\x65\xd8\x3c\xdf\xc3\x00\x38\x00\xf3\xd8\x3c\xdf\xca"
#|b"\x8d\xd1\x6b\x65\xd8\x3c\xdf\xc3\x6e\x38\x6c\xf3\xd8\x3c\xdf\xca"
,
)
let chars = @encoding.decode_lossy(UTF8, buf.to_bytes())
inspect!(
chars.iter().collect(),
content=
#|['\x00', '�', 'e', '�', '�', '\x00', '8', '\x00', '�', '�']
#|['', '�', 'e', '�', '�', 'n', '8', 'l', '�', '�']
,
)
}
Expand Down Expand Up @@ -247,13 +247,13 @@ test "lossy decoding UTF16BE encoded data with UTF16LE" {
inspect!(
buf.to_bytes(),
content=
#|b"\x00\xd1\x00\x65\xd8\x3c\xdf\xc3\x00\x38\x00\xf3\xd8\x3c\xdf\xca"
#|b"\x8d\xd1\x6b\x65\xd8\x3c\xdf\xc3\x6e\x38\x6c\xf3\xd8\x3c\xdf\xca"
,
)
let chars = @encoding.decode_lossy(UTF16LE, buf.to_bytes())
inspect!(
chars.iter().collect(),
content="['', '', '㳘', '쏟', '', '', '㳘', '쫟']",
content="['', '', '㳘', '쏟', '', '', '㳘', '쫟']",
)
}

Expand Down Expand Up @@ -331,14 +331,14 @@ test "strictly decoding UTF16BE encoded data with UTF8" {
inspect!(
buf.to_bytes(),
content=
#|b"\x00\xd1\x00\x65\xd8\x3c\xdf\xc3\x00\x38\x00\xf3\xd8\x3c\xdf\xca"
#|b"\x8d\xd1\x6b\x65\xd8\x3c\xdf\xc3\x6e\x38\x6c\xf3\xd8\x3c\xdf\xca"
,
)
let chars = @encoding.decode_strict(UTF8, buf.to_bytes())
inspect!(
chars.iter().collect(),
content=
#|[Ok('\x00'), Err(b"\xd1\x00"), Ok('e'), Err(b"\xd8\x3c"), Err(b"\xdf\xc3"), Ok('\x00'), Ok('8'), Ok('\x00'), Err(b"\xf3\xd8\x3c\xdf"), Err(b"\x00")]
#|[Err(b"\x8d"), Err(b"\xd1\x6b"), Ok('e'), Err(b"\xd8\x3c"), Err(b"\xdf\xc3"), Ok('n'), Ok('8'), Ok('l'), Err(b"\xf3\xd8\x3c\xdf"), Err(b"\x00")]
,
)
}
2 changes: 1 addition & 1 deletion encoding/encoding.mbt
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,7 @@ pub fn write_utf16le_char(buf : @buffer.T, value : Char) -> Unit {
pub fn write_utf16be_char(buf : @buffer.T, value : Char) -> Unit {
let code = value.to_uint()
if code < 0x10000 {
let b0 = (code >> 0xFF).to_byte()
let b0 = (code >> 8).to_byte()
let b1 = (code & 0xFF).to_byte()
buf.write_byte(b0)
buf.write_byte(b1)
Expand Down
8 changes: 4 additions & 4 deletions encoding/encoding_test.mbt
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ test "encoding String to UTF16BE" {
inspect!(
bytes,
content=
#|b"\x00\xbb\x00\x66\x00\x2e\x00\x28\x00\xbb\x00\x78\x00\x2e\x00\x66\x00\x28\x00\x78\x00\x20\x00\x78\x00\x29\x00\x29\x00\x28\x00\xbb\x00\x78\x00\x2e\x00\x66\x00\x28\x00\x78\x00\x20\x00\x78\x00\x29\x00\x29"
#|b"\x03\xbb\x00\x66\x00\x2e\x00\x28\x03\xbb\x00\x78\x00\x2e\x00\x66\x00\x28\x00\x78\x00\x20\x00\x78\x00\x29\x00\x29\x00\x28\x03\xbb\x00\x78\x00\x2e\x00\x66\x00\x28\x00\x78\x00\x20\x00\x78\x00\x29\x00\x29"
,
)
}
Expand Down Expand Up @@ -147,13 +147,13 @@ test "to_utf16be_bytes" {
inspect!(
@encoding.to_utf16be_bytes('α'),
content=
#|b"\x00\xb1"
#|b"\x03\xb1"
,
)
inspect!(
@encoding.to_utf16be_bytes('啊'),
content=
#|b"\x00\x4a"
#|b"\x55\x4a"
,
)
inspect!(
Expand Down Expand Up @@ -215,7 +215,7 @@ test "write_utf16be_char" {
inspect!(
buf.to_bytes(),
content=
#|b"\x00\x41\x00\xb1\x00\x4a\xd8\x3d\xde\x26"
#|b"\x00\x41\x03\xb1\x55\x4a\xd8\x3d\xde\x26"
,
)
}

0 comments on commit 8802a11

Please sign in to comment.