stringutil

package
v1.3.8 Latest Latest
Warning

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

Go to latest
Published: Apr 11, 2022 License: MIT Imports: 10 Imported by: 2

Documentation

Overview

Package stringutil contains utility functions for working with strings.

Index

Constants

View Source
const (
	// DOpen is the character sequence used to determine when a
	// block of templated variable starts.
	DOpen = "{{"
	// DClose is the character sequence used to determine when a
	// block of templated variable closes.
	DClose = "}}"
	// DOpenLen is the length of the DOpen characters
	DOpenLen = len(DOpen)
	// DCloseLen is the length of the DClose characters
	DCloseLen = len(DClose)
)

Variables

This section is empty.

Functions

func Anonymize

func Anonymize(arr []string) []interface{}

Anonymize converts an array of strings to an array of anonymous interfaces

func AnonymizeRunes

func AnonymizeRunes(arr []rune) []interface{}

AnonymizeRunes converts an array of runes to an array of anonymous interfaces

func AppendIfMissing

func AppendIfMissing(slice []string, i string) []string

AppendIfMissing adds a string to a slice if it's not already in it

func ByteToHexASCII

func ByteToHexASCII(b []byte) []byte

ByteToHexASCII returns a byte slice containing the hex representation of the passed byte array in ASCII characters.

func Clean

func Clean(s []string) []string

Clean removes empty strings from a slice of strings and returns the new slice.

func CleanWhiteSpace

func CleanWhiteSpace(s []string) []string

CleanWhiteSpace removes all strings in the passed slice that contain only white space.

func ConstantTimeComparison

func ConstantTimeComparison(expected, actual string) bool

ConstantTimeComparison evaluates strings in a constant time to avoid hack attempts based on string comparison response rates

func Contains

func Contains(slice []string, i string) bool

Contains checks if a string is in a slice

func Dash added in v1.2.5

func Dash(s string) string

Dash converts strings to dashed version (e.g. CamelCase to camel-case)

func Detemplate

func Detemplate(template string, rendered string) (map[string]string, error)

Detemplate takes a pattern with template style markup and a rendered template and attempts to construct the context that would be fed to generate the rendering. NOTE: This function does not support sequential template variables. e.g. 'test {{id1}}{{id2}}' as there is no way of knowing where one would end

and the next would begin.

func Filter

func Filter(ss []string, filter func(string) bool) []string

Filter removes all strings from a slice of strings that do not have a value of 'true' when passed to the filter function.

func GetSchemaName

func GetSchemaName(obj proto.Message) (name string)

GetSchemaName from an object for use in the schema field on a routed message payload.

func IsEmpty

func IsEmpty(s string) bool

IsEmpty returns a bool indicating that the passed string is Empty. used primarily as a filter function.

func IsWhiteSpace

func IsWhiteSpace(s string) bool

IsWhiteSpace returns a bool indicating whether the passed string is composed entirely of whitespace characters.

func LastRune

func LastRune(s string) rune

LastRune returns the last rune in a string. If the string is Empty the default value for rune is returned.

func LowerCamelCase

func LowerCamelCase(s string) string

LowerCamelCase converts a string to camel case with the first letter lowercase

func MakeASCIIZeros

func MakeASCIIZeros(count uint) []byte

MakeASCIIZeros in a byte array of the passed size.

func NewCharacterMismatchError

func NewCharacterMismatchError(templateIndex int, renderedIndex int) errors.TracerError

NewCharacterMismatchError occurs during demplating and indicates where in the template the issue happened

func NullTerminatedString

func NullTerminatedString(b []byte) string

NullTerminatedString from the passed byte array. Note: this only works with ASCII or UTF-8

func NumericOnly added in v1.1.9

func NumericOnly(s string) string

NumericOnly returns only the numeric characters, in order, in the passed string as a new string.

func Obfuscate added in v1.3.3

func Obfuscate(raw string, number int, direction Direction, obfuscator string) string

Obfuscate the raw string from the given Direction, with the given number of repetition of the obfuscator string.

func ObfuscateLeft added in v1.3.3

func ObfuscateLeft(raw string, number int, obfuscator string) string

ObfuscateLeft will obfuscate the raw string from the left hand side with the given number of repitions of the obfuscator string.

func ObfuscateRight added in v1.3.3

func ObfuscateRight(raw string, number int, obfuscator string) string

ObfuscateRight will obfuscate the raw string from the right hand side with the given number of repitions of the obfuscator string.

func PSPrint

func PSPrint(prefix string, m map[string]string) string

PSPrint pretty prints the passed map to string.

func Pointer

func Pointer(str string) *string

Pointer converts a string to a string pointer

func Reverse

func Reverse(s string) string

Reverse returns its argument string reversed rune-wise left to right.

func RuneAtIndex

func RuneAtIndex(s string, i int) rune

RuneAtIndex returns the rune located at the specified index in the passed string. Supports negative indexing.

func SafeSubstring

func SafeSubstring(value string, start int, end int) string

SafeSubstring returns a substring that should be safe to use with strings that contain non-ascii characters, with python style indexing. If end equals 0 it will be interpreted as the end of the string.

func SprintHex

func SprintHex(b []byte) string

SprintHex the passed byte array as a string of Hexadecimal numbers space separated.

func Underscore

func Underscore(s string) string

Underscore converts strings to underscored version (e.g. CamelCase to camel_case)

func UpperCamelCase

func UpperCamelCase(s string) string

UpperCamelCase converts a string to camel case with the first letter uppercase

Types

type CharacterMismatchError

type CharacterMismatchError struct {
	TemplateIndex int
	RenderedIndex int
	// contains filtered or unexported fields
}

CharacterMismatchError returned when the characters in a rendered template do not match those in the template outside of a phrase.

func (*CharacterMismatchError) Error

func (err *CharacterMismatchError) Error() string

func (*CharacterMismatchError) Trace

func (err *CharacterMismatchError) Trace() []string

Trace returns the stack trace for the error

type Direction added in v1.3.3

type Direction int
const (
	// Left specifies the Direction left
	Left Direction = iota
	// Right specifies the Direction right
	Right
)

type TagOptions

type TagOptions []string

TagOptions is the string following a comma in a struct field's tag, or the empty string. It does not include the leading comma.

func ParseTag

func ParseTag(tag string) (string, TagOptions)

ParseTag splits a struct field's tag into its name and comma-separated options.

func (TagOptions) Contains

func (o TagOptions) Contains(optionName string) bool

Contains reports whether a comma-separated list of options contains a particular substr flag. substr must be surrounded by a string boundary or commas.

Jump to

Keyboard shortcuts

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