stringsutil

package
v0.62.6 Latest Latest
Warning

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

Go to latest
Published: Dec 24, 2023 License: MIT Imports: 11 Imported by: 104

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 CarriageReturnsToLinefeeds added in v0.47.0

func CarriageReturnsToLinefeeds(s string) string

CarriageReturnsToLinefeeds replaces `\r\n` with `\n` followed by replacing `\r` by `\n`.

func CommonInitialismsMap

func CommonInitialismsMap() map[string]bool

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

func CondenseLines added in v0.47.0

func CondenseLines(s, outNewline string) string

CondenseLines improves text layout by (a) remove staring and trailing spaces from eacn line, (b) remove starting and training new lines before and after characters, and (c) ensuring that there is a max of 2 consecutive line feeds.

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 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 any) []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 IsLower added in v0.46.0

func IsLower(s string) bool

func IsNumeric added in v0.41.0

func IsNumeric(s string) bool

func IsUpper added in v0.46.0

func IsUpper(s string) bool

func JoinInterface

func JoinInterface(arr []any, 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[C comparable](mat [][]C, colIdx uint, s C) (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 Repeat added in v0.51.0

func Repeat(s string, length uint) string

Repeat returns atring of length `length` by repeating string `s`. If `length` is less than then length of `s`, the result is cut to `length`.

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 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 SliceJoinFunc added in v0.53.0

func SliceJoinFunc(elems []string, sep string, f func(string) string) string

SliceJoinFunc joins a slice passing each elemen through the supplied function `f`.

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 SliceToDoc added in v0.54.0

func SliceToDoc(s []string, cfg map[string]int, trimSpace, inclEmpty bool) map[string]string

SliceToDoc converts a slice to a map, trimming the values if desired. The `cfg` keys are the document property names or keys and the values are the index location of the slice.

func SliceToMap

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

SliceToMap returns the slide where the slice elements are the keys of the map, and the value is the number of times it appears.

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 StripChars added in v0.47.0

func StripChars(s, cutset string) string

StripChars removes chars specified by `cutset` while maintaining order of remaining chars and shortening string per removed chars.

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 UniqueRunes added in v0.47.0

func UniqueRunes(s string) bool

UniqueRunes checks to see if a string's runes are unique.

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
	MatchStringTrimSpace
	MatchStringTrimSpaceLower
	MatchStringPrefix
	MatchStringSuffix
	MatchStringRegexp
	MatchTimeGT
	MatchTimeGTE
	MatchTimeLT
	MatchTimeLTE
)

type Quoter

type Quoter struct {
	Beg         string
	End         string
	SkipNesting bool
}

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