public void CharReaderShouldReadToCharacter() { StringSegment source = new StringSegment("abc:123"); CharReader reader = new CharReader(source); reader.ReadTo(':', false); Assert.Equal(3, reader.Position); }
public void CharReaderShouldSkipEscape() { string source = "a\\:c:123"; CharReader reader = new CharReader(source); reader.ReadTo(':', true); Assert.Equal(4, reader.Position); }
public void CharReaderNotAtEndIfFindsCharacter() { string source = "abc:123"; CharReader reader = new CharReader(source); bool found = reader.ReadTo(':', false); Assert.True(found); Assert.False(reader.IsDone); }
/// <summary> /// Returns the first index of the supplied <paramref name="ch"/> <c>char</c> in <paramref name="text"/> /// </summary> /// <param name="text">The <see cref="string"/> to search</param> /// <param name="ch">The <c>char</c> to test for</param> /// <returns>The zero-based index of the first instance of <paramref name="ch"/>, or -1 if no instances found/</returns> public static int IndexOf(string text, char ch) { CharReader reader = new CharReader(text); if (reader.ReadTo(ch, false)) { return(reader.Position); } return(-1); }
/// <summary> /// Read name value parameters from the given segment /// </summary> /// <param name="parameterText"></param> /// <returns></returns> public static KeyValuePair <string, string> ReadNameValue(StringSegment parameterText) { CharReader reader = new CharReader(parameterText); // Struct. Cheap int nameStartAt = parameterText.StartIndex; string name; string value; if (!reader.ReadTo(MimeFieldParameters.NameValueSeparator, true)) { // We ran out of segment. Treat the entire segment as a value with no name value = reader.Substring(nameStartAt, reader.Position); return(new KeyValuePair <string, string>(string.Empty, value)); } name = reader.Substring(nameStartAt, reader.Position - 1); if (string.IsNullOrEmpty(name)) { throw new MimeException(MimeError.InvalidFieldParameter); } value = ReadValue(ref reader); return(new KeyValuePair <string, string>(name.TrimStart(), value)); }
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); } }
public void CharReaderShouldBeAtEndIfItDoesNotFindChar() { string source = "abc:123"; CharReader reader = new CharReader(source); bool found = reader.ReadTo('?', false); Assert.False(found); Assert.True(reader.IsDone); }
public void ParseQuotedFail(string source) { CharReader reader = new CharReader(source); Assert.False(reader.ReadTo(':', true, '"')); }
public void ParseQuotedSuccess(string source) { CharReader reader = new CharReader(source); Assert.True(reader.ReadTo(':', true, '"')); Assert.Equal("123", reader.GetRemainder().ToString()); }
public void CharReaderKeepsReadingAfterFoundPosition() { string source = "abc:123"; CharReader reader = new CharReader(source); reader.ReadTo(':', false); Assert.Equal('1', reader.Read()); }