Skip to content

Commit

Permalink
fix(rfc2047): replace broken ccaf5a7 fix with workaround
Browse files Browse the repository at this point in the history
  • Loading branch information
paolobarbolini committed Mar 27, 2024
1 parent 4a97a9d commit c128b46
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 19 deletions.
21 changes: 15 additions & 6 deletions src/headers/rfc2047.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,18 +28,27 @@ const ENCODING_END_SUFFIX: &str = "?=";
/// # }
/// ```
pub fn encode(mut s: &str, w: &mut EmailWriter<'_>) -> fmt::Result {
let mut wrote = false;

while !s.is_empty() {
let remaining_line_len = MAX_LINE_LEN.saturating_sub(
ENCODING_START_PREFIX.len() + ENCODING_END_SUFFIX.len() + w.line_len() + "\r\n".len(),
);
let unencoded_remaining_line_len = remaining_line_len / 4 * 3;

let word = utils::truncate_to_char_boundary(s, unencoded_remaining_line_len.min(s.len()));

let mut word =
utils::truncate_to_char_boundary(s, unencoded_remaining_line_len.min(s.len()));
if word.is_empty() {
// No space remaining on this line, go to a new one
w.new_line_and_space()?;
continue;
if wrote {
// No space remaining on this line, go to a new one
w.new_line()?;
w.space();
continue;
}

// No space remaining, but going to a new line will require us
// to introduce a new space, which will mess up things even more.
word = &s[..s.chars().next().unwrap().len_utf8()];
}

// Write the prefix
Expand All @@ -55,8 +64,8 @@ pub fn encode(mut s: &str, w: &mut EmailWriter<'_>) -> fmt::Result {
// Write the suffix
w.write_str(ENCODING_END_SUFFIX)?;

// Advance `s`
s = &s[word.len()..];
wrote = true;
}

Ok(())
Expand Down
19 changes: 6 additions & 13 deletions src/headers/writer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -53,17 +53,6 @@ impl<'a> EmailWriter<'a> {
Ok(())
}

/// Equivalent to calling `new_line()` and `space()` consecutively.
pub(crate) fn new_line_and_space(&mut self) -> fmt::Result {
self.spaces = 0;
self.writer.write_str("\r\n ")?;
self.line_len = 1;
self.optional_breakpoint = false;
self.can_go_to_new_line_now = false;

Ok(())
}

#[cfg(not(tarpaulin_include))]
#[doc(hidden)]
#[deprecated(note = "Renamed to `new_line`", since = "0.1.2")]
Expand All @@ -76,6 +65,10 @@ impl<'a> EmailWriter<'a> {
self.spaces += 1;
}

pub(super) fn has_spaces(&self) -> bool {
self.spaces > 0
}

/// Write a space which won't be printed if the line wraps.
///
/// This method shouldn't be called multiple times consecutively,
Expand Down Expand Up @@ -367,8 +360,8 @@ mod tests {
assert_eq!(
s,
concat!(
"Subject: AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA BBBBBBBBBBBBB\r\n",
" =?utf-8?b?c8OpbGVjdGlvbg==?=",
"Subject: AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA BBBBBBBBBBBBB =?utf-8?b?cw==?=\r\n",
" =?utf-8?b?w6lsZWN0aW9u?=",
)
);
}
Expand Down

0 comments on commit c128b46

Please sign in to comment.