parser: fix panic on invalid message id
parent
f2ecb81612
commit
5981f98f17
|
@ -95,12 +95,18 @@ impl Address {
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn get_tags(&self, separator: char) -> Vec<String> {
|
pub fn get_tags(&self, separator: char) -> Vec<String> {
|
||||||
let mut ret = Vec::new();
|
|
||||||
let email = self.get_email();
|
let email = self.get_email();
|
||||||
let at_pos = email.as_bytes().iter().position(|&b| b == b'@').unwrap();
|
let at_pos = email
|
||||||
|
.as_bytes()
|
||||||
|
.iter()
|
||||||
|
.position(|&b| b == b'@')
|
||||||
|
.unwrap_or(0);
|
||||||
let email: &str = email[..at_pos].into();
|
let email: &str = email[..at_pos].into();
|
||||||
ret.extend(email.split(separator).skip(1).map(str::to_string));
|
email
|
||||||
ret
|
.split(separator)
|
||||||
|
.skip(1)
|
||||||
|
.map(str::to_string)
|
||||||
|
.collect::<_>()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -190,7 +196,7 @@ pub struct MessageID(pub Vec<u8>, pub StrBuilder);
|
||||||
|
|
||||||
impl StrBuild for MessageID {
|
impl StrBuild for MessageID {
|
||||||
fn new(string: &[u8], slice: &[u8]) -> Self {
|
fn new(string: &[u8], slice: &[u8]) -> Self {
|
||||||
let offset = string.find(slice).unwrap();
|
let offset = string.find(slice).unwrap_or(0);
|
||||||
MessageID(
|
MessageID(
|
||||||
string.to_owned(),
|
string.to_owned(),
|
||||||
StrBuilder {
|
StrBuilder {
|
||||||
|
|
|
@ -77,11 +77,17 @@ impl BytesExt for [u8] {
|
||||||
}
|
}
|
||||||
// https://stackoverflow.com/a/35907071
|
// https://stackoverflow.com/a/35907071
|
||||||
fn find(&self, needle: &[u8]) -> Option<usize> {
|
fn find(&self, needle: &[u8]) -> Option<usize> {
|
||||||
|
if needle.is_empty() {
|
||||||
|
return None;
|
||||||
|
}
|
||||||
self.windows(needle.len())
|
self.windows(needle.len())
|
||||||
.position(|window| window == needle)
|
.position(|window| window == needle)
|
||||||
}
|
}
|
||||||
|
|
||||||
fn rfind(&self, needle: &[u8]) -> Option<usize> {
|
fn rfind(&self, needle: &[u8]) -> Option<usize> {
|
||||||
|
if needle.is_empty() {
|
||||||
|
return None;
|
||||||
|
}
|
||||||
self.windows(needle.len())
|
self.windows(needle.len())
|
||||||
.rposition(|window| window == needle)
|
.rposition(|window| window == needle)
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue