Skip to content

Commit

Permalink
build: bring in strings.Cut to build on go1.17
Browse files Browse the repository at this point in the history
  • Loading branch information
howeyc committed Mar 8, 2022
1 parent a19ad08 commit c26f5eb
Show file tree
Hide file tree
Showing 6 changed files with 47 additions and 24 deletions.
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
@@ -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
Expand Down
2 changes: 1 addition & 1 deletion ledgerReader.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion parse.go
Original file line number Diff line number Diff line change
Expand Up @@ -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))) {
Expand Down
30 changes: 30 additions & 0 deletions parseFuzz_test.go
Original file line number Diff line number Diff line change
@@ -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")
}
})
}
21 changes: 0 additions & 21 deletions parse_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
14 changes: 14 additions & 0 deletions scut.go
Original file line number Diff line number Diff line change
@@ -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
}

0 comments on commit c26f5eb

Please sign in to comment.