txt

package
v1.88.0 Latest Latest
Warning

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

Go to latest
Published: Jul 31, 2023 License: MPL-2.0 Imports: 11 Imported by: 34

Documentation

Overview

Package txt provides various text utilities.

Index

Constants

This section is empty.

Variables

View Source
var SelectEmojiRegex = regexp.MustCompile(`[\x{200D}\x{203C}\x{2049}\x{20E3}\x{2122}\x{2139}\x{2194}-\x{2199}` +
	`\x{21A9}-\x{21AA}\x{231A}-\x{231B}\x{2328}\x{2388}\x{23CF}\x{23E9}-\x{23F3}\x{23F8}-\x{23FA}\x{24C2}` +
	`\x{25AA}-\x{25AB}\x{25B6}\x{25C0}\x{25FB}-\x{25FE}\x{2600}-\x{2605}\x{2607}-\x{2612}\x{2614}-\x{2705}` +
	`\x{2708}-\x{2712}\x{2714}\x{2716}\x{271D}\x{2721}\x{2728}\x{2733}-\x{2734}\x{2744}\x{2747}\x{274C}\x{274E}` +
	`\x{2753}-\x{2755}\x{2757}\x{2763}-\x{2767}\x{2795}-\x{2797}\x{27A1}\x{27B0}\x{27BF}\x{2934}-\x{2935}` +
	`\x{2B05}-\x{2B07}\x{2B1B}-\x{2B1C}\x{2B50}\x{2B55}\x{3030}\x{303D}\x{3297}\x{3299}\x{FE00}-\x{FE0F}` +
	`\x{1F000}-\x{1F0FF}\x{1F10D}-\x{1F10F}\x{1F12F}\x{1F16C}-\x{1F171}\x{1F17E}-\x{1F17F}\x{1F18E}` +
	`\x{1F191}-\x{1F19A}\x{1F1AD}-\x{1F1FF}\x{1F201}-\x{1F20F}\x{1F21A}\x{1F22F}\x{1F232}-\x{1F23A}` +
	`\x{1F23C}-\x{1F23F}\x{1F249}-\x{1F62B}\x{1F62C}-\x{1F64F}\x{1F680}-\x{1F6FF}\x{1F774}-\x{1F77F}` +
	`\x{1F7D5}-\x{1F7FF}\x{1F80C}-\x{1F80F}\x{1F848}-\x{1F84F}\x{1F85A}-\x{1F85F}\x{1F888}-\x{1F88F}` +
	`\x{1F8AE}-\x{1F93A}\x{1F93C}-\x{1F945}\x{1F947}-\x{1F9FF}\x{E0020}-\x{E007F}]`)

SelectEmojiRegex identifies emoji runs.

View Source
var StdAllCaps = MustNewAllCaps("acl", "api", "ascii", "cpu", "css", "dns",
	"eof", "guid", "html", "http", "https", "id", "ip", "json", "lhs", "qps",
	"ram", "rhs", "rpc", "sla", "smtp", "sql", "ssh", "tcp", "tls", "ttl",
	"udp", "ui", "uid", "uuid", "uri", "url", "utf8", "vm", "xml", "xmpp",
	"xsrf", "xss")

StdAllCaps provides the standard list of words that golint expects to be capitalized, found in the variable 'commonInitialisms' in https://github.com/golang/lint/blob/master/lint.go#L771-L808

Functions

func CaselessSliceContains added in v1.69.0

func CaselessSliceContains(slice []string, target string) bool

CaselessSliceContains returns true if the target is within the slice, regardless of case.

func CloneStringSlice added in v1.43.0

func CloneStringSlice(in []string) []string

CloneStringSlice returns a copy of the slice of strings.

func CollapseSpaces

func CollapseSpaces(in string) string

CollapseSpaces removes leading and trailing spaces and reduces any runs of two or more spaces to a single space.

func DigitToValue

func DigitToValue(ch rune) (int, error)

DigitToValue converts a unicode digit into a numeric value.

func DurationToCode

func DurationToCode(duration time.Duration) string

DurationToCode turns a time.Duration into more human-readable text required for code than a simple number of nanoseconds.

func FirstN

func FirstN(s string, n int) string

FirstN returns the first n runes of a string.

func FirstToLower added in v1.2.1

func FirstToLower(in string) string

FirstToLower converts the first character to lower case.

func FirstToUpper

func FirstToUpper(in string) string

FirstToUpper converts the first character to upper case.

func FormatDuration

func FormatDuration(duration time.Duration, includeMillis bool) string

FormatDuration formats the duration as either "0:00:00" or "0:00:00.000".

func IsTruthy added in v1.11.0

func IsTruthy(in string) bool

IsTruthy returns true for "truthy" values, i.e. ones that should be interpreted as true.

func IsVowel added in v1.84.0

func IsVowel(ch rune) bool

IsVowel is a concrete implementation of VowelChecker.

func IsVowely added in v1.84.0

func IsVowely(ch rune) bool

IsVowely is a concrete implementation of VowelChecker that includes 'y'.

func LastN

