Skip to content

Commit

Permalink
apply review feedback
Browse files Browse the repository at this point in the history
  • Loading branch information
judwhite committed Nov 15, 2016
1 parent 52e29b1 commit a9a71ce
Show file tree
Hide file tree
Showing 5 changed files with 18 additions and 17 deletions.
15 changes: 8 additions & 7 deletions decode.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,10 @@ package logfmt

import (
"bufio"
"bytes"
"fmt"
"io"
"unicode/utf8"
)

// A Decoder reads and decodes logfmt records from an input stream.
Expand Down Expand Up @@ -70,14 +72,14 @@ func (dec *Decoder) ScanKeyval() bool {
key:
const invalidKeyError = "invalid key"

start := dec.pos
start, multibyte := dec.pos, false
for p, c := range line[dec.pos:] {
switch {
case c == '=':
dec.pos += p
if dec.pos > start {
dec.key = line[start:dec.pos]
if invalidKey(dec.key) {
if multibyte && bytes.IndexRune(dec.key, utf8.RuneError) != -1 {
dec.syntaxError(invalidKeyError)
return false
}
Expand All @@ -95,18 +97,20 @@ key:
dec.pos += p
if dec.pos > start {
dec.key = line[start:dec.pos]
if invalidKey(dec.key) {
if multibyte && bytes.IndexRune(dec.key, utf8.RuneError) != -1 {
dec.syntaxError(invalidKeyError)
return false
}
}
return true
case c >= utf8.RuneSelf:
multibyte = true
}
}
dec.pos = len(line)
if dec.pos > start {
dec.key = line[start:dec.pos]
if invalidKey(dec.key) {
if multibyte && bytes.IndexRune(dec.key, utf8.RuneError) != -1 {
dec.syntaxError(invalidKeyError)
return false
}
Expand Down Expand Up @@ -200,9 +204,6 @@ func (dec *Decoder) Value() []byte {
return dec.value
}

// func (dec *Decoder) DecodeValue() ([]byte, error) {
// }

// Err returns the first non-EOF error that was encountered by the Scanner.
func (dec *Decoder) Err() error {
return dec.err
Expand Down
1 change: 1 addition & 0 deletions decode_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,7 @@ func TestDecoder_errors(t *testing.T) {
{"a=\"\\u1\"", &SyntaxError{Msg: "invalid quoted value", Line: 1, Pos: 8}},
{"a\ufffd=bar", &SyntaxError{Msg: "invalid key", Line: 1, Pos: 5}},
{"\x80=bar", &SyntaxError{Msg: "invalid key", Line: 1, Pos: 2}},
{"\x80", &SyntaxError{Msg: "invalid key", Line: 1, Pos: 2}},
}

for _, test := range tests {
Expand Down
8 changes: 4 additions & 4 deletions encode.go
Original file line number Diff line number Diff line change
Expand Up @@ -170,11 +170,11 @@ func invalidKeyRune(r rune) bool {
}

func invalidKeyString(key string) bool {
return len(key) == 0 || strings.IndexFunc(key, invalidKeyRune) != -1 || !utf8.ValidString(key)
return len(key) == 0 || strings.IndexFunc(key, invalidKeyRune) != -1
}

func invalidKey(key []byte) bool {
return len(key) == 0 || bytes.IndexFunc(key, invalidKeyRune) != -1 || !utf8.Valid(key)
return len(key) == 0 || bytes.IndexFunc(key, invalidKeyRune) != -1
}

func writeStringKey(w io.Writer, key string) error {
Expand Down Expand Up @@ -239,7 +239,7 @@ func writeStringValue(w io.Writer, value string, ok bool) error {
var err error
if ok && value == "null" {
_, err = io.WriteString(w, `"null"`)
} else if strings.IndexFunc(value, needsQuotedValueRune) != -1 || !utf8.ValidString(value) {
} else if strings.IndexFunc(value, needsQuotedValueRune) != -1 {
_, err = writeQuotedString(w, value)
} else {
_, err = io.WriteString(w, value)
Expand All @@ -249,7 +249,7 @@ func writeStringValue(w io.Writer, value string, ok bool) error {

func writeBytesValue(w io.Writer, value []byte) error {
var err error
if bytes.IndexFunc(value, needsQuotedValueRune) >= 0 || !utf8.Valid(value) {
if bytes.IndexFunc(value, needsQuotedValueRune) != -1 {
_, err = writeQuotedBytes(w, value)
} else {
_, err = w.Write(value)
Expand Down
10 changes: 5 additions & 5 deletions encode_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,11 +53,11 @@ func TestEncodeKeyval(t *testing.T) {
{key: decimalStringer{5, 9}, value: "v", want: "5.9=v"},
{key: (*decimalStringer)(nil), value: "v", err: logfmt.ErrNilKey},
{key: marshalerStringer{5, 9}, value: "v", want: "5.9=v"},
{key: "k", value: "\xbd", want: "k=\"\\ufffd\""},
{key: "k", value: "\ufffd\x00", want: "k=\"\\ufffd\\u0000\""},
{key: "k", value: "\ufffd", want: "k=\"\\ufffd\""},
{key: "k", value: []byte("\ufffd\x00"), want: "k=\"\\ufffd\\u0000\""},
{key: "k", value: []byte("\ufffd"), want: "k=\"\\ufffd\""},
{key: "k", value: "\xbd", want: `k="\ufffd"`},
{key: "k", value: "\ufffd\x00", want: `k="\ufffd\u0000"`},
{key: "k", value: "\ufffd", want: `k="\ufffd"`},
{key: "k", value: []byte("\ufffd\x00"), want: `k="\ufffd\u0000"`},
{key: "k", value: []byte("\ufffd"), want: `k="\ufffd"`},
}

for _, d := range data {
Expand Down
1 change: 0 additions & 1 deletion fuzz.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,6 @@ func parse(data []byte) ([][]kv, error) {
kvs = append(kvs, kv{dec.Key(), dec.Value()})
}
got = append(got, kvs)
kvs = nil
}
return got, dec.Err()
}
Expand Down

0 comments on commit a9a71ce

Please sign in to comment.