stringutil

package
v1.20210917.5 Latest Latest
Warning

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

Go to latest
Published: Sep 17, 2021 License: MIT Imports: 8 Imported by: 7

Documentation

Overview

Package stringutil includes string utility functions and helpers.

Index

Constants

View Source
const (
	// Empty is the empty string
	Empty string = ""

	// RuneSpace is a single rune representing a space.
	RuneSpace rune = ' '

	// RuneNewline is a single rune representing a newline.
	RuneNewline rune = '\n'

	// LowerA is the ascii int value for 'a'
	LowerA uint = uint('a')
	// LowerZ is the ascii int value for 'z'
	LowerZ uint = uint('z')
)
View Source
const (
	ErrInvalidBoolValue ex.Class = "invalid bool value"
)

Error Constants

View Source
const (
	ErrMissingRouteParameters ex.Class = "missing route parameter in params"
)

Error Constants

View Source
const (
	GlobStar = "*"
)

Glob constants

Variables

View Source
var (
	// LowerLetters is a runset of lowercase letters.
	LowerLetters Runeset = []rune("abcdefghijklmnopqrstuvwxyz")

	// UpperLetters is a runset of uppercase letters.
	UpperLetters Runeset = []rune("ABCDEFGHIJKLMNOPQRSTUVWXYZ")

	// Letters is a runset of both lower and uppercase letters.
	Letters = append(LowerLetters, UpperLetters...)

	// Numbers is a runset of numeric characters.
	Numbers Runeset = []rune("0123456789")

	// LettersAndNumbers is a runset of letters and numeric characters.
	LettersAndNumbers = append(Letters, Numbers...)

	// Symbols is a runset of symbol characters.
	Symbols Runeset = []rune(`!@#$%^&*()_+-=[]{}\|:;`)

	// LettersNumbersAndSymbols is a runset of letters, numbers and symbols.
	LettersNumbersAndSymbols = append(LettersAndNumbers, Symbols...)
)
View Source
var (
	// LowerDiff is the difference between lower Z and lower A
	LowerDiff = (LowerZ - LowerA)
)

Functions

func CSV

func CSV(values []string) string

CSV produces a csv from a given set of values.

func CombineRunsets

func CombineRunsets(runesets ...[]rune) []rune

CombineRunsets combines given runsets into a single runset.

func CompressSpace

func CompressSpace(text string) (output string)

CompressSpace compresses whitespace characters into single spaces. It trims leading and trailing whitespace as well.

func EqualsCaseless

func EqualsCaseless(a, b string) bool

EqualsCaseless compares two strings regardless of case.

func FileSize

func FileSize(sizeBytes int) string

FileSize returns a string representation of a file size in bytes.

func Fixed

func Fixed(text string, width int) string

Fixed returns a fixed width, right aligned, string with a given minimum space padded width.

func FixedLeft

func FixedLeft(text string, width int) string

FixedLeft returns a fixed width, left aligned, string with a given minimum space padded width.

func Glob added in v1.20210116.3

func Glob(subj, pattern string) bool

Glob returns if a subject matches a given pattern.

func GlobAny added in v1.20210116.3

func GlobAny(subj string, patterns ...string) bool

GlobAny tests if a file matches a (potentially) csv of glob filters.

func HasPrefixCaseless

func HasPrefixCaseless(corpus, prefix string) bool

HasPrefixCaseless returns if a corpus has a prefix regardless of casing.

func HasSuffixCaseless

func HasSuffixCaseless(corpus, suffix string) bool

HasSuffixCaseless returns if a corpus has a suffix regardless of casing.

func Indent

func Indent(indent string, corpus string) string

Indent applies an indent prefix to a given corpus.

func IndentLines

func IndentLines(indent string, corpus []string) []string

IndentLines adds a prefix to a given list of strings.

func MustParseBool

func MustParseBool(str string) bool

MustParseBool parses a boolean value and panics if there is an error.

func ParseBool

func ParseBool(str string) (bool, error)

ParseBool parses a given string as a boolean value.

func Random

func Random(runeset []rune, length int) string

