diff options
author | Manos Pitsidianakis <el13635@mail.ntua.gr> | 2018-08-05 12:14:26 +0300 |
---|---|---|
committer | Manos Pitsidianakis <el13635@mail.ntua.gr> | 2019-06-10 19:40:26 +0300 |
commit | 63ce729205f4063112ebdd9ba5bdef858f1028c9 (patch) | |
tree | 018ca62b73c9d3031fe1751de318508043aaf538 | |
parent | e91f22cb4f1c2324847e7895d25e6a5446929cd0 (diff) |
Optimize some parser stuff
-rw-r--r-- | melib/src/mailbox/email/parser.rs | 51 | ||||
-rw-r--r-- | melib/src/mailbox/thread.rs | 4 | ||||
-rw-r--r-- | src/bin.rs | 5 |
3 files changed, 27 insertions, 33 deletions
diff --git a/melib/src/mailbox/email/parser.rs b/melib/src/mailbox/email/parser.rs index 08baa6ed..b383ac10 100644 --- a/melib/src/mailbox/email/parser.rs +++ b/melib/src/mailbox/email/parser.rs @@ -28,6 +28,15 @@ use nom::{ErrorKind, IResult, Needed}; use std; use std::str::from_utf8; +macro_rules! is_whitespace { + ($var:ident) => { + $var == b' ' && $var == b'\t' && $var == b'\n' && $var == b'\r' + }; + ($var:expr) => { + $var == b' ' && $var == b'\t' && $var == b'\n' && $var == b'\r' + }; +} + pub trait BytesExt { fn trim(&self) -> &Self; fn find(&self, needle: &[u8]) -> Option<usize>; @@ -36,16 +45,8 @@ pub trait BytesExt { impl BytesExt for [u8] { fn trim(&self) -> &[u8] { - fn is_whitespace(c: &u8) -> bool { - *c == b'\t' || *c == b' ' - } - - fn is_not_whitespace(c: &u8) -> bool { - !is_whitespace(c) - } - - if let Some(first) = self.iter().position(is_not_whitespace) { - if let Some(last) = self.iter().rposition(is_not_whitespace) { + if let Some(first) = self.iter().position(|b| !is_whitespace!(*b)) { + if let Some(last) = self.iter().rposition(|b| !is_whitespace!(*b)) { &self[first..last + 1] } else { unreachable!(); @@ -67,17 +68,9 @@ impl BytesExt for [u8] { } } -macro_rules! is_whitespace { - ($var:ident) => { - $var == b' ' && $var == b'\t' && $var == b'\n' && $var == b'\r' - }; - ($var:expr) => { - $var == b' ' && $var == b'\t' && $var == b'\n' && $var == b'\r' - }; -} fn quoted_printable_byte(input: &[u8]) -> IResult<&[u8], u8> { - if input.is_empty() || input.len() < 3 { + if input.len() < 3 { IResult::Incomplete(Needed::Size(1)) } else if input[0] == b'=' && is_hex_digit(input[1]) && is_hex_digit(input[2]) { let a = if input[1] < b':' { @@ -111,21 +104,17 @@ fn quoted_printable_byte(input: &[u8]) -> IResult<&[u8], u8> { */ fn header_value(input: &[u8]) -> IResult<&[u8], &[u8]> { - if input.is_empty() || input[0] == b'\n' { - IResult::Incomplete(Needed::Unknown) - } else { - let input_len = input.len(); - for (i, x) in input.iter().enumerate() { - if *x == b'\n' { - if (i + 1) < input_len && input[i + 1] != b' ' && input[i + 1] != b'\t' { - return IResult::Done(&input[(i + 1)..], &input[0..i]); - } else if i + 1 == input_len { - return IResult::Done(&input[(i + 1)..], &input[0..i]); - } + let input_len = input.len(); + for (i, x) in input.iter().enumerate() { + if *x == b'\n' { + if (i + 1) < input_len && input[i + 1] != b' ' && input[i + 1] != b'\t' { + return IResult::Done(&input[(i + 1)..], &input[0..i]); + } else if i + 1 == input_len { + return IResult::Done(&input[(i + 1)..], &input[0..i]); } } - IResult::Error(error_code!(ErrorKind::Custom(43))) } + IResult::Incomplete(Needed::Unknown) } /* Parse the name part of the header -> &str */ diff --git a/melib/src/mailbox/thread.rs b/melib/src/mailbox/thread.rs index 2803886d..d5a01d41 100644 --- a/melib/src/mailbox/thread.rs +++ b/melib/src/mailbox/thread.rs @@ -372,8 +372,8 @@ pub fn build_threads( collection: &[Envelope], ) { let thread = threads[i]; - if threads[root_subject_idx].has_message() { - let root_subject = collection[threads[root_subject_idx].message().unwrap()].subject(); + if let Some(msg_idx) = threads[root_subject_idx].message() { + let root_subject = collection[msg_idx].subject(); /* If the Container has no Message, but does have children, remove this container but * promote its children to this level (that is, splice them in to the current child * list.) */ @@ -24,6 +24,11 @@ The mail handling stuff is done in the `melib` crate which includes all backend needs. The split is done to theoretically be able to create different frontends with the same innards. */ +use std::alloc::System; + +#[global_allocator] +static GLOBAL: System = System; + extern crate melib; extern crate ui; |