Example #1
0
        private void WriteAndFold(string value, int charsAlreadyOnLine, bool allowUnicode)
        {
            int lastSpace = 0, startOfLine = 0;

            for (int index = 0; index < value.Length; index++)
            {
                // When we find a FWS (CRLF) copy it as is.
                if (MailBnfHelper.IsFWSAt(value, index)) // At the first char of "\r\n " or "\r\n\t"
                {
                    index += 2;                          // Skip the FWS
                    this.bufferBuilder.Append(value, startOfLine, index - startOfLine, allowUnicode);
                    // Reset for the next line
                    startOfLine        = index;
                    lastSpace          = index;
                    charsAlreadyOnLine = 0;
                }
                // When we pass the line length limit, and know where there was a space to fold at, fold there
                else if (((index - startOfLine) > (this.lineLength - charsAlreadyOnLine)) &&
                         lastSpace != startOfLine)
                {
                    this.bufferBuilder.Append(value, startOfLine, lastSpace - startOfLine, allowUnicode);
                    this.bufferBuilder.Append(CRLF);
                    startOfLine        = lastSpace;
                    charsAlreadyOnLine = 0;
                }
                // Mark a foldable space.  If we go over the line length limit, fold here.
                else if (value[index] == MailBnfHelper.Space || value[index] == MailBnfHelper.Tab)
                {
                    lastSpace = index;
                }
            }
            // Write any remaining data to the buffer.
            if (value.Length - startOfLine > 0)
            {
                this.bufferBuilder.Append(value, startOfLine, value.Length - startOfLine, allowUnicode);
            }
        }