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

            reader.ReadTo(':', true);
            Assert.Equal(4, reader.Position);
        }
Exemple #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);
        }
Exemple #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);
 }
Exemple #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));
            }
        }
        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));
        }
        /// <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);
            }
        }
        /// <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;
             }
         }
     }
 }
Exemple #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);
        }
Exemple #12
0
 public void ParseQuotedFail(string source)
 {
     CharReader reader = new CharReader(source);
     Assert.False(reader.ReadTo(':', true, '"'));
 }
Exemple #13
0
 public void ParseQuotedSuccess(string source)
 {
     CharReader reader = new CharReader(source);
     Assert.True(reader.ReadTo(':', true, '"'));
     Assert.Equal("123", reader.GetRemainder().ToString());
 }
Exemple #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());
 }
Exemple #15
0
 public void CharReaderKeepsReadingAfterFoundPosition()
 {
     string source = "abc:123";
     CharReader reader = new CharReader(source);
     reader.ReadTo(':', false);
     Assert.Equal('1', reader.Read());
 }