bytes

package
v0.0.4 Latest Latest
Warning

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

Go to latest
Published: Oct 15, 2024 License: MIT Imports: 2 Imported by: 0

README

Bytes

Split

Benchmark
goos: linux
goarch: amd64
pkg: github.com/akramarenkov/alter/bytes
cpu: AMD Ryzen 5 3600 6-Core Processor              
BenchmarkSplitStd-12              7953026           157.5 ns/op         128 B/op           1 allocs/op
BenchmarkSplit-12                 8095598           144.5 ns/op         128 B/op           1 allocs/op
BenchmarkSplitPreparer-12        33560821            36.94 ns/op          0 B/op           0 allocs/op
PASS
ok      github.com/akramarenkov/alter/bytes    4.767s
Usage

Example:

package main

import (
    "fmt"

    "github.com/akramarenkov/alter/bytes"
)

func main() {
    buffer := make([][]byte, 10)

    preparer := func(length int) [][]byte {
        if length > len(buffer) {
            return nil
        }

        return buffer[:length]
    }

    fmt.Println(bytes.Split([]byte("1 2 3 4 5"), []byte(" ")))
    fmt.Println(bytes.Split([]byte("1 2 3 4 5"), []byte(" "), preparer))
    // Output:
    // [[49] [50] [51] [52] [53]]
    // [[49] [50] [51] [52] [53]]
}

Documentation

Overview

Some functionality identical to the bytes package from the standard library.

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func Split

func Split(input, separator []byte, preparer ...Preparer) [][]byte

Splits input slice to subslices separated by separator.

If a preparer function is specified, the slice prepared by it will be used as the output slice. Otherwise, an individual slice will be created for each Split function call. The slice returned by the preparer function may be shorter than the requested length, in which case the splitting of the input slice will be limited by the length of the output slice.

Example
package main

import (
	"fmt"

	"github.com/akramarenkov/alter/bytes"
)

func main() {
	buffer := make([][]byte, 10)

	preparer := func(length int) [][]byte {
		if length > len(buffer) {
			return nil
		}

		return buffer[:length]
	}

	fmt.Println(bytes.Split([]byte("1 2 3 4 5"), []byte(" ")))
	fmt.Println(bytes.Split([]byte("1 2 3 4 5"), []byte(" "), preparer))
}
Output:

[[49] [50] [51] [52] [53]]
[[49] [50] [51] [52] [53]]

Types

type Preparer

type Preparer func(length int) [][]byte

Prepares slice of type [][]byte with specified length.

Typical purpose - working with a reusable buffer and/or limit the length of the output slice.

Jump to

Keyboard shortcuts

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