|
@ -16,31 +16,19 @@ pub fn array_to_utf16(array: &[u8]) -> String { |
|
|
}
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// TODO: const fn version of this! (helpful with tests)
|
|
|
|
|
|
#[macro_export]
|
|
|
|
|
|
macro_rules! utf8_to_array {
|
|
|
|
|
|
($s: expr, $size: expr) => {
|
|
|
|
|
|
{
|
|
|
|
|
|
let mut array = [0u8; $size];
|
|
|
|
|
|
let bytes = $s.as_bytes();
|
|
|
|
|
|
|
|
|
pub fn utf8_to_array<const N: usize>(s: impl Into<String>) -> [u8; N] {
|
|
|
|
|
|
let mut array = [0u8; N];
|
|
|
|
|
|
let s = s.into();
|
|
|
|
|
|
let bytes = s.as_bytes();
|
|
|
array[..bytes.len()].clone_from_slice(&bytes);
|
|
|
array[..bytes.len()].clone_from_slice(&bytes);
|
|
|
array
|
|
|
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<_>>();
|
|
|
|
|
|
|
|
|
pub fn utf8_to_utf16_array<const N: usize>(s: impl Into<String>) -> [u16; N] {
|
|
|
|
|
|
let mut array = [0u16; N];
|
|
|
|
|
|
let bytes = s.into().encode_utf16().collect::<Vec<_>>();
|
|
|
array[..bytes.len()].clone_from_slice(&bytes);
|
|
|
array[..bytes.len()].clone_from_slice(&bytes);
|
|
|
array
|
|
|
array
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
}
|
|
|
|
|
|
|
|
|
pub fn vec_to_array<T: Default + Copy, const N: usize>(vec: Vec<T>) -> [T; N] {
|
|
|
pub fn vec_to_array<T: Default + Copy, const N: usize>(vec: Vec<T>) -> [T; N] {
|
|
@ -53,10 +41,12 @@ pub fn vec_to_array<T: Default + Copy, const N: usize>(vec: Vec<T>) -> [T; N] { |
|
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
#[cfg(test)]
|
|
|
mod test {
|
|
|
mod test {
|
|
|
|
|
|
use super::*;
|
|
|
|
|
|
|
|
|
#[test]
|
|
|
#[test]
|
|
|
fn test_utf8_to_array() {
|
|
|
fn test_utf8_to_array() {
|
|
|
let s = "asdf".to_owned();
|
|
|
let s = "asdf".to_owned();
|
|
|
let a = utf8_to_array!(s, 8);
|
|
|
|
|
|
|
|
|
let a = utf8_to_array(s);
|
|
|
|
|
|
|
|
|
let mut e = [0u8; 8];
|
|
|
let mut e = [0u8; 8];
|
|
|
e[..4].clone_from_slice(b"asdf");
|
|
|
e[..4].clone_from_slice(b"asdf");
|
|
@ -64,14 +54,14 @@ mod test { |
|
|
}
|
|
|
}
|
|
|
|
|
|
|
|
|
#[test]
|
|
|
#[test]
|
|
|
fn utf8_to_utf16_array() {
|
|
|
|
|
|
let utf16 = utf8_to_utf16_array!("asdf", 16);
|
|
|
|
|
|
|
|
|
fn test_utf8_to_utf16_array() {
|
|
|
|
|
|
let utf16 = utf8_to_utf16_array("asdf");
|
|
|
assert!(utf16 == [97, 115, 100, 102, 0,0,0,0,0,0,0,0,0,0,0,0])
|
|
|
assert!(utf16 == [97, 115, 100, 102, 0,0,0,0,0,0,0,0,0,0,0,0])
|
|
|
}
|
|
|
}
|
|
|
|
|
|
|
|
|
#[test]
|
|
|
#[test]
|
|
|
fn utf8_to_utf16_array_unicode() {
|
|
|
|
|
|
let utf16 = utf8_to_utf16_array!("あいうえお", 16);
|
|
|
|
|
|
|
|
|
fn test_utf8_to_utf16_array_unicode() {
|
|
|
|
|
|
let utf16 = utf8_to_utf16_array("あいうえお");
|
|
|
assert!(utf16 == [0x3042 , 0x3044, 0x3046, 0x3048, 0x304A, 0,0,0,0,0,0,0,0,0,0,0])
|
|
|
assert!(utf16 == [0x3042 , 0x3044, 0x3046, 0x3048, 0x304A, 0,0,0,0,0,0,0,0,0,0,0])
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
}
|