diff --git a/src/descriptors.rs b/src/descriptors.rs index eadf427..ab9e6c6 100644 --- a/src/descriptors.rs +++ b/src/descriptors.rs @@ -7,20 +7,22 @@ use std::{ use crate::ParseError; -// Returns the unqualified segment and the following char ('/', ';', or None) -// or an error. This only extracts the unqualified segment at the start of -// the given data, and ignores anything following. -fn parse_unqualified_segment(data: &str) -> Result<(&str, Option), ParseError> { +// Returns the length of the unqualified segment and the following char +// ('/', ';', or None) or an error. This only extracts the unqualified +// segment at the start of the given data, and ignores anything following. +// Returns None if no unqualified segment terminator was found. +#[inline(always)] +fn parse_unqualified_segment(data: &str) -> Result, ParseError> { for (ix, c) in data.char_indices() { match c { '/' if ix == 0 => fail!("Unexpected '/' at start of unqualified segment"), ';' if ix == 0 => fail!("Unexpected ';' at start of unqualified segment"), - '/' | ';' => return Ok((&data[0..ix], Some(c))), + '/' | ';' => return Ok(Some((ix, c))), '.' | '[' | '<' | '>' => fail!("Disallowed character in unqualified segment"), _ => (), }; } - Ok((data, None)) + Ok(None) } /// Represents a valid binary class or interface name in the syntax of @@ -36,9 +38,9 @@ impl<'a> TryFrom> for ClassName<'a> { let mut index = 0; loop { match parse_unqualified_segment(&value[index..])? { - (_, None) => break, - (_, Some(';')) => fail!("Disallowed ';' in class name"), - (segment, Some('/')) => index += segment.len() + 1, + None => break, + Some((_, ';')) => fail!("Disallowed ';' in class name"), + Some((segment_len, '/')) => index += segment_len + 1, _ => panic!("Got unexpected return value from parse_unqualified_segment"), } } @@ -81,18 +83,16 @@ fn parse_class_descriptor<'a>( let mut next_index = index; loop { match parse_unqualified_segment(&data[next_index..])? { - (segment, Some(';')) => { + Some((segment_len, ';')) => { return Ok(ClassName(match data { - Cow::Borrowed(data) => { - Cow::Borrowed(&data[index..(next_index + segment.len())]) - } + Cow::Borrowed(data) => Cow::Borrowed(&data[index..(next_index + segment_len)]), Cow::Owned(data) => { - Cow::Owned(data[index..(next_index + segment.len())].to_string()) + Cow::Owned(data[index..(next_index + segment_len)].to_string()) } })) } - (segment, Some('/')) => next_index += segment.len() + 1, - (_, None) => fail!("Unterminated unqualified segment"), + Some((segment_len, '/')) => next_index += segment_len + 1, + None => fail!("Unterminated unqualified segment"), _ => panic!("Got unexpected return value from parse_unqualified_segment"), } }