stringsutil

package
v0.41.5 Latest Latest
Warning

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

Go to latest
Published: Nov 28, 2022 License: MIT Imports: 9 Imported by: 103

Documentation

Index

Constants

View Source
const (
	StringToLower     = "StringToLower"
	SpaceToHyphen     = "SpaceToHyphen"
	SpaceToUnderscore = "SpaceToUnderscore"
)
View Source
const CommonInitialisms = "" /* 160-byte string literal not displayed */

CommonInitialisms is the listed by Go Lint.

Variables

This section is empty.

Functions

func Capitalize

func Capitalize(s1 string) string

Capitalize returns a string with the first character capitalized and the rest lower cased.

func CommonInitialismsMap

func CommonInitialismsMap() map[string]bool

CommonInitialismsMap returns map[string]bool of upper case initialisms.

func CondenseSpace

func CondenseSpace(s string) string

CondenseSpace removes extra spaces.

func CondenseString

func CondenseString(content string, joinLines bool) string

CondenseString trims whitespace at the ends of the string as well as in between.

func ContainsMore added in v0.34.4

func ContainsMore(s string, substrs []string, all, lc, trimSpaceSubstr bool) bool

func Dedupe

func Dedupe(elems []string) []string

Dedupe returns a string slice with duplicate values removed. First observance is kept.

func DigitsOnly

func DigitsOnly(input string) string

func EmptyError added in v0.36.2

func EmptyError(s string, err error) string

EmptyError takes a string and error, returning the string value or an empty string if an error is encountered. It is used for simplifying code that returns a value or an error if not present.

func EndsWith

func EndsWith(s string, substrs ...string) bool

func Equal

func Equal(str1, str2 string, trim, lower bool) bool

func EqualFoldFull added in v0.41.5

func EqualFoldFull(s, t string, caser *cases.Caser) bool

EqualFoldFull provides "full Unicode case-folding", unlike `strings.EqualFold` which provides "simple Unicode case-folding". If `caser` is set to `nil`, the default caser with no additional `cases.Option` is used.

func FirstNonEmpty

func FirstNonEmpty(vals ...string) string

func FirstNotEmptyTrimSpace

func FirstNotEmptyTrimSpace(candidates ...string) string

