stringutil

package
v2.0.0+incompatible Latest Latest
Warning

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

Go to latest
Published: Apr 1, 2019 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')
)

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 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 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 Slugify

func Slugify(v string) string

Slugify replaces non-letter or digit runes with '-'.

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) []string

SplitLines splits a corpus into individual lines by end of line character(s). Possible end of line characters include `\n`, `\r` and `\r\n`.

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": "bailey" } => "foo/bailey/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 Buffer

type Buffer struct {
	*bytes.Buffer
}

Buffer is an extension of bytes.Buffer with some helpers.

func NewBuffer

func NewBuffer(input []byte) *Buffer

NewBuffer creates a new buffer.

func (*Buffer) Writeline

func (b *Buffer) Writeline(contents ...interface{}) (int, error)

Writeline is a macro for writing a string to the buffer that ends in \n.

func (*Buffer) Writelinef

func (b *Buffer) Writelinef(format string, args ...interface{}) (int, error)

Writelinef is a macro for writing a string to the buffer with a given format and args that ends in \n.

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 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