strings

package
v0.0.5 Latest Latest
Warning

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

Go to latest
Published: Feb 14, 2025 License: MIT Imports: 2 Imported by: 0

README

Strings

Split

Benchmark
goos: linux
goarch: amd64
pkg: github.com/akramarenkov/alter/strings
cpu: AMD Ryzen 5 3600 6-Core Processor              
BenchmarkSplitStd-12              8205315           152.7 ns/op          80 B/op           1 allocs/op
BenchmarkSplit-12                 8196849           142.9 ns/op          80 B/op           1 allocs/op
BenchmarkSplitPreparer-12        34443756            34.30 ns/op          0 B/op           0 allocs/op
PASS
ok      github.com/akramarenkov/alter/strings    4.849s
Usage

Example:

package main

import (
    "fmt"

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

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

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

        return buffer[:length]
    }

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

Documentation

Overview

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

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func Split

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

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/strings"
)

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

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

		return buffer[:length]
	}

	fmt.Println(strings.Split("1 2 3 4 5", " "))
	fmt.Println(strings.Split("1 2 3 4 5", " ", preparer))
}
Output:

[1 2 3 4 5]
[1 2 3 4 5]

Types

type Preparer

type Preparer func(length int) []string

Prepares slice of type []string 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