How it works
trackparse is a fixed pipeline of small hand-written scanners. There are no regexes with lookaround
or backreferences, so nothing backtracks. A 10,000-code-point input takes at most tens of
milliseconds in JavaScript, Rust, Go and C, and well under a second in Python (a very long
knownArtists name is slower). The same design
is what lets the JavaScript, Python, Rust, Go and C packages give the same output:
each step is a short function that reads the same way in all five languages.
Each step below is a rule group in the spec:
input └─ normalize Unicode NFC, invisible characters, dash/quote/bracket variants (R1) └─ mode prelude file extension, pipes, platform suffixes, uploader (R2) └─ prefixes track number, cue time (R4) └─ groups brackets swapped for placeholders, each group classified (R3, R5) └─ separator find the artist/title split (R6) ├─ artist side joiners, features (R7) └─ title side versions, junk, title credits (R8) └─ assemble dedupe credits, build the ParsedTrack (R9)The tree above runs inside parse; normalize
exposes its first step on its own. Every port keeps one source file per step, with the same rule
IDs in the comments:
js/src,
python/src/trackparse,
rust/src,
go and
c/src.
Brackets first
Section titled “Brackets first”Before anything is split, every bracket group is swapped for a placeholder. That's what keeps a &
inside (Chase & Status Remix) from breaking the artist list, and a dash inside [Drum - Bass] from
being taken as the separator.
Each group is then classified on its own: junk, feat, producer, flag, year, version, or unknown. Unknown groups go back into the title, untouched. This is where "extract, don't delete" lives in the code: there's no path that drops an unknown group. The scanner is R3 and the classification is R5.
Finding the separator
Section titled “Finding the separator”The separator search is the part that differs most between modes. In clean mode only a spaced
dash counts, and dash suffixes that look like versions (- Remastered 2015) are peeled off the end
first. youtube adds pipes, quoted titles, "Title" by Artist and unspaced dashes. filename
adds underscores and 01-Track numbering. Each fallback leaves a
warning, so you can always tell a confident split from a guess.
The search is R6, and
Modes shows the difference on real strings.
Words come from data
Section titled “Words come from data”Every word list (joiners, feat markers, version keywords, junk phrases, genres, extensions) lives in
JSON files under spec/data. The ports
compile them into source at build time. No keyword is hard-coded in a scanner, which is why
keywords in the options can extend any of them.
Portability rules
Section titled “Portability rules”The spec opens with rule R0, which exists so that every port gives the same bytes:
- Whitespace is an explicit list of code points, never a language's
\sortrim()(R0.2). - Keywords compare with ASCII-only lowercasing (R0.3). Full Unicode folding is used only for deduplicating names (R0.4).
- Strings are walked by code point, not by UTF-16 unit or byte (R0.5).
- A "digit" is
0to9and nothing else. - "Letter" and "uppercase letter" mean the Unicode general categories L and Lu, not whatever a
language's
isalphaaccepts (R0.6). - Any string must produce a result. Nothing throws or panics, and the C library returns
NULLonly when memory runs out (R0.8).
Ports lists the places where a language's standard library disagreed with these rules.
Tracklists on top
Section titled “Tracklists on top”Tracklists (R13) reuse the whole pipeline. The text is split into
lines, keeping each line's terminator. A small scanner reads the time at the start of each line, and
the rest goes through parse() in tracklist mode.
Two decisions look across lines. A bare track number is only accepted when a neighbor continues the sequence. And an unspaced dash only splits when at least half the list uses spaced dashes. To make both decisions without parsing everything twice, each line first gets a cheap probe (time, normalization, prefixes), then one real parse. The checks run last, over the finished entries.
The tracklist rules are R13.
See also
Section titled “See also”- The spec for the full rules.
- How it's tested for the fixtures and the cross-port diff.
- Ports for the five implementations.