slice

package
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: Sep 12, 2019 License: CC0-1.0 Imports: 11 Imported by: 0

Documentation

Index

Constants

View Source
const (
	MsgFailedToConvertIndex = "Failed to convert entry at index %d."
)

Error messages.

View Source
const Sep = ','

Sep defines the separator used when splitting strings

Variables

View Source
var (
	ErrUninitializedSplitter = errors.New("Uninitialized splitter")
	ErrInvalidRune           = errors.New("Invalid rune")
	ErrEOFWhilstEscaped      = errors.New("Unexpected EOF whilst in an escaped sequence")
	ErrTooLarge              = errors.New("Substring is too large")
	ErrUnbalancedBraces      = errors.New("Unbalanced braces")
	ErrUnexpectedEndOfString = errors.New("Unexpected end of string")
)

Common errors.

Functions

func AreIntegers

func AreIntegers(s string, sep rune, maxLen int) (bool, int)

AreIntegers performs a check that the given string is of the form

[[:space:]*[-][:space:]*[0-9,:space:]+[:space:]*:sep:]*

i.e. represents a sequence of integers separated by the rune 'sep'. The "maxLen" argument specifies the maximum number of digits allowed in an integer; set to 0 to impose no limit. Returns true followed by the slice length on success, false followed by the index (in the slice) of the first malformed entry otherwise. (A rune is defined to be white-space if it satisfies unicode.IsSpace. If 'sep' is white-space, a digit, or '-' then a panic occurs.)

func AreUnsignedIntegers

func AreUnsignedIntegers(s string, sep rune, maxLen int) (bool, int)

AreUnsignedIntegers performs a check that the given string is of the form

[[:space:]*[0-9,:space:]+[:space:]*:sep:]*

i.e. represents a sequence of unsigned integers separated by the rune 'sep'. The "maxLen" argument specifies the maximum number of digits allowed in an integer; set to 0 to impose no limit. Returns true followed by the slice length on success, false followed by the index (in the slice) of the first malformed entry otherwise. (A rune is defined to be white-space if it satisfies unicode.IsSpace.)

func BigIntSliceFromString

func BigIntSliceFromString(str string) ([]*big.Int, error)

BigIntSliceFromString converts a string into a slice of *big.Ints (i.e. []*big.Int).

func BoolSliceFromString

func BoolSliceFromString(str string) ([]bool, error)

BoolSliceFromString converts a string into a slice of bools (i.e. []bool).

func CopyInt

func CopyInt(S []int) (T []int)

CopyInt returns a copy of the given slice.

func CopyStr

func CopyStr(S []string) (T []string)

CopyStr returns a copy of the given slice.

func Float64SliceFromString

func Float64SliceFromString(str string) ([]float64, error)

Float64SliceFromString converts a string into a slice of float64s (i.e. []float64).

func IndexInt

func IndexInt(S []int, s int) int

IndexInt returns the first index of s in the slice S, or -1 if s cannot be found.

func IndexStr

func IndexStr(S []string, s string) int

IndexStr returns the first index of s in the slice S, or -1 if s cannot be found.

func InsertInt

func InsertInt(S []int, i int, s int) []int

InsertInt inserts the value s at the i-th position in the slice S.

func Int16SliceFromString

func Int16SliceFromString(str string) ([]int16, error)

Int16SliceFromString converts a string into a slice of int16s (i.e. []int16).

func Int32SliceFromString

func Int32SliceFromString(str string) ([]int32, error)

Int32SliceFromString converts a string into a slice of int32s (i.e. []int32).

func Int64SliceFromString

func Int64SliceFromString(str string) ([]int64, error)

Int64SliceFromString converts a string into a slice of int64s (i.e. []int64).

func Int64SlicesFromString

func Int64SlicesFromString(str string) ([][]int64, error)

Int64SlicesFromString returns str as a slice of slices of int64s (i.e. [][]int64).

func Int8SliceFromString

func Int8SliceFromString(str string) ([]int8, error)

Int8SliceFromString converts a string into a slice of int8s (i.e. []int8).

