Skip to content

Other functions

All of these are exported from trackparse. The Python package and the Rust crate have the same functions in snake_case, the Go package in CamelCase, and the C library in snake_case with a trackparse_ prefix. parse and the tracklist functions have pages of their own.

JavaScript What it does
parse Parses one track string
parseArtists Splits a bare artist string into credits
format Renders a track back to a string
normalize The Unicode cleanup every function runs first
allArtists The artists and remixers in one list, without duplicates
createParser Compiles options once for many calls
SPEC_VERSION The spec version the package implements

The tracklist functions are parseTracklist and applyEdits, from the entry trackparse/tracklist in JavaScript. They are parse_tracklist and apply_edits in Python (from trackparse.tracklist); parse_tracklist, parse_tracklist_with, apply_edits and apply_edits_with in Rust (from trackparse::tracklist); ParseTracklist, ParseTracklistWith, ApplyEdits and ApplyEditsWith in Go (same package); and trackparse_parse_tracklist, trackparse_parse_tracklist_with and trackparse_apply_edits in C (same header).

What to know for each language:

  • JavaScript (trackparse on npm). Options are one object, the last argument. No function throws on a string. The tracklist functions are in the separate entry trackparse/tracklist.
  • Python (trackparse on PyPI). Options are keyword arguments, and fields are snake_case attributes (full_title, unknown_artist). Every function that takes a string raises TypeError if the input isn't a str. format_track and all_artists take a ParsedTrack and don't check it, so a wrong value fails with AttributeError. Anything else, bad options included, falls back to defaults.
  • Rust (trackparse on crates.io). There are no optional arguments, so each function that takes options comes in two forms. The plain one uses the defaults, and the _with one takes a reference to an options struct (parse_with(input, &options)). Inputs are &str, fields are snake_case, results are returned by value with no Result, and no function panics. The signatures are on docs.rs.
  • Go (github.com/4matic/trackparse/go). Like Rust, each function that takes options comes in two forms: Parse(input) and ParseWith(input, options). Options are structs passed by value, and their zero value means the defaults (the two fields whose zero value is also a real setting are pointers, set with trackparse.Ptr). Fields are exported CamelCase (FullTitle, UnknownArtist), nullable fields are pointers, and results are returned by value with no error. No function panics, and input that is not valid UTF-8 is read with U+FFFD in place of each bad sequence. The tracklist functions are in the same package. Every function has an entry with examples on pkg.go.dev.
  • C (one header, trackparse.h). Each function that takes options has a _with form that takes an options handle, and NULL there means the defaults. Input is UTF-8 as a pointer and a byte length; pass TRACKPARSE_NUL_TERMINATED for a C string. Results are read-only structs that own all their memory: free each with its own function (trackparse_track_free, trackparse_artists_free, trackparse_string_free, and so on). Lists are a pointer and a _count member, nullable fields are pointers that are NULL when absent, and enums have a _name() function that gives the spec's string. Strings in results are trackparse_str, a ptr and a len, and ptr is also NUL-terminated. A function returns NULL only when memory runs out; it never crashes on any input. All functions are thread-safe. trackparse_set_allocator replaces malloc, realloc and free for the whole process; call it once, before any other function.
  • C++ (trackparse.hpp). A header-only C++17 wrapper over the C library, installed next to trackparse.h. The functions are in the namespace trackparse with the C names minus the prefix (trackparse::parse, trackparse::parse_tracklist), and options are a second argument instead of a _with form. Results are move-only owners that free their C object, strings read from results are std::string_view (format, normalize and to_json return std::string), nullable fields are std::optional, and options chain: trackparse::parse_options{}.known_artist("Chase & Status"). The only exception it throws is std::bad_alloc, when memory runs out. The C README has a full example.

The options themselves are on the Options page.

(Python: parse_artists. Rust: parse_artists, parse_artists_with. Go: ParseArtists, ParseArtistsWith. C: trackparse_parse_artists, trackparse_parse_artists_with)

Splits a bare artist string, with no title, into credits.

import { allArtists, format, normalize, parse, parseArtists } from "trackparse";
parseArtists("Sub Focus, Wilkinson & Dimension feat. Kojo").map((a) => `${a.name}:${a.role}`);
// [ 'Sub Focus:primary', 'Wilkinson:primary', 'Dimension:primary', 'Kojo:featured' ]

