diff options
author | Adam Scarr <adam@vektah.net> | 2017-08-06 23:32:10 +1000 |
---|---|---|
committer | Adam Scarr <adam@vektah.net> | 2017-08-06 23:32:10 +1000 |
commit | 666ea93dba377f267a2c8ecf97378a420db18383 (patch) | |
tree | 5efee0b6e4ccf44b854d9bb65e4e6fa5e7f86548 /html | |
parent | 9d7779e8ca5404f26abbd8cce0314d9cee967bba (diff) |
Eliminate a bunch of allocations
Diffstat (limited to 'html')
-rw-r--r-- | html/html.go | 22 | ||||
-rw-r--r-- | html/html_test.go | 5 |
2 files changed, 13 insertions, 14 deletions
diff --git a/html/html.go b/html/html.go index adee935..847af96 100644 --- a/html/html.go +++ b/html/html.go @@ -2,32 +2,32 @@ package html import . "github.com/vektah/goparsify" -func Parse(input string) (result Node, remaining string, err error) { +func Parse(input string) (result interface{}, remaining string, err error) { return ParseString(tag, input) } type Tag struct { Name string Attributes map[string]string - Body []Node + Body []interface{} } var ( tag Parser - identifier = Merge(And(Range("a-z", 1, 1), Range("a-zA-Z0-9", 0))) - text = CharRunUntil("<>") + identifier = Merge(And(Chars("a-z", 1, 1), Chars("a-zA-Z0-9", 0))) + text = NotChars("<>") element = Any(text, &tag) elements = Kleene(element) //attr := And(identifier, equal, String()) attr = And(WS, identifier, WS, "=", WS, Any(String('"'), String('\''))) - attrs = Map(Kleene(attr, WS), func(node Node) Node { - nodes := node.([]Node) + attrs = Map(Kleene(attr, WS), func(node interface{}) interface{} { + nodes := node.([]interface{}) attr := map[string]string{} for _, attrNode := range nodes { - attrNodes := attrNode.([]Node) + attrNodes := attrNode.([]interface{}) attr[attrNodes[0].(string)] = attrNodes[2].(string) } @@ -39,13 +39,13 @@ var ( ) func init() { - tag = Map(And(tstart, elements, tend), func(node Node) Node { - nodes := node.([]Node) - openTag := nodes[0].([]Node) + tag = Map(And(tstart, elements, tend), func(node interface{}) interface{} { + nodes := node.([]interface{}) + openTag := nodes[0].([]interface{}) return Tag{ Name: openTag[1].(string), Attributes: openTag[2].(map[string]string), - Body: nodes[1].([]Node), + Body: nodes[1].([]interface{}), } }) diff --git a/html/html_test.go b/html/html_test.go index defdc10..f2f8e6f 100644 --- a/html/html_test.go +++ b/html/html_test.go @@ -4,14 +4,13 @@ import ( "testing" "github.com/stretchr/testify/require" - . "github.com/vektah/goparsify" ) func TestParse(t *testing.T) { result, _, err := Parse(`<body>hello <p color="blue">world</p></body>`) require.NoError(t, err) - require.Equal(t, Tag{Name: "body", Attributes: map[string]string{}, Body: []Node{ + require.Equal(t, Tag{Name: "body", Attributes: map[string]string{}, Body: []interface{}{ "hello ", - Tag{Name: "p", Attributes: map[string]string{"color": "blue"}, Body: []Node{"world"}}, + Tag{Name: "p", Attributes: map[string]string{"color": "blue"}, Body: []interface{}{"world"}}, }}, result) } |