Documentation ¶
Overview ¶
Package stringz implements a collection of utility functions for manipulating strings and lists of strings.
Index ¶
- Variables
- func CopyStringMap(xs map[string]string) map[string]string
- func Dedup(xs []string) []string
- func Default(val, fallback string, zeroValues ...string) string
- func DefaultEmpty(val, fallback string) string
- func Join(prefix string, xs ...string) string
- func LastCut(s, sep string) (before, after string, found bool)
- func MatrixEqual(xs, ys [][]string) bool
- func SliceCombinationsR(pool []string, r int) [][]string
- func SliceCombinationsWithReplacement(pool []string, r int) [][]string
- func SliceContains(ys []string, x string) bool
- func SliceEqual(xs, ys []string) bool
- func SliceIndex(ys []string, x string) int
- func SliceMap(xs []string, fn func(string) error) error
- func SlicePermutations(xs []string) [][]string
- func SlicePermutationsR(pool []string, r int) [][]string
- func SplitExact(s, sep string, vars ...*string) error
- func SplitInto(s, sep string, vars ...*string) error
- func TrimPrefixIndex(s, index string) string
- func TrimSurrounding(s, surrounding string) string
- func Unpack(xs []string, vars ...*string) error
Examples ¶
Constants ¶
This section is empty.
Variables ¶
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 ¶
CopyStringMap returns a new copy of a map of strings.
func Default ¶
Default returns a fallback value when the provided value is equal to any of the zero values.
func DefaultEmpty ¶
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 LastCut ¶ added in v0.0.3
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 ¶
MatrixEqual returns true if two [][]string are equal. This function is sensitive to order.
func SliceCombinationsR ¶
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 ¶
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 ¶
SliceContains returns true if the provided string is in the provided string slice.
func SliceEqual ¶
SliceEqual returns true if two string slices are the same. This function is sensitive to order.
func SliceIndex ¶
SliceIndex returns the index of the first instance of x in ys, or -1 if it is not present.
func SliceMap ¶
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 ¶
SlicePermutations returns all permutations of a string slice.
It is equivalent to `SliceCombinationsR(xs, len(xs))`.
func SlicePermutationsR ¶
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 ¶
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
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 ¶
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 ¶
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 ¶
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.