Skip to content

Commit

Permalink
docs & tests
Browse files Browse the repository at this point in the history
  • Loading branch information
clipperhouse committed Jul 29, 2024
1 parent 111aabc commit 0549a47
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 8 deletions.
22 changes: 16 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,13 +1,11 @@
A more efficient splitter for bytes and strings, with a focus on zero allocation, for Go. Use this where you would might use `bytes.Split` or `strings.Split`.

You might be interested if you are splitting strings or bytes on a hot path.

### Usage

See [pkg.go.dev](https://pkg.go.dev/github.com/clipperhouse/split)

```bash
go get https://github.com/clipperhouse/split
go get github.com/clipperhouse/split
```

```go
Expand Down Expand Up @@ -39,16 +37,28 @@ Some initial benchmarks:
1267 ns/op 378.07 MB/s 1280 B/op 1 allocs/op
```

Overall, this package is a bit faster, but perhaps more importantly, notice the difference in allocations. If you're on a hot path, reducing GC might add up and make you a "better neighbor".
Overall, this package is a _little_ faster, but more importantly, notice the difference in allocations. If you're on a hot path, this might add up, and reducing GC might help your app to scale.

### Why you might use this

The standard library collects all the splits at once into an array, and allocates to do so (this is true in other languages as well).

This package lazily iterates over each split as needed, and avoids that allocation. Think of it as streaming instead of batching.

If you do not actually need the array, but only need to iterate over the splits, this package may be useful.

### Data types

This packages handles `string` and `[]byte` (and named types based on them). If you have an `io.Reader`, we suggest [`bufio.Scanner`](https://pkg.go.dev/bufio) from the standard library.

### Testing

We work to ensure that `split.Bytes` and `split.String` offer an identical API and results as their standard library counterparts, `bytes.Split` and `strings.Split`. Have a look at the tests to verify that this is true.
We work to ensure that `split.Bytes` and `split.String` offer an identical API and results as their standard library counterparts, `bytes.Split` and `strings.Split`. Have a look at the [tests](https://github.com/clipperhouse/split/blob/main/tests_test.go) to verify that this is true.

[![Test](https://github.com/clipperhouse/split/actions/workflows/gotest.yml/badge.svg)](https://github.com/clipperhouse/split/actions/workflows/gotest.yml)

### Status

Not ready for production yet! More testing and API consideration to come.
We've published a v0.1.0. Try it and leave feedback.

PR's are welcome, perhaps you'd like to [implement a range iterator](https://tip.golang.org/doc/go1.23).
8 changes: 6 additions & 2 deletions tests_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,20 @@ var abcd = "abc๐šซ"
var emoji = "๐Ÿ‘๐Ÿถ"
var efghi = "รฉfghi"

var testSeparators = []string{" ", ",", ", ", "๐Ÿถ", "๐šซ", "๐ŸŒ๐Ÿ‘", ""}
var testSeparators = []string{"", " ", ",", ", ", "๐Ÿถ", "๐šซ", "๐ŸŒ๐Ÿ‘", abcd, emoji, efghi}

var testStrings []string

func init() {
testStrings = append(testStrings, "") // empty
testStrings = append(testStrings, abcd)
testStrings = append(testStrings, emoji)
testStrings = append(testStrings, efghi)

for _, sep := range testSeparators {
testStrings = append(testStrings, sep)
center := abcd + sep + emoji + sep + efghi
testStrings = append(testStrings, sep) // leading
testStrings = append(testStrings, center)
testStrings = append(testStrings, sep+center) // leading
testStrings = append(testStrings, center+sep) // trailing
testStrings = append(testStrings, sep+center+sep) // both
Expand Down

0 comments on commit 0549a47

Please sign in to comment.