我也不确定会不会优化,就先这么写吧(
This commit is contained in:
parent
d0ce0ef3b3
commit
3168a2c8f6
@ -84,7 +84,7 @@ pub mod raw_reading {
|
|||||||
/// 多少有点脱裤子放屁
|
/// 多少有点脱裤子放屁
|
||||||
#[inline(always)]
|
#[inline(always)]
|
||||||
pub fn slice_as_byte_array(slice: &[u8]) -> Vec<i8> {
|
pub fn slice_as_byte_array(slice: &[u8]) -> Vec<i8> {
|
||||||
slice.iter().map(|&x| x as i8).collect::<Vec<i8>>()
|
unsafe { std::slice::from_raw_parts(slice.as_ptr() as *mut i8, slice.len()).to_vec() }
|
||||||
}
|
}
|
||||||
/// unsafe 从这里开始
|
/// unsafe 从这里开始
|
||||||
#[inline(always)]
|
#[inline(always)]
|
||||||
@ -312,7 +312,12 @@ impl<'value> Value<'value> {
|
|||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
let name_len = data.read_short();
|
let name_len = data.read_short();
|
||||||
let name = String::from_utf8(data.read_bytes(name_len as usize)).unwrap();
|
let name = if name_len != 0 {
|
||||||
|
let name = String::from_utf8(data.read_bytes(name_len as usize)).unwrap();
|
||||||
|
name
|
||||||
|
} else {
|
||||||
|
String::new()
|
||||||
|
};
|
||||||
let value = match type_id {
|
let value = match type_id {
|
||||||
0 => break,
|
0 => break,
|
||||||
1 => Self::read_byte(data),
|
1 => Self::read_byte(data),
|
||||||
@ -321,33 +326,39 @@ impl<'value> Value<'value> {
|
|||||||
4 => Self::read_long(data),
|
4 => Self::read_long(data),
|
||||||
5 => Self::read_float(data),
|
5 => Self::read_float(data),
|
||||||
6 => Self::read_double(data),
|
6 => Self::read_double(data),
|
||||||
7 => {
|
7 => Self::read_byte_array(data),
|
||||||
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)
|
|
||||||
}
|
|
||||||
8 => Self::read_string(data),
|
8 => Self::read_string(data),
|
||||||
9 => Self::read_list(data),
|
9 => Self::read_list(data),
|
||||||
10 => Self::read_compound(data),
|
10 => Self::read_compound(data),
|
||||||
11 => {
|
11 => Self::read_int_array(data),
|
||||||
let length = data.read_int();
|
12 => Self::read_long_array(data),
|
||||||
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)
|
|
||||||
}
|
|
||||||
12 => {
|
|
||||||
let length = data.read_int();
|
|
||||||
let raw_data = data.read_bytes(length as usize * 8);
|
|
||||||
let value = raw_reading::slice_as_long_array(raw_data.as_slice()).unwrap();
|
|
||||||
Self::LongArray(value)
|
|
||||||
}
|
|
||||||
_ => panic!("WTF, type_id = {}", type_id),
|
_ => panic!("WTF, type_id = {}", type_id),
|
||||||
};
|
};
|
||||||
list.push((name, value));
|
list.push((name, value));
|
||||||
}
|
}
|
||||||
Self::Compound(list)
|
Self::Compound(list)
|
||||||
}
|
}
|
||||||
|
#[inline(always)]
|
||||||
|
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)
|
||||||
|
}
|
||||||
|
#[inline(always)]
|
||||||
|
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)
|
||||||
|
}
|
||||||
|
#[inline(always)]
|
||||||
|
pub fn read_long_array(data: &mut NbtData) -> Self {
|
||||||
|
let length = data.read_int();
|
||||||
|
let raw_data = data.read_bytes(length as usize * 8);
|
||||||
|
let value = raw_reading::slice_as_long_array(raw_data.as_slice()).unwrap();
|
||||||
|
Self::LongArray(value)
|
||||||
|
}
|
||||||
pub fn as_byte(&self) -> Option<i8> {
|
pub fn as_byte(&self) -> Option<i8> {
|
||||||
match self {
|
match self {
|
||||||
Self::Byte(value) => Some(*value),
|
Self::Byte(value) => Some(*value),
|
||||||
|
@ -137,6 +137,7 @@ fn big_read_test() {
|
|||||||
|
|
||||||
fn read_test(data: Vec<u8>) {
|
fn read_test(data: Vec<u8>) {
|
||||||
let len = data.len();
|
let len = data.len();
|
||||||
|
println!("file size: {}", len);
|
||||||
let start_time = std::time::Instant::now();
|
let start_time = std::time::Instant::now();
|
||||||
let _data = data_struct::Value::from_vec(data);
|
let _data = data_struct::Value::from_vec(data);
|
||||||
let end_time = std::time::Instant::now();
|
let end_time = std::time::Instant::now();
|
||||||
|
Loading…
Reference in New Issue
Block a user