strut

package module
v1.1.0 Latest Latest
Warning

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

Go to latest
Published: May 25, 2023 License: MIT Imports: 5 Imported by: 1

README

strut (string utilities)

Provides additional desirable functions for string processing not present in package strings.

Function overview

Arguments (args.go)

These functions are useful for printing string slice contents and variadic string arguments.

Function Explanation
Arg Returns arg as a backquoted string where possible and sensible; e.g. for arg = "\\n" returns `\n`, but for arg = "\n" returns "\\n".
Args Like Arg(), but for []string, joined together with ", ".
ArgsBefore Like Args(), but appends an extra ", " if at least one string was passed.
ArgsAfter Like Args(), but prepends an extra ", " if at least one string was passed.
ArgsBetween Like Args(), but prepends and appends an extra ", " if at least one string was passed.

Bool to string (bool.go)

Function Explanation
NewBoolToStringFunc Returns a function which takes a bool parameter and returns trueString if the parameter is true and falseString if the parameter is false.

The functions in the following table are contained in sub-package but:

Function Explanation
X Returns "x" if bool is true and "" if bool is false.
XU Returns "X" if bool is true and "" if bool is false.
OZ Returns "1" if bool is true and "0" if bool is false.
Tf Returns "true" if bool is true and "false" if bool is false.
TF Returns "True" if bool is true and "False" if bool is false.
TFU Returns "TRUE" if bool is true and "FALSE" if bool is false.
Y Returns "y" if bool is true and "n" if bool is false.
YU Returns "Y" if bool is true and "N" if bool is false.
Yn Returns "yes" if bool is true and "no" if bool is false.
YN Returns "Yes" if bool is true and "No" if bool is false.
YNU Returns "YES" if bool is true and "NO" if bool is false.
Oo Returns "on" if bool is true and "off" if bool is false.
OO Returns "On" if bool is true and "Off" if bool is false.
OOU Returns "ON" if bool is true and "OFF" if bool is false.

Find longest/shortest (find.go)

Function Explanation
IndexOfLongest Returns index of first string in passed []string for which no shorter string exists.
IndexOfShortest Returns index of first string in passed []string for which no longer string exists.

Join (join.go)

Function Explanation
JoinUnary Like strings.Join(), except that all leading and trailing occurrences of sep within elems are trimmed where they are going to be joined with sep beforehand.
Slash Shorthand for JoinUnary(elems, "/") which accepts a variadic argument list.

Misc (replace.go, translate.go)

Function Explanation
RangeReplaceAll Returns a copy of a []string where strings.ReplaceAll() was called for every string.
Translate Map a non-zero-indexed integer constant to a list of translation terms in a []string.

Pre- and postfix analysis (pfix.go)

Function Explanation
CommonPrefix Returns the longest prefix common to all strings in passed []string.
PrefixLen Returns the length of the longest common prefix of two strings.
CommonSuffix Returns the longest suffix common to all strings in passed []string.
SuffixLen Returns the length of the longest common suffix of two strings.

Process user input (user.go)

Function Explanation
Filepath Like filepath.Clean(), but treats both / and \ as a file path element separator regardless of OS.
RedactMinMax Redact up to minRedact runes before leaving no more than minReveal runes non-redacted in input string.
Redact Shorthand for RedactMinMax(secret, 12, 2). E.g. abcd-1234-efgh becomes a...h.

Slicing and indexing (index.go)

Function Explanation
IndexRune Returns byte index for given rune index.
RuneIndex Returns index of rune at given byte index.
RangeLen Returns the amount of iterations which take place when ranging over given string.
SliceRunes Slice a string with rune indices instead of byte indices.
SliceClusters Slice a string with grapheme cluster indices instead of byte or rune indices.

Splitting (split.go)

Function Explanation
SplitTwinUnescape Split string around single occurrences of separator, reducing double occurrences to a single occurrence.

Substrings (substrings.go)

Function Explanation
IterateSubstrings Invoke a callback-function for every substring having between minSubstringRuneCount and maxSubstringRuneCount runes.
IterateSubstringsUnique Like IterateSubstrings(), but never calls the callback-function more than once for equal substrings.

Trim (trim.go)

