I’ve just written this function:
fn decode_to_utf16(bytes: &[u8], encoding: &'static Encoding) -> Vec<u16> {
let mut decoder = encoding.new_decoder();
let capacity = decoder.max_utf16_buffer_length(bytes.len()).exepct("Overflow");
let mut utf16 = Vec::with_capacity(capacity);
let uninitialized = unsafe {
slice::from_raw_parts_mut(utf16.as_ptr(), capacity)
};
let last = true;
let (_, read, written, _) = decoder.decode_to_utf16(bytes, uninitialized, last);
assert!(read == bytes.len());
unsafe {
utf16.set_len(written)
}
utf16
}
Do you think it would belong as a method of Encoding?
I’ve just written this function:
Do you think it would belong as a method of
Encoding?