strings

package
v0.139.5 Latest Latest
Warning

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

Go to latest
Published: Dec 14, 2024 License: Apache-2.0 Imports: 17 Imported by: 38

Documentation

Overview

Package strings provides template functions for manipulating strings.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Namespace

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

Namespace provides template functions for the "strings" namespace. Most functions mimic the Go stdlib, but the order of the parameters may be different to ease their use in the Go template system.

func New

func New(d *deps.Deps) *Namespace

New returns a new instance of the strings-namespaced template functions.

func (*Namespace) Chomp

func (ns *Namespace) Chomp(s any) (any, error)

Chomp returns a copy of s with all trailing newline characters removed.

func (*Namespace) Contains

func (ns *Namespace) Contains(s, substr any) (bool, error)

Contains reports whether substr is in s.

func (*Namespace) ContainsAny

func (ns *Namespace) ContainsAny(s, chars any) (bool, error)

ContainsAny reports whether any Unicode code points in chars are within s.

func (*Namespace) ContainsNonSpace added in v0.111.0

func (ns *Namespace) ContainsNonSpace(s any) (bool, error)

ContainsNonSpace reports whether s contains any non-space characters as defined by Unicode's White Space property, <docsmeta>{"newIn": "0.111.0" }</docsmeta>

func (*Namespace) Count added in v0.74.0

func (ns *Namespace) Count(substr, s any) (int, error)

Count counts the number of non-overlapping instances of substr in s. If substr is an empty string, Count returns 1 + the number of Unicode code points in s.

func (*Namespace) CountRunes

func (ns *Namespace) CountRunes(s any) (int, error)

CountRunes returns the number of runes in s, excluding whitespace.

func (*Namespace) CountWords

func (ns *Namespace) CountWords(s any) (int, error)

CountWords returns the approximate word count in s.

func (*Namespace) Diff added in v0.125.0

func (ns *Namespace) Diff(oldname string, old any, newname string, new any) (string, error)

Diff returns an anchored diff of the two texts old and new in the “unified diff” format. If old and new are identical, Diff returns an empty string.

func (*Namespace) FindRE

func (ns *Namespace) FindRE(expr string, content any, limit ...any) ([]string, error)

FindRE returns a list of strings that match the regular expression. By default all matches will be included. The number of matches can be limited with an optional third parameter.

func (*Namespace) FindRESubmatch added in v0.110.0

func (ns *Namespace) FindRESubmatch(expr string, content any, limit ...any) ([][]string, error)

FindRESubmatch returns a slice of all successive matches of the regular expression in content. Each element is a slice of strings holding the text of the leftmost match of the regular expression and the matches, if any, of its subexpressions.

By default all matches will be included. The number of matches can be limited with the optional limit parameter. A return value of nil indicates no match.

func (*Namespace) FirstUpper added in v0.49.1

func (ns *Namespace) FirstUpper(s any) (string, error)

FirstUpper converts s making the first character upper case.

func (*Namespace) HasPrefix

func (ns *Namespace) HasPrefix(s, prefix any) (bool, error)

HasPrefix tests whether the input s begins with prefix.

func (*Namespace) HasSuffix

func (ns *Namespace) HasSuffix(s, suffix any) (bool, error)

HasSuffix tests whether the input s begins with suffix.

func (*Namespace) Repeat added in v0.42.1

func (ns *Namespace) Repeat(n, s any) (string, error)

Repeat returns a new string consisting of n copies of the string s.

func (*Namespace) Replace

func (ns *Namespace) Replace(s, old, new any, limit ...any) (string, error)

Replace returns a copy of the string s with all occurrences of old replaced with new. The number of replacements can be limited with an optional fourth parameter.

func (*Namespace) ReplaceRE

func (ns *Namespace) ReplaceRE(pattern, repl, s any, n ...any) (_ string, err error)

ReplaceRE returns a copy of s, replacing all matches of the regular expression pattern with the replacement text repl. The number of replacements can be limited with an optional fourth parameter.

func (*Namespace) RuneCount added in v0.42.1

func (ns *Namespace) RuneCount(s any) (int, error)

RuneCount returns the number of runes in s.

func (*Namespace) SliceString

func (ns *Namespace) SliceString(a any, startEnd ...any) (string, error)

SliceString slices a string by specifying a half-open range with two indices, start and end. 1 and 4 creates a slice including elements 1 through 3. The end index can be omitted, it defaults to the string's length.

func (*Namespace) Split

func (ns *Namespace) Split(a any, delimiter string) ([]string, error)

Split slices an input string into all substrings separated by delimiter.

func (*Namespace) Substr

func (ns *Namespace) Substr(a any, nums ...any) (string, error)

Substr extracts parts of a string, beginning at the character at the specified position, and returns the specified number of characters.

It normally takes two parameters: start and length. It can also take one parameter: start, i.e. length is omitted, in which case the substring starting from start until the end of the string will be returned.

To extract characters from the end of the string, use a negative start number.

In addition, borrowing from the extended behavior described at http://php.net/substr, if length is given and is negative, then that many characters will be omitted from the end of string.

func (*Namespace) Title

func (ns *Namespace) Title(s any) (string, error)

Title returns a copy of the input s with all Unicode letters that begin words mapped to their title case.

func (*Namespace) ToLower

func (ns *Namespace) ToLower(s any) (string, error)

ToLower returns a copy of the input s with all Unicode letters mapped to their lower case.

func (*Namespace) ToUpper

func (ns *Namespace) ToUpper(s any) (string, error)

ToUpper returns a copy of the input s with all Unicode letters mapped to their upper case.

func (*Namespace) Trim

func (ns *Namespace) Trim(s, cutset any) (string, error)

Trim returns converts the strings s removing all leading and trailing characters defined contained.

func (*Namespace) TrimLeft added in v0.27.1

func (ns *Namespace) TrimLeft(cutset, s any) (string, error)

TrimLeft returns a slice of the string s with all leading characters contained in cutset removed.

func (*Namespace) TrimPrefix

func (ns *Namespace) TrimPrefix(prefix, s any) (string, error)

TrimPrefix returns s without the provided leading prefix string. If s doesn't start with prefix, s is returned unchanged.

func (*Namespace) TrimRight added in v0.27.1

func (ns *Namespace) TrimRight(cutset, s any) (string, error)

TrimRight returns a slice of the string s with all trailing characters contained in cutset removed.

func (*Namespace) TrimSpace added in v0.136.3

func (ns *Namespace) TrimSpace(s any) (string, error)

TrimSpace returns the given string, removing leading and trailing whitespace as defined by Unicode.

func (*Namespace) TrimSuffix

func (ns *Namespace) TrimSuffix(suffix, s any) (string, error)

TrimSuffix returns s without the provided trailing suffix string. If s doesn't end with suffix, s is returned unchanged.

func (*Namespace) Truncate

func (ns *Namespace) Truncate(s any, options ...any) (template.HTML, error)

Truncate truncates the string in s to the specified length.

Jump to

Keyboard shortcuts

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