diff --git a/HTMLParser/SeparatorReader.cs b/HTMLParser/SeparatorReader.cs new file mode 100644 index 0000000..a36c27e --- /dev/null +++ b/HTMLParser/SeparatorReader.cs @@ -0,0 +1,32 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Runtime.InteropServices; +using System.Text; +using System.Threading.Tasks; + +namespace Mark.HTMLParser +{ + public class SeparatorReader : ITokenReader + { + private static readonly char[] Separators = new char[] {'.', ',', '\'', '\"', '!', '?', '%', '@', + '#', '$', '^', '&', '*', '/', '<', '>', '-', '=' }; + + private static readonly Dictionary NeedEscape = new Dictionary() + { + {"&", "&"}, {"<", "<"}, {">", ">"} + }; + + public Token ReadToken(string from) + { + if (from.Length == 0) + return null; + string result = from.Substring(0, 1); + if (NeedEscape.ContainsKey(result)) + { + result = NeedEscape[result]; + } + return Separators.Contains(from[0]) ? new Token(from.Substring(0, 1), result, TokenType.Separator) : null; + } + } +} diff --git a/Mark.csproj b/Mark.csproj index f89ced3..e23115f 100644 --- a/Mark.csproj +++ b/Mark.csproj @@ -48,6 +48,7 @@ + diff --git a/Tests/SeparatorReader_should.cs b/Tests/SeparatorReader_should.cs new file mode 100644 index 0000000..ac0f410 --- /dev/null +++ b/Tests/SeparatorReader_should.cs @@ -0,0 +1,51 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using NUnit.Framework; +using Mark.HTMLParser; + +namespace Tests +{ + [TestFixture] + class SeparatorReader_should + { + private static readonly SeparatorReader Reader = new SeparatorReader(); + + [Test] + public void return_null_if_empty_string() + { + var result = Reader.ReadToken(""); + Assert.IsNull(result); + } + + [Test] + public void read_single_separator() + { + var result = Reader.ReadToken(",."); + Assert.True(new Token(",", TokenType.Separator).Equals(result)); + } + + [Test] + public void replace_ampersand_with_amp() + { + var result = Reader.ReadToken("&."); + Assert.True(new Token("&", "&", TokenType.Separator).Equals(result)); + } + + [Test] + public void replace_less_with_lt() + { + var result = Reader.ReadToken("<."); + Assert.True(new Token("<", "<", TokenType.Separator).Equals(result)); + } + + [Test] + public void replace_greater_with_gt() + { + var result = Reader.ReadToken(">we"); + Assert.True(new Token(">", ">", TokenType.Separator).Equals(result)); + } + } +} diff --git a/Tests/Tests.csproj b/Tests/Tests.csproj index 6dbac0d..c655efd 100644 --- a/Tests/Tests.csproj +++ b/Tests/Tests.csproj @@ -47,6 +47,7 @@ +