/// <summary> /// Return the EncodedLine Segments in the given segment /// </summary> /// <param name="segment">Segment to parse</param> /// <returns>An enumeration of <see cref="EncodedLine"/></returns> public static IEnumerable <EncodedLine> ReadLines(StringSegment segment) { CharReader reader = new CharReader(segment); int startIndex = reader.Position + 1; int endIndex = startIndex - 1; char ch; while ((ch = reader.Read()) != CharReader.EOF) { switch (ch) { default: endIndex = reader.Position; break; case MimeStandard.CR: if (reader.Read() != MimeStandard.LF) { throw new MimeException(MimeError.InvalidCRLF); } yield return(LineFromSegment(reader.GetSegment(startIndex, endIndex), true)); startIndex = reader.Position + 1; endIndex = reader.Position; break; } } if (endIndex >= startIndex) { yield return(LineFromSegment(reader.GetSegment(startIndex, endIndex), false)); } }
public void CharReaderShouldReadCharacters() { string source = "abc"; CharReader reader = new CharReader(source); Assert.False(reader.IsDone); Assert.Equal('a', reader.Read()); Assert.Equal('b', reader.Read()); Assert.Equal('c', reader.Read()); Assert.Equal(CharReader.EOF, reader.Read()); Assert.True(reader.IsDone); }
/// <summary> /// Advances the start position of <paramref name="text"/> to the first non-whitespace position /// </summary> /// <param name="text">The <see cref="StringSegment"/> potentially containing initial whitepace</param> /// <returns>A <see cref="StringSegment"/> with no intial whitespace (possibly an empty segment if the <paramref name="text"/> was empty or all whitespace)</returns> public static StringSegment SkipWhitespace(StringSegment text) { CharReader reader = new CharReader(text); char ch; while ((ch = reader.Read()) != CharReader.EOF && MimeStandard.IsWhitespace(ch)) { // quick skip } return(new StringSegment(text.Source, reader.Position, text.EndIndex)); }
/// <summary> /// Breaks the supplied <paramref name="entity"/> into constituent lines by CRLF. /// </summary> /// <param name="entity">The entity to parse into lines.</param> /// <returns>An enumeration of <see cref="StringSegment"/> instances, one for each parsed line</returns> public static IEnumerable <StringSegment> ReadLines(StringSegment entity) { CharReader reader = new CharReader(entity); int startIndex = reader.Position + 1; int endIndex = startIndex - 1; char ch; while ((ch = reader.Read()) != CharReader.EOF) { switch (ch) { default: endIndex = reader.Position; break; case MimeStandard.CR: // // RFC 2822 mandates that CRLF be together always // if (reader.Read() != MimeStandard.LF) { throw new MimeException(MimeError.InvalidCRLF); } yield return(reader.GetSegment(startIndex, endIndex)); startIndex = reader.Position + 1; endIndex = reader.Position; break; } } if (endIndex >= 0) { yield return(reader.GetSegment(startIndex, endIndex)); } }
IEnumerable <char> DecodeLine() { m_lineReader = new CharReader(m_currentLine.Text); char ch; while ((ch = m_lineReader.Read()) != CharReader.EOF) { if (ch != EncodingChar) { yield return(ch); } else { char decodedChar = this.DecodeChar(); if (decodedChar != char.MinValue) { yield return(decodedChar); } } } }
/// <summary> /// Return the EncodedLine Segments in the given segment /// </summary> /// <param name="segment">Segment to parse</param> /// <returns>An enumeration of <see cref="EncodedLine"/></returns> public static IEnumerable<EncodedLine> ReadLines(StringSegment segment) { CharReader reader = new CharReader(segment); int startIndex = reader.Position + 1; int endIndex = startIndex - 1; char ch; while ((ch = reader.Read()) != CharReader.EOF) { switch (ch) { default: endIndex = reader.Position; break; case MimeStandard.CR: if (reader.Read() != MimeStandard.LF) { throw new MimeException(MimeError.InvalidCRLF); } yield return LineFromSegment(reader.GetSegment(startIndex, endIndex), true); startIndex = reader.Position + 1; endIndex = reader.Position; break; } } if (endIndex >= startIndex) { yield return LineFromSegment(reader.GetSegment(startIndex, endIndex), false); } }
IEnumerable<char> DecodeLine() { m_lineReader = new CharReader(m_currentLine.Text); char ch; while ((ch = m_lineReader.Read()) != CharReader.EOF) { if (ch != EncodingChar) { yield return ch; } else { char decodedChar = this.DecodeChar(); if (decodedChar != char.MinValue) { yield return decodedChar; } } } }
public void CharReaderKeepsReadingAfterFoundPosition() { string source = "abc:123"; CharReader reader = new CharReader(source); reader.ReadTo(':', false); Assert.Equal('1', reader.Read()); }