Exemplo n.º 1
0
 public void CharReaderFromSegment()
 {
     var segment = new StringSegment(TestContent);
     var reader = new CharReader(segment);
     Assert.False(reader.IsDone);
     Assert.Equal(-1, reader.Position);
 }
Exemplo n.º 2
0
 public void CharReaderShouldReadToCharacter()
 {
     StringSegment source = new StringSegment("abc:123");
     CharReader reader = new CharReader(source);
     reader.ReadTo(':', false);
     Assert.Equal(3, reader.Position);
 }
Exemplo n.º 3
0
        public void CharReaderShouldSkipEscape()
        {
            string source = "a\\:c:123";
            CharReader reader = new CharReader(source);

            reader.ReadTo(':', true);
            Assert.Equal(4, reader.Position);
        }
Exemplo n.º 4
0
        public void CharReaderNotAtEndIfFindsCharacter()
        {
            string source = "abc:123";
            CharReader reader = new CharReader(source);

            bool found = reader.ReadTo(':', false);
            Assert.True(found);
            Assert.False(reader.IsDone);
        }
Exemplo n.º 5
0
 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);
 }
Exemplo n.º 6
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.º 7
0
        static string ReadValue(ref CharReader reader)
        {
            int startAt = reader.Position + 1;
            int endAt;

            reader.ReadTo(MimeFieldParameters.NameValueSeparator, true, MailStandard.DQUOTE);
            // We're forgiving here
            if (reader.Current() == MailStandard.DQUOTE)
            {
                startAt++;
                endAt = reader.Position - 1;
            }
            else
            {
                endAt = reader.Position;
            }
            string value = reader.Substring(startAt, endAt);

            if (string.IsNullOrEmpty(value))
            {
                return(string.Empty);
            }
            return(UnescapeQuotes(value));
        }
Exemplo n.º 8
0
        /// <summary>
        /// Same as Split above, except automatically consumes quoted sections
        /// </summary>
        internal static IEnumerable <StringSegment> Split(StringSegment source, char separator, char quoteChar)
        {
            if (source.IsNull || source.IsEmpty)
            {
                yield break;
            }

            int        startAt = source.StartIndex;
            CharReader reader  = new CharReader(source);

            while (reader.ReadTo(separator, true, quoteChar))
            {
                yield return(new StringSegment(source.Source, startAt, reader.Position - 1)); // STRUCTS - fast

                startAt = reader.Position + 1;
            }

            StringSegment last = new StringSegment(source.Source, startAt, reader.Position);

            if (!last.IsEmpty)
            {
                yield return(last);
            }
        }
Exemplo n.º 9
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.º 10
0
 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;
             }
         }
     }
 }
Exemplo n.º 11
0
        public void CharReaderShouldBeAtEndIfItDoesNotFindChar()
        {
            string source = "abc:123";
            CharReader reader = new CharReader(source);

            bool found = reader.ReadTo('?', false);
            Assert.False(found);
            Assert.True(reader.IsDone);
        }
Exemplo n.º 12
0
 public void ParseQuotedFail(string source)
 {
     CharReader reader = new CharReader(source);
     Assert.False(reader.ReadTo(':', true, '"'));
 }
Exemplo n.º 13
0
 public void ParseQuotedSuccess(string source)
 {
     CharReader reader = new CharReader(source);
     Assert.True(reader.ReadTo(':', true, '"'));
     Assert.Equal("123", reader.GetRemainder().ToString());
 }
Exemplo n.º 14
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());
 }
Exemplo n.º 15
0
 public void CharReaderKeepsReadingAfterFoundPosition()
 {
     string source = "abc:123";
     CharReader reader = new CharReader(source);
     reader.ReadTo(':', false);
     Assert.Equal('1', reader.Read());
 }