Documentation ¶
Overview ¶
Package stringutil contains utilities for dealing with strings.
Index ¶
- func AllUnique(strs []string) (ok bool)
- func CloneSlice(strs []string) (clone []string)
- func CloneSliceOrEmpty(strs []string) (clone []string)
- func Coalesce(strs ...string) (res string)
- func ContainsFold(s, substr string) (ok bool)
- func FilterOut(strs []string, f func(s string) (ok bool)) (filtered []string)
- func InSlice(strs []string, str string) (ok bool)
- func SplitTrimmed(str, sep string) (strs []string)
- func WriteToBuilder(b *strings.Builder, strs ...string)
- type Set
- func (set *Set) Add(s string)
- func (set *Set) Del(s string)
- func (set *Set) Equal(other *Set) (ok bool)
- func (set *Set) Has(s string) (ok bool)
- func (set *Set) Len() (n int)
- func (set *Set) Range(f func(s string) (cont bool))
- func (set *Set) String() (s string)
- func (set *Set) Values() (strs []string)
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func AllUnique ¶ added in v0.9.3
AllUnique returns true if all items of strs are unique.
Example ¶
package main import ( "fmt" "github.com/AdguardTeam/golibs/stringutil" ) func main() { unique := []string{"a", "b", "c"} fmt.Printf("%q is unique: %t\n", unique, stringutil.AllUnique(unique)) nonUnique := []string{"a", "b", "a"} fmt.Printf("%q is unique: %t\n", nonUnique, stringutil.AllUnique(nonUnique)) }
Output: ["a" "b" "c"] is unique: true ["a" "b" "a"] is unique: false
func CloneSlice ¶
CloneSlice returns the exact copy of strs.
Example ¶
package main import ( "fmt" "github.com/AdguardTeam/golibs/stringutil" ) func main() { var a, b []string b = stringutil.CloneSlice(a) fmt.Printf("b == nil is %t\n", b == nil) a = []string{} b = stringutil.CloneSlice(a) fmt.Printf("b == nil is %t, len(b) is %d\n", b == nil, len(b)) a = []string{"a", "b", "c"} b = stringutil.CloneSlice(a) fmt.Printf("b is %v\n", b) fmt.Printf("&a[0] == &b[0] is %t\n", &a[0] == &b[0]) }
Output: b == nil is true b == nil is false, len(b) is 0 b is [a b c] &a[0] == &b[0] is false
func CloneSliceOrEmpty ¶
CloneSliceOrEmpty returns the copy of strs or empty strings slice if strs is a nil slice.
Example ¶
package main import ( "fmt" "github.com/AdguardTeam/golibs/stringutil" ) func main() { var a, b []string b = stringutil.CloneSliceOrEmpty(a) fmt.Printf("b == nil is %t, len(b) is %d\n", b == nil, len(b)) a = []string{} b = stringutil.CloneSliceOrEmpty(a) fmt.Printf("b == nil is %t, len(b) is %d\n", b == nil, len(b)) a = []string{"a", "b", "c"} b = stringutil.CloneSliceOrEmpty(a) fmt.Printf("b is %v\n", b) fmt.Printf("&a[0] == &b[0] is %t\n", &a[0] == &b[0]) }
Output: b == nil is false, len(b) is 0 b == nil is false, len(b) is 0 b is [a b c] &a[0] == &b[0] is false
func Coalesce ¶
Coalesce returns the first non-empty string. It is named after the function COALESCE in SQL except that since strings in Go are non-nullable, it uses an empty string as a NULL value. If strs or all it's elements are empty, it returns an empty string.
Example ¶
package main import ( "fmt" "github.com/AdguardTeam/golibs/stringutil" ) func main() { fmt.Printf("%q\n", stringutil.Coalesce()) fmt.Printf("%q\n", stringutil.Coalesce("", "a")) fmt.Printf("%q\n", stringutil.Coalesce("a", "")) fmt.Printf("%q\n", stringutil.Coalesce("a", "b")) }
Output: "" "a" "a" "a"
func ContainsFold ¶ added in v0.9.1
ContainsFold reports whether s contains, ignoring letter case, substr.
Example ¶
package main import ( "fmt" "github.com/AdguardTeam/golibs/stringutil" ) func main() { if stringutil.ContainsFold("abc", "b") { fmt.Println("works with the same case") } if stringutil.ContainsFold("abc", "B") { fmt.Println("works with a different case") } }
Output: works with the same case works with a different case
func FilterOut ¶
FilterOut returns a copy of strs with all strings for which f returned true removed.
Example ¶
package main import ( "fmt" "github.com/AdguardTeam/golibs/stringutil" ) func main() { strs := []string{ "some text", "", "# comments", } // Remove all empty and comment lines. filtered := stringutil.FilterOut(strs, func(s string) (ok bool) { return len(s) == 0 || s[0] == '#' }) fmt.Printf("%q\n", filtered) }
Output: ["some text"]
func InSlice ¶
InSlice checks if strs contains str.
Example ¶
package main import ( "fmt" "github.com/AdguardTeam/golibs/stringutil" ) func main() { const nl = "\n" strs := []string{} fmt.Printf(`%q contains "1" is %t`+nl, strs, stringutil.InSlice(strs, "1")) strs = []string{"1", "2", "3"} fmt.Printf(`%q contains "1" is %t`+nl, strs, stringutil.InSlice(strs, "1")) fmt.Printf(`%q contains "4" is %t`+nl, strs, stringutil.InSlice(strs, "4")) }
Output: [] contains "1" is false ["1" "2" "3"] contains "1" is true ["1" "2" "3"] contains "4" is false
func SplitTrimmed ¶ added in v0.8.4
SplitTrimmed slices str into all substrings separated by sep and returns a slice of the trimmed substrings between those separators with empty strings skipped. If str has no such substrings, strs is an empty slice.
Example ¶
package main import ( "fmt" "github.com/AdguardTeam/golibs/stringutil" ) func main() { s := "" fmt.Printf("%q is split into %q\n", s, stringutil.SplitTrimmed(s, ",")) s = "a, b , , c" fmt.Printf("%q is split into %q\n", s, stringutil.SplitTrimmed(s, ",")) }
Output: "" is split into [] "a, b , , c" is split into ["a" "b" "c"]
func WriteToBuilder ¶
WriteToBuilder is a convenient wrapper for strings.(*Builder).WriteString that deals with multiple strings and ignores errors, since they are guaranteed to be nil.
b must not be nil.
Example ¶
package main import ( "fmt" "strings" "github.com/AdguardTeam/golibs/stringutil" ) func main() { b := &strings.Builder{} stringutil.WriteToBuilder( b, "a", "b", "c", ) fmt.Println(b) }
Output: abc
Types ¶
type Set ¶
type Set struct {
// contains filtered or unexported fields
}
Set is a set of strings.
Example ¶
package main import ( "fmt" "github.com/AdguardTeam/golibs/stringutil" ) func main() { const s = "a" const nl = "\n" set := stringutil.NewSet() ok := set.Has(s) fmt.Printf(`%s contains "a" is %t`+nl, set, ok) set.Add(s) ok = set.Has(s) fmt.Printf(`%s contains "a" is %t`+nl, set, ok) other := stringutil.NewSet("a") ok = set.Equal(other) fmt.Printf("%s is equal to %s is %t\n", set, other, ok) fmt.Printf("values of %s are %q\n", set, set.Values()) set.Range(func(s string) (cont bool) { fmt.Printf("got value %q\n", s) return false }) set.Del(s) ok = set.Has(s) fmt.Printf(`%s contains "a" is %t`+nl, set, ok) set = stringutil.NewSet(s) fmt.Printf("%s has length %d\n", set, set.Len()) }
Output: [] contains "a" is false ["a"] contains "a" is true ["a"] is equal to ["a"] is true values of ["a"] are ["a"] got value "a" [] contains "a" is false ["a"] has length 1
Example (Nil) ¶
package main import ( "fmt" "github.com/AdguardTeam/golibs/stringutil" ) func main() { const s = "a" var set *stringutil.Set panicked := false setPanicked := func() { if v := recover(); v != nil { panicked = true } } func() { defer setPanicked() set.Del(s) }() fmt.Printf("panic after del: %t\n", panicked) func() { defer setPanicked() set.Has(s) }() fmt.Printf("panic after has: %t\n", panicked) func() { defer setPanicked() set.Len() }() fmt.Printf("panic after len: %t\n", panicked) func() { defer setPanicked() set.Range(func(s string) (cont bool) { fmt.Printf("got value %q\n", s) return true }) }() fmt.Printf("panic after range: %t\n", panicked) func() { defer setPanicked() set.Values() }() fmt.Printf("panic after values: %t\n", panicked) func() { defer setPanicked() set.Add(s) }() fmt.Printf("panic after add: %t\n", panicked) }
Output: panic after del: false panic after has: false panic after len: false panic after range: false panic after values: false panic after add: true
func (*Set) Add ¶
Add adds s to the set. Add panics if the set is a nil set, just like a nil map does.
func (*Set) Del ¶
Del deletes s from the set. Calling Del on a nil set has no effect, just like delete on an empty map doesn't.
func (*Set) Equal ¶ added in v0.9.3
Equal returns true if set is equal to other.
Example ¶
package main import ( "fmt" "github.com/AdguardTeam/golibs/stringutil" ) func main() { set := stringutil.NewSet("a") fmt.Printf("same: %t\n", set.Equal(stringutil.NewSet("a"))) fmt.Printf("other elem: %t\n", set.Equal(stringutil.NewSet("b"))) fmt.Printf("other len: %t\n", set.Equal(stringutil.NewSet("a", "b"))) fmt.Printf("nil: %t\n", set.Equal(nil)) fmt.Printf("nil eq nil: %t\n", (*stringutil.Set)(nil).Equal(nil)) }
Output: same: true other elem: false other len: false nil: false nil eq nil: true
func (*Set) Has ¶
Has returns true if s is in the set. Calling Has on a nil set returns false, just like indexing on an empty map does.
func (*Set) Len ¶
Len returns the length of the set. A nil set has a length of zero, just like an empty map.
func (*Set) Range ¶ added in v0.9.3
Range calls f with each value of the set in an undefined order. If cont is false, Range stops the iteration. Calling Range on a nil *Set has no effect.