stringz

package
v0.48.1 Latest Latest
Warning

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

Go to latest
Published: Mar 8, 2024 License: MIT Imports: 21 Imported by: 0

Documentation

Overview

Package stringz contains string functions similar in spirit to the stdlib strings package.

Index

Constants

View Source
const Redacted = "xxxxx"

Redacted is the "xxxxx" string used for redacted values, such as passwords. We use "xxxxx" instead of the arguably prettier "*****" because stdlib uses this for redacted strings.

See: url.URL.Redacted.

Variables

This section is empty.

Functions

func BacktickQuote added in v0.30.0

func BacktickQuote(s string) string

BacktickQuote backtick-quotes (and escapes) s.

hello `world`  --> `hello ``world```

func ByteSized

func ByteSized(size int64, precision int, sep string) string

ByteSized returns a human-readable byte size, e.g. "2.1 MB", "3.0 TB", etc. TODO: replace this usage with "github.com/c2h5oh/datasize", or maybe https://github.com/docker/go-units/.

func DecimalFloatOK added in v0.45.0

func DecimalFloatOK(d decimal.Decimal) bool

DecimalFloatOK returns true if d can be stored as a float64 without losing precision.

func DecimalPlaces added in v0.45.0

func DecimalPlaces(d decimal.Decimal) int32

DecimalPlaces returns the count of decimal places in d. That is to say, it returns the number of digits after the decimal point.

func DoubleQuote added in v0.30.0

func DoubleQuote(s string) string

DoubleQuote double-quotes (and escapes) s.

hello "world"  -->  "hello ""world"""

func ElementsHavingPrefix added in v0.37.0

func ElementsHavingPrefix(a []string, prefix string) []string

ElementsHavingPrefix returns the elements of a that have prefix.

func Ellipsify added in v0.47.0

func Ellipsify(s string, width int) string

Ellipsify shortens s to a length of maxLen by cutting the middle and inserting an ellipsis rune "…".This is the actual ellipsis rune, not three periods. For very short strings, the ellipsis may be elided.

Be warned, Ellipsify may not be unicode-safe. Use at your own risk.

See also: EllipsifyASCII.

func EllipsifyASCII added in v0.47.0

func EllipsifyASCII(s string, width int) string

EllipsifyASCII returns s but with a maximum length of maxLen, with the middle of s replaced with "...". If maxLen is a small number, the ellipsis may be shorter, e.g. a single char. This func is only tested with ASCII chars; results are not guaranteed for multibyte runes.

See also: Ellipsify.

func ExecuteTemplate added in v0.40.0

func ExecuteTemplate(name, tpl string, data any) (string, error)

ExecuteTemplate is a convenience function that constructs and executes a text template, returning the string value.

func FilterPrefix added in v0.37.0

func FilterPrefix(prefix string, a ...string) []string

FilterPrefix returns a new slice containing each element of a that has prefix.

func FormatDecimal added in v0.45.0

func FormatDecimal(d decimal.Decimal) string

FormatDecimal formats d with the appropriate number of decimal places as defined by d's exponent.

func FormatFloat

func FormatFloat(f float64) string

FormatFloat formats f. This method exists to provide a standard float formatting across the codebase.

func GenerateAlphaColName

func GenerateAlphaColName(n int, lower bool) string

GenerateAlphaColName returns an Excel-style column name for index n, starting with A, B, C... and continuing to AA, AB, AC, etc...

func HasAnyPrefix added in v0.37.0

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

HasAnyPrefix returns true if s has any of the prefixes.

func Head1 added in v0.48.0

func Head1(s string) string

Head1 returns the first line of s, without the linebreak.

func InSlice

func InSlice(haystack []string, needle string) bool

InSlice returns true if the needle is present in the haystack.

func IndentLines added in v0.34.0

func IndentLines(s, indent string) string

IndentLines returns a new string built from indenting each line of s.

func LineCount

func LineCount(r io.Reader, skipEmpty bool) int

LineCount returns the number of lines in r. If skipEmpty is true, empty lines are skipped (a whitespace-only line is not considered empty). If r is nil or any error occurs, -1 is returned.

func NewTemplate added in v0.40.0

func NewTemplate(name, tpl string) (*template.Template, error)

NewTemplate returns a new text template, with the sprig functions already loaded.

func ParseBool

func ParseBool(s string) (bool, error)

ParseBool is an expansion of strconv.ParseBool that also accepts variants of "yes" and "no" (which are bool representations returned by some data sources).

func Plu

func Plu(s string, i int) string

Plu handles the most common (English language) case of pluralization. With arg s being "row(s) col(s)", Plu returns "row col" if arg i is 1, otherwise returns "rows cols".

func PrefixSlice

func PrefixSlice(a []string, prefix string) []string

PrefixSlice returns a new slice with each element of a prefixed with prefix, unless a is nil, in which case nil is returned.

func RepeatJoin

func RepeatJoin(s string, count int, sep string) string

RepeatJoin returns a string consisting of count copies of s separated by sep. For example:

stringz.RepeatJoin("?", 3, ", ") == "?, ?, ?"

func Reverse

