Functions
Cap
method
#
Cap returns the capacity of the builder's underlying byte slice. It is the
total space allocated for the string being built and includes any bytes
already written.
func (b *Builder) Cap() int
Clone
function
#
Clone returns a fresh copy of s.
It guarantees to make a copy of s into a new allocation,
which can be important when retaining only a small substring
of a much larger string. Using Clone can help such programs
use less memory. Of course, since using Clone makes a copy,
overuse of Clone can make programs use more memory.
Clone should typically be used only rarely, and only when
profiling indicates that it is needed.
For strings of length zero the string "" will be returned
and no allocation is made.
func Clone(s string) string
Compare
function
#
Compare returns an integer comparing two strings lexicographically.
The result will be 0 if a == b, -1 if a < b, and +1 if a > b.
Use Compare when you need to perform a three-way comparison (with
[slices.SortFunc], for example). It is usually clearer and always faster
to use the built-in string comparison operators ==, <, >, and so on.
func Compare(a string, b string) int
Contains
function
#
Contains reports whether substr is within s.
func Contains(s string, substr string) bool
ContainsAny
function
#
ContainsAny reports whether any Unicode code points in chars are within s.
func ContainsAny(s string, chars string) bool
ContainsFunc
function
#
ContainsFunc reports whether any Unicode code points r within s satisfy f(r).
func ContainsFunc(s string, f func(rune) bool) bool
ContainsRune
function
#
ContainsRune reports whether the Unicode code point r is within s.
func ContainsRune(s string, r rune) bool
Count
function
#
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 Count(s string, substr string) int
Cut
function
#
Cut slices s around the first instance of sep,
returning the text before and after sep.
The found result reports whether sep appears in s.
If sep does not appear in s, cut returns s, "", false.
func Cut(s string, sep string) (before string, after string, found bool)
CutPrefix
function
#
CutPrefix returns s without the provided leading prefix string
and reports whether it found the prefix.
If s doesn't start with prefix, CutPrefix returns s, false.
If prefix is the empty string, CutPrefix returns s, true.
func CutPrefix(s string, prefix string) (after string, found bool)
CutSuffix
function
#
CutSuffix returns s without the provided ending suffix string
and reports whether it found the suffix.
If s doesn't end with suffix, CutSuffix returns s, false.
If suffix is the empty string, CutSuffix returns s, true.
func CutSuffix(s string, suffix string) (before string, found bool)
EqualFold
function
#
EqualFold reports whether s and t, interpreted as UTF-8 strings,
are equal under simple Unicode case-folding, which is a more general
form of case-insensitivity.
func EqualFold(s string, t string) bool
Fields
function
#
Fields splits the string s around each instance of one or more consecutive white space
characters, as defined by [unicode.IsSpace], returning a slice of substrings of s or an
empty slice if s contains only white space.
func Fields(s string) []string
FieldsFunc
function
#
FieldsFunc splits the string s at each run of Unicode code points c satisfying f(c)
and returns an array of slices of s. If all code points in s satisfy f(c) or the
string is empty, an empty slice is returned.
FieldsFunc makes no guarantees about the order in which it calls f(c)
and assumes that f always returns the same value for a given c.
func FieldsFunc(s string, f func(rune) bool) []string
FieldsFuncSeq
function
#
FieldsFuncSeq returns an iterator over substrings of s split around runs of
Unicode code points satisfying f(c).
The iterator yields the same strings that would be returned by [FieldsFunc](s),
but without constructing the slice.
func FieldsFuncSeq(s string, f func(rune) bool) *ast.IndexExpr
FieldsSeq
function
#
FieldsSeq returns an iterator over substrings of s split around runs of
whitespace characters, as defined by [unicode.IsSpace].
The iterator yields the same strings that would be returned by [Fields](s),
but without constructing the slice.
func FieldsSeq(s string) *ast.IndexExpr
Grow
method
#
Grow grows b's capacity, if necessary, to guarantee space for
another n bytes. After Grow(n), at least n bytes can be written to b
without another allocation. If n is negative, Grow panics.
func (b *Builder) Grow(n int)
HasPrefix
function
#
HasPrefix reports whether the string s begins with prefix.
func HasPrefix(s string, prefix string) bool
HasSuffix
function
#
HasSuffix reports whether the string s ends with suffix.
func HasSuffix(s string, suffix string) bool
Index
function
#
Index returns the index of the first instance of substr in s, or -1 if substr is not present in s.
func Index(s string, substr string) int
IndexAny
function
#
IndexAny returns the index of the first instance of any Unicode code point
from chars in s, or -1 if no Unicode code point from chars is present in s.
func IndexAny(s string, chars string) int
IndexByte
function
#
IndexByte returns the index of the first instance of c in s, or -1 if c is not present in s.
func IndexByte(s string, c byte) int
IndexFunc
function
#
IndexFunc returns the index into s of the first Unicode
code point satisfying f(c), or -1 if none do.
func IndexFunc(s string, f func(rune) bool) int
IndexRune
function
#
IndexRune returns the index of the first instance of the Unicode code point
r, or -1 if rune is not present in s.
If r is [utf8.RuneError], it returns the first instance of any
invalid UTF-8 byte sequence.
func IndexRune(s string, r rune) int
Join
function
#
Join concatenates the elements of its first argument to create a single string. The separator
string sep is placed between elements in the resulting string.
func Join(elems []string, sep string) string
LastIndex
function
#
LastIndex returns the index of the last instance of substr in s, or -1 if substr is not present in s.
func LastIndex(s string, substr string) int
LastIndexAny
function
#
LastIndexAny returns the index of the last instance of any Unicode code
point from chars in s, or -1 if no Unicode code point from chars is
present in s.
func LastIndexAny(s string, chars string) int
LastIndexByte
function
#
LastIndexByte returns the index of the last instance of c in s, or -1 if c is not present in s.
func LastIndexByte(s string, c byte) int
LastIndexFunc
function
#
LastIndexFunc returns the index into s of the last
Unicode code point satisfying f(c), or -1 if none do.
func LastIndexFunc(s string, f func(rune) bool) int
Len
method
#
Len returns the number of accumulated bytes; b.Len() == len(b.String()).
func (b *Builder) Len() int
Len
method
#
Len returns the number of bytes of the unread portion of the
string.
func (r *Reader) Len() int
Lines
function
#
Lines returns an iterator over the newline-terminated lines in the string s.
The lines yielded by the iterator include their terminating newlines.
If s is empty, the iterator yields no lines at all.
If s does not end in a newline, the final yielded line will not end in a newline.
It returns a single-use iterator.
func Lines(s string) *ast.IndexExpr
Map
function
#
Map returns a copy of the string s with all its characters modified
according to the mapping function. If mapping returns a negative value, the character is
dropped from the string with no replacement.
func Map(mapping func(rune) rune, s string) string
NewReader
function
#
NewReader returns a new [Reader] reading from s.
It is similar to [bytes.NewBufferString] but more efficient and non-writable.
func NewReader(s string) *Reader
NewReplacer
function
#
NewReplacer returns a new [Replacer] from a list of old, new string
pairs. Replacements are performed in the order they appear in the
target string, without overlapping matches. The old string
comparisons are done in argument order.
NewReplacer panics if given an odd number of arguments.
func NewReplacer(oldnew ...string) *Replacer
Read
method
#
Read implements the [io.Reader] interface.
func (r *Reader) Read(b []byte) (n int, err error)
ReadAt
method
#
ReadAt implements the [io.ReaderAt] interface.
func (r *Reader) ReadAt(b []byte, off int64) (n int, err error)
ReadByte
method
#
ReadByte implements the [io.ByteReader] interface.
func (r *Reader) ReadByte() (byte, error)
ReadRune
method
#
ReadRune implements the [io.RuneReader] interface.
func (r *Reader) ReadRune() (ch rune, size int, err error)
Repeat
function
#
Repeat returns a new string consisting of count copies of the string s.
It panics if count is negative or if the result of (len(s) * count)
overflows.
func Repeat(s string, count int) string
Replace
function
#
Replace returns a copy of the string s with the first n
non-overlapping instances of old replaced by new.
If old is empty, it matches at the beginning of the string
and after each UTF-8 sequence, yielding up to k+1 replacements
for a k-rune string.
If n < 0, there is no limit on the number of replacements.
func Replace(s string, old string, new string, n int) string
Replace
method
#
func (r *byteReplacer) Replace(s string) string
Replace
method
#
Replace returns a copy of s with all replacements performed.
func (r *Replacer) Replace(s string) string
Replace
method
#
func (r *byteStringReplacer) Replace(s string) string
Replace
method
#
func (r *genericReplacer) Replace(s string) string
Replace
method
#
func (r *singleStringReplacer) Replace(s string) string
ReplaceAll
function
#
ReplaceAll returns a copy of the string s with all
non-overlapping instances of old replaced by new.
If old is empty, it matches at the beginning of the string
and after each UTF-8 sequence, yielding up to k+1 replacements
for a k-rune string.
func ReplaceAll(s string, old string, new string) string
Reset
method
#
Reset resets the [Builder] to be empty.
func (b *Builder) Reset()
Reset
method
#
Reset resets the [Reader] to be reading from s.
func (r *Reader) Reset(s string)
Seek
method
#
Seek implements the [io.Seeker] interface.
func (r *Reader) Seek(offset int64, whence int) (int64, error)
Size
method
#
Size returns the original length of the underlying string.
Size is the number of bytes available for reading via [Reader.ReadAt].
The returned value is always the same and is not affected by calls
to any other method.
func (r *Reader) Size() int64
Split
function
#
Split slices s into all substrings separated by sep and returns a slice of
the substrings between those separators.
If s does not contain sep and sep is not empty, Split returns a
slice of length 1 whose only element is s.
If sep is empty, Split splits after each UTF-8 sequence. If both s
and sep are empty, Split returns an empty slice.
It is equivalent to [SplitN] with a count of -1.
To split around the first instance of a separator, see [Cut].
func Split(s string, sep string) []string
SplitAfter
function
#
SplitAfter slices s into all substrings after each instance of sep and
returns a slice of those substrings.
If s does not contain sep and sep is not empty, SplitAfter returns
a slice of length 1 whose only element is s.
If sep is empty, SplitAfter splits after each UTF-8 sequence. If
both s and sep are empty, SplitAfter returns an empty slice.
It is equivalent to [SplitAfterN] with a count of -1.
func SplitAfter(s string, sep string) []string
SplitAfterN
function
#
SplitAfterN slices s into substrings after each instance of sep and
returns a slice of those substrings.
The count determines the number of substrings to return:
- n > 0: at most n substrings; the last substring will be the unsplit remainder;
- n == 0: the result is nil (zero substrings);
- n < 0: all substrings.
Edge cases for s and sep (for example, empty strings) are handled
as described in the documentation for [SplitAfter].
func SplitAfterN(s string, sep string, n int) []string
SplitAfterSeq
function
#
SplitAfterSeq returns an iterator over substrings of s split after each instance of sep.
The iterator yields the same strings that would be returned by [SplitAfter](s, sep),
but without constructing the slice.
It returns a single-use iterator.
func SplitAfterSeq(s string, sep string) *ast.IndexExpr
SplitN
function
#
SplitN slices s into substrings separated by sep and returns a slice of
the substrings between those separators.
The count determines the number of substrings to return:
- n > 0: at most n substrings; the last substring will be the unsplit remainder;
- n == 0: the result is nil (zero substrings);
- n < 0: all substrings.
Edge cases for s and sep (for example, empty strings) are handled
as described in the documentation for [Split].
To split around the first instance of a separator, see [Cut].
func SplitN(s string, sep string, n int) []string
SplitSeq
function
#
SplitSeq returns an iterator over all substrings of s separated by sep.
The iterator yields the same strings that would be returned by [Split](s, sep),
but without constructing the slice.
It returns a single-use iterator.
func SplitSeq(s string, sep string) *ast.IndexExpr
String
method
#
String returns the accumulated string.
func (b *Builder) String() string
Title
function
#
Title returns a copy of the string s with all Unicode letters that begin words
mapped to their Unicode title case.
Deprecated: The rule Title uses for word boundaries does not handle Unicode
punctuation properly. Use golang.org/x/text/cases instead.
func Title(s string) string
ToLower
function
#
ToLower returns s with all Unicode letters mapped to their lower case.
func ToLower(s string) string
ToLowerSpecial
function
#
ToLowerSpecial returns a copy of the string s with all Unicode letters mapped to their
lower case using the case mapping specified by c.
func ToLowerSpecial(c unicode.SpecialCase, s string) string
ToTitle
function
#
ToTitle returns a copy of the string s with all Unicode letters mapped to
their Unicode title case.
func ToTitle(s string) string
ToTitleSpecial
function
#
ToTitleSpecial returns a copy of the string s with all Unicode letters mapped to their
Unicode title case, giving priority to the special casing rules.
func ToTitleSpecial(c unicode.SpecialCase, s string) string
ToUpper
function
#
ToUpper returns s with all Unicode letters mapped to their upper case.
func ToUpper(s string) string
ToUpperSpecial
function
#
ToUpperSpecial returns a copy of the string s with all Unicode letters mapped to their
upper case using the case mapping specified by c.
func ToUpperSpecial(c unicode.SpecialCase, s string) string
ToValidUTF8
function
#
ToValidUTF8 returns a copy of the string s with each run of invalid UTF-8 byte sequences
replaced by the replacement string, which may be empty.
func ToValidUTF8(s string, replacement string) string
Trim
function
#
Trim returns a slice of the string s with all leading and
trailing Unicode code points contained in cutset removed.
func Trim(s string, cutset string) string
TrimFunc
function
#
TrimFunc returns a slice of the string s with all leading
and trailing Unicode code points c satisfying f(c) removed.
func TrimFunc(s string, f func(rune) bool) string
TrimLeft
function
#
TrimLeft returns a slice of the string s with all leading
Unicode code points contained in cutset removed.
To remove a prefix, use [TrimPrefix] instead.
func TrimLeft(s string, cutset string) string
TrimLeftFunc
function
#
TrimLeftFunc returns a slice of the string s with all leading
Unicode code points c satisfying f(c) removed.
func TrimLeftFunc(s string, f func(rune) bool) string
TrimPrefix
function
#
TrimPrefix returns s without the provided leading prefix string.
If s doesn't start with prefix, s is returned unchanged.
func TrimPrefix(s string, prefix string) string
TrimRight
function
#
TrimRight returns a slice of the string s, with all trailing
Unicode code points contained in cutset removed.
To remove a suffix, use [TrimSuffix] instead.
func TrimRight(s string, cutset string) string
TrimRightFunc
function
#
TrimRightFunc returns a slice of the string s with all trailing
Unicode code points c satisfying f(c) removed.
func TrimRightFunc(s string, f func(rune) bool) string
TrimSpace
function
#
TrimSpace returns a slice of the string s, with all leading
and trailing white space removed, as defined by Unicode.
func TrimSpace(s string) string
TrimSuffix
function
#
TrimSuffix returns s without the provided trailing suffix string.
If s doesn't end with suffix, s is returned unchanged.
func TrimSuffix(s string, suffix string) string
UnreadByte
method
#
UnreadByte implements the [io.ByteScanner] interface.
func (r *Reader) UnreadByte() error
UnreadRune
method
#
UnreadRune implements the [io.RuneScanner] interface.
func (r *Reader) UnreadRune() error
Write
method
#
Write appends the contents of p to b's buffer.
Write always returns len(p), nil.
func (b *Builder) Write(p []byte) (int, error)
Write
method
#
Write writes to the buffer to satisfy [io.Writer].
func (w *appendSliceWriter) Write(p []byte) (int, error)
WriteByte
method
#
WriteByte appends the byte c to b's buffer.
The returned error is always nil.
func (b *Builder) WriteByte(c byte) error
WriteRune
method
#
WriteRune appends the UTF-8 encoding of Unicode code point r to b's buffer.
It returns the length of r and a nil error.
func (b *Builder) WriteRune(r rune) (int, error)
WriteString
method
#
WriteString writes to the buffer without string->[]byte->string allocations.
func (w *appendSliceWriter) WriteString(s string) (int, error)
WriteString
method
#
func (w stringWriter) WriteString(s string) (int, error)
WriteString
method
#
func (r *genericReplacer) WriteString(w io.Writer, s string) (n int, err error)
WriteString
method
#
WriteString appends the contents of s to b's buffer.
It returns the length of s and a nil error.
func (b *Builder) WriteString(s string) (int, error)
WriteString
method
#
func (r *singleStringReplacer) WriteString(w io.Writer, s string) (n int, err error)
WriteString
method
#
WriteString writes s to w with all replacements performed.
func (r *Replacer) WriteString(w io.Writer, s string) (n int, err error)
WriteString
method
#
func (r *byteReplacer) WriteString(w io.Writer, s string) (n int, err error)
WriteString
method
#
func (r *byteStringReplacer) WriteString(w io.Writer, s string) (n int, err error)
WriteTo
method
#
WriteTo implements the [io.WriterTo] interface.
func (r *Reader) WriteTo(w io.Writer) (n int64, err error)
add
method
#
func (t *trieNode) add(key string, val string, priority int, r *genericReplacer)
build
method
#
func (b *Replacer) build() replacer
buildOnce
method
#
func (r *Replacer) buildOnce()
contains
method
#
contains reports whether c is inside the set.
func (as *asciiSet) contains(c byte) bool
copyCheck
method
#
func (b *Builder) copyCheck()
explode
function
#
explode splits s into a slice of UTF-8 strings,
one string per Unicode character up to a maximum of n (n < 0 means no limit).
Invalid UTF-8 bytes are sliced individually.
func explode(s string, n int) []string
explodeSeq
function
#
explodeSeq returns an iterator over the runes in s.
func explodeSeq(s string) *ast.IndexExpr
genSplit
function
#
Generic split: splits after each instance of sep,
including sepSave bytes of sep in the subarrays.
func genSplit(s string, sep string, sepSave int, n int) []string
getStringWriter
function
#
func getStringWriter(w io.Writer) io.StringWriter
grow
method
#
grow copies the buffer to a new, larger buffer so that there are at least n
bytes of capacity beyond len(b.buf).
func (b *Builder) grow(n int)
indexFunc
function
#
indexFunc is the same as IndexFunc except that if
truth==false, the sense of the predicate function is
inverted.
func indexFunc(s string, f func(rune) bool, truth bool) int
isSeparator
function
#
isSeparator reports whether the rune could mark a word boundary.
TODO: update when package unicode captures more of the properties.
func isSeparator(r rune) bool
lastIndexFunc
function
#
lastIndexFunc is the same as LastIndexFunc except that if
truth==false, the sense of the predicate function is
inverted.
func lastIndexFunc(s string, f func(rune) bool, truth bool) int
longestCommonSuffix
function
#
func longestCommonSuffix(a string, b string) (i int)
lookup
method
#
func (r *genericReplacer) lookup(s string, ignoreRoot bool) (val string, keylen int, found bool)
makeASCIISet
function
#
makeASCIISet creates a set of ASCII characters and reports whether all
characters in chars are ASCII.
func makeASCIISet(chars string) (as asciiSet, ok bool)
makeGenericReplacer
function
#
func makeGenericReplacer(oldnew []string) *genericReplacer
makeSingleStringReplacer
function
#
func makeSingleStringReplacer(pattern string, value string) *singleStringReplacer
makeStringFinder
function
#
func makeStringFinder(pattern string) *stringFinder
next
method
#
next returns the index in text of the first occurrence of the pattern. If
the pattern is not found, it returns -1.
func (f *stringFinder) next(text string) int
splitSeq
function
#
splitSeq is SplitSeq or SplitAfterSeq, configured by how many
bytes of sep to include in the results (none or all).
func splitSeq(s string, sep string, sepSave int) *ast.IndexExpr
trimLeftASCII
function
#
func trimLeftASCII(s string, as *asciiSet) string
trimLeftByte
function
#
func trimLeftByte(s string, c byte) string
trimLeftUnicode
function
#
func trimLeftUnicode(s string, cutset string) string
trimRightASCII
function
#
func trimRightASCII(s string, as *asciiSet) string
trimRightByte
function
#
func trimRightByte(s string, c byte) string
trimRightUnicode
function
#
func trimRightUnicode(s string, cutset string) string