diff options
author | Harald Eilertsen <haraldei@anduin.net> | 2020-12-07 14:08:50 +0100 |
---|---|---|
committer | Harald Eilertsen <haraldei@anduin.net> | 2020-12-07 14:08:50 +0100 |
commit | 116962a3b059323a3214dcad040997adef3c5bb7 (patch) | |
tree | e795a7ad39bd9bddf58dd1b32f742d338db704b0 | |
parent | af057f48f833744b9d20eb536e220b6df247f7e7 (diff) | |
download | cbconv-116962a3b059323a3214dcad040997adef3c5bb7.tar.gz cbconv-116962a3b059323a3214dcad040997adef3c5bb7.tar.bz2 cbconv-116962a3b059323a3214dcad040997adef3c5bb7.zip |
Optimize cmstring parser a bit.
Stop parsing at terminating null instead of searching for it afterwards.
-rw-r--r-- | src/main.rs | 10 |
1 files changed, 6 insertions, 4 deletions
diff --git a/src/main.rs b/src/main.rs index 6eb105b..dfeea93 100644 --- a/src/main.rs +++ b/src/main.rs @@ -21,7 +21,8 @@ use cubase_project::*; use nom::{ bytes::complete::*, - multi::{length_data, many1}, + combinator::map, + multi::{length_data, length_value, many1}, number::complete::*, sequence::tuple, Finish, @@ -37,10 +38,11 @@ use std::{ /// Cubase uses a string format where the length of the string is given as a 32 bit big endian word /// before the actual string data. The string may also be zero terminated, with the zero term char /// included in the length. In that case, we trim off the ending zero bytes. +/// +/// We assume strings to be UTF-8 encoded. fn cmstring(data: &[u8]) -> IResult<&[u8], &str> { - let (r, s) = length_data(be_u32)(data)?; - let end = s.iter().rposition(|b| b != &0u8).unwrap() + 1; - Ok((r, std::str::from_utf8(&s[0..end]).unwrap())) + let strdata = length_value(be_u32, take_till(|c| c == b'\0')); + map(strdata, |s| std::str::from_utf8(s).unwrap())(data) } /** |