From b4f5fb423e5dec43ec702987dc3ddcb9df317d75 Mon Sep 17 00:00:00 2001 From: Adam Scarr Date: Mon, 7 Aug 2017 21:57:12 +1000 Subject: [PATCH] Fast single byte matcher --- parser.go | 15 +++++++++++++++ parser_test.go | 14 +++++++++++++- 2 files changed, 28 insertions(+), 1 deletion(-) diff --git a/parser.go b/parser.go index c6d7f9b..2477bb4 100644 --- a/parser.go +++ b/parser.go @@ -81,6 +81,21 @@ func ParseString(parser Parserish, input string) (result interface{}, remaining } func Exact(match string) Parser { + if len(match) == 1 { + matchByte := match[0] + return NewParser(match, func(ps *State) Node { + ps.AutoWS() + if ps.Input[ps.Pos] != matchByte { + ps.ErrorHere(match) + return Node{} + } + + ps.Advance(1) + + return Node{Token: match} + }) + } + return NewParser(match, func(ps *State) Node { ps.AutoWS() if !strings.HasPrefix(ps.Get(), match) { diff --git a/parser_test.go b/parser_test.go index c61dcda..594fcae 100644 --- a/parser_test.go +++ b/parser_test.go @@ -49,17 +49,29 @@ func TestParsifyAll(t *testing.T) { } func TestExact(t *testing.T) { - t.Run("success", func(t *testing.T) { + t.Run("success string", func(t *testing.T) { node, ps := runParser("foobar", Exact("fo")) require.Equal(t, "fo", node.Token) require.Equal(t, "obar", ps.Get()) }) + t.Run("success char", func(t *testing.T) { + node, ps := runParser("foobar", Exact("f")) + require.Equal(t, "f", node.Token) + require.Equal(t, "oobar", ps.Get()) + }) + t.Run("error", func(t *testing.T) { _, ps := runParser("foobar", Exact("bar")) require.Equal(t, "bar", ps.Error.Expected) require.Equal(t, 0, ps.Pos) }) + + t.Run("error char", func(t *testing.T) { + _, ps := runParser("foobar", Exact("o")) + require.Equal(t, "o", ps.Error.Expected) + require.Equal(t, 0, ps.Pos) + }) } func TestChars(t *testing.T) {