func IntSliceFromString

func IntSliceFromString(str string) ([]int, error)

IntSliceFromString converts a string into a slice of ints (i.e. []int).

func JoinBigIntSlice

func JoinBigIntSlice(S []*big.Int, sep string) string

JoinBigIntSlice concatenates the elements of S to create a single string. The separator string sep is placed between elements in the resulting string

func JoinBoolSlice

func JoinBoolSlice(S []bool, sep string) string

JoinBoolSlice concatenates the elements of S to create a single string. The separator string sep is placed between elements in the resulting string.

func JoinBoolSliceWithNames

func JoinBoolSliceWithNames(S []bool, sep string, trueStr string, falseStr string) string

JoinBoolSliceWithNames concatenates the elements of S to create a single string. The separator string sep is placed between elements in the resulting string. The string trueStr will be used when a boolean is true; the string falseStr will be used when a boolean is false.

func JoinInt16Slice

func JoinInt16Slice(S []int16, sep string) string

JoinInt16Slice concatenates the elements of S to create a single string. The separator string sep is placed between elements in the resulting string

func JoinInt32Slice

func JoinInt32Slice(S []int32, sep string) string

JoinInt32Slice concatenates the elements of S to create a single string. The separator string sep is placed between elements in the resulting string

func JoinInt64Slice

func JoinInt64Slice(S []int64, sep string) string

JoinInt64Slice concatenates the elements of S to create a single string. The separator string sep is placed between elements in the resulting string

func JoinInt8Slice

func JoinInt8Slice(S []int8, sep string) string

JoinInt8Slice concatenates the elements of S to create a single string. The separator string sep is placed between elements in the resulting string

func JoinIntSlice

func JoinIntSlice(S []int, sep string) string

JoinIntSlice concatenates the elements of S to create a single string. The separator string sep is placed between elements in the resulting string

func JoinUint16Slice

func JoinUint16Slice(S []uint16, sep string) string

JoinUint16Slice concatenates the elements of S to create a single string. The separator string sep is placed between elements in the resulting string

func JoinUint32Slice

func JoinUint32Slice(S []uint32, sep string) string

JoinUint32Slice concatenates the elements of S to create a single string. The separator string sep is placed between elements in the resulting string

func JoinUint64Slice

func JoinUint64Slice(S []uint64, sep string) string

JoinUint64Slice concatenates the elements of S to create a single string. The separator string sep is placed between elements in the resulting string.

func JoinUint8Slice

func JoinUint8Slice(S []uint8, sep string) string

JoinUint8Slice concatenates the elements of S to create a single string. The separator string sep is placed between elements in the resulting string

func JoinUintSlice

func JoinUintSlice(S []uint, sep string) string

JoinUintSlice concatenates the elements of S to create a single string. The separator string sep is placed between elements in the resulting string

func RemoveInt

func RemoveInt(S []int, i int) []int

RemoveInt removes the i-th entry from the slice S.

func RemoveStr

func RemoveStr(S []string, i int) []string

RemoveStr removes the i-th entry from the slice S.

func ReverseInt

func ReverseInt(S []int) []int

ReverseInt returns a new slice whose entries are equal to the entries of S, but in the reverse order.

func ReverseInt32

func ReverseInt32(S []int32) []int32

ReverseInt32 returns a new slice whose entries are equal to the entries of S, but in the reverse order.

func ReverseInt64

func ReverseInt64(S []int64) []int64

ReverseInt64 returns a new slice whose entries are equal to the entries of S, but in the reverse order.

func SplitAndTrim

func SplitAndTrim(s string, sep string) (T []string)

SplitAndTrim returns splits the string s using the separator sep, and trims any whitespace from the resulting substrings. Only non-empty substrings will be included in the returned slice.

func StringNTimes

func StringNTimes(S string, sep string, N int) string

StringNTimes concatenates N copies of S to create a single string. The separator string sep is placed between elements in the resulting string.

func TrimSpaceAndSurroundingBrackets

