split

package module
v0.1.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jul 29, 2024 License: MIT Imports: 3 Imported by: 0

README

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

go get https://github.com/clipperhouse/split
import "github.com/clipperhouse/split"

text := "Hello, 世界. Nice dog! 👍🐶"
sep := " "

split := split.String(text, sep)

for split.Next() {
    fmt.Println(split.Value())
}

Performance

Some initial benchmarks:

split.String (this package)

1185 ns/op	    404.28 MB/s	       0 B/op	       0 allocs/op

strings.Split (standard library)

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".

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.

Test

Status

Not ready for production yet! More testing and API consideration to come.

PR's are welcome, perhaps you'd like to implement a range iterator.

Documentation

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type ByteIterator

type ByteIterator = Iterator[[]byte]

func Bytes

func Bytes(s []byte, sep []byte) *ByteIterator

Bytes slices s into all subslices separated by sep.

If s does not contain sep and sep is not empty, Bytes returns a slice of length 1 whose only element is s.

If sep is empty, Bytes splits after each UTF-8 sequence. If both s and sep are empty, Bytes returns an empty slice.

Bytes returns an iterator over subslices. Use Iterator.Next to loop, and Iterator.Value to get the current subslice.

Example
package main

import (
	"fmt"

	"github.com/clipperhouse/split"
)

func main() {
	text := []byte("Hello, 世界. Nice dog! 👍🐶")
	sep := []byte(" ")

	split := split.Bytes(text, sep)

	for split.Next() {
		fmt.Println(split.Value())
	}
}

func BytesAny

func BytesAny(s []byte, separators []byte) *ByteIterator

BytesAny slices s into all subslices separated by any bytes in sep.

If s does not contain sep and sep is not empty, BytesAny returns a slice of length 1 whose only element is s.

If sep is empty, BytesAny splits after each UTF-8 sequence. If both s and sep are empty, BytesAny returns an empty slice.

BytesAny returns an iterator over subslices. Use Iterator.Next to loop, and Iterator.Value to get the current subslice.

type Iterator

type Iterator[T seq] struct {
	// contains filtered or unexported fields
}

Iterator is an iterator over subslices of `[]byte` or `string`. See Iterator.Next and Iterator.Value.

func (*Iterator[T]) Next

func (it *Iterator[T]) Next() bool

Next tests whether there are any remaining subslices.

Intended for use in a for loop. Inside the loop, retrieve the current subslice with Iterator.Value.

func (*Iterator[T]) ToArray

func (it *Iterator[T]) ToArray() []T

ToArray collects all the subslices into an array.

This is a convenience method, and the result should identical to strings|bytes.Split from the standard library. You should just use strings|bytes.Split if your goal is an array of results.

func (*Iterator[T]) Value

func (it *Iterator[T]) Value() T

Value retrieves the value of the current subslice. Use it with Iterator.Next.

type StringIterator

type StringIterator = Iterator[string]

func String

func String(s string, separator string) *StringIterator

String slices s into all substrings separated by sep.

If s does not contain sep and sep is not empty, String returns a slice of length 1 whose only element is s.

If sep is empty, String splits after each UTF-8 sequence. If both s and sep are empty, String returns an empty slice.

String returns an iterator over substrings. Use Iterator.Next to loop, and Iterator.Value to get the current substring.

Example
package main

import (
	"fmt"

	"github.com/clipperhouse/split"
)

func main() {
	text := "Hello, 世界. Nice dog! 👍🐶"
	sep := " "

	split := split.String(text, sep)

	for split.Next() {
		fmt.Println(split.Value())
	}
}

func StringAny

func StringAny(s string, chars string) *StringIterator

StringAny slices s into all substrings separated by any Unicode code point in chars.

If s does not contain any Unicode code point in chars, and chars is not empty, StringAny returns a slice of length 1 whose only element is s.

If chars is empty, StringAny splits after each UTF-8 sequence. If both s and chars are empty, StringAny returns an empty slice.

StringAny returns an iterator over substrings. Use Iterator.Next to loop, and Iterator.Value to get the current substrings.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL