stringutil

package
v1.9.5 Latest Latest
Warning

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

Go to latest
Published: Aug 26, 2023 License: MIT Imports: 19 Imported by: 29

Documentation

Overview

Utilities for converting and manipulating data to and from strings

Index

Constants

View Source
const (
	None  SiPrefix = 0
	Kilo           = 1
	Mega           = 2
	Giga           = 3
	Tera           = 4
	Peta           = 5
	Exa            = 6
	Zetta          = 7
	Yotta          = 8
)
View Source
const (
	Invalid ConvertType = utils.Invalid
	Nil                 = utils.Nil
	String              = utils.String
	Boolean             = utils.Boolean
	Float               = utils.Float
	Integer             = utils.Integer
	Time                = utils.Time
	Bytes               = utils.Bytes
)

Variables

View Source
var BooleanFalseValues = utils.BooleanFalseValues
View Source
var BooleanTrueValues = utils.BooleanTrueValues
View Source
var DefaultDecimalSeparator = `.`
View Source
var DefaultThousandsSeparator = `,`
View Source
var ExpandEnvPreserveIfEmpty = false

If set to true, ExpandEnv() will preserve ${...} sequences whose resulting value would yield an empty string. Fallback and formatting values are still supported, as the determination of value emptiness is made after parsing the sequence. This is to avoid unset environment variables resulting in the escape sequences being erased, which is often the case when running ExpandEnv() against various shell languages (Bash, et. al)

View Source
var ExpandEnvTempDelimiterClose = "\u3019" // RIGHT WHITE TORTOISE SHELL BRACKET (U+3019, Pe): 〙
View Source
var ExpandEnvTempDelimiterOpen = "\u3018" // LEFT WHITE TORTOISE SHELL BRACKET (U+3018, Ps): 〘
View Source
var NilStrings = utils.NilStrings
View Source
var TimeFormats = utils.TimeFormats

Functions

func Autotype

func Autotype(in interface{}) interface{}

func Camelize

func Camelize(in interface{}) string

func ConvertTo

func ConvertTo(toType ConvertType, inI interface{}) (interface{}, error)

func ConvertToBool

func ConvertToBool(in interface{}) (bool, error)

func ConvertToBytes

func ConvertToBytes(in interface{}) ([]byte, error)

func ConvertToFloat

func ConvertToFloat(in interface{}) (float64, error)

func ConvertToInteger

func ConvertToInteger(in interface{}) (int64, error)

func ConvertToString

func ConvertToString(in interface{}) (string, error)

func ConvertToTime

func ConvertToTime(in interface{}) (time.Time, error)

func DetectTimeFormat

func DetectTimeFormat(in string) string

func Elide

func Elide(in string, charcount int, trailer ...string) string

Truncate the given string to a certain number of characters.

func ElideRight

func ElideRight(in string, charcount int, leader ...string) string

Truncate the given string to a certain number of characters from the end.

func ElideWords

func ElideWords(in string, wordcount int) string

Truncate the given string to a certain number of words.

func ExpandEnv

func ExpandEnv(in string) string

Return the given string with environment variable substitution sequences expanded and (optionally) formatted. This function operates similarly to os.ExpandEnv, but accepts custom fmt.Printf formatting directives.

func HasAnyPrefix

func HasAnyPrefix(s string, prefixes ...string) bool

HasPrefix tests whether the string s begins with any prefix in prefixes.

func HasAnySuffix

func HasAnySuffix(s string, suffixes ...string) bool

HasSuffix tests whether the string s begins with any suffix in suffixes.

func Hyphenate

func Hyphenate(in interface{}) string

func IsBoolean

func IsBoolean(in interface{}) bool

func IsBooleanFalse

func IsBooleanFalse(in interface{}) bool

func IsBooleanTrue

func IsBooleanTrue(in interface{}) bool

func IsFloat

func IsFloat(in interface{}) bool

func IsHexadecimal

func IsHexadecimal(in string, length int) bool

Returns whether the given string is a hexadecimal number. If the string is prefixed with "0x", the prefix is removed first. If length is greater than 0, the length of the input (excluding prefix) is checked as well.

func IsInteger

func IsInteger(in interface{}) bool

func IsMixedCase

func IsMixedCase(in string) bool

Returns whether the letters (Unicode Catgeory 'L') in a given string are homogenous in case (all upper-case or all lower-case).

func IsNumeric

func IsNumeric(in interface{}) bool

func IsSeparator

func IsSeparator(r rune) bool

func IsSurroundedBy

func IsSurroundedBy(inI interface{}, prefix string, suffix string) bool

func IsTime

func IsTime(in interface{}) bool

func LongestCommonPrefix

func LongestCommonPrefix(inputs []string) string

func MustBool

func MustBool(in interface{}, fallbackOpt ...bool) bool

func MustFloat

func MustFloat(in interface{}, fallbackOpt ...float64) float64

func MustInteger

func MustInteger(in interface{}, fallbackOpt ...int64) int64

func MustString

func MustString(in interface{}, fallbackOpt ...string) string

func MustTime

func MustTime(in interface{}, fallbackOpt ...time.Time) time.Time

func PrefixEach

func PrefixEach(in []string, prefix string) []string

Prefix each element in the given string slice with prefix.

func PrefixIf

func PrefixIf(in string, prefix string) string

Prefix the given string if it is non-empty

func PrefixLines

func PrefixLines(in interface{}, prefix string) string

Takes the given string, splits it into lines, and prefixes each line with the given prefix string.

