Discover Packages
github.com/akramarenkov/alter
bytes
package
Version:
v0.0.4
Opens a new window with list of versions in this module.
Published: Oct 15, 2024
License: MIT
Opens a new window with license information.
Imports: 2
Opens a new window with list of imports.
Imported by: 0
Opens a new window with list of known importers.
README
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]]
}
Expand ▾
Collapse ▴
Documentation
¶
Some functionality identical to the bytes package from the standard library.
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.
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]]
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.
Source Files
¶
Click to show internal directories.
Click to hide internal directories.