Can you explain how `encode_utf16` works?
shanmukhateja opened this issue · comments
Hello,
I came across
Line 91 in 46a3a76
encoding_rs
crate and I don't quite understand how it works.
I would like to borrow this function to my project and it would really help if I can understand it better.
Thanks in advance :-)
Steal away, This is the function annotated with comments.
fn encode_utf16<B>(buf: &mut Vec<u8>, s: &str) -> anyhow::Result<()>
where
// Uses the byteorder crate, but could as easily just use u16::to_le_bytes or u16::to_be_bytes
// and copy the bytes. But that would make it harder to be generic. `byteorder::ByteOrder`
// already provides that.
B: byteorder::ByteOrder,
{
use std::iter;
// This is the built-in str::encode_utf16 method which produces an iterator of u16's.
for c in s.encode_utf16() {
// Just a silly way to extend buf with two 0 bytes, today I
// would use `[0, 0]` instead since it directly implements `IntoIterator`.
buf.extend(iter::repeat(0x0).take(2));
// Write to the two zeroed bytes we just allocated above in
// the byte-order specified by the parameter `B`.
let s = buf.len() - 2;
B::write_u16(&mut buf[s..], c);
}
Ok(())
}