Skip to content

Commit

Permalink
fix: take added space into account when wrapping lines
Browse files Browse the repository at this point in the history
Up until now the folding did not correctly adhere to [RFC5545 3.1](https://datatracker.ietf.org/doc/html/rfc5545#section-3.1).
  • Loading branch information
marcantoinem committed Aug 25, 2024
1 parent e759a8f commit e455add
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 7 deletions.
11 changes: 6 additions & 5 deletions src/properties.rs
Original file line number Diff line number Diff line change
Expand Up @@ -381,14 +381,15 @@ impl From<chrono::Duration> for Property {
// Fold a content line as described in RFC 5545, Section 3.1
#[allow(clippy::indexing_slicing)]
pub(crate) fn fold_line(line: &str) -> String {
let limit = 75;
const LIMIT: usize = 75;
let len = line.len();
let mut ret = String::with_capacity(len + (len / limit * 3));
let mut ret = String::with_capacity(len + (len / LIMIT * 3));
let mut bytes_remaining = len;

let mut pos = 0;
let mut next_pos = limit;
while bytes_remaining > limit {
let mut next_pos = LIMIT;

while bytes_remaining > LIMIT {
let pos_is_whitespace = |line: &str, next_pos| {
line.chars()
.nth(next_pos)
Expand All @@ -410,7 +411,7 @@ pub(crate) fn fold_line(line: &str) -> String {

bytes_remaining -= next_pos - pos;
pos = next_pos;
next_pos += limit;
next_pos += LIMIT - 1;
}

ret.push_str(&line[len - bytes_remaining..]);
Expand Down
4 changes: 2 additions & 2 deletions tests/reserialize.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,8 @@ ACTION:EMAIL\r
ATTENDEE:\"mailto:[email protected]\"\r
SUMMARY:*** REMINDER: SEND AGENDA FOR WEEKLY STAFF MEETING ***\r
DESCRIPTION:A draft agenda needs to be sent out to the attendees to the wee\r
kly managers meeting (MGR-LIST). Attached is a pointer the document templat\r
e for the agenda file.\r
kly managers meeting (MGR-LIST). Attached is a pointer the document templa\r
te for the agenda file.\r
ATTACH;FMTTYPE=application/msword:http://example.com/templates/agenda.doc\r
END:VALARM\r
END:VCALENDAR\r
Expand Down

0 comments on commit e455add

Please sign in to comment.