Documentation
¶
Overview ¶
Go programming helpers for common string-processing needs.
Index ¶
- func After(val string, needle string, elseempty bool) string
- func AfterLast(val string, needle string, elseempty bool) string
- func AnyOf(val string, anyof ...string) bool
- func Before(val string, needle string, elseempty bool) string
- func BeginsUpper(s string) bool
- func Both(s1 string, join string, s2 string) string
- func BreakAt(val string, index int) (left string, right string)
- func BreakOn(val string, on string) (left string, right string)
- func BreakOnLast(val string, on string) (left string, right string)
- func Concat(vals ...string) string
- func DistBetween(val string, needle1 string, needle2 string) int
- func ExtractAllIdentifiers(src, prefix string) (identifiers []string)
- func ExtractFirstIdentifier(src, prefix string, minPos int) (identifier string)
- func First(check func(s string) bool, step int, vals ...string) string
- func FirstNonEmpty(vals ...string) (val string)
- func FirstRune(s string) (r rune)
- func FromInt(i int) string
- func Has(s, substr string) bool
- func HasAny(s string, subs ...string) bool
- func HasAnyCase(s1, s2 string) bool
- func HasAnyPrefix(s string, prefixes ...string) bool
- func HasAnySuffix(s string, suffixes ...string) bool
- func HasOnce(str1, str2 string) bool
- func Idx(val string, needle string) int
- func Ifm(cond bool, ifTrue, ifFalse map[string]string) map[string]string
- func Ifs(cond bool, ifTrue, ifFalse string) string
- func IndexAny(s string, seps ...string) (pos int)
- func IsAscii(str string) bool
- func IsLower(s string) bool
- func IsOneOf(s string, all ...string) bool
- func IsUpper(s string) bool
- func IsUpperAscii(s string) bool
- func Join(vals []string, delim string) string
- func LettersOnly(s string) string
- func Longest(vals ...string) (maxlen int)
- func MapEq(m1, m2 map[string]string) bool
- func MatchesAny(value string, patterns ...string) bool
- func N(vals ...string) []string
- func NonEmpties(breakAtFirstEmpty bool, vals ...string) (slice []string)
- func NotPrefixed(pref string) func(string) bool
- func PadRight(s string, ensurelen int) string
- func ParseBool(s string) bool
- func ParseFloat(s string) float64
- func ParseFloats(vals ...string) []float64
- func ParseInt(s string) int64
- func ParseUint(s string) uint64
- func Pluralize(s string) string
- func Pref(val string, pref string) bool
- func PrefixWithSep(prefix, sep, v string) string
- func PrependIf(s, p string) string
- func ReduceSpaces(s string) string
- func Replace(str string, repls map[string]string) string
- func SafeIdentifier(s string) string
- func Split(v, sep string) (sl []string)
- func SplitOnce(v string, sep rune) (left string, right string)
- func StripPrefix(val, prefix string) string
- func StripSuffix(val, suffix string) string
- func Suff(val string, suff string) bool
- func ToInt(s string) (i int)
- func ToLowerIfUpper(s string) string
- func ToUpperIfLower(s string) string
- func Trim(val string) string
- func Until(s string, r rune) string
- type Buffer
- type Matcher
- type Pattern
- type RuneCaser
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func BeginsUpper ¶
func ExtractAllIdentifiers ¶
Extracts all "identifiers" (as per `ExtractFirstIdentifier`) in `src` and starting with `prefix` (no duplicates, ordered by occurrence).
func ExtractFirstIdentifier ¶
Extracts the first occurrence (at or after `minPos`) of the "identifier" starting with `prefix` in `src`.
func First ¶
Returns the first `string` in `vals` to match the specified `check`.
`step`: 1 to test all values, a higher value to skip n values after each test, negative for reverse slice traversal, or use 0 to get stuck in an infinite loop.
func FirstNonEmpty ¶
Returns the first non-empty `string` in `vals`.
func HasAnyCase ¶
Returns whether `s1` contains `s2` or lower-case `s1` contains lower-case `s2`.
func HasAnyPrefix ¶
Returns whether `s` starts with any one of the specified `prefixes`.
func HasAnySuffix ¶
Returns whether `s` ends with any one of the specified `suffixes`.
func IndexAny ¶
For all `seps`, records its position of first occurrence in `s`, then returns the smallest such position.
func IsUpperAscii ¶
func LettersOnly ¶
Returns a representation of `s` with all non-`unicode.IsLetter` runes removed.
func MatchesAny ¶
Uses a `Matcher` to determine whether `value` matches any one of the specified simple-`patterns`.
func NonEmpties ¶
Returns a slice that contains the non-empty items in `vals`.
func NotPrefixed ¶
func ParseFloats ¶
Returns the parsed `float64`s from `vals` in the same order, or `nil` if one of them failed to parse.
func Pluralize ¶
A most simplistic (not linguistically-correct) English-language pluralizer that may be useful for code or doc generation.
If `s` ends with "s", only appends "es": bus -> buses, mess -> messes etc.
If `s` ends with "y" (but not "ay", "ey", "oy", "uy" or "iy"), removes "y" and appends "ies": autonomy -> autonomies, dictionary -> dictionaries etc.
Otherwise, appends "s": gopher -> gophers, laptop -> laptops etc.
func PrefixWithSep ¶
Prepends `prefix + sep` to `v` only if `prefix` isn't empty.
func ReduceSpaces ¶
All occurrences in `s` of multiple subsequent spaces in a row are collapsed into one single space.
func Replace ¶
Replaces in `str` all occurrences of all `repls` hash-map keys with their respective associated (mapped) value.
func SafeIdentifier ¶
Creates a Pascal-cased "identifier" version of the specified string.
func StripPrefix ¶
Strips `prefix` off `val` if possible.
func StripSuffix ¶
Strips `suffix` off `val` if possible.
func ToLowerIfUpper ¶
Returns the lower-case representation of `s` only if it is currently fully upper-case as per `IsUpper`.
func ToUpperIfLower ¶
Returns the upper-case representation of `s` only if it is currently fully lower-case as per `IsLower`.
Types ¶
type Buffer ¶
A convenient wrapper for `bytes.Buffer`.
type Matcher ¶
type Matcher struct {
// contains filtered or unexported fields
}
Matches a string against "simple-patterns": patterns that can have asterisk (*) wildcards only at the beginning ("ends-with"), at the end ("begins-with"), or both ("contains"), or not at all ("equals").
For more complex pattern-matching needs, go forth and unleash the full force of the standard library's `regexp` package. But I found that in a big portion of pattern-matching use-cases, I'm just doing "begins-or-ends-or-contains-or-equals" testing. Hence the conception of the "simple-pattern".
There is also an alternative `Pattern` type in this package. Use `Matcher` to match strings against multiple patterns at once, especially if the patterns don't change often and the matchings occur frequently / repeatedly. In simpler, rarer one-off matchings, `Pattern` is preferable for simpler "setup-less" matching.
func (*Matcher) AddPatterns ¶
Adds the specified simple-`patterns` to me.
func (*Matcher) HasWildcardPatterns ¶
Returns whether any of the simple-patterns specified for `me` declares a (usable) *-wildcard.
type Pattern ¶
type Pattern string
An "leaner" alternative to `Matcher` (see docs for `Matcher`). This represents a single "simple-pattern" and provides matching methods for one or multiple values.
func (Pattern) AnyMatches ¶
Returns the first of the specified `values` to match this simple-pattern, or empty if none of them match.