func LastN(s string, n int) string

LastN returns the last n runes of a string.

func MapToStringSlice added in v1.19.0

func MapToStringSlice(m map[string]bool) []string

MapToStringSlice returns a slice created from the keys of a map.

func NaturalCmp added in v1.87.0

func NaturalCmp(s1, s2 string, caseInsensitive bool) int

NaturalCmp compares two strings using natural ordering. This means that "a2" < "a12".

Non-digit sequences and numbers are compared separately. The former are compared byte-wise, while the latter are compared numerically (except that the number of leading zeros is used as a tie-breaker, so "2" < "02").

Limitations:

  • only ASCII digits (0-9) are considered.

Original algorithm: https://github.com/fvbommel/util/blob/master/sortorder/natsort.go

func NaturalLess

func NaturalLess(s1, s2 string, caseInsensitive bool) bool

NaturalLess compares two strings using natural ordering. This means that "a2" < "a12".

Non-digit sequences and numbers are compared separately. The former are compared byte-wise, while the latter are compared numerically (except that the number of leading zeros is used as a tie-breaker, so "2" < "02").

Limitations:

  • only ASCII digits (0-9) are considered.

Original algorithm: https://github.com/fvbommel/util/blob/master/sortorder/natsort.go

func NormalizeLineEndings added in v1.86.0

func NormalizeLineEndings(input string) string

NormalizeLineEndings converts CRLF and CR into LF.

func ParseDuration

func ParseDuration(duration string) (time.Duration, error)

ParseDuration parses the duration string, as produced by FormatDuration().

func RomanNumerals added in v1.50.0

func RomanNumerals(value int) string

RomanNumerals converts a number into roman numerals.

func RunesEqual added in v1.64.0

func RunesEqual(left, right []rune) bool

RunesEqual returns true if the two slices of runes are equal.

func SortStringsNaturalAscending added in v1.19.0

func SortStringsNaturalAscending(in []string)

SortStringsNaturalAscending sorts a slice of strings using NaturalLess in least to most order.

func SortStringsNaturalDescending added in v1.19.0

func SortStringsNaturalDescending(in []string)

SortStringsNaturalDescending sorts a slice of strings using NaturalLess in most to least order.

func StringSliceToMap added in v1.19.0

func StringSliceToMap(slice []string) map[string]bool

StringSliceToMap returns a map created from the strings of a slice.

func StripBOM added in v1.19.0

func StripBOM(b []byte) []byte

StripBOM removes the BOM marker from UTF-8 data, if present.

func ToCamelCase

func ToCamelCase(in string) string

ToCamelCase converts a string to CamelCase.

func ToCamelCaseWithExceptions added in v1.1.5

func ToCamelCaseWithExceptions(in string, exceptions *AllCaps) string

ToCamelCaseWithExceptions converts a string to CamelCase, but forces certain words to all caps.

func ToSnakeCase

func ToSnakeCase(in string) string

ToSnakeCase converts a string to snake_case.

func Truncate added in v1.78.0

func Truncate(s string, count int, keepFirst bool) string

Truncate the input string to count runes, trimming from the end if keepFirst is true or the start if not. If trimming occurs, a … will be added in place of the trimmed characters.

func Unquote added in v1.70.0

func Unquote(text string) string

Unquote strips up to one set of surrounding double quotes from the bytes and returns them as a string. For a more capable version that supports different quoting types and unescaping, consider using strconv.Unquote().

func UnquoteBytes added in v1.70.0

func UnquoteBytes(text []byte) []byte

UnquoteBytes strips up to one set of surrounding double quotes from the bytes and returns them as a string. For a more capable version that supports different quoting types and unescaping, consider using strconv.Unquote().

func Wrap added in v1.3.0

func Wrap(prefix, text string, maxColumns int) string

Wrap text to a certain length, giving it an optional prefix on each line. Words will not be broken, even if they exceed the maximum column size and instead will extend past the desired length.

Types

type AllCaps added in v1.1.5

type AllCaps struct {
	// contains filtered or unexported fields
}

AllCaps holds information for transforming text with ToCamelCaseWithExceptions.

func MustNewAllCaps added in v1.1.5

func MustNewAllCaps(in ...string) *AllCaps

MustNewAllCaps takes a list of words that should be all uppercase when part of a camel-cased string. Failure to create the AllCaps object causes the program to exit.

func NewAllCaps added in v1.1.5

func NewAllCaps(in ...string) (*AllCaps, error)

NewAllCaps takes a list of words that should be all uppercase when part of a camel-cased string.

type RuneReader added in v1.1.5

type RuneReader struct {
	Src []rune
	Pos int
}

RuneReader implements io.RuneReader

func (*RuneReader) ReadRune added in v1.1.5

func (rr *RuneReader) ReadRune() (r rune, size int, err error)

ReadRune returns the next rune and its size in bytes.

type VowelChecker added in v1.84.0

type VowelChecker func(rune) bool

VowelChecker defines a function that returns true if the specified rune is to be considered a vowel.

Jump to

Keyboard shortcuts

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