Keep size of names

This commit is contained in:
jaby 2022-10-11 19:41:23 +02:00
parent 48e9a87882
commit 3dfa9871c2
1 changed files with 18 additions and 14 deletions

View File

@ -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<DirectoryName, Error> {
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<DString<3>>
name: DString<8>,
len: usize,
ext: Option<DString<3>>,
ext_len: Option<usize>
}
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<const SIZE: usize>(string: &DString<SIZE>) -> &str {
match std::str::from_utf8(string.as_raw()) {
fn dstring_as_str<const SIZE: usize>(string: &DString<SIZE>, len: usize) -> &str {
match std::str::from_utf8(&string.as_raw()[0..len]) {
Ok(str) => str,
Err(_) => "???",
}