Function Explanation
TrimPrefixAll Like strings.TrimPrefix(), but repeats until all occurrences of the prefix are gone.
TrimPrefixN Like TrimPrefixAll, but limited to trimming the prefix no more than n times.
TrimSuffixAll Like strings.TrimSuffix(), but repeats until all occurrences of the suffix are gone.
TrimSuffixN Like TrimSuffixAll, but limited to trimming the suffix no more than n times.
TrimSequenceAll Shorthand for TrimSuffixAll(TrimSequenceAll(s)).

Documentation

Overview

Package strut ("string utilities") provides additional functions for string processing not present in package strings.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Arg

func Arg(arg string) string

Arg is a shorthand for fmt.Sprintf("%#q", arg) which returns `arg` as a backquoted string where possible and sensible; e.g. for arg = "\\n" returns `\n`, but for arg = "\n" returns "\\n".

func Args

func Args(args []string) string

Args returns a string joining the "%#q"-formatted string of every string in args with ", ". E.g. Args([]string{"Hello\nWorld!", "D:\\Foo"}) returns a string which prints as "Hello\nWorld", `D:\Foo`.

func ArgsAfter

func ArgsAfter(args []string) string

ArgsAfter is like Args, but also adds the prefix ", " at the start of the returned string if len(args) > 0. I.e. args appear *after* some other preceding arguments.

func ArgsBefore

func ArgsBefore(args []string) string

ArgsBefore is like Args, but also adds the suffix ", " at the end of the returned string if len(args) > 0. I.e. args appear *before* some other arguments that follow.

func ArgsBetween added in v1.1.0

func ArgsBetween(args []string) string

ArgsBetween is like Args, but also adds the prefix ", " at the start and the suffix ", " at the end of the returned string if len(args) > 0. I.e. args appear *between* some other surrounding arguments.

func CommonPrefix

func CommonPrefix(ss []string) string

CommonPrefix returns the longest prefix common to all strings of s.

func CommonSuffix

func CommonSuffix(ss []string) string

CommonSuffix returns the longest suffix common to all strings of s.

func Filepath

func Filepath(path string) string