func RelaxedEqual

func RelaxedEqual(first interface{}, second interface{}) (bool, error)

func Snakeify

func Snakeify(in interface{}, separator rune) string

func SplitLines

func SplitLines(in interface{}, delimiter string) []string

Split the given input into lines.

func SplitPair

func SplitPair(in string, delimiter string) (string, string)

Split the given string into two parts. If there is only one resulting part, that part will be the first return value and the second return value will be empty.

func SplitPairRight

func SplitPairRight(in string, delimiter string) (string, string)

Split the given string into two parts from the right. If there is only one resulting part, that part will be the first return value and the second return value will be empty.

func SplitPairRightTrailing

func SplitPairRightTrailing(in string, delimiter string) (string, string)

Split the given string into two parts. If there is only one resulting part, that part will be the second return value and the first return value will be empty.

func SplitPairTrailing

func SplitPairTrailing(in string, delimiter string) (string, string)

Split the given string into two parts. If there is only one resulting part, that part will be the second return value and the first return value will be empty.

func SplitTriple

func SplitTriple(in string, delimiter string) (string, string, string)

func SplitWords

func SplitWords(in string) []string

Split the given string into words.

func Squeeze

func Squeeze(in string) string

Return the given string with sequences repeating character replaced with a single instance of that character.

func SqueezeFunc

func SqueezeFunc(in string, fn OnlySqueezeFunc) string

Return the given string with sequences of characters matching the given function replaced with a single instance of that character.

func SqueezeSpace

func SqueezeSpace(in string) string

Return the given string with sequences of whitespace characters replaced with a single instance of that character.

func SuffixEach

func SuffixEach(in []string, prefix string, suffix string) []string

Suffix each element in the given string slice with suffix.

func SuffixIf

func SuffixIf(in string, suffix string) string

Suffix the given string if it is non-empty

func SuffixLines

func SuffixLines(in interface{}, suffix string) string

Takes the given string, splits it into lines, and suffixes each line with the given suffix string.

func Thousandify

func Thousandify(in interface{}, separator string, decimal string) string

func ToByteString

func ToByteString(in interface{}, formatString ...string) (string, error)

func ToBytes

func ToBytes(input string) (float64, error)

func ToString

func ToString(in interface{}) (string, error)

func ToStringSlice

func ToStringSlice(in interface{}) ([]string, error)

func TokenizeFunc

func TokenizeFunc(in string, tokenizer func(rune) bool, partfn func(part string) []string) []string

func Underscore

func Underscore(in interface{}) string

func Unwrap

func Unwrap(in string, prefix string, suffix string) string

Return the given string with the given prefix and suffix removed.

func Wrap

func Wrap(in string, prefix string, suffix string) string

Return the given string with prefixed and suffixed with other strings.

func WrapEach

func WrapEach(in []string, prefix string, suffix string) []string

Wrap each element in the given string slice with prefix and suffix.

func WrapIf

func WrapIf(in string, prefix string, suffix string) string

Wrap the given string if it is non-empty

Types

type ConvertType

type ConvertType = utils.ConvertType

func ParseType

func ParseType(name string) ConvertType

type InterceptFunc

type InterceptFunc func(seq []byte)

type OnlySqueezeFunc

type OnlySqueezeFunc func(r rune) bool

type ScanInterceptor

type ScanInterceptor struct {
	Disabled bool
	// contains filtered or unexported fields
}

A ScanInterceptor is used as a SplitFunc on a bufio.Scanner. It will look at the stream of bytes being scanned for specific substrings. The registered handler function associated with a substring will be called whenever it is seen in the stream. The passthrough SplitFunc is called as normal. This allows for a stream to be split and processed while also being inspected for specific content, allowing the user to react to that content as it comes by.

func NewScanInterceptor

func NewScanInterceptor(passthrough bufio.SplitFunc, intercepts ...map[string]InterceptFunc) *ScanInterceptor

func (*ScanInterceptor) BytesScanned

func (self *ScanInterceptor) BytesScanned() int64

Return the total number of bytes this scanner has scanned.

func (*ScanInterceptor) Intercept

func (self *ScanInterceptor) Intercept(sequence string, handler InterceptFunc)

Add an intercept sequence and handler. If the sequence is already registered, its handler function will be replaced with this one.

func (*ScanInterceptor) InterceptCounts

func (self *ScanInterceptor) InterceptCounts() map[string]int64

Returns a map of intercept sequences and the number of times each one was fired.

func (*ScanInterceptor) Scan

func (self *ScanInterceptor) Scan(data []byte, atEOF bool) (advance int, token []byte, err error)

Implements the bufio.SplitFunc function signature for use in a bufio.Scanner.

type SiPrefix

type SiPrefix int

func GetSiPrefix

func GetSiPrefix(input string) (SiPrefix, error)

func (SiPrefix) String

func (self SiPrefix) String() string

type Uuid

type Uuid struct {
	uuid.UUID
}

func MustUUID

func MustUUID(in string) *Uuid

func ParseUUID

func ParseUUID(in string) (*Uuid, error)

func UUID

func UUID() *Uuid

func UuidFromBytes

func UuidFromBytes(b []byte) (*Uuid, error)

func (*Uuid) Base58

func (self *Uuid) Base58() string

func (*Uuid) Base64

func (self *Uuid) Base64() string

func (*Uuid) Bytes

func (self *Uuid) Bytes() []byte

func (*Uuid) Hex

func (self *Uuid) Hex() string

Jump to

Keyboard shortcuts

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