Help us shape the future of Go! Take the 2026 Go Developer Survey

str

package
v0.5.3 Latest Latest
Warning

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

Go to latest
Published: Nov 3, 2026 License: MIT Imports: 6 Imported by: 0

Documentation

Overview

Package str provides rune-safe string helpers: slugs, limits, masks, English inflection and human-readable file sizes. Functions are pure, never modify their input and are safe for concurrent use.

Index

Constants

View Source
const MaxFileSizePrecision = 6

MaxFileSizePrecision bounds HumanFileSize fractional digits.

Variables

This section is empty.

Functions

func HumanFileSize

func HumanFileSize(size int64, precision int) string

HumanFileSize formats a byte count with binary (1024-based) IEC units and at most precision fractional digits, rounded half up without trailing zeros: 1536 with precision 1 is "1.5 KiB" and 1048575 with precision 0 is "1 MiB". Precision is clamped to 0–MaxFileSizePrecision. Digits are plain ASCII; use the numberformat package for locale presentation.

func Limit

func Limit(text string, limit int, suffix string) string

Limit keeps at most limit runes of text. When text is shortened, trailing whitespace of the kept part is removed and suffix is appended, so the result can exceed limit by the suffix: Limit("The quick fox", 9, "...") is "The quick...". Text within the limit is returned unchanged.

func Mask

func Mask(text string, mask rune, start, length int) string

Mask replaces length runes starting at rune position start with mask. A negative start counts from the end and a negative length masks through the end: Mask("taylor@example.com", '*', -15, 3) is "tay***@example.com". Positions outside text are ignored.

func Plural

func Plural(text string) string

Plural returns the English plural of the last word of text, preserving the rest of text and the word's capitalization: "blog post" becomes "blog posts" and "Person" becomes "People". Words already in an irregular plural form, uncountable nouns and words ending in a single "s" are returned unchanged.

func Pluralize

func Pluralize(text string, count int) string

Pluralize returns text for a count of 1 or -1 and Plural(text) otherwise, for labels such as "1 item" and "3 items".

func Singular

func Singular(text string) string

Singular returns the English singular of the last word of text, preserving the rest of text and the word's capitalization: "Categories" becomes "Category". Words that already look singular are returned unchanged.

func Slug

func Slug(text string) string

Slug returns a lowercase URL slug separated by "-". Latin letters with diacritics fold to ASCII ("Crème Brûlée" becomes "creme-brulee"), letters from other scripts are kept lowercase, whitespace, "-" and "_" separate words, and other punctuation and symbols are removed ("Don't stop!" becomes "dont-stop"). The result can be empty; validate it where one is required.

func Truncate

func Truncate(text string, width int, suffix string) string

Truncate shortens text so the result, including suffix, has at most width runes: Truncate("The quick fox", 8, "…") is "The qui…". When suffix alone does not fit, the first width runes are returned without it.

Types

This section is empty.

Jump to

Keyboard shortcuts

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