diff options
author | Manos Pitsidianakis <el13635@mail.ntua.gr> | 2020-09-16 13:07:26 +0300 |
---|---|---|
committer | Manos Pitsidianakis <el13635@mail.ntua.gr> | 2020-09-16 15:17:48 +0300 |
commit | 3618bdcffba4911a5eb0e4a7a4c846e5445cdaf5 (patch) | |
tree | 02318e8c9eeba557b67554554c5fb4ef6e7e8ff6 /melib/src/email/parser.rs | |
parent | 366e557e1cc96a7f9b12c10f28f088d2ef4712cb (diff) |
melib/imap: treat server input as bytes
Server input was assumed valid ascii and converted haphazardly to &str.
Don't do that, since it might not be valid UTF8.
Diffstat (limited to 'melib/src/email/parser.rs')
-rw-r--r-- | melib/src/email/parser.rs | 21 |
1 files changed, 17 insertions, 4 deletions
diff --git a/melib/src/email/parser.rs b/melib/src/email/parser.rs index bbb79e53..4eee7610 100644 --- a/melib/src/email/parser.rs +++ b/melib/src/email/parser.rs @@ -177,9 +177,19 @@ macro_rules! is_whitespace { pub trait BytesExt { fn rtrim(&self) -> &Self; fn ltrim(&self) -> &Self; + fn trim_start(&self) -> &Self { + self.ltrim() + } + fn trim_end(&self) -> &Self { + self.rtrim() + } fn trim(&self) -> &Self; - fn find(&self, needle: &[u8]) -> Option<usize>; - fn rfind(&self, needle: &[u8]) -> Option<usize>; + fn find<T: AsRef<[u8]>>(&self, needle: T) -> Option<usize>; + fn contains_subsequence<T: AsRef<[u8]>>(&self, needle: T) -> bool { + self.find(needle.as_ref()).is_some() + } + + fn rfind<T: AsRef<[u8]>>(&self, needle: T) -> Option<usize>; fn replace(&self, from: &[u8], to: &[u8]) -> Vec<u8>; fn is_quoted(&self) -> bool; } @@ -202,8 +212,10 @@ impl BytesExt for [u8] { fn trim(&self) -> &[u8] { self.rtrim().ltrim() } + // https://stackoverflow.com/a/35907071 - fn find(&self, needle: &[u8]) -> Option<usize> { + fn find<T: AsRef<[u8]>>(&self, needle: T) -> Option<usize> { + let needle = needle.as_ref(); if needle.is_empty() { return None; } @@ -211,7 +223,8 @@ impl BytesExt for [u8] { .position(|window| window == needle) } - fn rfind(&self, needle: &[u8]) -> Option<usize> { + fn rfind<T: AsRef<[u8]>>(&self, needle: T) -> Option<usize> { + let needle = needle.as_ref(); if needle.is_empty() { return None; } |