stringutils

package
v1.25.0 Latest Latest
Warning

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

Go to latest
Published: Aug 5, 2026 License: GPL-2.0 Imports: 7 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var DefaultNormalizer = NewDefaultNormalizer()

DefaultNormalizer is a statically allocated default normalizer for strings.

Functions

func NormalizeForMatching added in v1.10.0

func NormalizeForMatching(s string) string

NormalizeForMatching applies cached full normalization for cross-seed matching:

  • Unicode normalization (removes diacritics, decomposes ligatures)
  • Lowercase
  • Strip apostrophes (including Unicode variants)
  • Strip colons, exclamation and question marks
  • Convert commas to spaces
  • Convert ampersand to "and"
  • Convert hyphens to spaces
  • Replace decorative anime title symbols via animeTitleSymbolReplacer, e.g. "Classic★Stars" to "classic stars"
  • Collapse multiple spaces to single space

Results are cached per input string (5 minute TTL) to avoid repeated expensive transformations.

Examples:

  • "Shōgun S01" → "shogun s01"
  • "Bob's Burgers" → "bobs burgers"
  • "CSI: Miami" → "csi miami"
  • "Title, With Comma" → "title with comma"
  • "Spider-Man" → "spider man"
  • "His & Hers" → "his and hers"

func NormalizeUnicode added in v1.10.0

func NormalizeUnicode(s string) string

NormalizeUnicode removes diacritics and decomposes ligatures with caching. Results are cached per input string (5 minute TTL) to avoid repeated expensive transformations. For the full normalization with additional punctuation handling, use NormalizeForMatching instead. Examples:

  • "Shōgun" → "Shogun"
  • "Amélie" → "Amelie"
  • "naïve" → "naive"
  • "Björk" → "Bjork"
  • "æ" → "ae"
  • "fi" → "fi"

func SanitizeUTF8 added in v1.24.0

func SanitizeUTF8(s string) string

SanitizeUTF8 replaces invalid UTF-8 byte sequences in s with U+FFFD, returning valid UTF-8.

The BitTorrent spec requires all string fields in a torrent file (torrent name, file paths, comment, etc.) to be UTF-8, but torrents with raw legacy-encoded bytes (e.g. "á" as Latin-1 0xe1) exist in the wild and break code that assumes valid UTF-8, for example regexp.Compile rejects invalid-UTF-8 patterns and panics via MustCompile. Invalid bytes are replaced with U+FFFD rather than dropped because the other lossy decoders in the pipeline (encoding/json, which delivers all qBittorrent API strings, and the NFKD transform in NormalizeForMatching) coerce invalid bytes to U+FFFD too, so sanitized strings still compare equal to those forms. This is a no-op on well-formed input, so legitimate values are unaffected.

Types

type Normalizer

type Normalizer[K comparable, V any] struct {
	// contains filtered or unexported fields
}

Normalizer caches transformed results so we do not repeatedly transform the same inputs.

func NewDefaultNormalizer

func NewDefaultNormalizer() *Normalizer[string, string]

NewDefaultNormalizer returns a normalizer using the default TTL and default transform (ToLower + TrimSpace).

func NewNormalizer

func NewNormalizer[K comparable, V any](ttl time.Duration, transform TransformFunc[K, V]) *Normalizer[K, V]

NewNormalizer returns a normalizer with the provided TTL and transform function for cached entries.

func (*Normalizer[K, V]) Clear

func (n *Normalizer[K, V]) Clear(key K)

Clear removes a cached entry.

func (*Normalizer[K, V]) Normalize

func (n *Normalizer[K, V]) Normalize(key K) V

Normalize returns the transformed value.

type TransformFunc

type TransformFunc[K, V any] func(K) V

TransformFunc is a function that transforms K to V.

Jump to

Keyboard shortcuts

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