Documentation ¶
Overview ¶
Package stringz contains string functions similar in spirit to the stdlib strings package.
Index ¶
- Constants
- func BacktickQuote(s string) string
- func ByteSized(size int64, precision int, sep string) string
- func DoubleQuote(s string) string
- func FormatFloat(f float64) string
- func GenerateAlphaColName(n int, lower bool) string
- func InSlice(haystack []string, needle string) bool
- func IndentLines(s, indent string) string
- func LineCount(r io.Reader, skipEmpty bool) int
- func ParseBool(s string) (bool, error)
- func Plu(s string, i int) string
- func PrefixSlice(a []string, w string) []string
- func RepeatJoin(s string, count int, sep string) string
- func Reverse(input string) string
- func SanitizeAlphaNumeric(s string, r rune) string
- func SingleQuote(s string) string
- func SliceIndex(haystack []string, needle string) int
- func SprintJSON(value any) string
- func Strings[E any](a []E) []string
- func StringsD[E any](a []E) []string
- func SuffixSlice(a []string, w string) []string
- func Surround(s, w string) string
- func SurroundSlice(a []string, w string) []string
- func TrimLen(s string, maxLen int) string
- func Type(v any) string
- func UUID() string
- func Uniq32() string
- func Uniq8() string
- func UniqN(length int) string
- func UniqPrefix(s string) string
- func UniqSuffix(s string) string
- func UniqTableName(tbl string) string
- func Val(i any) any
- func ValidIdent(s string) error
- func VisitLines(s string, fn func(i int, line string) string) string
Constants ¶
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
BacktickQuote backtick-quotes (and escapes) s.
hello `world` --> `hello ``world```
func ByteSized ¶
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 DoubleQuote ¶ added in v0.30.0
DoubleQuote double-quotes (and escapes) s.
hello "world" --> "hello ""world"""
func FormatFloat ¶
FormatFloat formats f. This method exists to provide a standard float formatting across the codebase.
func GenerateAlphaColName ¶
GenerateAlphaColName returns an Excel-style column name for index n, starting with A, B, C... and continuing to AA, AB, AC, etc...
func IndentLines ¶ added in v0.34.0
IndentLines returns a new string built from indenting each line of s.
func LineCount ¶
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 ParseBool ¶
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 ¶
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 ¶
PrefixSlice returns a new slice with each element of a prefixed with w, unless a is nil, in which case nil is returned.
func RepeatJoin ¶
RepeatJoin returns a string consisting of count copies of s separated by sep. For example:
stringz.RepeatJoin("?", 3, ", ") == "?, ?, ?"
func SanitizeAlphaNumeric ¶
SanitizeAlphaNumeric replaces any non-alphanumeric runes of s with r (which is typically underscore).
a#2%3.4_ --> a_2_3_4_
func SingleQuote ¶ added in v0.30.0
SingleQuote single-quotes (and escapes) s.
jessie's girl --> 'jessie''s girl'
func SliceIndex ¶
SliceIndex returns the index of needle in haystack, or -1.
func SprintJSON ¶
func Strings ¶ added in v0.32.0
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
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 SuffixSlice ¶ added in v0.33.0
SuffixSlice returns a new slice containing each element of a with suffix w. If a is nil, nil is returned.
func SurroundSlice ¶
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 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
UniqN returns a uniq string of length n. The first element is guaranteed to be a letter.
func UniqPrefix ¶ added in v0.16.0
UniqPrefix returns s with a unique prefix.
func UniqTableName ¶
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 Val ¶ added in v0.32.0
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
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.
Types ¶
This section is empty.