stringz

package module
v0.0.3 Latest Latest
Warning

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

Go to latest
Published: Dec 8, 2023 License: Apache-2.0 Imports: 2 Imported by: 31

README

stringz

Go Report Card Godoc LICENSE

Package stringz provides string manipulations not found in the standard library.

Documentation

Overview

Package stringz implements a collection of utility functions for manipulating strings and lists of strings.

Index

Examples

Constants

This section is empty.

Variables

View Source
var ErrInconsistentUnpackLen = errors.New("the length of the unpacked is not equal to the provided input")

ErrInconsistentUnpackLen is returned when Unpack is provided two slices without the same length.

Functions

func CopyStringMap

func CopyStringMap(xs map[string]string) map[string]string

CopyStringMap returns a new copy of a map of strings.

func Dedup

func Dedup(xs []string) []string

Dedup returns a new slice with any duplicates removed.

func Default

func Default(val, fallback string, zeroValues ...string) string

Default returns a fallback value when the provided value is equal to any of the zero values.

func DefaultEmpty

func DefaultEmpty(val, fallback string) string

DefaultEmpty returns the fallback when val is empty string.

This function is inspired by Python's `dict.get()`.

Example
package main

import (
	"fmt"

	"github.com/jzelinskie/stringz"
)

func main() {
	myFunc := func() string {
		// Oh no! I failed!
		// Return the empty string!
		return ""
	}

	myFuncOutput := stringz.DefaultEmpty(myFunc(), "sane default")

	fmt.Println(myFuncOutput)

}
Output:

sane default

func Join

func Join(prefix string, xs ...string) string

Join is strings.Join, but variadic.

func LastCut added in v0.0.3

func LastCut(s, sep string) (before, after string, found bool)

LastCut slices s around the last instance of sep, returning the text before and after sep.

The found result reports whether sep appears in s. If sep does not appear in s, LastCut returns s, "", false.

func MatrixEqual

func MatrixEqual(xs, ys [][]string) bool

MatrixEqual returns true if two [][]string are equal. This function is sensitive to order.

func SliceCombinationsR

func SliceCombinationsR(pool []string, r int) [][]string

SliceCombinationsR returns r-length subsequences of elements from the provided string slice.

If r is less than 0 or larger than the length of the pool, nil is returned.

The combinations are emitted in lexicographic ordering according to the order of the input iterable. So, if the input iterable is sorted, the combination tuples will be produced in sorted order.

Elements are treated as unique based on their position, not on their value. So if the input elements are unique, there will be no repeat values in each combination.

This is the algorithm used in Python's itertools library: itertools.combinations(iterable, r)

func SliceCombinationsWithReplacement

func SliceCombinationsWithReplacement(pool []string, r int) [][]string

SliceCombinationsWithReplacement returns r-length subsequences of elements from the provided string slice allowing individual elements to be repeated more than once.

If r is less than 0 or larger than the length of the pool, nil is returned.

The combination tuples are emitted in lexicographic ordering according to the order of the input iterable. So, if the input iterable is sorted, the combination tuples will be produced in sorted order.

Elements are treated as unique based on their position, not on their value. So if the input elements are unique, the generated combinations will also be unique.

This is the algorithm used in Python's itertools library: itertools.combinations_with_replacement(iterable, r)

func SliceContains

func SliceContains(ys []string, x string) bool

SliceContains returns true if the provided string is in the provided string slice.

func SliceEqual

func SliceEqual(xs, ys []string) bool

SliceEqual returns true if two string slices are the same. This function is sensitive to order.

func SliceIndex

func SliceIndex(ys []string, x string) int

SliceIndex returns the index of the first instance of x in ys, or -1 if it is not present.

func SliceMap

func SliceMap(xs []string, fn func(string) error) error

SliceMap is a functional-style mapping function for slices of strings.

This is particularly useful when you would normally use a for-loop, but want `defer` to execute for each iteration.

Example
package main

import (
	"fmt"
	"io"
	"io/ioutil"
	"os"

	"github.com/jzelinskie/stringz"
)

func main() {
	files := []string{"/home/my/file", "/home/your/file"}

	if err := stringz.SliceMap(files, func(path string) error {
		f, err := os.Open(path)
		if err != nil {
			return err
		}
		defer f.Close() // Defer runs for every iteration!

		_, err = io.Copy(ioutil.Discard, f)
		return err
	}); err != nil {
		fmt.Println(err)
	}
}
Output:

func SlicePermutations

func SlicePermutations(xs []string) [][]string

SlicePermutations returns all permutations of a string slice.

It is equivalent to `SliceCombinationsR(xs, len(xs))`.

func SlicePermutationsR

func SlicePermutationsR(pool []string, r int) [][]string

SlicePermutationsR returns successive r-length permutations of elements in the provided string slice.

If r is less than 0 or larger than the length of the pool, nil is returned.

The permutation tuples are emitted in lexicographic ordering according to the order of the input iterable. So, if the input iterable is sorted, the combination tuples will be produced in sorted order.

Elements are treated as unique based on their position, not on their value. So if the input elements are unique, there will be no repeat values in each permutation.

This is the algorithm used in Python's itertools library: itertools.permutations(iterable, r=None)

func SplitExact

func SplitExact(s, sep string, vars ...*string) error

SplitExact splits the string `s` into `len(vars)` number of strings and unpacks them into those vars.

Returns ErrInconsistentUnpackLen if len(vars) doesn't match the number of split segments.

func SplitInto added in v0.0.2

func SplitInto(s, sep string, vars ...*string) error

SplitInto splits the string `s` into `len(vars)` number of strings and unpacks them into those vars. If there are more substrings that would be split after len(vars), they will be all be put into the final variable.

Returns ErrInconsistentUnpackLen if len(vars) is greater than the number of split substrings.

func TrimPrefixIndex

func TrimPrefixIndex(s, index string) string

TrimPrefixIndex trims everything before the provided index.

Example
package main

import (
	"fmt"

	"github.com/jzelinskie/stringz"
)

func main() {
	fmt.Println(stringz.TrimPrefixIndex("this:that", ":"))
	fmt.Println(stringz.TrimPrefixIndex("this::that", "::"))

}
Output:

that
that

func TrimSurrounding

func TrimSurrounding(s, surrounding string) string

TrimSurrounding returns a string with both a prefix and suffix trimmed from it.

Do not confuse this with strings.Trim() which removes characters in a cutset rather than working on prefixes and suffixes.

Example
package main

import (
	"fmt"

	"github.com/jzelinskie/stringz"
)

func main() {
	unquoted := stringz.TrimSurrounding(`"quoted text"`, `"`)
	fmt.Println(unquoted)

}
Output:

quoted text

func Unpack

func Unpack(xs []string, vars ...*string) error

Unpack assigns a slice into local variables.

Example
package main

import (
	"fmt"

	"github.com/jzelinskie/stringz"
)

func main() {
	var first, second string
	if err := stringz.Unpack([]string{"hello", "world"}, &first, &second); err != nil {
		panic(err)
	}
	fmt.Println(first, second)

}
Output:

hello world

Types

This section is empty.

Jump to

Keyboard shortcuts

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