From 77c172b823b64ebface655681ab0749b9d2f7081 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ra=C3=BAl=20Benencia?= Date: Fri, 13 Apr 2018 16:30:31 -0700 Subject: First public commit --- vendor/github.com/kr/logfmt/scanner.go | 149 +++++++++++++++++++++++++++++++++ 1 file changed, 149 insertions(+) create mode 100644 vendor/github.com/kr/logfmt/scanner.go (limited to 'vendor/github.com/kr/logfmt/scanner.go') diff --git a/vendor/github.com/kr/logfmt/scanner.go b/vendor/github.com/kr/logfmt/scanner.go new file mode 100644 index 0000000..095221f --- /dev/null +++ b/vendor/github.com/kr/logfmt/scanner.go @@ -0,0 +1,149 @@ +package logfmt + +import ( + "errors" + "fmt" +) + +var ErrUnterminatedString = errors.New("logfmt: unterminated string") + +func gotoScanner(data []byte, h Handler) (err error) { + saveError := func(e error) { + if err == nil { + err = e + } + } + + var c byte + var i int + var m int + var key []byte + var val []byte + var ok bool + var esc bool + +garbage: + if i == len(data) { + return + } + + c = data[i] + switch { + case c > ' ' && c != '"' && c != '=': + key, val = nil, nil + m = i + i++ + goto key + default: + i++ + goto garbage + } + +key: + if i >= len(data) { + if m >= 0 { + key = data[m:i] + saveError(h.HandleLogfmt(key, nil)) + } + return + } + + c = data[i] + switch { + case c > ' ' && c != '"' && c != '=': + i++ + goto key + case c == '=': + key = data[m:i] + i++ + goto equal + default: + key = data[m:i] + i++ + saveError(h.HandleLogfmt(key, nil)) + goto garbage + } + +equal: + if i >= len(data) { + if m >= 0 { + i-- + key = data[m:i] + saveError(h.HandleLogfmt(key, nil)) + } + return + } + + c = data[i] + switch { + case c > ' ' && c != '"' && c != '=': + m = i + i++ + goto ivalue + case c == '"': + m = i + i++ + esc = false + goto qvalue + default: + if key != nil { + saveError(h.HandleLogfmt(key, val)) + } + i++ + goto garbage + } + +ivalue: + if i >= len(data) { + if m >= 0 { + val = data[m:i] + saveError(h.HandleLogfmt(key, val)) + } + return + } + + c = data[i] + switch { + case c > ' ' && c != '"' && c != '=': + i++ + goto ivalue + default: + val = data[m:i] + saveError(h.HandleLogfmt(key, val)) + i++ + goto garbage + } + +qvalue: + if i >= len(data) { + if m >= 0 { + saveError(ErrUnterminatedString) + } + return + } + + c = data[i] + switch c { + case '\\': + i += 2 + esc = true + goto qvalue + case '"': + i++ + val = data[m:i] + if esc { + val, ok = unquoteBytes(val) + if !ok { + saveError(fmt.Errorf("logfmt: error unquoting bytes %q", string(val))) + goto garbage + } + } else { + val = val[1 : len(val)-1] + } + saveError(h.HandleLogfmt(key, val)) + goto garbage + default: + i++ + goto qvalue + } +} -- cgit v1.2.3