Formatting
format(track, options?) renders a parsed
track back to a string. In Python it is format_track(track, **options). In Rust it is
format_track(&track), or
format_track_with(&track, &options) with options. In Go it is
trackparse.Format(track), or trackparse.FormatWith(track, options).
In C it is trackparse_format(track), or trackparse_format_with(track, options), and the
string it returns is freed with trackparse_string_free
(trackparse.h).
Use it to bring a messy library to one style, or to build a display title without the junk. The
rules are R11 in the spec.
import { format, parse } from "trackparse";
const t = parse( "01. Wilkinson ft. Becky Hill & Tom Cane - Afterglow (Sub Focus VIP Remix) [Official Video]",);
format(t);// 'Wilkinson ft. Becky Hill & Tom Cane - Afterglow (Sub Focus VIP Remix)'from trackparse import format_track, parse
t = parse( "01. Wilkinson ft. Becky Hill & Tom Cane - Afterglow (Sub Focus VIP Remix) [Official Video]")
format_track(t)# 'Wilkinson ft. Becky Hill & Tom Cane - Afterglow (Sub Focus VIP Remix)'use trackparse::{format_track, parse};
let t = parse( "01. Wilkinson ft. Becky Hill & Tom Cane - Afterglow (Sub Focus VIP Remix) [Official Video]",);
format_track(&t);// "Wilkinson ft. Becky Hill & Tom Cane - Afterglow (Sub Focus VIP Remix)"import ( "fmt"
"github.com/4matic/trackparse/go")
t := trackparse.Parse( "01. Wilkinson ft. Becky Hill & Tom Cane - Afterglow (Sub Focus VIP Remix) [Official Video]",)
fmt.Printf("%q\n", trackparse.Format(t))// "Wilkinson ft. Becky Hill & Tom Cane - Afterglow (Sub Focus VIP Remix)"#include <trackparse.h>
trackparse_track *t = trackparse_parse( "01. Wilkinson ft. Becky Hill & Tom Cane - Afterglow (Sub Focus VIP Remix) [Official Video]", TRACKPARSE_NUL_TERMINATED);
trackparse_string *s = trackparse_format(t);printf("%s\n", s->str.ptr);// Wilkinson ft. Becky Hill & Tom Cane - Afterglow (Sub Focus VIP Remix)trackparse_string_free(s);Junk is never written back. Everything else is, by default, the way it was written. The
playground has a format() panel with this track loaded.
Options
Section titled “Options”| Option | Values | Default |
|---|---|---|
feat |
"source" keep features where they were, "artist" move them left of the dash, "title" move them into the title, "omit" drop them (R11.2) |
"source" |
featMarker |
the word to write before features | the one from the input |
joiners |
"original" as written, "canonical" normalized (and → &, ; → ,, ft. → feat.; R11.5) |
"original" |
versions |
"all" or "none" |
"all" |
producers |
write (prod. by …) credits |
true |
position |
write the track number back | false |
label |
write the label back after the title, • VISION (R11.3) |
true |
Python takes the options as keyword arguments, so featMarker is feat_marker. Rust takes a
FormatOptions value built with one setter per option, and the
values are enums (FeatPlacement,
JoinerStyle, VersionsOption). Go
takes a FormatOptions struct whose zero value is the default, and the values
are typed string constants (FeatPlacement, JoinerStyle,
VersionsOption); trackparse.Ptr sets FeatMarker.
Since a Go bool field defaults to false, producers becomes OmitProducers and label
becomes OmitLabel. C takes a
trackparse_format_options handle from trackparse_format_options_new(), with one setter per
option and C enums for the values; an option you don't set keeps its default, and
trackparse_format_options_free releases the handle. Each tab below starts with the names in
that language.
The names and values are those of the table above, as keys of one object.
format(t, { feat: "title" });// 'Wilkinson - Afterglow (ft. Becky Hill & Tom Cane) (Sub Focus VIP Remix)'
format(t, { feat: "title", joiners: "canonical" });// 'Wilkinson - Afterglow (feat. Becky Hill & Tom Cane) (Sub Focus VIP Remix)'
format(t, { feat: "artist", featMarker: "featuring" });// 'Wilkinson featuring Becky Hill & Tom Cane - Afterglow (Sub Focus VIP Remix)'
format(t, { feat: "omit" }); // 'Wilkinson - Afterglow (Sub Focus VIP Remix)'format(t, { versions: "none" }); // 'Wilkinson ft. Becky Hill & Tom Cane - Afterglow'format(t, { position: true }); // '01. Wilkinson ft. Becky Hill & Tom Cane - Afterglow (Sub Focus VIP Remix)'
format(parse("Future - Mask Off (prod. by Metro Boomin)"), { producers: false });// 'Future - Mask Off'feat="title":"source","artist","title"or"omit"feat_marker="featuring",joiners="canonical",versions="none"producers=False,position=True,label=False
format_track(t, feat="title")# 'Wilkinson - Afterglow (ft. Becky Hill & Tom Cane) (Sub Focus VIP Remix)'
format_track(t, feat="title", joiners="canonical")# 'Wilkinson - Afterglow (feat. Becky Hill & Tom Cane) (Sub Focus VIP Remix)'
format_track(t, feat="artist", feat_marker="featuring")# 'Wilkinson featuring Becky Hill & Tom Cane - Afterglow (Sub Focus VIP Remix)'
format_track(t, feat="omit") # 'Wilkinson - Afterglow (Sub Focus VIP Remix)'format_track(t, versions="none") # 'Wilkinson ft. Becky Hill & Tom Cane - Afterglow'format_track(t, position=True) # '01. Wilkinson ft. Becky Hill & Tom Cane - Afterglow (Sub Focus VIP Remix)'
format_track(parse("Future - Mask Off (prod. by Metro Boomin)"), producers=False)# 'Future - Mask Off'.feat(FeatPlacement::Title):Source,Artist,TitleorOmit.feat_marker("featuring").joiners(JoinerStyle::Canonical):OriginalorCanonical.versions(VersionsOption::None):AllorNone.producers(false),.position(true),.label(false)
use trackparse::{format_track_with, FeatPlacement, FormatOptions, JoinerStyle, VersionsOption};
format_track_with(&t, &FormatOptions::new().feat(FeatPlacement::Title));// "Wilkinson - Afterglow (ft. Becky Hill & Tom Cane) (Sub Focus VIP Remix)"
format_track_with(&t, &FormatOptions::new().feat(FeatPlacement::Title).joiners(JoinerStyle::Canonical));// "Wilkinson - Afterglow (feat. Becky Hill & Tom Cane) (Sub Focus VIP Remix)"
format_track_with(&t, &FormatOptions::new().feat(FeatPlacement::Artist).feat_marker("featuring"));// "Wilkinson featuring Becky Hill & Tom Cane - Afterglow (Sub Focus VIP Remix)"
format_track_with(&t, &FormatOptions::new().feat(FeatPlacement::Omit));// "Wilkinson - Afterglow (Sub Focus VIP Remix)"format_track_with(&t, &FormatOptions::new().versions(VersionsOption::None));// "Wilkinson ft. Becky Hill & Tom Cane - Afterglow"format_track_with(&t, &FormatOptions::new().position(true));// "01. Wilkinson ft. Becky Hill & Tom Cane - Afterglow (Sub Focus VIP Remix)"
format_track_with( &parse("Future - Mask Off (prod. by Metro Boomin)"), &FormatOptions::new().producers(false),);// "Future - Mask Off"Feat: trackparse.FeatTitle:FeatSource,FeatArtist,FeatTitleorFeatOmitFeatMarker: trackparse.Ptr("featuring")(a*string)Joiners: trackparse.JoinersCanonical:JoinersOriginalorJoinersCanonicalVersions: trackparse.VersionsNone:VersionsAllorVersionsNoneOmitProducers: trueforproducers: false,OmitLabel: trueforlabel: false, andPosition: true
fmt.Printf("%q\n", trackparse.FormatWith(t, trackparse.FormatOptions{Feat: trackparse.FeatTitle}))// "Wilkinson - Afterglow (ft. Becky Hill & Tom Cane) (Sub Focus VIP Remix)"
fmt.Printf("%q\n", trackparse.FormatWith(t, trackparse.FormatOptions{ Feat: trackparse.FeatTitle, Joiners: trackparse.JoinersCanonical,}))// "Wilkinson - Afterglow (feat. Becky Hill & Tom Cane) (Sub Focus VIP Remix)"
fmt.Printf("%q\n", trackparse.FormatWith(t, trackparse.FormatOptions{Feat: trackparse.FeatArtist, FeatMarker: trackparse.Ptr("featuring")}))// "Wilkinson featuring Becky Hill & Tom Cane - Afterglow (Sub Focus VIP Remix)"
fmt.Printf("%q\n", trackparse.FormatWith(t, trackparse.FormatOptions{Feat: trackparse.FeatOmit}))// "Wilkinson - Afterglow (Sub Focus VIP Remix)"fmt.Printf("%q\n", trackparse.FormatWith(t, trackparse.FormatOptions{Versions: trackparse.VersionsNone}))// "Wilkinson ft. Becky Hill & Tom Cane - Afterglow"fmt.Printf("%q\n", trackparse.FormatWith(t, trackparse.FormatOptions{Position: true}))// "01. Wilkinson ft. Becky Hill & Tom Cane - Afterglow (Sub Focus VIP Remix)"
fmt.Printf("%q\n", trackparse.FormatWith( trackparse.Parse("Future - Mask Off (prod. by Metro Boomin)"), trackparse.FormatOptions{OmitProducers: true},))// "Future - Mask Off"trackparse_format_options_set_feat(o, TRACKPARSE_FEAT_TITLE):_SOURCE,_ARTIST,_TITLEor_OMITtrackparse_format_options_set_feat_marker(o, "featuring", TRACKPARSE_NUL_TERMINATED)trackparse_format_options_set_joiners(o, TRACKPARSE_JOINERS_CANONICAL):_ORIGINALor_CANONICALtrackparse_format_options_set_versions(o, TRACKPARSE_VERSIONS_NONE):_ALLor_NONEtrackparse_format_options_set_omit_producers(o, true)forproducers: falsetrackparse_format_options_set_omit_label(o, true)forlabel: falsetrackparse_format_options_set_position(o, true)
trackparse_format_options *o = trackparse_format_options_new();trackparse_format_options_set_feat(o, TRACKPARSE_FEAT_TITLE);s = trackparse_format_with(t, o);printf("%s\n", s->str.ptr);// Wilkinson - Afterglow (ft. Becky Hill & Tom Cane) (Sub Focus VIP Remix)trackparse_string_free(s);
trackparse_format_options_set_joiners(o, TRACKPARSE_JOINERS_CANONICAL);s = trackparse_format_with(t, o);printf("%s\n", s->str.ptr);// Wilkinson - Afterglow (feat. Becky Hill & Tom Cane) (Sub Focus VIP Remix)trackparse_string_free(s);trackparse_format_options_free(o);
o = trackparse_format_options_new();trackparse_format_options_set_feat(o, TRACKPARSE_FEAT_ARTIST);trackparse_format_options_set_feat_marker(o, "featuring", TRACKPARSE_NUL_TERMINATED);s = trackparse_format_with(t, o);printf("%s\n", s->str.ptr);// Wilkinson featuring Becky Hill & Tom Cane - Afterglow (Sub Focus VIP Remix)trackparse_string_free(s);trackparse_format_options_free(o);
o = trackparse_format_options_new();trackparse_format_options_set_feat(o, TRACKPARSE_FEAT_OMIT);s = trackparse_format_with(t, o);printf("%s\n", s->str.ptr); // Wilkinson - Afterglow (Sub Focus VIP Remix)trackparse_string_free(s);trackparse_format_options_free(o);
o = trackparse_format_options_new();trackparse_format_options_set_versions(o, TRACKPARSE_VERSIONS_NONE);s = trackparse_format_with(t, o);printf("%s\n", s->str.ptr); // Wilkinson ft. Becky Hill & Tom Cane - Afterglowtrackparse_string_free(s);trackparse_format_options_free(o);
o = trackparse_format_options_new();trackparse_format_options_set_position(o, true);s = trackparse_format_with(t, o);printf("%s\n", s->str.ptr);// 01. Wilkinson ft. Becky Hill & Tom Cane - Afterglow (Sub Focus VIP Remix)trackparse_string_free(s);trackparse_format_options_free(o);
trackparse_track *mask = trackparse_parse("Future - Mask Off (prod. by Metro Boomin)", TRACKPARSE_NUL_TERMINATED);o = trackparse_format_options_new();trackparse_format_options_set_omit_producers(o, true);s = trackparse_format_with(mask, o);printf("%s\n", s->str.ptr); // Future - Mask Offtrackparse_string_free(s);trackparse_format_options_free(o);trackparse_track_free(mask);trackparse_track_free(t);Labels
Section titled “Labels”A label after a bullet is written back
after the versions and producers, as " • " + label. Notes are junk, so they are not
(try it):
const l = parse("Noisia - concussion • VISION • VISION RADIO RELOAD");format(l); // 'Noisia - concussion • VISION'format(l, { label: false }); // 'Noisia - concussion'l = parse("Noisia - concussion • VISION • VISION RADIO RELOAD")format_track(l) # 'Noisia - concussion • VISION'format_track(l, label=False) # 'Noisia - concussion'let l = parse("Noisia - concussion • VISION • VISION RADIO RELOAD");format_track(&l); // "Noisia - concussion • VISION"format_track_with(&l, &FormatOptions::new().label(false)); // "Noisia - concussion"l := trackparse.Parse("Noisia - concussion • VISION • VISION RADIO RELOAD")fmt.Printf("%q\n", trackparse.Format(l)) // "Noisia - concussion • VISION"fmt.Printf("%q\n", trackparse.FormatWith(l, trackparse.FormatOptions{OmitLabel: true})) // "Noisia - concussion"trackparse_track *l = trackparse_parse("Noisia - concussion • VISION • VISION RADIO RELOAD", TRACKPARSE_NUL_TERMINATED);s = trackparse_format(l);printf("%s\n", s->str.ptr); // Noisia - concussion • VISIONtrackparse_string_free(s);o = trackparse_format_options_new();trackparse_format_options_set_omit_label(o, true);s = trackparse_format_with(l, o);printf("%s\n", s->str.ptr); // Noisia - concussiontrackparse_string_free(s);trackparse_format_options_free(o);trackparse_track_free(l);Round trip
Section titled “Round trip”Parsing the formatted string gives back the same artist names and roles, the same title, the same version types and remixers and the same label, as long as the first parse raised no warnings. The spec makes this an invariant (R11.6), and the property tests check it on generated input:
const s = "Noisia feat. Foreign Beggars - Shellshock (Phace Remix)";// same artists, title and versions as parse(s):const again = parse(format(parse(s)));again.fullTitle; // 'Shellshock (Phace Remix)'s = "Noisia feat. Foreign Beggars - Shellshock (Phace Remix)"# same artists, title and versions as parse(s):again = parse(format_track(parse(s)))again.full_title # 'Shellshock (Phace Remix)'let s = "Noisia feat. Foreign Beggars - Shellshock (Phace Remix)";// same artists, title and versions as parse(s):let again = parse(&format_track(&parse(s)));&again.full_title; // "Shellshock (Phace Remix)"s := "Noisia feat. Foreign Beggars - Shellshock (Phace Remix)"// same artists, title and versions as Parse(s):again := trackparse.Parse(trackparse.Format(trackparse.Parse(s)))fmt.Printf("%q\n", again.FullTitle) // "Shellshock (Phace Remix)"const char *in = "Noisia feat. Foreign Beggars - Shellshock (Phace Remix)";/* same artists, title and versions as trackparse_parse(in, ...): */trackparse_track *first = trackparse_parse(in, TRACKPARSE_NUL_TERMINATED);trackparse_string *written = trackparse_format(first);trackparse_track *again = trackparse_parse(written->str.ptr, written->str.len);printf("%s\n", again->full_title.ptr); // Shellshock (Phace Remix)trackparse_track_free(again);trackparse_string_free(written);trackparse_track_free(first);The label has two exceptions: it only comes back when the formatted string has an artist part
and a title. ID - Stigma • VISION has an unknown artist, so it formats to Stigma • VISION. That
string has no dash, and without one the bullet is not read as a label: the second parse gives the
title Stigma • VISION, no label and a noSeparator warning. Noisia - [HD] • VISION has an
empty title (the [HD] is junk), so it formats to Noisia - • VISION, and a bullet right after
the dash starts no label either: the second parse gives the title • VISION.
The playground
has a format() panel if you want to try the options on this track or your own strings. Its code
view writes the call for each language.
See also
Section titled “See also”formatin the reference, and Format options for how the options are typed in each language.- Editing tracklists uses
formatto rewrite the track text of a line. - Artists and credits and Versions and remixes explain the parts that get written.