stringex

package
v0.3.0 Latest Latest
Warning

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

Go to latest
Published: Jan 13, 2020 License: BSD-3-Clause Imports: 7 Imported by: 0

Documentation

Overview

Package stringex helps when working with strings. So SplitFilter() and SplitMap() split given strings by a separator and user defined functions are called for each part to filter or map those.

Matches() provides a more simple string matching than regular expressions. Patterns are ? for one char, * for multiple chars, and [aeiou] or [0-9] for group or ranges of chars. Both can be negotiated with [^abc] while the pattern chars also can be escaped with \.

While the Valuer defines the interface to anything that may return a value as string the Default helps to interpret these strings as other data types. In case they don't match a default value will be returned.

Processor defines an interface for the processing of strings. Those easily can be chained or used for stream splitting again working with processor chains. Some processors are already pre-defined.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Matches

func Matches(pattern, value string, ignoreCase bool) bool

Matches checks if the pattern matches a given value. Here ? matches one char, * a group of chars, [any] any of these chars, and [0-9] any of this char range. Groups can be negated with [^abc] and \ escapes pattern chars.

func SplitFilter

func SplitFilter(s, sep string, f func(p string) bool) []string

SplitFilter splits the string s by the separator sep and then filters the parts. Only those where f returns true will be part of the result. So it even cout be empty.

func SplitMap

func SplitMap(s, sep string, m func(p string) (string, bool)) []string

SplitMap splits the string s by the separator sep and then maps the parts by the function m. Only those where m also returns true will be part of the result. So it even could be empty.

Types

type Defaulter

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

Defaulter provides an access to valuers interpreting the strings as types. In case of access or conversion errors these errors will be logged and the passed default values are returned.

func NewDefaulter

func NewDefaulter(id string, log bool) *Defaulter

NewDefaulter creates a defaulter with the given settings.

func (*Defaulter) AsBool

func (d *Defaulter) AsBool(v Valuer, dv bool) bool

AsBool returns the value interpreted as bool. Here the strings 1, t, T, TRUE, true, True are interpreted as true, the strings 0, f, F, FALSE, false, False as false.

func (*Defaulter) AsDuration

func (d *Defaulter) AsDuration(v Valuer, dv time.Duration) time.Duration

AsDuration returns the value interpreted as duration.

func (*Defaulter) AsFloat64

func (d *Defaulter) AsFloat64(v Valuer, dv float64) float64

AsFloat64 returns the value interpreted as float64.

func (*Defaulter) AsInt

func (d *Defaulter) AsInt(v Valuer, dv int) int

AsInt returns the value interpreted as int.

func (*Defaulter) AsInt64

func (d *Defaulter) AsInt64(v Valuer, dv int64) int64

AsInt64 returns the value interpreted as int64.

func (*Defaulter) AsString

func (d *Defaulter) AsString(v Valuer, dv string) string

AsString returns the value itself, only an error will return the default.

func (*Defaulter) AsStringMap

func (d *Defaulter) AsStringMap(v Valuer, rsep, kvsep string, dv map[string]string) map[string]string

AsStringMap returns the value as map of strings to strings. The rows are separated by the rsep, the key/values per row with kvsep.

func (*Defaulter) AsStringSlice

func (d *Defaulter) AsStringSlice(v Valuer, sep string, dv []string) []string

AsStringSlice returns the value as slice of strings separated by the passed separator.

func (*Defaulter) AsTime

func (d *Defaulter) AsTime(v Valuer, layout string, dv time.Time) time.Time

AsTime returns the value interpreted as time in the given layout.

func (*Defaulter) AsUint

func (d *Defaulter) AsUint(v Valuer, dv uint) uint

AsUint returns the value interpreted as uint.

func (*Defaulter) AsUint64

func (d *Defaulter) AsUint64(v Valuer, dv uint64) uint64

AsUint64 returns the value interpreted as uint64.

func (*Defaulter) String

func (d *Defaulter) String() string

String implements fmt.Stringer.

type Processor

type Processor interface {
	// Process takes a string and processes it. If the result is to
	// be ignored the bool has to be false.
	Process(in string) (string, bool)
}

Processor defines a type able to process strings.

type ProcessorFunc

type ProcessorFunc func(in string) (string, bool)

ProcessorFunc describes functions processing a string and returning the new one. A returned false means to ignore the result.

func NewChainProcessor

func NewChainProcessor(processors ...Processor) ProcessorFunc

NewChainProcessor creates a processor chaning the passed processors.

func NewConditionProcessor

func NewConditionProcessor(decider, affirmer, negater Processor) ProcessorFunc

NewConditionProcessor creates a processor taking the first processor for creating a temporary result and a decision. Based on the decision the temporary result is passed to an affirmer or a negater.

func NewLoopProcessor

func NewLoopProcessor(processor Processor) ProcessorFunc

NewLoopProcessor creates a processor letting the processor function work on the input until it returns false (aka while true). Itself then will return the processed sting and always true.

func NewLowerProcessor

func NewLowerProcessor() ProcessorFunc

NewLowerProcessor returns a processor converting the input to lower-case.

func NewMatchProcessor

func NewMatchProcessor(pattern string) ProcessorFunc

NewMatchProcessor returns a processor evaluating the input against a given pattern and returns the input and true when it is matching.

func NewSplitMapProcessor

func NewSplitMapProcessor(sep string, mapper Processor) ProcessorFunc

NewSplitMapProcessor creates a processor splitting the input and mapping the parts. It will only contain those where the mapper returns true. So it can be used as a filter too. Afterwards the collected mapped parts are joined again.

func NewSubstringProcessor

func NewSubstringProcessor(index, length int) ProcessorFunc

NewSubstringProcessor returns a processor slicing the input based on the index and length.

func NewTrimFuncProcessor

func NewTrimFuncProcessor(f func(r rune) bool) ProcessorFunc

NewTrimFuncProcessor returns a processor trimming prefix and suffix of the input based on the return value of the passed function checking each rune.

func NewTrimPrefixProcessor

func NewTrimPrefixProcessor(prefix string) ProcessorFunc

NewTrimPrefixProcessor returns a processor trimming a prefix of the input as long as it can find it.

func NewTrimSuffixProcessor

func NewTrimSuffixProcessor(prefix string) ProcessorFunc

NewTrimSuffixProcessor returns a processor trimming a prefix of the input as long as it can find it.

func NewUpperProcessor

func NewUpperProcessor() ProcessorFunc

NewUpperProcessor returns a processor converting the input to upper-case.

func WrapProcessorFunc

func WrapProcessorFunc(f func(fin string) string) ProcessorFunc

WrapProcessorFunc takes a standard string processing function and returns it as a ProcessorFunc.

func (ProcessorFunc) Process

func (pf ProcessorFunc) Process(in string) (string, bool)

Process implements Processor.

type StringValuer

type StringValuer string

StringValuer implements the Valuer interface for simple strings.

func (StringValuer) Value

func (sv StringValuer) Value() (string, error)

Value implements the Valuer interface.

type Valuer

type Valuer interface {
	// Value returns a string or a potential error during access.
	Value() (string, error)
}

Valuer describes returning a string value or an error if it does not exist are another access error happened.

Jump to

Keyboard shortcuts

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