Documentation ¶
Overview ¶
Package stringutil contains utilities for dealing with strings.
Index ¶
- func CloneSlice(strs []string) (clone []string)
- func CloneSliceOrEmpty(strs []string) (clone []string)
- func Coalesce(strs ...string) (res string)
- 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
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
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 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) fmt.Printf(`values of %s are %q`+nl, set, set.Values()) 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`+nl, set, set.Len()) }
Output: [] contains "a" is false ["a"] contains "a" is true values of ["a"] are ["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.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 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) 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.