stringutil

package
v0.10.2 Latest Latest
Warning

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

Go to latest
Published: Oct 13, 2021 License: Unlicense Imports: 4 Imported by: 19

Documentation

Overview

Package stringutil contains utilities for dealing with strings.

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func AllUnique added in v0.9.3

func AllUnique(strs []string) (ok bool)

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

func CloneSlice(strs []string) (clone []string)

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

func CloneSliceOrEmpty(strs []string) (clone []string)

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

func Coalesce(strs ...string) (res string)

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

func ContainsFold(s, substr string) (ok bool)

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

func FilterOut(strs []string, f func(s string) (ok bool)) (filtered []string)

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

func InSlice(strs []string, str string) (ok bool)

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

func SplitTrimmed(str, sep string) (strs []string)

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

func WriteToBuilder(b *strings.Builder, strs ...string)

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 NewSet

func NewSet(strs ...string) (set *Set)

NewSet returns a new string set containing strs.

func (*Set) Add

func (set *Set) Add(s string)

Add adds s to the set. Add panics if the set is a nil set, just like a nil map does.

func (*Set) Del

func (set *Set) Del(s string)

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

func (set *Set) Equal(other *Set) (ok bool)

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

func (set *Set) Has(s string) (ok bool)

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

func (set *Set) Len() (n int)

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

func (set *Set) Range(f func(s string) (cont bool))

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.

func (*Set) String

func (set *Set) String() (s string)

String implements the fmt.Stringer interface for *Set.

func (*Set) Values

func (set *Set) Values() (strs []string)

Values returns all values in the set. The order of the values is undefined. Values returns nil if the set is nil.

Jump to

Keyboard shortcuts

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