From c26f5eb311b91547b8ef2260df3952b76afd4bd2 Mon Sep 17 00:00:00 2001 From: Chris Howey Date: Mon, 7 Mar 2022 18:26:22 -0600 Subject: [PATCH] build: bring in strings.Cut to build on go1.17 --- go.mod | 2 +- ledgerReader.go | 2 +- parse.go | 2 +- parseFuzz_test.go | 30 ++++++++++++++++++++++++++++++ parse_test.go | 21 --------------------- scut.go | 14 ++++++++++++++ 6 files changed, 47 insertions(+), 24 deletions(-) create mode 100644 parseFuzz_test.go create mode 100644 scut.go diff --git a/go.mod b/go.mod index ca509624..d71915a5 100644 --- a/go.mod +++ b/go.mod @@ -1,6 +1,6 @@ module github.com/howeyc/ledger -go 1.18 +go 1.17 require ( github.com/alfredxing/calc v0.0.0-20180827002445-77daf576f976 diff --git a/ledgerReader.go b/ledgerReader.go index e905b113..4225d766 100644 --- a/ledgerReader.go +++ b/ledgerReader.go @@ -57,7 +57,7 @@ func includeFile(filename string, buf *bytes.Buffer) error { for s.Scan() { line := s.Text() - if prefix, incname, found := strings.Cut(line, " "); found && prefix == "include" { + if prefix, incname, found := stringsCut(line, " "); found && prefix == "include" { // Resolve filepaths includedPath := filepath.Join(filename, "..", incname) includedPaths, err := filepath.Glob(includedPath) diff --git a/parse.go b/parse.go index 7db02f66..e677e5aa 100644 --- a/parse.go +++ b/parse.go @@ -91,7 +91,7 @@ func parseLedger(ledgerReader io.Reader, callback func(t *Transaction, err error continue } - before, after, split := strings.Cut(trimmedLine, " ") + before, after, split := stringsCut(trimmedLine, " ") if !split { if callback(nil, fmt.Errorf("%s:%d: Unable to parse transaction: %w", lp.filename, lp.lineCount, fmt.Errorf("Unable to parse payee line: %s", line))) { diff --git a/parseFuzz_test.go b/parseFuzz_test.go new file mode 100644 index 00000000..a71dfb1a --- /dev/null +++ b/parseFuzz_test.go @@ -0,0 +1,30 @@ +//go:build go1.18 + +package ledger + +import ( + "bytes" + "math/big" + "testing" +) + +func FuzzParseLedger(f *testing.F) { + for _, tc := range testCases { + if tc.err == nil { + f.Add(tc.data) + } + } + f.Fuzz(func(t *testing.T, s string) { + b := bytes.NewBufferString(s) + trans, _ := ParseLedger(b) + overall := new(big.Rat) + for _, t := range trans { + for _, p := range t.AccountChanges { + overall.Add(overall, p.Balance) + } + } + if overall.Cmp(new(big.Rat)) != 0 { + t.Error("Bad balance") + } + }) +} diff --git a/parse_test.go b/parse_test.go index adda30f4..60189e01 100644 --- a/parse_test.go +++ b/parse_test.go @@ -449,27 +449,6 @@ func TestParseLedger(t *testing.T) { } } -func FuzzParseLedger(f *testing.F) { - for _, tc := range testCases { - if tc.err == nil { - f.Add(tc.data) - } - } - f.Fuzz(func(t *testing.T, s string) { - b := bytes.NewBufferString(s) - trans, _ := ParseLedger(b) - overall := new(big.Rat) - for _, t := range trans { - for _, p := range t.AccountChanges { - overall.Add(overall, p.Balance) - } - } - if overall.Cmp(new(big.Rat)) != 0 { - t.Error("Bad balance") - } - }) -} - func BenchmarkParseLedger(b *testing.B) { tc := testCase{ "benchmark", diff --git a/scut.go b/scut.go new file mode 100644 index 00000000..3ceabd74 --- /dev/null +++ b/scut.go @@ -0,0 +1,14 @@ +package ledger + +import "strings" + +// Cut slices s around the first instance of sep, +// returning the text before and after sep. +// The found result reports whether sep appears in s. +// If sep does not appear in s, cut returns s, "", false. +func stringsCut(s, sep string) (before, after string, found bool) { + if i := strings.Index(s, sep); i >= 0 { + return s[:i], s[i+len(sep):], true + } + return s, "", false +}