I am creating a simple iCalendar using C# and found that the Content Folding per Section 4.1 of RFC 2445 to be quite a headache (for me :-).
http://www.apps.ietf.org/rfc/rfc2445.html#sec-4.1
For long lines, you are to escape some characters (backslash, semi-colon, comma and newline, I believe) and then fold it so that no line is longer than 75 octets. I found several straight forward way of doing this on the web. The simplest is to replace the characters in question with escaped version and then insert CRLF at every 75th character. Something like:
// too simple, could break at an escape sequence boundary or multi-byte character may overflow 75 octets
txt = txt.Replace(@"\", "\\\\").Replace(";", "\\;").Replace(",", "\\,").Replace("\r\n", "\\n");
var regex = new System.Text.RegularExpressions.Regex( ".{75}");
var escape_and_folded = regex.Replace( txt, "$0\r\n ");
I see two issues. It’s possible that the CRLF is inserted into an escaped sequence. For example, if insertion occurs such that an escaped new line sequence “\n” becomes “\CRLF” (then the “n” will be on the next line). The second issue is when there are multi-byte characters. Since calculation is per characters it’s possible that the line may become longer than 75 octets.
A simple solution is to walk the string character by character and escape and fold but this seems rather brute force. Does anybody have a more elegant solution?