use crate::unicode_encoding::UnicodeEncodingError::*;
use crate::unicode_encoding::UnicodeEncodingError;
use crate::unicode_encoding::UnicodeEncoding;
use crate::endian_aware_byte_streamer;
use crate::utf_16;
pub struct Utf32 {
pub data: Vec<u32> }
impl PartialEq for Utf32 {
fn eq(&self, other: &Utf32) -> bool {
return self.data == other.data;
}
}
impl Clone for Utf32 {
fn clone(&self) -> Utf32 {
let new_data = self.data.clone();
let new_struct = Utf32{data: new_data};
return new_struct;
}
}
impl Utf32 {
pub fn check_sanity_utf32(&self) -> UnicodeEncodingError {
const VALID_CODEPOINT: u32 = 0b00011111_11111111_11111111;
for i in 0..self.data.len() {
let glyph = self.data[i];
if glyph & !VALID_CODEPOINT != 0 {
return InvalidCodepointTooManyBits;
}
let other_glyph = if i + 1 == self.data.len() {
0
} else {
self.data[i+1]
};
match utf_16::compatible_codepoints(glyph, other_glyph) {
NoError => {},
x => return x,
}
}
return NoError;
}
}
impl UnicodeEncoding for Utf32 {
fn from_utf_32(data_utf_32: &Utf32) -> Utf32 {
return data_utf_32.clone();
}
fn to_utf_32(&self) -> Utf32 {
return self.clone();
}
fn from_bytes_no_check(bytes: &[u8], big_endian: bool) -> Result<Self, UnicodeEncodingError> {
let ret = Utf32{data: endian_aware_byte_streamer::from_bytes::<u32>(bytes, big_endian)?};
return Ok(ret);
}
fn to_bytes(&self, big_endian: bool) -> Vec<u8> {
return endian_aware_byte_streamer::to_bytes::<u32>(&self.data, big_endian);
}
}
#[test]
fn test_data_content() {
let data: [u8; 4] = [0, 1, 2, 3];
let utf_32_glyph = Utf32::from_bytes(data.as_slice(), true).unwrap();
assert_eq!(utf_32_glyph.data[0], 0x00010203);
}