Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 16 additions & 16 deletions src/descriptors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<char>), 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<Option<(usize, char)>, 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
Expand All @@ -36,9 +38,9 @@ impl<'a> TryFrom<Cow<'a, str>> 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"),
}
}
Expand Down Expand Up @@ -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"),
}
}
Expand Down