Filepath is a shorthand for filepath.Clean(strings.ReplaceAll(path, `\`, `/`)).

func IndexOfLongest

func IndexOfLongest(s []string) int

IndexOfLongest returns the index of the first string for which no longer string exists in s, or -1 if len(s) == 0.

func IndexOfShortest

func IndexOfShortest(s []string) int

IndexOfShortest returns the index of the first string for which no shorter string exists in s, or -1 if len(s) == 0.

func IndexRune

func IndexRune(s string, runeIndex int) int

IndexRune returns the byte index of rune index runeIndex in O(n). This function panics if runeIndex is less than zero or greater than the amount of runes in s.

func IterateSubstrings

func IterateSubstrings(s string, minSubstringRuneCount int, maxSubstringRuneCount int, iterFunc func(from, to int))

IterateSubstrings calls iterFunc for every substring of s which has at least a length of minSubstringRuneCount and at most a length of maxSubstringRuneCount. When iterFunc is called, the substring is given by s[from:to].

func IterateSubstringsUnique

func IterateSubstringsUnique(s string, minSubstringRuneCount int, maxSubstringRuneCount int, iterFunc func(from, to int))

Like IterateSubstrings, but never invokes iterFunc more than once for multiple occurrences of equal substrings. This function also allows to set minSubstringRuneCount to 0 to cause iterFunc(0, 0) to be called with respect to the empty substring.

func JoinUnary

func JoinUnary(elems []string, separator string) string

JoinUnary is like strings.Join(elems, sep), except that all leading and trailing occurrences of sep within elems are trimmed where they are going to be joined with sep beforehand. Non-trailing leading separators in the first element as well as non-leading trailing separators in the last element are kept.

func NewBoolToStringFunc

func NewBoolToStringFunc(trueString, falseString string) func(bool) string

Returns a function which takes a bool parameter that returns trueString if the parameter is true and falseString if the parameter is false.

func PrefixLen

func PrefixLen(p string, q string) int

PrefixLen returns the length of the longest common prefix of p and q.

func RangeLen

func RangeLen(s string) int

RangeLen returns the amount of iterations which would take place when iterating over s using the range-expression.

func RangeReplaceAll

func RangeReplaceAll(ss []string, old string, new string) []string

RangeReplaceAll returns a new slice which is a copy of ss with every contained string having all non-overlapping instances of old replaced by new.

func Redact

func Redact(secret string) string

Redact is a shorthand for RedactMinMax(secret, 12, 2), which has a minRedact parameter of 12 set according to NIST publications regarding recommended minimum password length and a maxReveal parameter of 2, never letting more than the first and the last character of secret be revealed.

func RedactMinMax

func RedactMinMax(secret string, minRedact int, maxReveal int) string

RedactMinMax returns secret with up to minRedact characters redacted before allowing at most maxReveal characters to not be redacted. The amount of characters not redacted is distributed between the leading and the trailing characters of secret, whereby leading characters take precedence in case the amount of characters to be left revealed is odd.

While this function is agnostic to the contents of secret, it is not agnostic to its length, i.e. this function does not execute in constant time and thus is not cryptographically secure to be called multiple times. It is intended to be called only once per secret per program execution, e.g. for logging during startup of a service.

func RuneIndex

func RuneIndex(s string, i int) int

RuneIndex returns the rune index of the rune at byte index i in O(n). If i does not index the firts byte of a rune, returns the index of the rune which that byte is a part of. This function panics if i is less than zero or greater than len(s).

func Slash

func Slash(elems ...string) string

Slash is like strings.Join(elems, "/"), except that all leading and trailing occurrences of '/' between elems are trimmed before they are joined together. Non-trailing leading slashes in the first element as well as non-leading trailing slashes in the last element are kept.

func SliceClusters

func SliceClusters(s string, fromGraphemeClusterIndex, toGraphemeClusterIndex int) string

SliceClusters is like SliceRunes, except that it operates on grapheme cluster indices rather than rune indices.

func SliceRunes

func SliceRunes(s string, fromRuneIndex, toRuneIndex int) string

SliceRunes returns the result of s[IndexRune(s, fromRuneIndex) : IndexRune(s, toRuneIndex)] in O(n). This function panics if either of the passed indices is less than zero or greater than the amount of runes in s, or when fromRuneIndex is greater than toRuneIndex.

func SplitTwinUnescape added in v1.1.0

func SplitTwinUnescape(s string, sep string) (unescapedSubstrings []string)

SplitTwinUnescape splits s around all occurrences of sep, except where sep occurs twice in a row which will be reduced to a single occurrence.

If s does not contain an isolated occurrence of sep, this function returns a slice whose only element is s.

If sep is the empty string, this function panics.

func SuffixLen

func SuffixLen(p string, q string) int

SuffixLen returns the length of the longest common suffix of p and q.

func Translate

func Translate(value int, firstIndex int, translations ...string) string

Translate returns translations[value - firstIndex], except that if index would be out of bounds, it returns strconv.Itoa(value) instead. This function is intended to obtain humanly readable names for package-level constants which cannot be found with reflection.

E.g. Translate(Mouse, Cat, "cat", "dog", "mouse", "bird") returns "mouse", given that the value of Mouse equals the value of Cat+2.

func TrimPrefixAll

func TrimPrefixAll(s string, prefix string) string

TrimPrefixAll returns a new string with all occurrences of prefix at the start of s removed. If prefix is the empty string, this function returns s.

func TrimPrefixN

func TrimPrefixN(s string, prefix string, n int) string

TrimPrefixN returns a new string with up to n occurrences of prefix at the start of s removed. If prefix is the empty string, this function returns s. If n is negative, returns TrimPrefixAll(s, prefix).

func TrimSequenceAll

func TrimSequenceAll(s string, sequence string) string

TrimSequenceAll returns a new string with all occurrences of sequence at the start and end of s removed. If sequence is the empty string, this function returns s.

func TrimSuffixAll

func TrimSuffixAll(s string, suffix string) string

TrimSuffixAll returns a new string with all occurrences of suffix at the end of s removed. If suffix is the empty string, this function returns s.

func TrimSuffixN

func TrimSuffixN(s string, suffix string, n int) string

TrimSuffixN returns a new string with up to n occurrences of suffix at the end of s removed. If suffix is the empty string, this function returns s. If n is negative, returns TrimSuffixAll(s, suffix).

Types

This section is empty.

Directories

Path Synopsis
Package but ("bool utilities") provides helper functions to convert from bool to string.
Package but ("bool utilities") provides helper functions to convert from bool to string.

Jump to

Keyboard shortcuts

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