zstring

package
Version: v0.0.0-...-6fe5ffd Latest Latest
Warning

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

Go to latest
Published: Jun 28, 2021 License: MIT Imports: 11 Imported by: 13

Documentation

Overview

Package zstring implements functions for strings.

All functions work correctly on Unicode codepoints/runes, but usually *don't* work on unicode clusters. That is, things like emojis composed of multiple codepoints and combining characters aren't dealt with unless explicitly mentioned otherwise.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func AlignCenter

func AlignCenter(s string, n int) string

AlignCenter centre-aligns a string, filling up any remaining width with spaces.

func AlignLeft

func AlignLeft(s string, n int) string

AlignLeft left-aligns a string, filling up any remaining width with spaces.

func AlignRight

func AlignRight(s string, n int) string

AlignRight right-aligns a string, filling up any remaining width with spaces.

func Choose

func Choose(l []string) string

Choose chooses a random item from the list.

func Contains

func Contains(list []string, str string) bool

Contains reports whether str is within the list.

func ContainsAny

func ContainsAny(list []string, strs ...string) bool

ContainsAny reports whether any of the strings are in the list

func Difference

func Difference(set []string, others ...[]string) []string

Difference returns a new slice with elements that are in "set" but not in "others".

func DisplayWidth

func DisplayWidth(s string) int

DisplayWidth gets the display width of a string, taking tabs and escape sequences in to account.

This does *not* handle various unicode aspects (i.e. graphmeme clusters, display width).

func ElideCenter

func ElideCenter(s string, n int) string

ElideCenter returns the "n" characters of the string.

If the string is shorter than "n" it will return the first n/2 characters and last n/2 characters of the string with "…" inserted in the centre. Otherwise the entire string is returned as-is.

func ElideLeft

func ElideLeft(s string, n int) string

ElideLeft returns the "n" left characters of the string.

If the string is shorter than "n" it will return the first "n" characters of the string with "…" appended. Otherwise the entire string is returned as-is.

func ElideRight

func ElideRight(s string, n int) string

ElideRight returns the "n" right characters of the string.

If the string is shorter than "n" it will return the first "n" characters of the string with "…" appended. Otherwise the entire string is returned as-is.

func Fields

func Fields(s, sep string) []string

Fields slices s to all substrings separated by sep. Leading/trailing whitespace and empty elements will be removed.

e.g. "a;b", "a; b", " a ; b", and "a; b;" will all result in ["a", "b"].

func Filter

func Filter(list []string, fun func(string) bool) []string

Filter a list.

The function will be called for every item and those that return false will not be included in the return value.

func FilterEmpty

func FilterEmpty(e string) bool

FilterEmpty is a filter for Filter() to remove empty entries.

An entry is considered "empty" if it's "" or contains only whitespace.

func From

func From(s string, sep string) string

From slices the string from first occurrence of sep. This is a shortcut for:

if i := strings.Index(s, sep); i > -1 {
  s = s[i+len(sep):]
}

func GetLine

func GetLine(in string, n int) string

GetLine gets the nth line \n-denoted line from a string.

func HasPrefixes

func HasPrefixes(s string, prefixes ...string) bool

HasPrefixes tests whether the string s starts with any of the prefixes.

Identical to:

strings.HasPrefix(s, "one") || strings.HasPrefix(s, "two")

func HasSuffixes

func HasSuffixes(s string, suffixes ...string) bool

HasSuffixes tests whether the string s ends with any of the suffixes.

Identical to:

strings.HasSuffix(s, "one") || strings.HasSuffix(s, "two")

func Indent

func Indent(s string, n int) string

Ident adds n spaces of indentation to every line.

func IndexAll

func IndexAll(s, find string) []int

IndexAll finds all occurrences of the string "find".

func IndexN

func IndexN(s, find string, n uint) int

IndexN finds the nth occurrence of a string.

n starts at 1; returns -1 if there is no nth occurrence of this string.

func IndexPairs

func IndexPairs(str, start, end string) [][]int

IndexPairs finds the position of all start/end pairs.

Nested pairs are not supported.

The return value is from last match to first match; this makes it easier to manipulate the string based on the indexes.

func IsASCII

func IsASCII(s string) bool

IsASCII reports if this string looks like it's plain 7-bit ASCII.

func LowerFirst

func LowerFirst(s string) string

