Common string transformations: Title (capitalise each word), Slug (URL-safe), RemoveAccents, etc.
Go's standard library provides basic string manipulation in strings and unicode,
but common higher-level transformations are missing or deprecated (strings.Title
is deprecated and doesn't handle Unicode properly). This package fills those gaps
with zero external dependencies.
import "github.com/azghr/forge/stringutil"
fmt.Println(stringutil.Title("hello world")) // "Hello World"
fmt.Println(stringutil.Slug("Go Lang Library")) // "go-lang-library"
fmt.Println(stringutil.RemoveAccents("café")) // "cafe"Title(s string) string— capitalise every word (letter sequence) ins. Unlikestrings.Title(deprecated), correctly handles Unicode punctuation.Slug(s string, opts ...SlugOption) string— produce a URL-safe slug: lowercased, non-alphanumeric characters replaced by a separator, consecutive separators collapsed, leading/trailing trimmed.RemoveAccents(s string) string— strip diacritical marks fromsusing an internal mapping of common accented characters to their ASCII base forms.
WithSeparator(sep string) SlugOption— separator between words (default"-").WithMaxLength(n int) SlugOption— limit slug tonbytes, truncating at the nearest separator boundary.0means unlimited (default).
| Function | Time | Space | Notes |
|---|---|---|---|
| Title | O(n) | O(n) | Operates on runes in-place |
| Slug | O(n) | O(n) | Single pass, builder-backed |
| RemoveAccents | O(n) | O(n) | Map lookup per rune; zero allocs for pure ASCII |
All functions are concurrency-safe (no shared mutable state).