Skip to content

Commit

Permalink
refactor: reduce necessary decode
Browse files Browse the repository at this point in the history
  • Loading branch information
jetjinser committed Dec 10, 2024
1 parent a12a7c8 commit 23e24d8
Showing 1 changed file with 3 additions and 5 deletions.
8 changes: 3 additions & 5 deletions encoding/encoding.mbt
Original file line number Diff line number Diff line change
Expand Up @@ -36,17 +36,15 @@ pub fn encode(encoding : Encoding, src : String) -> Bytes {
UTF16 | UTF16LE => return src.to_bytes()
_ => ()
}
let bytes = src.to_bytes()
let chars = decode_strict(UTF16LE, bytes)
let new_buf = @buffer.T::new(size_hint=bytes.length())
let new_buf = @buffer.T::new(size_hint=src.length() * 4)
let write = match encoding {
UTF8 => write_utf8_char
UTF16BE => write_utf16be_char
_ => abort("unreachable")
}
for char in chars {
for char in src {
// SAFETY: Assume String are always valid UTF16LE
write(new_buf, char.unwrap())
write(new_buf, char)
}
new_buf.to_bytes()
}
Expand Down

0 comments on commit 23e24d8

Please sign in to comment.