stringhelp

package
Version: v0.0.0-...-0d16973 Latest Latest
Warning

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

Go to latest
Published: Aug 10, 2021 License: MIT Imports: 5 Imported by: 0

Documentation

Overview

Package stringhelp contains various helpers for strings and collections thereof.

Index

Examples

Constants

This section is empty.

Variables

View Source
var ErrNotMap = errors.New("not a map with string keys")

ErrNotMap occurs when a generic string map function receives something that isn't a string map.

Functions

func CheckMap

func CheckMap(m interface{}) (reflect.Value, reflect.Type, error)

CheckMap checks, through reflection, to see if m is a map with string keys.

func JoinNonEmpty

func JoinNonEmpty(sep string, xs ...string) string

JoinNonEmpty joins the non-empty strings in xs using sep.

Example

ExampleJoinNonEmpty is a testable example for JoinNonEmpty.

package main

import (
	"fmt"

	"github.com/c4-project/c4t/internal/helper/stringhelp"
)

func main() {
	fmt.Println(stringhelp.JoinNonEmpty("/"))
	fmt.Println(stringhelp.JoinNonEmpty("/", ""))
	fmt.Println(stringhelp.JoinNonEmpty("/", "example1", ""))
	fmt.Println(stringhelp.JoinNonEmpty("/", "", "example2"))
	fmt.Println(stringhelp.JoinNonEmpty("/", "example1", "example2"))
	fmt.Println(stringhelp.JoinNonEmpty("/", "the", "", "quick brown", "", "fox"))

}
Output:


example1
example2
example1/example2
the/quick brown/fox

func MapKeys

func MapKeys(m interface{}) ([]string, error)

MapKeys gets the keys of a string map m as a slice.

func PluralQuantity

func PluralQuantity(n int, stem, one, many string) string

PluralQuantity formats a quantity n using the components stem, one, and many as follows: if n is 1, we return '[n] [stem][one]'; else, '[n] [stem][many]'.

Example

ExamplePluralQuantity is a runnable example for PluralQuantity.

package main

import (
	"fmt"

	"github.com/c4-project/c4t/internal/helper/stringhelp"
)

func main() {
	fmt.Println(stringhelp.PluralQuantity(0, "director", "y", "ies"))
	fmt.Println(stringhelp.PluralQuantity(1, "fil", "e", "es"))
	fmt.Println(stringhelp.PluralQuantity(2, "corp", "us", "ora"))

}
Output:

0 directories
1 file
2 corpora

func SafeMapKeys

func SafeMapKeys(m interface{}) ([]reflect.Value, error)

SafeMapKeys is the same as calling MapKeys on m, but checks to make sure m is a string map first.

Types

type Set

type Set map[string]struct{}

Set is a string set, implemented in the usual Go way as a map to empty structs.

func NewSet

func NewSet(xs ...string) Set

NewSet constructs a set from the elements xs.

func (Set) Add

func (s Set) Add(xs ...string)

Add adds each element in xs to this set.

func (Set) Copy

func (s Set) Copy() Set

Copy makes a deep copy of this set.

func (Set) Remove

func (s Set) Remove(xs ...string)

Remove removes each element in xs from this set.

func (Set) Slice

func (s Set) Slice() []string

Slice returns the elements of xs as a sorted slice.

Jump to

Keyboard shortcuts

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