Documentation ¶
Overview ¶
Package strut ("string utilities") provides additional functions for string processing not present in package strings.
Index ¶
- func Arg(arg string) string
- func Args(args []string) string
- func ArgsAfter(args []string) string
- func ArgsBefore(args []string) string
- func ArgsBetween(args []string) string
- func CommonPrefix(ss []string) string
- func CommonSuffix(ss []string) string
- func Filepath(path string) string
- func IndexOfLongest(s []string) int
- func IndexOfShortest(s []string) int
- func IndexRune(s string, runeIndex int) int
- func IterateSubstrings(s string, minSubstringRuneCount int, maxSubstringRuneCount int, ...)
- func IterateSubstringsUnique(s string, minSubstringRuneCount int, maxSubstringRuneCount int, ...)
- func JoinUnary(elems []string, separator string) string
- func NewBoolToStringFunc(trueString, falseString string) func(bool) string
- func PrefixLen(p string, q string) int
- func RangeLen(s string) int
- func RangeReplaceAll(ss []string, old string, new string) []string
- func Redact(secret string) string
- func RedactMinMax(secret string, minRedact int, maxReveal int) string
- func RuneIndex(s string, i int) int
- func Slash(elems ...string) string
- func SliceClusters(s string, fromGraphemeClusterIndex, toGraphemeClusterIndex int) string
- func SliceRunes(s string, fromRuneIndex, toRuneIndex int) string
- func SplitTwinUnescape(s string, sep string) (unescapedSubstrings []string)
- func SuffixLen(p string, q string) int
- func Translate(value int, firstIndex int, translations ...string) string
- func TrimPrefixAll(s string, prefix string) string
- func TrimPrefixN(s string, prefix string, n int) string
- func TrimSequenceAll(s string, sequence string) string
- func TrimSuffixAll(s string, suffix string) string
- func TrimSuffixN(s string, suffix string, n int) string
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func Arg ¶
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 ¶
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 ¶
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 ¶
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
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 ¶
CommonPrefix returns the longest prefix common to all strings of s.
func CommonSuffix ¶
CommonSuffix returns the longest suffix common to all strings of s.
func IndexOfLongest ¶
IndexOfLongest returns the index of the first string for which no longer string exists in s, or -1 if len(s) == 0.
func IndexOfShortest ¶
IndexOfShortest returns the index of the first string for which no shorter string exists in s, or -1 if len(s) == 0.
func IndexRune ¶
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 ¶
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 ¶
Returns a function which takes a bool parameter that returns trueString if the parameter is true and falseString if the parameter is false.
func RangeLen ¶
RangeLen returns the amount of iterations which would take place when iterating over s using the range-expression.
func RangeReplaceAll ¶
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 ¶
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 ¶
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 ¶
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 ¶
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 ¶
SliceClusters is like SliceRunes, except that it operates on grapheme cluster indices rather than rune indices.
func SliceRunes ¶
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
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 Translate ¶
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 ¶
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 ¶
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 ¶
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 ¶
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.
Types ¶
This section is empty.