Takes knownArtists, splitAnd and keywords (known_artists, split_and, keywords; in Rust through parse_artists_with, in Go through ParseArtistsWith, in C through trackparse_parse_artists_with). Use it for the artist field of ID3 tags or a streaming API, where the title is already separate. The result is a list of Artist values (a list in Python, a Vec in Rust, a slice in Go, a trackparse_artists with items and count in C), and the splitting rules are explained in Artists and credits. In the spec this is R10.

(Python: format_track. Rust: format_track, format_track_with. Go: Format, FormatWith. C: trackparse_format, trackparse_format_with)

Renders a ParsedTrack back to a string. Junk is left out, and a label is written after the title as • VISION.

format(parse("Wilkinson ft. Becky Hill - Afterglow (Sub Focus Remix)"), { feat: "title", joiners: "canonical" });
// 'Wilkinson - Afterglow (feat. Becky Hill) (Sub Focus Remix)'

The options are feat, featMarker (feat_marker), joiners, versions, producers, position and label. In Go they are the fields of FormatOptions, where OmitProducers: true stands for producers: false and OmitLabel: true for label: false. In C they are setters on a trackparse_format_options handle, where trackparse_format_options_set_omit_producers(o, true) stands for producers: false and trackparse_format_options_set_omit_label(o, true) for label: false. Formatting has the table and an example of each. In the spec this is R11.

(Python: normalize. Rust: normalize. Go: Normalize. C: trackparse_normalize)

The Unicode cleanup every function runs first. It applies NFC, removes invisible characters (zero-width spaces, BOMs, soft hyphens), turns dash, bracket and quote variants into their plain ASCII forms, and collapses whitespace.

normalize("Noisia​ — “Stigma”"); // 'Noisia - "Stigma"'

You don't need to call it before parse. It's exported because it helps when you build lookup keys. The steps are R1 in the spec.

(Python: all_artists. Rust: all_artists. Go: AllArtists. C: trackparse_all_artists)

Every credit of a track in one list: the artists, then every remixer, with duplicates removed. Names are compared with the dedup key of R0.4, so a remixer who is also credited as an artist appears once, with their first role. Artists and credits explains where each credit comes from.

allArtists(parse("Wilkinson ft. Becky Hill - Afterglow (Sub Focus Remix)")).map((a) => `${a.name}:${a.role}`);
// [ 'Wilkinson:primary', 'Becky Hill:featured', 'Sub Focus:remixer' ]

(Python: create_parser. Rust: Parser::new. Go: NewParser, which returns a Parser. C: trackparse_parser_new)

Compiles options and keywords once and returns an object with parse and parseArtists (parse_artists). Use it when you parse many strings with the same knownArtists or keywords.

In JavaScript and Python the two methods still accept options, and per-call options are merged over the compiled ones: parser.parse(s, { mode: "youtube" }), parser.parse(s, mode="youtube"). The Rust Parser takes its options once, in Parser::new(&options). Its parse and parse_artists take only the input, and it is Send + Sync, so one parser can serve many threads. The Go NewParser(options) works the same way: its Parse and ParseArtists methods take only the input, and a *Parser is safe for concurrent use by many goroutines. The C trackparse_parser_new(options) copies what it needs from the options handle, so the handle can be freed right away. trackparse_parser_parse and trackparse_parser_parse_artists take only the input, and a parser is immutable, so many threads can share one. Free it with trackparse_parser_free.

import { createParser } from "trackparse";
const parser = createParser({
knownArtists: ["Chase & Status", "Camo & Krooked"],
keywords: { versionHeads: { rerub: "rework" } },
});
parser.parse("Chase & Status x Camo & Krooked - Track (Alix Perez Rerub)").versions[0].type;
// 'rework'

(Rust: SPEC_VERSION. Go: SpecVersion. C: trackparse_spec_version())

The version of the spec the package implements, as a string (&str in Rust, the constant SpecVersion in Go, the function trackparse_spec_version() in C, which returns a static string). The packages and the spec are released together, so it's the same as the package version. A parsed tracklist carries it too, as specVersion (spec_version in Python, Rust and C, SpecVersion in Go). In C, trackparse_library_version() gives the version of the linked library and the macro TRACKPARSE_VERSION that of the header, which differ only when a shared library is replaced.