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 |
| JavaScript | Python |
|---|---|
parse |
parse |
parseArtists |
parse_artists |
format |
format_track |
normalize |
normalize |
allArtists |
all_artists |
createParser |
create_parser |
SPEC_VERSION |
SPEC_VERSION |
| JavaScript | Rust |
|---|---|
parse |
parse, parse_with |
parseArtists |
parse_artists, parse_artists_with |
format |
format_track, format_track_with |
normalize |
normalize |
allArtists |
all_artists |
createParser |
Parser::new |
SPEC_VERSION |
SPEC_VERSION |
| JavaScript | Go |
|---|---|
parse |
Parse, ParseWith |
parseArtists |
ParseArtists, ParseArtistsWith |
format |
Format, FormatWith |
normalize |
Normalize |
allArtists |
AllArtists |
createParser |
NewParser |
SPEC_VERSION |
SpecVersion |
| JavaScript | C |
|---|---|
parse |
trackparse_parse, trackparse_parse_with |
parseArtists |
trackparse_parse_artists, trackparse_parse_artists_with |
format |
trackparse_format, trackparse_format_with |
normalize |
trackparse_normalize |
allArtists |
trackparse_all_artists |
createParser |
trackparse_parser_new |
SPEC_VERSION |
trackparse_spec_version() |
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 (
trackparseon npm). Options are one object, the last argument. No function throws on a string. The tracklist functions are in the separate entrytrackparse/tracklist. - Python (
trackparseon PyPI). Options are keyword arguments, and fields are snake_case attributes (full_title,unknown_artist). Every function that takes a string raisesTypeErrorif the input isn't astr.format_trackandall_artiststake aParsedTrackand don't check it, so a wrong value fails withAttributeError. Anything else, bad options included, falls back to defaults. - Rust (
trackparseon 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_withone takes a reference to an options struct (parse_with(input, &options)). Inputs are&str, fields are snake_case, results are returned by value with noResult, 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)andParseWith(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 withtrackparse.Ptr). Fields are exported CamelCase (FullTitle,UnknownArtist), nullable fields are pointers, and results are returned by value with noerror. 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_withform that takes an options handle, andNULLthere means the defaults. Input is UTF-8 as a pointer and a byte length; passTRACKPARSE_NUL_TERMINATEDfor 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_countmember, nullable fields are pointers that areNULLwhen absent, and enums have a_name()function that gives the spec's string. Strings in results aretrackparse_str, aptrand alen, andptris also NUL-terminated. A function returnsNULLonly when memory runs out; it never crashes on any input. All functions are thread-safe.trackparse_set_allocatorreplacesmalloc,reallocandfreefor 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 totrackparse.h. The functions are in the namespacetrackparsewith the C names minus the prefix (trackparse::parse,trackparse::parse_tracklist), and options are a second argument instead of a_withform. Results are move-only owners that free their C object, strings read from results arestd::string_view(format,normalizeandto_jsonreturnstd::string), nullable fields arestd::optional, and options chain:trackparse::parse_options{}.known_artist("Chase & Status"). The only exception it throws isstd::bad_alloc, when memory runs out. The C README has a full example.
The options themselves are on the Options page.
parseArtists(input, options?)
Section titled “parseArtists(input, options?)”(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' ]from trackparse import all_artists, format_track, normalize, parse, parse_artists
[f"{a.name}:{a.role}" for a in parse_artists("Sub Focus, Wilkinson & Dimension feat. Kojo")]# ['Sub Focus:primary', 'Wilkinson:primary', 'Dimension:primary', 'Kojo:featured']use trackparse::{all_artists, format_track_with, normalize, parse, parse_artists};use trackparse::{FeatPlacement, FormatOptions, JoinerStyle};
parse_artists("Sub Focus, Wilkinson & Dimension feat. Kojo") .iter().map(|a| format!("{}:{}", a.name, a.role)).collect::<Vec<_>>();// ["Sub Focus:primary", "Wilkinson:primary", "Dimension:primary", "Kojo:featured"]import ( "fmt"
"github.com/4matic/trackparse/go")
for _, a := range trackparse.ParseArtists("Sub Focus, Wilkinson & Dimension feat. Kojo") { fmt.Printf("%s:%s\n", a.Name, a.Role)}// Sub Focus:primary// Wilkinson:primary// Dimension:primary// Kojo:featured#include <trackparse.h>
trackparse_artists *as = trackparse_parse_artists("Sub Focus, Wilkinson & Dimension feat. Kojo", TRACKPARSE_NUL_TERMINATED);for (size_t i = 0; i < as->count; i++) { printf("%s:%s\n", as->items[i].name.ptr, trackparse_role_name(as->items[i].role));}// Sub Focus:primary// Wilkinson:primary// Dimension:primary// Kojo:featuredtrackparse_artists_free(as);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.
format(track, options?)
Section titled “format(track, options?)”(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)'format_track(parse("Wilkinson ft. Becky Hill - Afterglow (Sub Focus Remix)"), feat="title", joiners="canonical")# 'Wilkinson - Afterglow (feat. Becky Hill) (Sub Focus Remix)'let options = FormatOptions::new().feat(FeatPlacement::Title).joiners(JoinerStyle::Canonical);format_track_with(&parse("Wilkinson ft. Becky Hill - Afterglow (Sub Focus Remix)"), &options);// "Wilkinson - Afterglow (feat. Becky Hill) (Sub Focus Remix)"options := trackparse.FormatOptions{Feat: trackparse.FeatTitle, Joiners: trackparse.JoinersCanonical}fmt.Printf("%q\n", trackparse.FormatWith(trackparse.Parse("Wilkinson ft. Becky Hill - Afterglow (Sub Focus Remix)"), options))// "Wilkinson - Afterglow (feat. Becky Hill) (Sub Focus Remix)"trackparse_format_options *fo = trackparse_format_options_new();trackparse_format_options_set_feat(fo, TRACKPARSE_FEAT_TITLE);trackparse_format_options_set_joiners(fo, TRACKPARSE_JOINERS_CANONICAL);trackparse_track *t = trackparse_parse("Wilkinson ft. Becky Hill - Afterglow (Sub Focus Remix)", TRACKPARSE_NUL_TERMINATED);trackparse_string *s = trackparse_format_with(t, fo);printf("%s\n", s->str.ptr);// Wilkinson - Afterglow (feat. Becky Hill) (Sub Focus Remix)trackparse_string_free(s);trackparse_format_options_free(fo);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.
normalize(input)
Section titled “normalize(input)”(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"'normalize("Noisia\u200b — “Stigma”") # 'Noisia - "Stigma"'normalize("Noisia\u{200b} — “Stigma”"); // "Noisia - \"Stigma\""fmt.Printf("%q\n", trackparse.Normalize("Noisia\u200b — “Stigma”")) // "Noisia - \"Stigma\""s = trackparse_normalize("Noisia\u200b — “Stigma”", TRACKPARSE_NUL_TERMINATED);printf("%s\n", s->str.ptr); // Noisia - "Stigma"trackparse_string_free(s);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.
allArtists(track)
Section titled “allArtists(track)”(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' ][f"{a.name}:{a.role}" for a in all_artists(parse("Wilkinson ft. Becky Hill - Afterglow (Sub Focus Remix)"))]# ['Wilkinson:primary', 'Becky Hill:featured', 'Sub Focus:remixer']all_artists(&parse("Wilkinson ft. Becky Hill - Afterglow (Sub Focus Remix)")) .iter().map(|a| format!("{}:{}", a.name, a.role)).collect::<Vec<_>>();// ["Wilkinson:primary", "Becky Hill:featured", "Sub Focus:remixer"]for _, a := range trackparse.AllArtists(trackparse.Parse("Wilkinson ft. Becky Hill - Afterglow (Sub Focus Remix)")) { fmt.Printf("%s:%s\n", a.Name, a.Role)}// Wilkinson:primary// Becky Hill:featured// Sub Focus:remixeras = trackparse_all_artists(t); /* t is the Wilkinson track from the format example */for (size_t i = 0; i < as->count; i++) { printf("%s:%s\n", as->items[i].name.ptr, trackparse_role_name(as->items[i].role));}// Wilkinson:primary// Becky Hill:featured// Sub Focus:remixertrackparse_artists_free(as);trackparse_track_free(t);createParser(options)
Section titled “createParser(options)”(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'from trackparse import create_parser
parser = create_parser( known_artists=["Chase & Status", "Camo & Krooked"], keywords={"version_heads": {"rerub": "rework"}},)
parser.parse("Chase & Status x Camo & Krooked - Track (Alix Perez Rerub)").versions[0].type# 'rework'use trackparse::{KeywordOptions, ParseOptions, Parser, VersionType};
let options = ParseOptions::new() .known_artists(["Chase & Status", "Camo & Krooked"]) .keywords(KeywordOptions::new().version_head("rerub", VersionType::Rework));let parser = Parser::new(&options);
parser.parse("Chase & Status x Camo & Krooked - Track (Alix Perez Rerub)").versions[0].version_type;// Reworkparser := trackparse.NewParser(trackparse.ParseOptions{ KnownArtists: []string{"Chase & Status", "Camo & Krooked"}, Keywords: trackparse.Keywords{ VersionHeads: []trackparse.VersionHead{{Key: "rerub", Type: trackparse.VersionTypeRework}}, },})
fmt.Println(parser.Parse("Chase & Status x Camo & Krooked - Track (Alix Perez Rerub)").Versions[0].Type)// reworktrackparse_parse_options *o = trackparse_parse_options_new();trackparse_parse_options_add_known_artist(o, "Chase & Status", TRACKPARSE_NUL_TERMINATED);trackparse_parse_options_add_known_artist(o, "Camo & Krooked", TRACKPARSE_NUL_TERMINATED);trackparse_parse_options_add_version_head(o, "rerub", TRACKPARSE_NUL_TERMINATED, "rework", TRACKPARSE_NUL_TERMINATED);trackparse_parser *parser = trackparse_parser_new(o);trackparse_parse_options_free(o);
t = trackparse_parser_parse(parser, "Chase & Status x Camo & Krooked - Track (Alix Perez Rerub)", TRACKPARSE_NUL_TERMINATED);printf("%s\n", t->versions[0].type_name.ptr);// reworktrackparse_track_free(t);trackparse_parser_free(parser);SPEC_VERSION
Section titled “SPEC_VERSION”(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.
See also
Section titled “See also”parseandParsedTrackand Options.- Tracklist API for
parseTracklistandapplyEdits. - Warnings for the codes these functions report.
- Ports for how the five packages are kept identical.