func Reverse(input string) string

Reverse reverses the input string.

func SanitizeAlphaNumeric

func SanitizeAlphaNumeric(s string, r rune) string

SanitizeAlphaNumeric replaces any non-alphanumeric runes of s with r (which is typically underscore).

a#2%3.4_ --> a_2_3_4_

func SanitizeFilename added in v0.47.0

func SanitizeFilename(name string) string

SanitizeFilename returns a sanitized version of filename. The supplied value should be the base file name, not a path.

func ShellEscape added in v0.40.0

func ShellEscape(s string) string

ShellEscape escapes s, making it safe to pass to a shell. Note that empty string will be returned as two single quotes.

func SingleQuote added in v0.30.0

func SingleQuote(s string) string

SingleQuote single-quotes (and escapes) s.

jessie's girl  -->  'jessie''s girl'

func SliceIndex

func SliceIndex(haystack []string, needle string) int

SliceIndex returns the index of needle in haystack, or -1.

func SprintJSON

func SprintJSON(value any) string

func Strings added in v0.32.0

func Strings[E any](a []E) []string

Strings returns a []string for a. If a is empty or nil, an empty slice is returned. A nil element is treated as empty string.

func StringsD added in v0.32.0

func StringsD[E any](a []E) []string

StringsD works like Strings, but it first dereferences every element of a. Thus if a is []any{*string, *int}, it is treated as if it were []any{string, int}.

func StripDoubleQuote added in v0.38.0

func StripDoubleQuote(s string) string

StripDoubleQuote strips double quotes from s, or returns s unchanged if it is not correctly double-quoted.

func SuffixSlice added in v0.33.0

func SuffixSlice(a []string, w string) []string

SuffixSlice returns a new slice containing each element of a with suffix w. If a is nil, nil is returned.

func Surround

func Surround(s, w string) string

Surround returns s prefixed and suffixed with w.

func SurroundSlice

func SurroundSlice(a []string, w string) []string

SurroundSlice returns a new slice with each element of a prefixed and suffixed with w, unless a is nil, in which case nil is returned.

func TrimHead added in v0.48.0

func TrimHead(s string, n int) string

TrimHead trims the first n lines from s. It panics if n is negative. If n is zero, s is returned unchanged.

func TrimLen added in v0.16.0

func TrimLen(s string, maxLen int) string

TrimLen returns s but with a maximum length of maxLen. This func is only tested with ASCII chars; results are not guaranteed for multibyte runes.

func Type added in v0.31.0

func Type(v any) string

Type returns the printed type of v.

func TypeNames added in v0.47.0

func TypeNames[T any](a ...T) []string

TypeNames returns the go type of each element of a, as rendered by fmt "%T".

func UUID

func UUID() string

UUID returns a new UUID string.

func Uniq32

func Uniq32() string

Uniq32 returns a UUID-like string that only contains alphanumeric chars. The result has length 32. The first element is guaranteed to be a letter.

func Uniq8

func Uniq8() string

Uniq8 returns a UUID-like string that only contains alphanumeric chars. The result has length 8. The first element is guaranteed to be a letter.

func UniqN added in v0.16.0

func UniqN(length int) string

UniqN returns a uniq string of length n. The first element is guaranteed to be a letter.

func UniqPrefix added in v0.16.0

func UniqPrefix(s string) string

UniqPrefix returns s with a unique prefix.

func UniqSuffix

func UniqSuffix(s string) string

UniqSuffix returns s with a unique suffix.

func UniqTableName

func UniqTableName(tbl string) string

UniqTableName returns a new lower-case table name based on tbl, with a unique suffix, and a maximum length of 63. This value of 63 is chosen because it's less than the maximum table name length for Postgres, SQL Server, SQLite and MySQL.

func UnsafeBytes added in v0.48.0

func UnsafeBytes(s string) []byte

UnsafeBytes returns a byte slice for s without copying. This should really only be used when we're chasing nanoseconds, and strings.Builder isn't a good fit. UnsafeBytes uses package unsafe, and is generally sketchy.

func UnsafeString added in v0.48.0

func UnsafeString(b []byte) string

UnsafeString returns a string for b without copying. This should really only be used when we're chasing nanoseconds. UnsafeString uses package unsafe, and is generally sketchy.

func Val added in v0.32.0

func Val(i any) any

Val returns the fully dereferenced value of i. If i is nil, nil is returned. If i has type *(*string), Val(i) returns string.

TODO: Should Val be renamed to Deref?

func ValidIdent added in v0.31.0

func ValidIdent(s string) error

ValidIdent returns an error if s is not a valid identifier. And identifier must start with a letter, and may contain letters, numbers, and underscore.

func ValidTemplate added in v0.40.0

func ValidTemplate(name, tpl string) error

ValidTemplate is a convenience wrapper around NewTemplate. It returns an error if the tpl is not a valid text template.

func VisitLines added in v0.34.0

func VisitLines(s string, fn func(i int, line string) string) string

VisitLines visits the lines of s, returning a new string built from applying fn to each line.

Types

This section is empty.

Jump to

Keyboard shortcuts

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