Skip to content
This repository has been archived by the owner on Aug 17, 2024. It is now read-only.

replaced split_line with a multibyte aware version #61

Merged
merged 5 commits into from
Mar 1, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 19 additions & 7 deletions src/generator/ical.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,14 @@ fn get_value(value: &Option<String>) -> String {
}

pub(crate) fn split_line<T: Into<String>>(str: T) -> String {
let mut str = str.into();
let mut x = 75;
while x < str.len() {
str.insert_str(x, "\r\n ");
x += 77;
}
str
let str = str.into();
let mut chars = str.chars();
let mut first = true;
let sub_string = (0..)
.map(|_| chars.by_ref().take(if first { first = false; 75 } else { 74 }).collect::<String>())
.take_while(|s| !s.is_empty())
.collect::<Vec<_>>();
sub_string.join("\r\n ")
}

//
Expand Down Expand Up @@ -85,6 +86,17 @@ mod should {
assert_eq!(text, split_line(text.replace("\r\n ", "")));
}

#[test]
fn split_long_line_multibyte() {
// the following text includes multibyte characters (UTF-8) at strategic places to ensure
// split_line would panic if not multibyte aware
let text = "DESCRIPTION:ABCDEFGHIJ\\n\\nKLMNOPQRSTUVWXYZ123456789üABCDEFGHIJKLMNOPQRS\\n\\n\r\n \
TUVWXYZ123456ä7890ABCDEFGHIJKLM\\n\\nNOPQRSTUVWXYZ1234567890ABCDEFGHIJKLMNOP\r\n \
QRSTUVWXöYZ1234567890ABCDEFGHIJKLMNOPQRSTUVWX\\n\\nYZ1234567890abcdefghiÜjkl\r\n \
m\\nnopqrstuvwx";
assert_eq!(text, split_line(text.replace("\r\n ", "")));
}

#[test]
fn protect_chars_in_params() {
assert_eq!(
Expand Down
Loading