From 116962a3b059323a3214dcad040997adef3c5bb7 Mon Sep 17 00:00:00 2001 From: Harald Eilertsen Date: Mon, 7 Dec 2020 14:08:50 +0100 Subject: Optimize cmstring parser a bit. Stop parsing at terminating null instead of searching for it afterwards. --- src/main.rs | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) (limited to 'src') 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) } /** -- cgit v1.2.3