utf8_to_utf16_array
This commit is contained in:
parent
dcd47fa384
commit
7aa0f907d5
@ -9,14 +9,6 @@ pub fn array_to_utf8<const X: usize>(array: [u8; X]) -> Result<String, std::stri
|
|||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/*pub fn utf8_to_array<const X: usize>(string: String) -> Result<[u8; X], std::string::FromUtf8Error> {
|
|
||||||
let mut array = [0u8; X];
|
|
||||||
let bytes = string.as_bytes();
|
|
||||||
array[..bytes.len()].clone_from_slice(&bytes);
|
|
||||||
Ok(array)
|
|
||||||
}*/
|
|
||||||
|
|
||||||
#[macro_export]
|
#[macro_export]
|
||||||
macro_rules! utf8_to_array {
|
macro_rules! utf8_to_array {
|
||||||
($s: expr, $size: expr) => {
|
($s: expr, $size: expr) => {
|
||||||
@ -29,6 +21,19 @@ macro_rules! utf8_to_array {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[macro_export]
|
||||||
|
macro_rules! utf8_to_utf16_array {
|
||||||
|
($s: expr, $size: expr) => {
|
||||||
|
{
|
||||||
|
let mut array = [0u16; $size];
|
||||||
|
//let bytes = $s.as_bytes();
|
||||||
|
let bytes = $s.encode_utf16().collect::<Vec<_>>();
|
||||||
|
array[..bytes.len()].clone_from_slice(&bytes);
|
||||||
|
array
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
@ -43,4 +48,16 @@ mod test {
|
|||||||
e[..4].clone_from_slice(b"asdf");
|
e[..4].clone_from_slice(b"asdf");
|
||||||
assert!(a == e);
|
assert!(a == e);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn utf8_to_utf16_array() {
|
||||||
|
let utf16 = utf8_to_utf16_array!("asdf", 16);
|
||||||
|
assert!(utf16 == [97, 115, 100, 102, 0,0,0,0,0,0,0,0,0,0,0,0])
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn utf8_to_utf16_array_unicode() {
|
||||||
|
let utf16 = utf8_to_utf16_array!("あいうえお", 16);
|
||||||
|
assert!(utf16 == [0x3042 , 0x3044, 0x3046, 0x3048, 0x304A, 0,0,0,0,0,0,0,0,0,0,0])
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user