From b66b2953d5ac279b3989be394ef5ad5920ebd2bb Mon Sep 17 00:00:00 2001 From: Ben Pfaff Date: Mon, 19 May 2025 08:31:52 -0700 Subject: [PATCH] Revert "Use Box instead of Vec in RawString." This reverts commit e1349d585a472b4da66dd06babfc8bcfdfa03942. --- rust/pspp/src/sys/cooked.rs | 4 +-- rust/pspp/src/sys/raw.rs | 62 ++++++++++++++----------------------- 2 files changed, 25 insertions(+), 41 deletions(-) diff --git a/rust/pspp/src/sys/cooked.rs b/rust/pspp/src/sys/cooked.rs index 5d59305def..ac7272dab0 100644 --- a/rust/pspp/src/sys/cooked.rs +++ b/rust/pspp/src/sys/cooked.rs @@ -744,7 +744,7 @@ pub fn decode( }; for (mut value, label) in record.labels.into_iter() { // XXX warn about too-long value? - value.resize(width); + value.0.resize(width, b' '); // XXX warn abouat duplicate value labels? variable.value_labels.insert(Value::String(value), label); } @@ -911,7 +911,7 @@ impl MultipleResponseType { Value::Number(Some(number)) } VarWidth::String(max_width) => { - let mut value = &*value.0; + let mut value = value.0.as_slice(); while value.ends_with(b" ") { value = &value[..value.len() - 1]; } diff --git a/rust/pspp/src/sys/raw.rs b/rust/pspp/src/sys/raw.rs index ea77e3b4c2..fe13fe3fdb 100644 --- a/rust/pspp/src/sys/raw.rs +++ b/rust/pspp/src/sys/raw.rs @@ -495,7 +495,7 @@ impl HeaderRecord { let magic: [u8; 4] = read_bytes(r)?; let magic: Magic = magic.try_into().map_err(|_| Error::NotASystemFile)?; - let eye_catcher = RawString(read_slice(r, 60)?); + let eye_catcher = RawString(read_vec(r, 60)?); let layout_code: [u8; 4] = read_bytes(r)?; let endian = Endian::identify_u32(2, layout_code) .or_else(|| Endian::identify_u32(2, layout_code)) @@ -523,9 +523,9 @@ impl HeaderRecord { let bias: f64 = endian.parse(read_bytes(r)?); - let creation_date = RawString(read_slice(r, 9)?); - let creation_time = RawString(read_slice(r, 8)?); - let file_label = RawString(read_slice(r, 64)?); + let creation_date = RawString(read_vec(r, 9)?); + let creation_time = RawString(read_vec(r, 8)?); + let file_label = RawString(read_vec(r, 64)?); let _: [u8; 3] = read_bytes(r)?; Ok(HeaderRecord { @@ -598,7 +598,7 @@ impl Decoder { } fn decode<'a>(&self, input: &'a RawString) -> Cow<'a, str> { - self.decode_slice(input.as_slice()) + self.decode_slice(input.0.as_slice()) } pub fn decode_identifier(&self, input: &RawString) -> Result { @@ -1352,17 +1352,17 @@ impl VariableRecord { let missing_value_code: i32 = endian.parse(read_bytes(r)?); let print_format = Spec(endian.parse(read_bytes(r)?)); let write_format = Spec(endian.parse(read_bytes(r)?)); - let name = RawString(read_slice(r, 8)?); + let name = RawString(read_vec(r, 8)?); let label = match has_variable_label { 0 => None, 1 => { let len: u32 = endian.parse(read_bytes(r)?); let read_len = len.min(65535) as usize; - let label = RawString(read_slice(r, read_len)?); + let label = RawString(read_vec(r, read_len)?); let padding_bytes = Integer::next_multiple_of(&len, &4) - len; - let _ = read_slice(r, padding_bytes as usize)?; + let _ = read_vec(r, padding_bytes as usize)?; Some(label) } @@ -1434,7 +1434,7 @@ impl Debug for UntypedValue { } #[derive(Clone, PartialEq, Default, Eq, PartialOrd, Ord, Hash)] -pub struct RawString(pub Box<[u8]>); +pub struct RawString(pub Vec); impl RawString { pub fn spaces(n: usize) -> Self { @@ -1443,30 +1443,16 @@ impl RawString { pub fn as_encoded(&self, encoding: &'static Encoding) -> EncodedStr<'_> { EncodedStr::new(&self.0, encoding) } - pub fn as_slice(&self) -> &[u8] { - &*self.0 - } - pub fn resize(&mut self, len: usize) { - let mut vec = take(&mut self.0).into_vec(); - vec.resize(len, b' '); - self.0 = vec.into_boxed_slice(); - } } impl From> for RawString { fn from(value: Cow<'_, [u8]>) -> Self { - Self(value.into_owned().into_boxed_slice()) + Self(value.into_owned()) } } impl From> for RawString { fn from(source: Vec) -> Self { - Self(source.into_boxed_slice()) - } -} - -impl From> for RawString { - fn from(source: Box<[u8]>) -> Self { Self(source) } } @@ -1479,7 +1465,7 @@ impl From<&[u8]> for RawString { impl Debug for RawString { fn fmt(&self, f: &mut Formatter) -> FmtResult { - write!(f, "{:?}", default_decode(self.as_slice())) + write!(f, "{:?}", default_decode(self.0.as_slice())) } } @@ -1685,10 +1671,10 @@ impl ValueLabelRecord, RawString> { let value = UntypedValue(read_bytes(r)?); let label_len: u8 = endian.parse(read_bytes(r)?); let label_len = label_len as usize; + let padded_len = Integer::next_multiple_of(&(label_len + 1), &8); - let mut label = read_slice(r, label_len)?; - let padding_len = Integer::next_multiple_of(&(label_len + 1), &8) - (label_len + 1); - read_slice(r, padding_len)?; + let mut label = read_vec(r, padded_len - 1)?; + label.truncate(label_len); labels.push((value, RawString(label))); } @@ -2323,10 +2309,8 @@ impl ExtensionRecord for EncodingRecord { ext.check_size::()?; Ok(Record::Encoding(EncodingRecord( - String::from_utf8(ext.data.clone().into_vec()).map_err(|_| { - Warning::BadEncodingName { - offset: ext.offsets.start, - } + String::from_utf8(ext.data.clone()).map_err(|_| Warning::BadEncodingName { + offset: ext.offsets.start, })?, ))) } @@ -2710,7 +2694,7 @@ pub struct Extension { pub count: u32, /// `size * count` bytes of data. - pub data: Box<[u8]>, + pub data: Vec, } impl Extension { @@ -2757,7 +2741,7 @@ impl Extension { }); }; let start_offset = r.stream_position()?; - let data = read_slice(r, product as usize)?; + let data = read_vec(r, product as usize)?; let end_offset = start_offset + product as u64; let extension = Extension { offsets: start_offset..end_offset, @@ -2949,15 +2933,15 @@ fn read_bytes(r: &mut R) -> Result<[u8; N], IoError> { Ok(buf) } -fn read_slice(r: &mut R, n: usize) -> Result, IoError> { - let mut data = vec![0; n].into_boxed_slice(); - r.read_exact(&mut data)?; - Ok(data) +fn read_vec(r: &mut R, n: usize) -> Result, IoError> { + let mut vec = vec![0; n]; + r.read_exact(&mut vec)?; + Ok(vec) } fn read_string(r: &mut R, endian: Endian) -> Result { let length: u32 = endian.parse(read_bytes(r)?); - Ok(read_slice(r, length as usize)?.into()) + Ok(read_vec(r, length as usize)?.into()) } #[derive(Clone, Debug)] -- 2.30.2