优化代码格式
This commit is contained in:
parent
18fe86224e
commit
28f76ad4ad
@ -7,7 +7,6 @@ fn main() {
|
||||
cli_read_test();
|
||||
}
|
||||
|
||||
|
||||
fn big_read_test() {
|
||||
let data: [u8; 0x608] = [
|
||||
0x0A, 0x00, 0x05, 0x4C, 0x65, 0x76, 0x65, 0x6C, 0x04, 0x00, 0x08, 0x6C, 0x6F, 0x6E, 0x67,
|
||||
@ -117,7 +116,6 @@ fn big_read_test() {
|
||||
read_test(data.to_vec());
|
||||
}
|
||||
|
||||
|
||||
macro_rules! test_lib {
|
||||
($func: block, $name: expr, $len: expr) => {
|
||||
std::thread::sleep(std::time::Duration::from_secs(1));
|
||||
@ -148,7 +146,9 @@ fn test_v1(data: Vec<u8>) {
|
||||
{
|
||||
let cursor: shen_nbt1::data::Reader = std::io::Cursor::new(data.as_slice());
|
||||
let _nbt_data = shen_nbt1::data::NbtItem::try_from(cursor).unwrap();
|
||||
}, "nbt v1", len
|
||||
},
|
||||
"nbt v1",
|
||||
len
|
||||
);
|
||||
}
|
||||
fn test_v2(data: Vec<u8>) {
|
||||
@ -157,7 +157,9 @@ fn test_v2(data: Vec<u8>) {
|
||||
test_lib!(
|
||||
{
|
||||
let _nbt_data = shen_nbt2::Value::from_vec(data);
|
||||
}, "nbt v2", len
|
||||
},
|
||||
"nbt v2",
|
||||
len
|
||||
);
|
||||
}
|
||||
|
||||
@ -167,7 +169,9 @@ fn test_v3(data: Vec<u8>) {
|
||||
test_lib!(
|
||||
{
|
||||
let _nbt_data = shen_nbt3::Value::from_vec(data);
|
||||
}, "nbt v3", len
|
||||
},
|
||||
"nbt v3",
|
||||
len
|
||||
);
|
||||
}
|
||||
|
||||
@ -177,7 +181,9 @@ fn test_v4(data: Vec<u8>) {
|
||||
test_lib!(
|
||||
{
|
||||
let _nbt_data = shen_nbt4::Value::from_vec(data);
|
||||
}, "nbt v4", len
|
||||
},
|
||||
"nbt v4",
|
||||
len
|
||||
);
|
||||
}
|
||||
|
||||
@ -187,7 +193,9 @@ fn test_fastnbt(data: Vec<u8>) {
|
||||
test_lib!(
|
||||
{
|
||||
let _nbt_data: fastnbt::Value = fastnbt::from_bytes(data.as_slice()).unwrap();
|
||||
}, "fastnbt", len
|
||||
},
|
||||
"fastnbt",
|
||||
len
|
||||
);
|
||||
}
|
||||
|
||||
@ -213,7 +221,6 @@ fn read_test(in_data: Vec<u8>) {
|
||||
test_fastnbt(data);
|
||||
}
|
||||
|
||||
|
||||
fn cli_read_test() {
|
||||
let mut args = std::env::args();
|
||||
// 如果有, 取出
|
||||
@ -246,31 +253,31 @@ fn cli_read_test() {
|
||||
// {
|
||||
// let cursor: shen_nbt1::data::Reader = std::io::Cursor::new(data.as_slice());
|
||||
// let nbt_data = shen_nbt1::data::NbtItem::try_from(cursor).unwrap();
|
||||
|
||||
|
||||
// }, "nbt v1", len
|
||||
// );
|
||||
|
||||
|
||||
// let data2 = std::fs::read(&arg).unwrap();
|
||||
// test_lib!(
|
||||
// {
|
||||
// let nbt_data = shen_nbt2::Value::from_vec(data2);
|
||||
// }, "nbt v2", len
|
||||
// );
|
||||
|
||||
|
||||
// let data3 = std::fs::read(&arg).unwrap();
|
||||
// test_lib!(
|
||||
// {
|
||||
// let nbt_data = shen_nbt3::Value::from_vec(data3);
|
||||
// }, "nbt v3", len
|
||||
// );
|
||||
|
||||
|
||||
// let data4 = std::fs::read(&arg).unwrap();
|
||||
// test_lib!(
|
||||
// {
|
||||
// let nbt_data = shen_nbt4::Value::from_vec(data);
|
||||
// }, "nbt v4", len
|
||||
// );
|
||||
|
||||
|
||||
// let data5 = std::fs::read(&arg).unwrap();
|
||||
// test_lib!(
|
||||
// {
|
||||
@ -280,4 +287,4 @@ fn cli_read_test() {
|
||||
} else {
|
||||
println!("Usage: cargo run --release -- <file>");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -19,18 +19,12 @@ impl<'value> NbtData<'value> {
|
||||
self.offset += length;
|
||||
self.offset
|
||||
}
|
||||
pub fn read_byte(&mut self) -> i8 {
|
||||
self.data.read_with::<i8>(&mut self.offset, BE).unwrap()
|
||||
}
|
||||
pub fn read_byte(&mut self) -> i8 { self.data.read_with::<i8>(&mut self.offset, BE).unwrap() }
|
||||
pub fn read_short(&mut self) -> i16 {
|
||||
self.data.read_with::<i16>(&mut self.offset, BE).unwrap()
|
||||
}
|
||||
pub fn read_int(&mut self) -> i32 {
|
||||
self.data.read_with::<i32>(&mut self.offset, BE).unwrap()
|
||||
}
|
||||
pub fn read_long(&mut self) -> i64 {
|
||||
self.data.read_with::<i64>(&mut self.offset, BE).unwrap()
|
||||
}
|
||||
pub fn read_int(&mut self) -> i32 { self.data.read_with::<i32>(&mut self.offset, BE).unwrap() }
|
||||
pub fn read_long(&mut self) -> i64 { self.data.read_with::<i64>(&mut self.offset, BE).unwrap() }
|
||||
pub fn read_float(&mut self) -> f32 {
|
||||
self.data.read_with::<f32>(&mut self.offset, BE).unwrap()
|
||||
}
|
||||
|
@ -87,12 +87,12 @@ pub mod raw_reading {
|
||||
use std::intrinsics::unlikely;
|
||||
|
||||
/// 多少有点脱裤子放屁
|
||||
|
||||
|
||||
pub fn slice_as_byte_array(slice: &[u8]) -> Vec<i8> {
|
||||
slice.to_vec().into_iter().map(|x| x as i8).collect()
|
||||
}
|
||||
/// unsafe 从这里开始
|
||||
|
||||
|
||||
pub fn slice_as_short_array(slice: &[u8]) -> Option<Vec<i16>> {
|
||||
#[cfg(feature = "internal_opt")]
|
||||
let length = if unlikely(slice.len() % 2 != 0) {
|
||||
@ -110,7 +110,7 @@ pub mod raw_reading {
|
||||
}
|
||||
/// 开始 unsafe 了
|
||||
/// unsafe rust, 小子!
|
||||
|
||||
|
||||
pub fn slice_as_int_array(slice: &[u8]) -> Option<Vec<i32>> {
|
||||
#[cfg(feature = "internal_opt")]
|
||||
let length = if unlikely(slice.len() % 4 != 0) {
|
||||
@ -127,7 +127,7 @@ pub mod raw_reading {
|
||||
Some(unsafe { std::slice::from_raw_parts(slice.as_ptr() as *mut i32, length).to_vec() })
|
||||
}
|
||||
/// 这边也是 unsafe 捏
|
||||
|
||||
|
||||
pub fn slice_as_long_array(slice: &[u8]) -> Option<Vec<i64>> {
|
||||
let length = if slice.len() % 8 != 0 {
|
||||
return None;
|
||||
@ -137,7 +137,7 @@ pub mod raw_reading {
|
||||
Some(unsafe { std::slice::from_raw_parts(slice.as_ptr() as *mut i64, length).to_vec() })
|
||||
}
|
||||
/// 这边也是 unsafe 捏
|
||||
|
||||
|
||||
pub fn slice_as_float_array(slice: &[u8]) -> Option<Vec<f32>> {
|
||||
let length = if slice.len() % 4 != 0 {
|
||||
return None;
|
||||
@ -147,7 +147,7 @@ pub mod raw_reading {
|
||||
Some(unsafe { std::slice::from_raw_parts(slice.as_ptr() as *mut f32, length).to_vec() })
|
||||
}
|
||||
/// 这边也是 unsafe 捏
|
||||
|
||||
|
||||
pub fn slice_as_double_array(slice: &[u8]) -> Option<Vec<f64>> {
|
||||
let length = if slice.len() % 8 != 0 {
|
||||
return None;
|
||||
@ -188,19 +188,18 @@ pub enum Value<'value> {
|
||||
}
|
||||
|
||||
impl<'value> Value<'value> {
|
||||
|
||||
pub fn read_byte(data: &mut NbtData) -> Self { Self::Byte(data.read_byte()) }
|
||||
|
||||
|
||||
pub fn read_short(data: &mut NbtData) -> Self { Self::Short(data.read_short()) }
|
||||
|
||||
|
||||
pub fn read_int(data: &mut NbtData) -> Self { Self::Int(data.read_int()) }
|
||||
|
||||
|
||||
pub fn read_long(data: &mut NbtData) -> Self { Self::Long(data.read_long()) }
|
||||
|
||||
|
||||
pub fn read_float(data: &mut NbtData) -> Self { Self::Float(data.read_float()) }
|
||||
|
||||
|
||||
pub fn read_double(data: &mut NbtData) -> Self { Self::Double(data.read_double()) }
|
||||
|
||||
|
||||
pub fn read_string(data: &mut NbtData) -> Self {
|
||||
let length = data.read_short();
|
||||
let value = data.read_bytes(length as usize);
|
||||
@ -358,21 +357,21 @@ impl<'value> Value<'value> {
|
||||
}
|
||||
Self::Compound(list)
|
||||
}
|
||||
|
||||
|
||||
pub fn read_byte_array(data: &mut NbtData) -> Self {
|
||||
let length = data.read_int();
|
||||
let raw_data = data.read_bytes(length as usize);
|
||||
let value = raw_reading::slice_as_byte_array(raw_data.as_slice());
|
||||
Self::ByteArray(value)
|
||||
}
|
||||
|
||||
|
||||
pub fn read_int_array(data: &mut NbtData) -> Self {
|
||||
let length = data.read_int();
|
||||
let raw_data = data.read_bytes(length as usize * 4);
|
||||
let value = raw_reading::slice_as_int_array(raw_data.as_slice()).unwrap();
|
||||
Self::IntArray(value)
|
||||
}
|
||||
|
||||
|
||||
pub fn read_long_array(data: &mut NbtData) -> Self {
|
||||
let length = data.read_int();
|
||||
let raw_data = data.read_bytes(length as usize * 8);
|
||||
@ -451,14 +450,14 @@ impl<'value> Value<'value> {
|
||||
_ => None,
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
pub fn into_list(self) -> Option<Vec<Value<'value>>> {
|
||||
match self {
|
||||
Self::List(value) => Some(value),
|
||||
_ => None,
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
pub fn into_compound(self) -> Option<Vec<(String, Value<'value>)>> {
|
||||
match self {
|
||||
Self::Compound(value) => Some(value),
|
||||
|
@ -19,10 +19,13 @@ pub struct NbtReader<'data> {
|
||||
macro_rules! read {
|
||||
($name:ident, $ty:ty, $size:literal) => {
|
||||
#[doc = concat!("读取 ", stringify!($ty), " 类型 ", $size, " 长度的数据")]
|
||||
///
|
||||
#[doc = "转换大小端"]
|
||||
pub fn $name(&mut self) -> $ty {
|
||||
unsafe {
|
||||
// 使用 std::ptr::read_unaligned 解决未对齐地址问题
|
||||
let value = std::ptr::read_unaligned(self.data[self.cursor..].as_ptr() as *const $ty);
|
||||
let value =
|
||||
std::ptr::read_unaligned(self.data[self.cursor..].as_ptr() as *const $ty);
|
||||
self.cursor += std::mem::size_of::<$ty>();
|
||||
value.to_be()
|
||||
}
|
||||
@ -30,10 +33,13 @@ macro_rules! read {
|
||||
};
|
||||
($name:ident, $ty:ty, $size:literal, false) => {
|
||||
#[doc = concat!("读取 ", stringify!($ty), " 类型 ", $size, " 长度的数据")]
|
||||
///
|
||||
#[doc = "不转换大小端"]
|
||||
pub fn $name(&mut self) -> $ty {
|
||||
unsafe {
|
||||
// 使用 std::ptr::read_unaligned 解决未对齐地址问题
|
||||
let value = std::ptr::read_unaligned(self.data[self.cursor..].as_ptr() as *const $ty);
|
||||
let value =
|
||||
std::ptr::read_unaligned(self.data[self.cursor..].as_ptr() as *const $ty);
|
||||
self.cursor += std::mem::size_of::<$ty>();
|
||||
value
|
||||
}
|
||||
@ -48,16 +54,16 @@ impl NbtReader<'_> {
|
||||
// endian: Endian::Big,
|
||||
}
|
||||
}
|
||||
pub fn read_i8(&mut self) -> i8 {
|
||||
let value = self.data[self.cursor] as i8;
|
||||
self.cursor += 1;
|
||||
value
|
||||
}
|
||||
/// 读取一个 u8 类型的数据
|
||||
#[inline]
|
||||
pub fn read_u8(&mut self) -> u8 {
|
||||
let value = self.data[self.cursor];
|
||||
self.cursor += 1;
|
||||
value
|
||||
}
|
||||
/// 读取一个 i8 类型的数据
|
||||
#[inline]
|
||||
pub fn read_i8(&mut self) -> i8 { self.read_u8() as i8 }
|
||||
read!(read_i16_unchecked, i16, 2);
|
||||
read!(read_u16_unchecked, u16, 2);
|
||||
read!(read_i32_unchecked, i32, 4);
|
||||
@ -87,7 +93,8 @@ impl NbtReader<'_> {
|
||||
pub fn read_int_array(&mut self, len: usize) -> &[i32] {
|
||||
unsafe {
|
||||
println!("data: {:?}", self.data);
|
||||
let value = std::slice::from_raw_parts_mut(self.data[self.cursor..].as_ptr() as *mut i32, len);
|
||||
let value =
|
||||
std::slice::from_raw_parts_mut(self.data[self.cursor..].as_ptr() as *mut i32, len);
|
||||
for n in &mut *value {
|
||||
*n = n.to_be();
|
||||
}
|
||||
@ -99,7 +106,8 @@ impl NbtReader<'_> {
|
||||
pub fn read_long_array(&mut self, len: usize) -> &[i64] {
|
||||
unsafe {
|
||||
println!("data: {:?}", self.data);
|
||||
let value = std::slice::from_raw_parts_mut(self.data[self.cursor..].as_ptr() as *mut i64, len);
|
||||
let value =
|
||||
std::slice::from_raw_parts_mut(self.data[self.cursor..].as_ptr() as *mut i64, len);
|
||||
for n in &mut *value {
|
||||
*n = n.to_be();
|
||||
}
|
||||
@ -108,7 +116,6 @@ impl NbtReader<'_> {
|
||||
value
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone)]
|
||||
@ -140,5 +147,3 @@ pub enum NbtValue {
|
||||
/// 12
|
||||
LongArray(Vec<i64>),
|
||||
}
|
||||
|
||||
|
||||
|
@ -1,5 +1,14 @@
|
||||
use crate::{NbtReader, NbtValue};
|
||||
|
||||
/// 生成测试数据
|
||||
pub fn gen_datas(len: usize) -> Vec<u8> {
|
||||
let mut datas = Vec::with_capacity(len);
|
||||
for i in 0..len {
|
||||
datas.push(i as u8);
|
||||
}
|
||||
datas
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn basic_init() {
|
||||
let mut data = vec![0x01, 0x02, 0x03, 0x04];
|
||||
@ -9,7 +18,6 @@ fn basic_init() {
|
||||
assert_eq!(reader.data, &same_data);
|
||||
}
|
||||
|
||||
|
||||
#[test]
|
||||
fn read_i8() {
|
||||
let mut data = vec![0x01, 0x02, 0x03, 0x04];
|
||||
@ -21,18 +29,42 @@ fn read_i8() {
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn read_one_datas() {
|
||||
let mut datas = vec![0x01, 0x02, 0x03, 0x04, 0x05,
|
||||
0x06, 0x07, 0x08, 0x09, 0x0A,
|
||||
0x0B, 0x0C, 0x0D, 0x0E, 0x0F,
|
||||
0x01, 0x02, 0x03, 0x04, 0x05,];
|
||||
let mut reader = NbtReader::new(&mut datas);
|
||||
assert_eq!(reader.read_i8(), 0x01);
|
||||
fn read_one_bytes() {
|
||||
let mut data = vec![0x01, 0x02];
|
||||
let mut reader = NbtReader::new(&mut data);
|
||||
assert_eq!(reader.read_u8(), 0x01);
|
||||
assert_eq!(reader.cursor, 1);
|
||||
assert_eq!(reader.read_u8(), 0x02);
|
||||
assert_eq!(reader.cursor, 2);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn read_data_unchecked() {
|
||||
let mut datas = gen_datas(100);
|
||||
let mut reader = NbtReader::new(&mut datas);
|
||||
assert_eq!(reader.read_u8(), 0x00);
|
||||
assert_eq!(reader.cursor, 1);
|
||||
assert_eq!(reader.read_i8(), 0x01);
|
||||
assert_eq!(reader.cursor, 2);
|
||||
assert_eq!(reader.read_u8(), 0x02);
|
||||
assert_eq!(reader.cursor, 3);
|
||||
assert_eq!(reader.read_i16_unchecked(), 0x0304);
|
||||
assert_eq!(reader.cursor, 5);
|
||||
assert_eq!(reader.read_u16_unchecked(), 0x0506);
|
||||
assert_eq!(reader.cursor, 7);
|
||||
assert_eq!(reader.read_i32_unchecked(), 0x0708090A);
|
||||
assert_eq!(reader.read_i64_unchecked(), 0x0B0C0D0E0F010203);
|
||||
assert_eq!(reader.cursor, 11);
|
||||
assert_eq!(reader.read_u32_unchecked(), 0x0B0C0D0E);
|
||||
assert_eq!(reader.cursor, 15);
|
||||
assert_eq!(reader.read_i64_unchecked(), 0x0F10111213141516);
|
||||
assert_eq!(reader.cursor, 23);
|
||||
assert_eq!(reader.read_u64_unchecked(), 0x1718191A1B1C1D1E);
|
||||
assert_eq!(reader.cursor, 31);
|
||||
assert_eq!(reader.read_f32_unchecked().to_ne_bytes(), [0x1F, 0x20, 0x21, 0x22]);
|
||||
assert_eq!(
|
||||
reader.read_f64_unchecked().to_ne_bytes(),
|
||||
[0x23, 0x24, 0x25, 0x26, 0x27, 0x28, 0x29, 0x2A]
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
|
Loading…
Reference in New Issue
Block a user