From 4c696d71784466d96d2a69b2b3b96e3072a38cbf Mon Sep 17 00:00:00 2001 From: shenjack <3695888@qq.com> Date: Sat, 9 Mar 2024 13:55:20 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BC=98=E5=8C=96NbtReader=E8=AF=BB=E5=8F=96?= =?UTF-8?q?=E6=96=B9=E6=B3=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- shen-nbt5/src/lib.rs | 36 ++++++++-------------- shen-nbt5/src/tests.rs | 70 ++++++++++++++++++++++++------------------ 2 files changed, 52 insertions(+), 54 deletions(-) diff --git a/shen-nbt5/src/lib.rs b/shen-nbt5/src/lib.rs index 95e4f49..5aa1704 100644 --- a/shen-nbt5/src/lib.rs +++ b/shen-nbt5/src/lib.rs @@ -63,6 +63,7 @@ impl NbtReader<'_> { /// 转换大小端(大端) /// /// 会在超出长度时 panic + #[inline] pub fn read_i16(&mut self) -> i16 { let value = i16::from_be_bytes([self.data[self.cursor], self.data[self.cursor + 1]]); self.cursor += 2; @@ -73,12 +74,14 @@ impl NbtReader<'_> { /// 转换大小端(大端) /// /// 会在超出长度时 panic + #[inline] pub fn read_u16(&mut self) -> u16 { self.read_i16() as u16 } /// 安全的读取 i32 类型的数据 /// /// 转换大小端(大端) /// /// 会在超出长度时 panic + #[inline] pub fn read_i32(&mut self) -> i32 { let value = i32::from_be_bytes([ self.data[self.cursor], @@ -94,12 +97,14 @@ impl NbtReader<'_> { /// 转换大小端(大端) /// /// 会在超出长度时 panic + #[inline] pub fn read_u32(&mut self) -> u32 { self.read_i32() as u32 } /// 安全的读取 i64 类型的数据 /// /// 转换大小端(大端) /// /// 会在超出长度时 panic + #[inline] pub fn read_i64(&mut self) -> i64 { let value = i64::from_be_bytes([ self.data[self.cursor], @@ -119,41 +124,22 @@ impl NbtReader<'_> { /// 转换大小端(大端) /// /// 会在超出长度时 panic + #[inline] pub fn read_u64(&mut self) -> u64 { self.read_i64() as u64 } /// 读取一个 f32 类型的数据 /// /// 转换大小端 /// /// 会在超出长度时 panic - pub fn read_f32(&mut self) -> f32 { - let value = f32::from_be_bytes([ - self.data[self.cursor], - self.data[self.cursor + 1], - self.data[self.cursor + 2], - self.data[self.cursor + 3], - ]); - self.cursor += 4; - value - } + #[inline] + pub fn read_f32(&mut self) -> f32 { f32::from_bits(self.read_u32()) } /// 读取一个 f64 类型的数据 /// /// 转换大小端 /// /// 会在超出长度时 panic - pub fn read_f64(&mut self) -> f64 { - let value = f64::from_be_bytes([ - self.data[self.cursor], - self.data[self.cursor + 1], - self.data[self.cursor + 2], - self.data[self.cursor + 3], - self.data[self.cursor + 4], - self.data[self.cursor + 5], - self.data[self.cursor + 6], - self.data[self.cursor + 7], - ]); - self.cursor += 8; - value - } + #[inline] + pub fn read_f64(&mut self) -> f64 { f64::from_bits(self.read_u64()) } /// 读取一个 f32 类型的数据 /// /// 转换大小端 @@ -182,6 +168,7 @@ impl NbtReader<'_> { /// # 安全性 /// /// 长度溢出会导致 panic + #[inline] pub fn read_u8_array(&mut self, len: usize) -> &[u8] { let value = &self.data[self.cursor..self.cursor + len]; self.cursor += len; @@ -204,6 +191,7 @@ impl NbtReader<'_> { /// # 安全性 /// /// 长度溢出会导致 panic + #[inline] pub fn read_string(&mut self, len: usize) -> String { let value = String::from_utf8_lossy(&self.data[self.cursor..self.cursor + len]); self.cursor += len; diff --git a/shen-nbt5/src/tests.rs b/shen-nbt5/src/tests.rs index b011d76..fa9ddf4 100644 --- a/shen-nbt5/src/tests.rs +++ b/shen-nbt5/src/tests.rs @@ -66,45 +66,55 @@ mod safe_test { #[test] fn read_fxx() { - let mut data = vec![ - 0x40, 0x49, 0x0f, 0xdb, 0x40, 0x49, 0x0f, 0xdb, 0x40, 0x49, 0x0f, 0xdb, 0x40, 0x49, - 0x0f, 0xdb, - ]; + let mut data = Vec::with_capacity(12); + data.extend_from_slice(&std::f32::consts::PI.to_be_bytes()); + data.extend_from_slice(&std::f64::consts::PI.to_be_bytes()); + println!("{:?}", data); let mut reader = NbtReader::new(&mut data); - println!("{}", f32::from_be_bytes([0x40, 0x49, 0x0f, 0xdb])); - assert_eq!(reader.read_f32(), 3.1415927); + assert_eq!(reader.read_f32(), std::f32::consts::PI); assert_eq!(reader.cursor, 4); - assert_eq!(reader.read_f64(), 3.14159265); + assert_eq!(reader.read_f64(), std::f64::consts::PI); assert_eq!(reader.cursor, 12); } + + #[test] + fn read_string() { + let mut data = Vec::with_capacity(20); + data.extend("Hello world!啊?".as_bytes()); + let len = data.len(); + println!("{:?}", data); + let mut reader = NbtReader::new(&mut data); + assert_eq!(reader.read_string(len), "Hello world!啊?"); + assert_eq!(reader.cursor, 18); + } } mod unsafe_test { use super::*; -} -#[test] -fn read_array() { - let mut data = vec![0x01, 0x02, 0x03, 0x04]; - let mut reader = NbtReader::new(&mut data); - assert_eq!(reader.read_u8_array(2), &[0x01, 0x02]); - assert_eq!(reader.cursor, 2); - assert_eq!(reader.read_i8_array_unchecked(2), &[0x03, 0x04]); - assert_eq!(reader.cursor, 4); -} + #[test] + fn read_array() { + let mut data = vec![0x01, 0x02, 0x03, 0x04]; + let mut reader = NbtReader::new(&mut data); + assert_eq!(reader.read_u8_array(2), &[0x01, 0x02]); + assert_eq!(reader.cursor, 2); + assert_eq!(reader.read_i8_array_unchecked(2), &[0x03, 0x04]); + assert_eq!(reader.cursor, 4); + } -#[test] -fn read_int_array() { - let mut value = 1234567890_i32.to_be_bytes(); - let mut reader = NbtReader::new(&mut value); - assert_eq!(reader.read_i32_array_unchecked(1), &[1234567890_i32]); - assert_eq!(reader.cursor, 4); -} + #[test] + fn read_int_array() { + let mut value = 1234567890_i32.to_be_bytes(); + let mut reader = NbtReader::new(&mut value); + assert_eq!(reader.read_i32_array_unchecked(1), &[1234567890_i32]); + assert_eq!(reader.cursor, 4); + } -#[test] -fn read_long_array() { - let mut value = 1234567890_i64.to_be_bytes(); - let mut reader = NbtReader::new(&mut value); - assert_eq!(reader.read_i64_array(1), &[1234567890_i64]); - assert_eq!(reader.cursor, 8); + #[test] + fn read_long_array() { + let mut value = 1234567890_i64.to_be_bytes(); + let mut reader = NbtReader::new(&mut value); + assert_eq!(reader.read_i64_array(1), &[1234567890_i64]); + assert_eq!(reader.cursor, 8); + } }