Exemplo n.º 1
0
        /// <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));
            }
        }
Exemplo n.º 2
0
        /// <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));
            }
        }
Exemplo n.º 3
0
        /// <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);
            }            
        }
Exemplo n.º 4
0
 public void GetSegmentShouldReturnSegment()
 {
     string source = "abc:123";
     CharReader reader = new CharReader(source);
     Assert.Equal("abc", reader.GetSegment(0, 2).ToString());
     Assert.Equal("123", reader.GetSegment(4, 6).ToString());
 }