Created
October 10, 2013 00:23
-
-
Save jgrar/6910949 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"bytes" | |
"fmt" | |
) | |
type Message2 struct { | |
Prefix string | |
Command string | |
Args []string | |
} | |
func (m Message2) String() string { | |
return fmt.Sprintf("prefix: %q command: %q args %#v", m.Prefix, m.Command, m.Args) | |
} | |
func (m *Message2) UnmarshalText2(b []byte) error { | |
var s [][]byte; | |
if b[0] == ':' { | |
s = bytes.SplitN(b, []byte{' '}, 2) | |
m.Prefix = string(s[0]) | |
b = s[1] | |
} | |
s = bytes.SplitN(b, []byte{' '}, 2); | |
m.Command = string(s[0]); | |
b = s[1]; | |
for { | |
if b[0] == ':' { | |
m.Args = append(m.Args, string(b)) | |
break | |
} else { | |
s = bytes.SplitN(b, []byte{' '}, 2) | |
if len(s) == 0 { | |
break | |
} | |
m.Args = append(m.Args, string(s[0])) | |
b = s[1] | |
} | |
} | |
return nil | |
} | |
func (m *Message2) MarshalText2() (text []byte, err error) { | |
var b bytes.Buffer | |
if len(m.Prefix) > 0 { | |
b.WriteByte(':') | |
b.WriteString(m.Prefix) | |
b.WriteByte(' ') | |
} | |
b.WriteString(m.Command) | |
for _, arg := range m.Args { | |
b.WriteByte(' ') | |
b.WriteString(arg) | |
} | |
return b.Bytes(), nil | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"testing" | |
) | |
var unmarshaltext_message_bench = []byte(":server.kevlar.net NOTICE user :*** This is a test") | |
func BenchmarkUnmarshalText2 (b *testing.B) { | |
var m Message2 | |
for i := 0; i < b.N; i++ { | |
m.UnmarshalText2(unmarshaltext_message_bench) | |
} | |
} | |
func BenchmarkUnmarshalText (b *testing.B) { | |
var m Message | |
for i := 0; i < b.N; i++ { | |
m.UnmarshalText(unmarshaltext_message_bench) | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$ go test -test.bench=. | |
testing: warning: no tests to run | |
PASS | |
BenchmarkUnmarshalText2 500000 5558 ns/op | |
BenchmarkUnmarshalText 200000 10425 ns/op | |
ok irc-parser 5.610s |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment