From 7847612c52fe5bc3f6dd7742dae535e40092ca17 Mon Sep 17 00:00:00 2001 From: Jaby Date: Tue, 11 Oct 2022 19:41:23 +0200 Subject: [PATCH] Keep size of names --- src/Tools/psxcdgen_ex/src/types/mod.rs | 32 +++++++++++++++----------- 1 file changed, 18 insertions(+), 14 deletions(-) diff --git a/src/Tools/psxcdgen_ex/src/types/mod.rs b/src/Tools/psxcdgen_ex/src/types/mod.rs index 6ad7d448..8bb0ad2c 100644 --- a/src/Tools/psxcdgen_ex/src/types/mod.rs +++ b/src/Tools/psxcdgen_ex/src/types/mod.rs @@ -98,24 +98,27 @@ impl std::fmt::Display for File { pub struct DirectoryName { name: DString<8>, + len: usize, } impl DirectoryName { pub fn from_str(dir_name: &str) -> Result { let dir_name = dir_name.to_uppercase(); - Ok(DirectoryName{name: DString::from_str(dir_name.as_ref())?}) + Ok(DirectoryName{name: DString::from_str(dir_name.as_ref())?, len: dir_name.len()}) } } impl std::fmt::Display for DirectoryName { fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { - write!(f, "{}", dstring_as_str(&self.name)) + write!(f, "{}", dstring_as_str(&self.name, self.len)) } } pub struct FileName { - name: DString<8>, - ext: Option> + name: DString<8>, + len: usize, + ext: Option>, + ext_len: Option } impl FileName { @@ -125,24 +128,25 @@ impl FileName { let mut sub_str = file_name.split('.'); (Error::ok_or_new(sub_str.next(), || "File name can't be emplty".to_owned())?, sub_str.next()) }; - - Ok(FileName{name: DString::from_str(name)?, ext: { + let (ext, ext_len) = { match ext { - Some(ext) => Some(DString::from_str(ext)?), - None => None - } - }}) + Some(ext) => (Some(DString::from_str(ext)?), Some(ext.len())), + None => (None, None) + } + }; + + Ok(FileName{name: DString::from_str(name)?, len: name.len(), ext, ext_len}) } } impl std::fmt::Display for FileName { fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { if let Some(ext) = &self.ext { - write!(f, "{}.{};1", dstring_as_str(&self.name), dstring_as_str(&ext)) + write!(f, "{}.{};1", dstring_as_str(&self.name, self.len), dstring_as_str(&ext, self.ext_len.unwrap_or(0))) } else { - write!(f, "{}", dstring_as_str(&self.name)) + write!(f, "{}", dstring_as_str(&self.name, self.len)) } } } @@ -159,8 +163,8 @@ impl Default for Properties { } } -fn dstring_as_str(string: &DString) -> &str { - match std::str::from_utf8(string.as_raw()) { +fn dstring_as_str(string: &DString, len: usize) -> &str { + match std::str::from_utf8(&string.as_raw()[0..len]) { Ok(str) => str, Err(_) => "???", }