Skip to content

Commit 7943380

Browse files
committed
dir/gen3: factor out common function to build name strings
Signed-off-by: Daniel Maslowski <[email protected]>
1 parent 05d5342 commit 7943380

File tree

1 file changed

+15
-14
lines changed

1 file changed

+15
-14
lines changed

src/dir/gen3.rs

Lines changed: 15 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,19 @@ pub const CPD_MAGIC_BYTES: &[u8] = CPD_MAGIC.as_bytes();
2323

2424
const FOUR_K: usize = 0x1000;
2525

26+
fn stringify_vec(v: Vec<u8>) -> String {
27+
v.iter().map(|b| format!("{b:02x}")).collect::<String>()
28+
}
29+
30+
fn u8_slice_name_to_str(n: &[u8]) -> String {
31+
match std::str::from_utf8(n) {
32+
// some names are padded with 0x0
33+
Ok(n) => n.trim_end_matches('\0').to_string(),
34+
// sometimes, there is no real name
35+
Err(_) => stringify_vec(n.to_vec()),
36+
}
37+
}
38+
2639
// see <https://troopers.de/downloads/troopers17/TR17_ME11_Static.pdf>
2740
#[derive(IntoBytes, FromBytes, Serialize, Deserialize, Clone, Copy, Debug)]
2841
#[repr(C, packed)]
@@ -37,12 +50,7 @@ pub struct CPDHeader {
3750

3851
impl CPDHeader {
3952
pub fn name(&self) -> String {
40-
let n = self.part_name;
41-
match std::str::from_utf8(&n) {
42-
// some names are shorter than 4 bytes and padded with 0x0
43-
Ok(n) => n.trim_end_matches('\0').to_string(),
44-
Err(_) => format!("{n:02x?}"),
45-
}
53+
u8_slice_name_to_str(&self.part_name)
4654
}
4755
}
4856

@@ -71,10 +79,7 @@ pub struct CPDEntry {
7179

7280
impl CPDEntry {
7381
pub fn name(&self) -> String {
74-
match std::str::from_utf8(&self.name) {
75-
Ok(n) => n.trim_end_matches('\0').to_string(),
76-
Err(_) => format!("{:02x?}", &self.name),
77-
}
82+
u8_slice_name_to_str(&self.name)
7883
}
7984
}
8085

@@ -111,10 +116,6 @@ pub struct CodePartitionDirectory {
111116
pub name: String,
112117
}
113118

114-
fn stringify_vec(v: Vec<u8>) -> String {
115-
v.iter().map(|b| format!("{b:02x}")).collect::<String>()
116-
}
117-
118119
impl Display for CodePartitionDirectory {
119120
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
120121
let checksum = self.header.version_or_checksum;

0 commit comments

Comments
 (0)