stringsp

package module
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: Mar 29, 2020 License: BSD-3-Clause Imports: 4 Imported by: 25

README

strings

Plus to the standard strings package.

Godoc

// CallbackFieldsFunc is similar to strings.FieldsFunc but results are returned by two callback functions.
// prepare is firstly called with the number of total fields. Then each field is given by calling newField
// in order. If no fields are found, prepare is called with a zero value.
//
// This function is especially useful when we want to split a string into a slice of named types other than
// string or in a non-slice data structure at all.
func CallbackFieldsFunc(s string, isSpace func(rune) bool, prepare func(n int), newField func(f string)) {...}

LICENSE

BSD license

Documentation

Overview

Package stringsp is a plus to the standard "strings" package.

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func CallbackFields

func CallbackFields(s string, prepare func(n int), newField func(f string))

CallbackFields calls CallbackFieldsFunc with unicode.IsSpace.

Example
CallbackFields("Hello World ", func(n int) {
	fmtp.Printfln("Totally %d lines:", n)
}, func(f string) {
	fmtp.Printfln("  %s", f)
})
Output:

Totally 2 lines:
  Hello
  World

func CallbackFieldsFunc

func CallbackFieldsFunc(s string, isSpace func(rune) bool, prepare func(n int), newField func(f string))

CallbackFieldsFunc is similar to strings.FieldsFunc but results are returned by two callback functions. prepare is firstly called with the number of total fields. Then each field is given by calling newField in order. If no fields are found, prepare is called with a zero value.

This function is especially useful when we want to split a string into a slice of named types other than strings or into a non-slice data structure at all.

func Compare

func Compare(a, b string) int

Compares returns -1 if a < b, 1 if a > b, 0 otherwise.

func FullJoin

func FullJoin(a []string, prefix, sep, suffix string) string

FullJoin is similar to strings.Join with additional prefix/suffix if a is non-empty.

Example
a := []string{
	"item1", "item two",
}
fmt.Println(FullJoin(a, "(", "), (", ")"))
Output:

(item1), (item two)

func Get

func Get(s *string) string

Get safely returns the pointed contents of a string pionter. Returns "" for a nil pointer.

func IndentLinesExceptFirst

func IndentLinesExceptFirst(s, indent string) string

IndentLinesExceptFirst appends a leading indent to each lines of the string except the first line.

func LessFunc

func LessFunc(l []string) func(i, j int) bool

LessFunc returns a closure for comparing elements of a string slice.

func MatchPrefix

func MatchPrefix(s, prefix string) (string, bool)

MatchPrefix checks whether the specified string has a prefix, if so a string removing the prefix is returned; otherwise the original string is returned

func SliceInsert

func SliceInsert(s []string, index int, e ...string) []string

SliceInsert inserts some elements at index-th position and resturns the result slice.

func SliceRemove

func SliceRemove(s []string, index int) []string

SliceRemove removes the index-th element from the slice and returns the result slice.

Types

type Set

type Set map[string]struct{}

Set is a set of strings

func NewSet

func NewSet(els ...string) (s Set)

NeSet creates a string set with specified elements. You need to use this function only when you want to create a Set with intial elements. Different from a normal map, a nil value of Set is ok to add elemnents by calling the Add method.

func (*Set) Add

func (s *Set) Add(els ...string) Set

Add adds elements to the set. The set can be nil

func (Set) Contain

func (s Set) Contain(el string) bool

Contain returns true if the specified element is in the set, false otherwise

func (Set) Delete

func (s Set) Delete(els ...string) Set

Delete removes elements from the set

func (Set) Elements

func (s Set) Elements() (els []string)

Elements returns all elements in the set as a string slice. NOTE the order of elements may change even if the set does not change.

func (Set) Equal

func (s Set) Equal(t Set) bool

Equal checks whether the set has same elements with another set.

func (Set) String

func (s Set) String() string

String returns the string presentation of the set

Jump to

Keyboard shortcuts

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