LowerFirst transforms the first character to lower case, leaving the rest of the casing alone.

func Remove

func Remove(l *[]string, name string) bool

Remove all values from a list.

The return value indicates if this value was found at all.

func Repeat

func Repeat(s string, n int) (r []string)

Repeat returns a slice with the string s repeated n times.

func ReplacePairs

func ReplacePairs(str, start, end string, f func(int, string) string) string

ReplacePairs replaces everything starting with start and ending with end with the return value of the callback.

func Reverse

func Reverse(s string) string

Reverse a string.

func Split2

func Split2(str, sep string) (string, string)

Split2 splits a string with strings.SplitN(.., 2) and returns the result.

This makes some string splits a bit more elegant:

key, value := zstring.Split2(line, "=")

func Split3

func Split3(str, sep string) (string, string, string)

Split3 splits a string with strings.SplitN(.., 3) and returns the result.

func Split4

func Split4(str, sep string) (string, string, string, string)

Split4 splits a string with strings.SplitN(.., 4) and returns the result.

func String

func String(v interface{}) string

String converts a value to a string.

This works for all built-in primitives, []byte, and values that implement fmt.Stringer.

func Sub

func Sub(s string, start, end int) string

Sub returns a substring starting at start and ending at end.

Unlike regular string slicing this operates on runes/UTF-8 codepoints, rather than bytes.

func TrimPrefixes

func TrimPrefixes(s string, prefixes ...string) string

TrimPrefixes returns s without the provided leading prefixes strings.

Identical to:

s = strings.TrimPrefix(s, "one")
s = strings.TrimPrefix(s, "two")

func TrimSuffixes

func TrimSuffixes(s string, suffixes ...string) string

TrimSuffixes returns s without the provided trailing suffixes strings.

Identical to:

s = strings.TrimSuffix(s, "one")
s = strings.TrimSuffix(s, "two")

func Uniq

func Uniq(list []string) []string

Uniq removes duplicate entries from list; the list will be sorted.

func UpperFirst

func UpperFirst(s string) string

UpperFirst transforms the first character to upper case, leaving the rest of the casing alone.

func Upto

func Upto(s string, sep string) string

Upto slices the string up to the first occurrence of sep. This is a shortcut for:

if i := strings.Index(s, sep); i > -1 {
  s = s[:i]
}

func WordWrap

func WordWrap(text, prefix string, lim int) string

WordWrap word wraps at n columns and prefixes subsequent lines with "prefix".

Note the prefix is excluded from the length calculations; so if you want to wrap at 80 with a prefix of "> ", then you should wrap at 78.

Adapted from: https://github.com/mitchellh/go-wordwrap

Types

type Ptr

type Ptr struct{ P *string }

Ptr provides a more convenient way to deal with pointers.

s := "x"
p = &s                     →  p = zstring.NewPtr("x").P

if p != nil && p == "v" {  →  if p.Val() == "v" {

v := "<nil>"
if p != nil { v = *p }     →  fmt.Println(p)

func NewPtr

func NewPtr(s string) Ptr

NewPtr creates a new Ptr instance set to a pointer of s.

func (Ptr) MarshalJSON

func (b Ptr) MarshalJSON() ([]byte, error)

MarshalJSON converts the data to JSON.

func (Ptr) MarshalText

func (p Ptr) MarshalText() ([]byte, error)

MarshalText converts the data to a human readable representation.

func (*Ptr) Scan

func (p *Ptr) Scan(src interface{}) error

Scan converts the data from the DB.

func (*Ptr) Set

func (p *Ptr) Set(s string)

Set to the pointer of s.

func (Ptr) String

func (p Ptr) String() string

String gets the string value, or "<nil>" if the pointer is nil.

func (*Ptr) UnmarshalJSON

func (p *Ptr) UnmarshalJSON(text []byte) error

UnmarshalJSON converts the data from JSON.

func (*Ptr) UnmarshalText

func (p *Ptr) UnmarshalText(text []byte) error

UnmarshalText parses text in to the Go data structure.

func (Ptr) Val

func (p Ptr) Val() string

Val gets the pointer value, or "" if the pointer is nil.

func (Ptr) Value

func (p Ptr) Value() (driver.Value, error)

Value converts a bool type into a number to persist it in the database.

Source Files

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
t or T : Toggle theme light dark auto
y or Y : Canonical URL