优化NbtReader的read_int_array和read_long_array方法的实现

This commit is contained in:
shenjack 2024-03-06 23:32:00 +08:00
parent 1c24b43762
commit 1dee92147d
Signed by: shenjack
GPG Key ID: 7B1134A979775551

View File

@ -61,6 +61,7 @@ impl NbtReader<'_> {
read!(read_u64, u64, 8); read!(read_u64, u64, 8);
read!(read_f32, f32, 4, false); read!(read_f32, f32, 4, false);
read!(read_f64, f64, 8, false); read!(read_f64, f64, 8, false);
pub fn read_u8_array(&mut self, len: usize) -> &[u8] { pub fn read_u8_array(&mut self, len: usize) -> &[u8] {
let value = &self.data[self.cursor..self.cursor + len]; let value = &self.data[self.cursor..self.cursor + len];
self.cursor += len; self.cursor += len;
@ -78,31 +79,17 @@ impl NbtReader<'_> {
self.cursor += len; self.cursor += len;
value.into_owned() value.into_owned()
} }
pub fn read_byte_array(&mut self, len: usize) -> Vec<i8> { pub fn read_int_array(&mut self, len: usize) -> &[i32] {
let value = self.data[self.cursor..self.cursor + len].to_vec(); let datas = self.read_u8_array(len * 4);
self.cursor += len; let value = unsafe {
value std::slice::from_raw_parts(datas.as_ptr() as *const i32, len)
};
} }
pub fn read_int_array(&mut self, len: usize) -> Vec<i32> { pub fn read_long_array(&mut self, len: usize) -> &[i64] {
let mut value = Vec::with_capacity(len); let datas = self.read_u8_array(len * 8);
for _ in 0..len { let value = unsafe {
value.push(self.read_i32()); std::slice::from_raw_parts(datas.as_ptr() as *const i64, len)
} };
value
}
pub fn read_long_array(&mut self, len: usize) -> Vec<i64> {
let mut value = Vec::with_capacity(len);
for _ in 0..len {
value.push(self.read_i64());
}
value
}
pub fn read_byte_array_array(&mut self, len: usize) -> Vec<Vec<i8>> {
let mut value = Vec::with_capacity(len);
for _ in 0..len {
value.push(self.read_byte_array(self.read_u8() as usize));
}
value
} }
} }