func TrimSpaceAndSurroundingBrackets(s string) string

TrimSpaceAndSurroundingBrackets removes initial and training white-space and at most one matching pair of brackets from the string. White-space is as defined by unicode.IsSpace, valid pairs of matching brackets are '(',')' and '{','}' and '[',']'.

func Uint16SliceFromString

func Uint16SliceFromString(str string) ([]uint16, error)

Uint16SliceFromString converts a string into a slice of uint16s (i.e. []uint16).

func Uint32SliceFromString

func Uint32SliceFromString(str string) ([]uint32, error)

Uint32SliceFromString converts a string into a slice of uint32s (i.e. []uint32).

func Uint64SliceFromString

func Uint64SliceFromString(str string) ([]uint64, error)

Uint64SliceFromString converts a string into a slice of uint64s (i.e. []uint64).

func Uint8SliceFromString

func Uint8SliceFromString(str string) ([]uint8, error)

Uint8SliceFromString converts a string into a slice of uint8s (i.e. []uint8).

func UintSliceFromString

func UintSliceFromString(str string) ([]uint, error)

UintSliceFromString converts a string into a slice of uints (i.e. []uint).

Types

type Splitter

type Splitter struct {
	// IsEscape is an optional function that should return true if the given rune is an escape rune. If true, the next rune will be escaped and added verbatim (along with the escape rune) to the returned substring. If io.EOF is reached whilst escaped, an ErrEOFWhilstEscaped error will be returned by Next().
	IsEscape func(ch rune) bool
	// Ignore is an optional function that can be be used to ignored a rune when scanning the stream. Ignored runes will be skipped-over and not form part of any returned substrings. Ignore should return true if the given rune should be ignored. The escape rune, or a rune preceded by the escape rune, will not be considered.
	Ignore func(ch rune) bool
	// SplitOn is an optional function that can be used to specify the end of a substring (the split rune itself will not be included). If notspecified, splits will occur on ','. Ignored runes, the escape rune, or runes preceded by the escape rune, will not be considered.
	SplitOn func(ch rune) bool
	// IsValid is an optional function that can be used to validate the stream. It should return true if the given rune is valid, false otherwise. If IsValid returns false, then the Splitter's Next() function will return an ErrInvalidRune error. Split runes, ignored runes, the escape rune, or runes preceded by the escape rune, will not be considered.
	IsValid func(ch rune) bool
	// Set TrimSpace to true to have consecutive white-space (as defined by unicode.IsSpace) trimmed from the start and end of each substring. A rune will only be considered if it is valid, is not ignored, has not been escaped, and is not used for splitting the stream into substrings.
	TrimSpace bool
	// Set CollateSpace to true to have consecutive white-space (as defined by unicode.IsSpace) to a single ' '. A rune will only be considered if it is valid, is not ignored, has not been escaped, and is not used for splitting the stream into substrings.
	CollateSpace bool
	// Set MaxLength to the maximum length of a substring (or the default of for no limit beyond that imposed by bytes.Buffer). If the maximum length is exceeded, Next() will return an ErrTooLarge error.
	MaxLength int
	// contains filtered or unexported fields
}

Splitter provides a convenient way of scanning an io.Reader and splitting it at specified runes.

func DefaultStringSplitter

func DefaultStringSplitter(str string, sep rune) *Splitter

DefaultStringSplitter returns a new splitter for the given string. It will split on the seperator sep, automatically removing all white-space (as determined by univode.IsSpace) as it goes.

func (*Splitter) Index

func (s *Splitter) Index() int

Index returns the index of the next rune in the stream to be read. Note that this count includes ignored runes, split runes, etc. hence may be larger the the total given by the substrings.

func (*Splitter) Init

func (s *Splitter) Init(src io.Reader) *Splitter

Init initializes a splitter with the given source and returns s.

func (*Splitter) Next

func (s *Splitter) Next() (string, error)

Next returns the next substring in the stream, or an empty string followed by an error (which, at the end of the stream, will be io.EOF).

Jump to

Keyboard shortcuts

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