Random returns a random selection of runes from the set.

func ReplaceAny

func ReplaceAny(corpus string, replacement rune, replaced ...rune) string

ReplaceAny replaces any runes in the 'replaced' list with a given replacement. Example:

output := ReplaceAny("foo bar_baz", '-', []rune(` _`)...)

func ReplacePathParameters added in v1.20210908.5

func ReplacePathParameters(str string, params map[string]string) (string, error)

ReplacePathParameters will replace path parameters in a URL path with values from the passed in `params` map. Path parameters in the format of `:<param_name>`. Example usage: `ReplacePathParameters("/resource/:resource_id", map[string]string{"resource_id": "1234"})`

func Slugify

func Slugify(v string) string

Slugify replaces non-letter or digit runes with '-'. It will not add repeated '-'.

func SplitCSV

func SplitCSV(text string) (output []string)

SplitCSV splits a corpus by the `,`, dropping leading or trailing whitespace unless quoted.

func SplitLines

func SplitLines(contents string, opts ...SplitLinesOption) []string

SplitLines splits a corpus into individual lines by the ascii control character `\n`. You can control some behaviors of the splitting process with variadic options.

func SplitSpace

func SplitSpace(text string) (output []string)

SplitSpace splits a string on whitespace.

func SplitSpaceQuoted

func SplitSpaceQuoted(text string) (output []string)

SplitSpaceQuoted splits a corpus on space but treats quoted strings i.e. within `"` as being atomic chunks.

func TSV

func TSV(values []string) string

TSV produces a tab seprated values from a given set of values.

func Title

func Title(corpus string) string

Title returns a string in title case.

func Tokenize

func Tokenize(corpus string, tokens Tokens) string

Tokenize replaces a given set of tokens in a corpus. Tokens should appear in the corpus in the form ${[KEY]} where [KEY] is the key in the map. Examples: corpus: "foo/${bar}/baz", { "bar": "example-string" } => "foo/example-string/baz" UTF-8 is handled via. runes.

func TrimLen

func TrimLen(val string, length int) string

TrimLen trims a string to a given length, i.e. the substring [0, length).

func TrimPrefixCaseless

func TrimPrefixCaseless(corpus, prefix string) string

TrimPrefixCaseless trims a prefix from a corpus ignoring case.

func TrimSuffixCaseless

func TrimSuffixCaseless(corpus, suffix string) string

TrimSuffixCaseless trims a case insensitive suffix from a corpus.

Types

type Runeset

type Runeset []rune

Runeset is a set of runes

func (Runeset) Combine

func (rs Runeset) Combine(other ...Runeset) Runeset

Combine merges runesets.

func (Runeset) Len

func (rs Runeset) Len() int

Len implements part of sorter.

func (Runeset) Less

func (rs Runeset) Less(i, j int) bool

Less implements part of sorter.

func (Runeset) Random

func (rs Runeset) Random(length int) string

Random returns a random selection of runes from the set.

func (Runeset) Set

func (rs Runeset) Set() map[rune]bool

Set returns a map of the runes in the set.

func (Runeset) Swap

func (rs Runeset) Swap(i, j int)

Swap implements part of sorter.

type SplitLinesOption

type SplitLinesOption func(*SplitLinesOptions)

SplitLinesOption is a mutator for SplitLinesOptions.

func OptSplitLinesIncludeEmptyLines

func OptSplitLinesIncludeEmptyLines(include bool) SplitLinesOption

OptSplitLinesIncludeEmptyLines sets if we should omit newlines in the returned lines.

func OptSplitLinesIncludeNewLine

func OptSplitLinesIncludeNewLine(include bool) SplitLinesOption

OptSplitLinesIncludeNewLine sets if we should omit newlines in the returned lines.

type SplitLinesOptions

type SplitLinesOptions struct {
	IncludeNewline    bool
	IncludeEmptyLines bool
}

SplitLinesOptions are options for the SplitLines function.

type Tokens

type Tokens = map[string]string

Tokens is a soft alias to map[string]string

Jump to

Keyboard shortcuts

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