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
- func HumanFileSize(size int64, precision int) string
- func Limit(text string, limit int, suffix string) string
- func Mask(text string, mask rune, start, length int) string
- func Plural(text string) string
- func Pluralize(text string, count int) string
- func Singular(text string) string
- func Slug(text string) string
- func Truncate(text string, width int, suffix string) string
Constants ¶
const MaxFileSizePrecision = 6
MaxFileSizePrecision bounds HumanFileSize fractional digits.
Variables ¶
This section is empty.
Functions ¶
func HumanFileSize ¶
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 ¶
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 ¶
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 ¶
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 ¶
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 ¶
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 ¶
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.
Types ¶
This section is empty.