fix mac_roman_cstr API (should never fail)

png-branch
an 2019-03-12 03:38:06 -04:00
parent 49b4b56aea
commit 745f32f3bd
5 changed files with 13 additions and 11 deletions

View File

@ -18,6 +18,7 @@ members = ["source/leela", "source/tycho"]
bitflags = "1.0" bitflags = "1.0"
failure = {version = "0.1", features = ["std"]} failure = {version = "0.1", features = ["std"]}
serde = {version = "1.0", features = ["derive"]} serde = {version = "1.0", features = ["derive"]}
memchr = "2.0"
[profile.dev] [profile.dev]
opt-level = 1 opt-level = 1

View File

@ -1,7 +1,5 @@
//! Text conversion utilities. //! Text conversion utilities.
use crate::durandal::err::*;
/// Formats a binary size string for any given number. /// Formats a binary size string for any given number.
/// ///
/// # Examples /// # Examples
@ -103,14 +101,17 @@ pub fn mac_roman_conv(s: &[u8]) -> String
/// ``` /// ```
/// use maraiah::durandal::text::mac_roman_cstr; /// use maraiah::durandal::text::mac_roman_cstr;
/// ///
/// assert_eq!(mac_roman_cstr(b"I\xd5ve awaken\0ed").unwrap(), "Ive awaken"); /// assert_eq!(mac_roman_cstr(b"I\xd5ve awaken\0ed"), "Ive awaken");
/// assert_eq!(mac_roman_cstr(b"I\xd5ve awaken\0"), "Ive awaken");
/// assert_eq!(mac_roman_cstr(b"I\xd5ve awaken"), "Ive awaken");
/// ``` /// ```
pub fn mac_roman_cstr(s: &[u8]) -> ResultS<String> #[inline]
pub fn mac_roman_cstr(s: &[u8]) -> String
{ {
if let Some(s) = s.split(|&n| n == 0).nth(0) { if let Some(n) = memchr::memchr(0, s) {
Ok(mac_roman_conv(s)) mac_roman_conv(&s[..n])
} else { } else {
bail!("no null in C string"); mac_roman_conv(s)
} }
} }

View File

@ -55,7 +55,7 @@ pub fn read_minf(b: &[u8]) -> ResultS<Minf>
skypict_id = u16[4]; skypict_id = u16[4];
miss_flags = u16[6]; miss_flags = u16[6];
envi_flags = u16[8]; envi_flags = u16[8];
level_name = mac_roman_cstr[18..84]; level_name = mac_roman_cstr[18..84] no_try;
entr_flags = u32[84]; entr_flags = u32[84];
} }
@ -425,7 +425,7 @@ pub fn read_note(b: &[u8]) -> ResultS<(Note, usize)>
72, BE in b => 72, BE in b =>
pos = read_point[2..6]; pos = read_point[2..6];
poly = u16[6]; poly = u16[6];
text = mac_roman_cstr[8..72]; text = mac_roman_cstr[8..72] no_try;
} }
Ok((Note{pos, poly, text}, 72)) Ok((Note{pos, poly, text}, 72))

View File

@ -83,7 +83,7 @@ pub fn read_term(b: &[u8]) -> ResultS<(Terminal, usize)>
let beg = grp.beg; let beg = grp.beg;
let len = grp.len; let len = grp.len;
let text = ok!(text.get(beg..beg + len), "bad offset")?; let text = ok!(text.get(beg..beg + len), "bad offset")?;
let text = mac_roman_cstr(text)?; let text = mac_roman_cstr(text);
groups.push(Group{flags, ttype, lines, text}); groups.push(Group{flags, ttype, lines, text});
} }

View File

@ -105,7 +105,7 @@ pub fn read_wad(b: &[u8]) -> ResultS<Wad>
128, BE in b => 128, BE in b =>
ver_wad = u16[0]; ver_wad = u16[0];
ver_dat = u16[2]; ver_dat = u16[2];
name = mac_roman_cstr[4..68]; name = mac_roman_cstr[4..68] no_try;
siz_app = u16[78] usize; siz_app = u16[78] usize;
siz_wcnk = u16[80] usize; siz_wcnk = u16[80] usize;
siz_went = u16[82] usize; siz_went = u16[82] usize;