textutil

package
v1.2.0 Latest Latest
Warning

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

Go to latest
Published: Nov 25, 2026 License: MIT Imports: 5 Imported by: ? modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func ASCIIDigits

func ASCIIDigits(s string) string

s with each decimal digit of another script (Arabic-Indic, Devanagari, fullwidth and every other Unicode Nd digit) replaced by the ASCII digit of the same value, everything else kept

func Between

func Between(s string, start, end int) string

Characters of s from position start up to but not including position end, both counted in characters and kept inside s

func Center

func Center(s string, width int) string

s between runs of spaces that make it width characters long, the odd space going to the right; s itself when it is already that long

func DecodeUTF8

func DecodeUTF8(data []byte) (string, error)

Text of data when it is valid UTF-8, a leading byte order mark kept, otherwise no text and an *InvalidUTF8Error

func DigitValue

func DigitValue(r rune) int

Value 0 to 9 of a decimal digit of any script, its offset in the run of digits around it counted in tens since Unicode encodes every script's digits as ten in a row from zero

func DropFirst

func DropFirst(s string, n int) string

What is left of s without its first n characters

func DropLast

func DropLast(s string, n int) string

What is left of s without its last n characters

func FieldsN

func FieldsN(s string, n int) []string

Words of s like strings.Fields, stopping after n words: what follows them is the last item, whole, its leading space cut

func First

func First(s string, n int) string

First n characters of s, all of s when it has no more and nothing when n is not positive

func Fold

func Fold(s string) string

Unicode full case folding of s for caseless matching, ß as ss and every Cherokee letter as the capital Unicode folds it to, which golang.org/x/text/cases alone folds the other way round for the capitals; bytes that are not UTF-8 kept

func IsDigits

func IsDigits(s string) bool

Whether s is non-empty and holds nothing but the ASCII digits 0 to 9

func Last

func Last(s string, n int) string

Last n characters of s, all of s when it has no more and nothing when n is not positive

func Lines

func Lines(s string) []string

Lines of s without their line ends, split at \n with a \r before it dropped; a line end at the very end starts no empty line

func NormalizeNewlines

func NormalizeNewlines(s string) string

s with every CRLF pair and every lone CR turned into LF

func PadLeft

func PadLeft(s string, width int) string

s after the spaces that make it width characters long, s itself when it is already that long

func PadRight

func PadRight(s string, width int) string

s followed by the spaces that make it width characters long, s itself when it is already that long

func ParseBool

func ParseBool(s string) (value, ok bool)

Truth value of a yes or no word: 1, true, yes or on and 0, false, no or off in any letter case, white space around ignored; ok is false for any other text

func ParseInt

func ParseInt(s string) (int64, error)

Whole number s writes in base ten, white space around it, a leading sign and single underscores between digits allowed; otherwise a *strconv.NumError, its ErrRange coming with the value clamped to the int64 bounds

func ParseIntAnyScript

func ParseIntAnyScript(s string) (int64, bool)

Whole number s writes in base ten as ParseInt reads it, digits of any script taken for their values; false for anything else and for a value past the int64 range

func Thousands

func Thousands(n int64) string

Whole number in decimal with a comma between each group of three digits

func Title

func Title(s string) string

s with the first letter of each word in title case and the word's other letters in lower case, a word being a run of letters and numbers with its marks and any apostrophe between letters

func TrimBOM

func TrimBOM[T ~string | ~[]byte](data T) T

Data without one leading UTF-8 byte order mark, unchanged when it starts with none

func UpperFirst

func UpperFirst(s string) string

s with its first character in title case and the rest left as it is

func ValidateUTF8

func ValidateUTF8[T ~string | ~[]byte](data T) error

Nil when data is valid UTF-8, otherwise an *InvalidUTF8Error at its first invalid byte

func Wrap

func Wrap(text string, width int, o WrapOptions) []string

Lines of text filled to at most width characters each, tabs expanded to stops every eight columns, words split at white space, after the hyphens of compound words and around dashes, and white space dropped where a line breaks

Types

type InvalidUTF8Error

type InvalidUTF8Error struct {
	// Byte offset of the first byte that starts no valid UTF-8 sequence
	Offset int
}

Error for text that is not valid UTF-8, located at its first invalid byte

func (*InvalidUTF8Error) Error

func (e *InvalidUTF8Error) Error() string

Message naming the byte offset where the valid UTF-8 ends

type WrapOptions

type WrapOptions struct {
	// Prefix of the first line, counted in its width
	Indent string
	// Prefix of every line after the first, counted in its width
	NextIndent string
	// Compound words stay whole instead of breaking after their hyphens
	NoHyphenBreaks bool
	// A word longer than a line stays whole on a line of its own instead of being cut
	KeepLongWords bool
	// Line breaks, vertical tabs and form feeds stay as they are instead of each becoming a space
	KeepWhitespace bool
}

Settings of Wrap, the zero value filling plain paragraphs

Jump to

Keyboard shortcuts

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