diff options
author | Raúl Benencia <raul@thousandeyes.com> | 2018-04-13 16:30:31 -0700 |
---|---|---|
committer | Raúl Benencia <raul@thousandeyes.com> | 2018-05-11 15:02:34 -0700 |
commit | 77c172b823b64ebface655681ab0749b9d2f7081 (patch) | |
tree | 09c13e626eb95ae1d33e76ed683172eab1ab6c96 /vendor/github.com/kr/logfmt/scanner.go |
First public commit
Diffstat (limited to 'vendor/github.com/kr/logfmt/scanner.go')
-rw-r--r-- | vendor/github.com/kr/logfmt/scanner.go | 149 |
1 files changed, 149 insertions, 0 deletions
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 + } +} |