release

package
v0.9.0-dev.2 Latest Latest
Warning

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

Go to latest
Published: Oct 12, 2026 License: AGPL-3.0 Imports: 9 Imported by: 0

Documentation

Overview

Package release provides release name parsing via PCRE-compatible regex patterns sourced from TRaSH Guides and Sonarr/Radarr QualityParser.

pcre.go implements the match-time half of a PCRE-to-RE2 compatibility layer (the compile half lives in pcre_compile.go). Go's regexp (RE2) does not support lookarounds; this layer compiles the RE2-compatible core of a PCRE pattern with an empty marker group at each lookaround site and verifies the assertions programmatically at their actual in-pattern offsets after each core match.

Supported constructs

Everything Go regexp (RE2) accepts, plus lookahead (?=...) (?!...) and lookbehind (?<=...) (?<!...) assertions and .NET-style named groups (?<name>...) (exposed by number only). Rejected with a typed *CompileError: backreferences (\1, \k<name>, (?P=name)), atomic groups (?>...), conditionals (?(...)...), inline comments (?#...), possessive quantifiers (a*+), quantified assertions ((?!x)*), assertions nested inside other assertions, and assertions inside quantified groups (Go regexp reports only the last iteration's marker offset, so per-iteration verification is impossible; silent under-checking is forbidden).

Semantics and documented divergence contracts

The layer targets the observable behavior of .NET's backtracking Regex (the patterns' native engine, pinned upstream revisions in the spec's study appendix), with these documented divergences:

  • Core syntax runs under RE2 semantics (divergence class (i), accepted contract): \d \w \s and \b are ASCII-only vs .NET's Unicode classes; $ matches only at end of text (.NET also before a final \n); case folding is Unicode simple folding vs .NET culture-sensitive folding.
  • Capture participation (class (k), accepted contract): the []string API cannot distinguish a participating-empty group from an unmatched one (both are ""), and captures inside lookaround assertions never participate (the assertion runs as a standalone anchored regex).
  • Backtracking retry (class (j), bounded default): when an assertion adjacent to a quantified subexpression fails at the greedy end, the layer re-runs the anchored assertion at each byte offset down to the chain's minimum width (bounded shrink-and-recheck). The candidate is accepted or rejected on that basis; reported capture spans stay at the RE2 greedy widths (a full .NET engine would also re-derive the shrunken captures and re-match the pattern tail).
  • A source pattern with top-level alternation is compiled per branch; FindStringSubmatch merges branch candidates with a single left-to-right non-overlapping positional scan (earliest start wins, ties broken by source branch order, cursor advances past each selection) and returns the LAST selection, matching .NET consumers' .Last()/.LastOrDefault() usage on a single Matches() scan.

Input bound (measured linear-time gate)

Assertion verification scans O(n) text per candidate and the shrink retry adds a group-length-bounded loop, so per-call work is not covered by RE2's linear-time guarantee alone. Callers MUST cap untrusted input at MaxNameLen bytes. The cap is enforced at the provider boundary: the download-retry provider wrapper (internal/provider.wrapRetry), which both composition roots apply to every provider, clamps each search result's ReleaseName via ClampName before it enters the engine. ParseName additionally clamps its own input (defense in depth for callers that bypass the provider path). Scaling benchmarks in pcre_bench_test.go demonstrate near-linear behavior within the bound.

Index

Constants

View Source
const MaxNameLen = 512

MaxNameLen is the maximum release-name length in bytes the layer is specified for. Provider-supplied names are truncated to this bound at the provider boundary (the search engine's result collector) before any pattern in this package sees them. 512 bytes comfortably covers real release names (filesystem basenames cap at 255 bytes) while bounding the assertion-check and shrink-retry work per candidate.

Variables

View Source
var (
	CompiledSources     []Format
	CompiledVideoCodecs []Format
	CompiledHDR         []Format
	CompiledStreaming   []Format
)

Compiled pattern caches — built once at init.

Functions

func ClampName added in v0.1.148

func ClampName(s string) string

ClampName truncates a release name to MaxNameLen bytes. It is the documented enforcement helper for the layer's input bound.

func CompareSource

func CompareSource(matches *subflux.MatchSet, a, b string)

CompareSource checks if two sources are in the same family.

func MatchFirst

func MatchFirst(formats []Format, name string) string

MatchFirst returns the Normalize label of the first matching format, or empty string if none match.

Types

type CompileError added in v0.1.148

type CompileError struct {
	Construct string // offending construct, e.g. "(?=", "(?>", "\\k", "++"
	Reason    string
	Offset    int // byte offset in the source pattern, -1 when unknown
}

CompileError is the typed error returned by CompilePCRE for malformed patterns and unsupported or semantically rejected constructs (spec subflux-release-parse-fidelity R3.6). It carries the offending construct, the byte offset in the SOURCE pattern (-1 when unknown, e.g. errors surfaced by the underlying RE2 compiler without position info), and a human-readable reason.

func (*CompileError) Error added in v0.1.148

func (e *CompileError) Error() string

Error implements the error interface.

type Format

type Format struct {
	Name      string   // Format name (e.g. "x265", "DTS-HD MA", "BluRay")
	Regex     string   // Regex pattern (PCRE syntax where needed)
	Compiled  *Pattern // Compiled via CompilePCRE (set by init)
	Normalize string   // Normalized label for scoring (e.g. "h265", "bluray")
}

Format holds one regex pattern for release name parsing.

type Info

type Info struct {
	Source           string
	VideoCodec       string
	ReleaseGroup     string
	StreamingService string
	Edition          string
	HDR              string
}

Info holds metadata extracted from a release/scene name.

func ParseName added in v0.4.0

func ParseName(name string) Info

ParseName extracts metadata from a scene/release name.

Input is clamped to MaxNameLen bytes before any pattern runs (defense in depth for the layer's measured linear-time gate): a longer name is treated as its MaxNameLen-byte prefix, matching the provider boundary's ClampName semantics, so callers that bypass provider.wrapRetry cannot violate the input bound. Provider-path behavior is unchanged (those names arrive already clamped).

type Pattern

type Pattern struct {
	// contains filtered or unexported fields
}

Pattern wraps one or more RE2-backed branches compiled from a PCRE pattern. The zero value is not usable; construct via CompilePCRE.

func CompilePCRE

func CompilePCRE(pat string) (*Pattern, error)

CompilePCRE compiles a case-insensitive PCRE pattern into an RE2 core with positional lookaround assertions. Malformed patterns and PCRE constructs the layer cannot express return a typed *CompileError. See the package documentation for the supported-construct table and the layer's documented divergence contracts.

func (*Pattern) FindStringSubmatch

func (p *Pattern) FindStringSubmatch(s string) []string

FindStringSubmatch returns the LAST match of the positional merge scan (see the package documentation) with submatches in SOURCE pattern numbering, or nil when no candidate survives its assertions.

func (*Pattern) MatchString

func (p *Pattern) MatchString(s string) bool

MatchString reports whether s contains a match satisfying all assertions of at least one branch.

Jump to

Keyboard shortcuts

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