FirstNotEmptyTrimSpace returns the first non-empty string after applying `strings.TrimSpace()`.`

func FormatString

func FormatString(s string, options []string) string

func IfBoolString

func IfBoolString(boolVal bool, valueA, valueB string) string

func IndexMulti added in v0.37.7

func IndexMulti(s string, substr ...string) int

IndexMulti returns the earliest match.

func InterfaceToSliceString

func InterfaceToSliceString(s interface{}) []string

func IsAlpha added in v0.41.0

func IsAlpha(s string) bool

func IsAlphaNumeric added in v0.41.0

func IsAlphaNumeric(s string) bool

func IsNumeric added in v0.41.0

func IsNumeric(s string) bool

func JoinInterface

func JoinInterface(arr []interface{}, sep string, stripRepeatedSep bool, stripEmbeddedSep bool, altSep string) string

JoinInterface joins an interface and returns a string. It takes a join separator, boolean to replace the join separator in the string parts and a separator alternate. `stripEmbeddedSep` strips separator string found within parts. `stripRepeatedSep` strips repeating separators. This flexibility is designed to support joining data for both CSVs and paths.

func JoinLiterary

func JoinLiterary(slice []string, sep, joinWord string) string

func JoinLiteraryQuote

func JoinLiteraryQuote(slice []string, leftQuote, rightQuote, sep, joinWord string) string

func JoinStringsTrimSpaceToLowerSort

func JoinStringsTrimSpaceToLowerSort(strs []string, sep string) string

func JoinTrimSpace

func JoinTrimSpace(strs []string) string

func Match

func Match(s string, matchInfo MatchInfo) (bool, error)

Match provides an canonical way to match strings using multiple approaches

func Matrix2DColRowIndex added in v0.40.3

func Matrix2DColRowIndex(mat [][]string, colIdx uint, s string) (int, error)

Matrix2DColRowIndex returns the row index where the string supplied is first encountered for a supplied column index.

func NewlineToLinux

func NewlineToLinux(input string) string

func PadLeft

func PadLeft(str string, pad string, length int) string

PadLeft prepends a string to a base string until the string length is greater or equal to the desired length.

func PadRight

func PadRight(str string, pad string, length int) string

PadRight appends a string to a base string until the string length is greater or equal to the desired length.

func Quote

func Quote(str, beg, end string) string

func RemoveSpaces

func RemoveSpaces(input string) string

RemoveSpaces eliminates all spaces in a string.

func Reverse

func Reverse(s string) string

Reverse reverses string using strings.Builder. It's about 3 times faster than the one with using a string concatenation

func ReverseIndex

func ReverseIndex(s, substr string) int

ReverseIndex returns the `Index` after reversing the supplied string and substring.

func Slice2FilterLinesHaveIndex

func Slice2FilterLinesHaveIndex(groups [][]string, needle string, wantIndex int) [][]string

func SliceBySplitLength

func SliceBySplitLength(elems []string, sep string) map[int][]string

SliceBySplitLength returns lines by split length. This is useful for analyzing what types of data exist with different lengths.

func SliceChooseOnePreferredLowerTrimSpace

func SliceChooseOnePreferredLowerTrimSpace(options, preferenceOrder []string) string

func SliceCondenseAndQuote

func SliceCondenseAndQuote(items []string, trimLeft, trimRight, quoteLeft, quoteRight string) []string

func SliceCondenseAndQuoteSpace

func SliceCondenseAndQuoteSpace(elems []string, quoteLeft, quoteRight string) []string

func SliceCondensePunctuation

func SliceCondensePunctuation(elems []string) []string

func SliceCondenseRegexps

func SliceCondenseRegexps(elems []string, regexps []*regexp.Regexp, replacement string) []string

func SliceCondenseSpace

func SliceCondenseSpace(elems []string, dedupeResults, sortResults bool) []string

SliceCondenseSpace trims space from lines and removes empty lines. `unique` dedupes lines and `sort` preforms a sort on the results.

func SliceDedupe added in v0.36.4

func SliceDedupe(elems []string) []string

SliceDedupe removes duplicate occurrences of a string from a slice, keeping the first one encountered. It maintains the order of elements in the slice.

func SliceIndex

func SliceIndex(haystack []string, needle string, equalFold bool, caser *cases.Caser) int

SliceIndex returns the index of the first match using `=`. Returns -1 if not found. if `equalFold` is selected and `caser` is `nil`, the default caser will be used.

func SliceIndexContains added in v0.36.1

func SliceIndexContains(s []string, substr string) int

SliceIndexContains returns the index of the first match using `strings.Contains()`. Returns -1 if not found.

func SliceIndexMore

func SliceIndexMore(haystack []string, needle string, trimSpace, toLower bool, matchType MatchType) int

SliceIndexMore returns the index of an element in a string slice. Returns -1 if not found.

func SliceIndexOrEmpty

func SliceIndexOrEmpty(s []string, index uint64) string

SliceIndexOrEmpty returns the element at the index provided or an empty string.

func SliceIntersection

func SliceIntersection(list1, list2 []string) []string

func SliceIntersectionCondenseSpace

func SliceIntersectionCondenseSpace(slice1, slice2 []string) []string

func SliceIsEmpty

func SliceIsEmpty(elems []string, skipEmptyStrings bool) bool

SliceIsEmpty checks to see if a slice is empty. If `skipEmptyStrings` it will also return empty if all elements are empty strings or only contain spaces.

func SliceLineHasIndex

func SliceLineHasIndex(haystack []string, needle string, wantIndex int) bool

func SliceSplitLengthStats

func SliceSplitLengthStats(elems []string, sep string) map[int]int

SliceSplitLengthStats returns a `map[int]int` indicating how many strings of which length are present.

func SliceSubtract

func SliceSubtract(real, filter []string) []string

SliceSubtract uses Set math to remove elements of filter from real.

func SliceToMap

func SliceToMap(elems []string) map[string]int

func SliceToSingleIntOrNeg

func SliceToSingleIntOrNeg(vals []string) int

SliceToSingleIntOrNeg converts a single element slice with a string to an integer or `-1`

func SliceTrim

func SliceTrim(elems []string, cutstr string, condense bool) []string

SliceTrim trims each line in a slice of lines using a provided cut string.

func SliceTrimSpace

func SliceTrimSpace(elems []string, condense bool) []string

SliceTrimSpace removes leading and trailing spaces per string. If condense is used, empty strings are removed.

func SlicesCompare

func SlicesCompare(sliceA, sliceB []string) ([]string, []string, []string)

SlicesCompare returns 3 slices given 2 slices which represent intersection sets. The first set is present in slice A but not B, second for both and third present in slice B but not A.

func SplitCondenseSpace

func SplitCondenseSpace(s, sep string) []string

SplitCondenseSpace splits a string and trims spaces on remaining elements, removing empty elements.

func SplitLines

func SplitLines(s string) []string

func SplitTextLines

func SplitTextLines(text string) []string

SplitTextLines splits a string on the regxp `(\r\n|\r|\n)`.

func SplitTrimSpace

func SplitTrimSpace(s, sep string) []string

SplitTrimSpace splits a string and trims spaces on remaining elements.

func StringToConstant

func StringToConstant(s string) string

StringToConstant is used to generate constant names for code generation. It uses the commonInitialisms in Go Lint.

func StripControl

func StripControl(s string) string

func StripSubstring added in v0.36.1

func StripSubstring(s, substr string, insensitive bool) string

func SubstringIsSuffix

func SubstringIsSuffix(s1, s2 string) bool

func SuffixMap

func SuffixMap(inputs, suffixes []string) (prefixes []string, matches map[string]string, nonmatches []string)

func SuffixParse added in v0.41.0

func SuffixParse(s, wantSuffix string) (fullstring, prefix, suffix string)

func SuffixReplace added in v0.41.0

func SuffixReplace(s, oldSuffix, newSuffix string) string

func SuffixStrip added in v0.41.0

func SuffixStrip(s, suffix string) string

func ToBool

func ToBool(v string) bool

ToBool converts a string to a boolean value converting "f", "false", "0" and the empty string to false with everything else being true.

func ToLineFeeds

func ToLineFeeds(s string) string

func ToLowerFirst

func ToLowerFirst(s1 string) string

ToLowerFirst lower cases the first letter in the string

func ToOpposite

func ToOpposite(s string) string

func ToUpperFirst

func ToUpperFirst(s1 string, lowerRest bool) string

ToUpperFirst upper cases the first letter in the string

func TrimSentenceLength

func TrimSentenceLength(sentenceInput string, maxLength int) string

TrimSentenceLength trims a string by a max length at word boundaries.

func TrimSpaceOrDefault

func TrimSpaceOrDefault(str, defaultValue string) string

TrimSpaceOrDefault trims spaces and replaces default value if result is empty string.

func URLToMarkdownLinkHostname added in v0.37.0

func URLToMarkdownLinkHostname(url string) string

func Unshift

func Unshift(elems []string, x string) []string

Unshift adds an element at the first position of the slice.

Types

type MatchInfo

type MatchInfo struct {
	MatchType  MatchType
	String     string
	Regexp     *regexp.Regexp
	TimeLayout string
	TimeMin    time.Time
	TimeMax    time.Time
}

type MatchType

type MatchType int
const (
	MatchExact MatchType = iota
	MatchTrimSpace
	MatchTrimSpaceLower
	MatchRegexp
	MatchTimeGT
	MatchTimeGTE
	MatchTimeLT
	MatchTimeLTE
	MatchHasSuffix
	MatchHasPrefix
)

type Quoter

type Quoter struct {
	Beg string
	End string
}

func (Quoter) Quote

func (qtr Quoter) Quote(input string) string

type StrUtil

type StrUtil struct {
	RxSpaceBeg   *regexp.Regexp
	RxSpaceEnd   *regexp.Regexp
	RxSpacePunct *regexp.Regexp
	RxDash       *regexp.Regexp
}

func NewStrUtil

func NewStrUtil() StrUtil

func (*StrUtil) Trim

func (str *StrUtil) Trim(bytes []byte) []byte

type StringSlice

type StringSlice []string

func (StringSlice) Exists

func (sx StringSlice) Exists(s string) bool

type Stringable added in v0.39.4

type Stringable interface {
	String() string
}

type StringableWithErr added in v0.40.13

type StringableWithErr interface {
	String() (string, error)
}

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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