stringz

package
v0.0.0-...-0fadc07 Latest Latest
Warning

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

Go to latest
Published: Mar 31, 2021 License: MIT Imports: 3 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Filter

func Filter(s []string, p Predicate) []string

Filter returns a shallow copy of the slice containing only the elements for which the predicate is true.

func MapToSlice

func MapToSlice(m map[string]struct{}) []string

MapToSlice converts a map to slice.

func Ptr

func Ptr(v string) *string

Ptr returns a pointer to the value.

func PtrDefToNil

func PtrDefToNil(v string, def string) *string

PtrDefToNil returns a pointer to the value, or nil if "def".

func PtrEmptyToNil

func PtrEmptyToNil(v string) *string

PtrEmptyToNil returns a pointer to the value, or nil if "".

func SafeIndex

func SafeIndex(s []string, i int) string

SafeIndex returns "s[i]" if possible, an empty string otherwise.

func SafeIndexPtr

func SafeIndexPtr(s []string, i int) *string

SafeIndexPtr returns "s[i]" if possible, an nil otherwise.

func SkipDuplicates

func SkipDuplicates(c *Collector)

SkipDuplicates skips adding duplicate strings.

func SkipEmpties

func SkipEmpties(c *Collector)

SkipEmpties skips adding empty strings.

func SliceToMap

func SliceToMap(s []string) map[string]struct{}

SliceToMap converts a slice to map.

func SwapMap

func SwapMap(m map[string]string) (map[string]string, error)

SwapMap returns a copy of the map with keys and values swapped. Fails in case of duplicate values.

func Val

func Val(v *string) string

Val returns the pointer value, defaulting to "" if nil.

func ValDef

func ValDef(v *string, def string) string

ValDef returns the pointer value, defaulting to "def" if nil.

Types

type Collector

type Collector struct {
	// contains filtered or unexported fields
}

Collector collects strings. It must be initialized using NewCollector, and its behavior can be regulated via options.

func NewCollector

func NewCollector(options ...CollectorOption) *Collector

NewCollector initializes a new collector.

func (*Collector) Add

func (c *Collector) Add(ss ...string) *Collector

Add a string.

func (*Collector) AddPtr

func (c *Collector) AddPtr(ss ...*string) *Collector

AddPtr a string pointer, skipping it if nil.

func (*Collector) Join

func (c *Collector) Join(sep string) string

Join the collected slice using the given "sep".

func (*Collector) Map

func (c *Collector) Map() map[string]struct{}

Map returns the collected map.

func (*Collector) Slice

func (c *Collector) Slice() []string

Slice returns the collected slice.

type CollectorOption

type CollectorOption func(*Collector)

CollectorOption configures a Collector.

func InitialCap

func InitialCap(cap int) CollectorOption

InitialCap sets the initial capacity of the underlying data structures.

type Predicate

type Predicate func(s string) bool

Predicate describes a predicate.

func And

func And(ps ...Predicate) Predicate

And returns a predicate that is true if all given predicates are true.

func Equal

func Equal(eq string) Predicate

Equal returns a predicate matching "eq".

func Not

func Not(p Predicate) Predicate

Not negates a predicate.

func Or

func Or(ps ...Predicate) Predicate

Or returns a predicate that is true if any of the given predicates are true.

type Slice

type Slice []string

Slice is a slice of values.

func (Slice) IsSorted

func (s Slice) IsSorted() bool

IsSorted returns true if the slice is sorted.

func (Slice) Len

func (s Slice) Len() int

Len implements the sort.Interface interface.

func (Slice) Less

func (s Slice) Less(i, j int) bool

Less implements the sort.Interface interface.

func (Slice) Sort

func (s Slice) Sort()

Sort the slice.

func (Slice) Swap

func (s Slice) Swap(i, j int)

Swap implements the sort.Interface interface.

Jump to

Keyboard shortcuts

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