Editing tracklists
applyEdits(text, edits)
(apply_edits in Python and Rust, ApplyEdits in Go, trackparse_apply_edits in C) takes a
tracklist as text and returns new text. It's built for editors: you get back the lines it changed, and every other line keeps its
bytes and its line ending. The rules are R13.9 in the
spec.
import { applyEdits, parseTracklist } from "trackparse/tracklist";from trackparse.tracklist import apply_edits, parse_tracklistuse trackparse::tracklist::{apply_edits, parse_tracklist, Edit};import ( "fmt"
"github.com/4matic/trackparse/go")#include <trackparse.h>Using the list from the previous page, apply the suggested fix, then push everything from line 5 on back by 45 seconds:
// text is the list from the previous page.const fix = parseTracklist(text).warnings[1].suggestion; // { line: 8, raw: '01:11:02', seconds: 4262 }
const result = applyEdits(text, [ { op: "set-time", line: fix.line, seconds: fix.seconds }, { op: "shift", seconds: 45, from: 5 },]);
result.changed; // [ 5, 6, 7, 8, 9 ]result.warnings; // [ { code: 'style-upgraded', edit: 1, line: 5 } ]result.text.split("\r\n").slice(4);// [// '01:00:16 Alix Perez - Fade Away',// '01:03:55 Interview with Hybris',// '01:08:29 Imanu - Sacrifice',// '01:11:47 Current Value feat. Teddy Killerz - Payload',// '01:16:21 Hybris - Spinning Top'// ]# text is the list from the previous page.fix = parse_tracklist(text).warnings[1].suggestion # Suggestion(line=8, raw='01:11:02', seconds=4262)
result = apply_edits(text, [ {"op": "set-time", "line": fix.line, "seconds": fix.seconds}, {"op": "shift", "seconds": 45, "from": 5},])
result.changed # (5, 6, 7, 8, 9)result.warnings # (EditWarning(code='style-upgraded', edit=1, line=5),)result.text.split("\r\n")[4:]# ['01:00:16 Alix Perez - Fade Away',# '01:03:55 Interview with Hybris',# '01:08:29 Imanu - Sacrifice',# '01:11:47 Current Value feat. Teddy Killerz - Payload',# '01:16:21 Hybris - Spinning Top']// text is the list from the previous page.let fix = parse_tracklist(&text).warnings[1].suggestion.clone().unwrap();// Suggestion { line: 8, raw: "01:11:02", seconds: 4262.0 }
let result = apply_edits(&text, &[Edit::set_time(fix.line, fix.seconds), Edit::shift_from(45.0, 5)]);
&result.changed; // [5, 6, 7, 8, 9]&result.warnings; // [EditWarning { code: StyleUpgraded, edit: 1, line: Some(5) }]result.text.split("\r\n").skip(4).collect::<Vec<_>>();// [// "01:00:16 Alix Perez - Fade Away",// "01:03:55 Interview with Hybris",// "01:08:29 Imanu - Sacrifice",// "01:11:47 Current Value feat. Teddy Killerz - Payload",// "01:16:21 Hybris - Spinning Top",// ]import "strings"
// text is the list from the previous page.text := strings.Join([]string{ "Hour 2", "48:20 Noisia - Stigma", "52:47 Mefjus & Kasra - Bandwagon (Phace Remix)", "56:05 Black Sun Empire - ID", "59:31 Alix Perez - Fade Away", "01:03:10 Interview with Hybris", "01:07:44 Imanu - Sacrifice", "00:11:02 Current Value feat. Teddy Killerz - Payload", "01:15:36 Hybris - Spinning Top",}, "\r\n")
fix := *trackparse.ParseTracklist(text).Warnings[1].Suggestionfmt.Printf("%+v\n", fix) // {Line:8 Raw:01:11:02 Seconds:4262}
result := trackparse.ApplyEdits(text, []trackparse.Edit{ trackparse.SetTime(fix.Line, fix.Seconds), trackparse.ShiftFrom(45, 5),})
fmt.Println(result.Changed) // [5 6 7 8 9]for _, w := range result.Warnings { fmt.Println(w.Code, w.Edit, *w.Line)}// style-upgraded 1 5for _, line := range strings.Split(result.Text, "\r\n")[4:] { fmt.Println(line)}// 01:00:16 Alix Perez - Fade Away// 01:03:55 Interview with Hybris// 01:08:29 Imanu - Sacrifice// 01:11:47 Current Value feat. Teddy Killerz - Payload// 01:16:21 Hybris - Spinning Top/* text is the list from the previous page. */const char *text = "Hour 2\r\n48:20 Noisia - Stigma\r\n52:47 Mefjus & Kasra - Bandwagon (Phace Remix)\r\n" "56:05 Black Sun Empire - ID\r\n59:31 Alix Perez - Fade Away\r\n" "01:03:10 Interview with Hybris\r\n01:07:44 Imanu - Sacrifice\r\n" "00:11:02 Current Value feat. Teddy Killerz - Payload\r\n01:15:36 Hybris - Spinning Top";
trackparse_tracklist *list = trackparse_parse_tracklist(text, TRACKPARSE_NUL_TERMINATED);const trackparse_suggestion *fix = list->warnings[1].suggestion;printf("%d %s %g\n", fix->line, fix->raw.ptr, fix->seconds); // 8 01:11:02 4262
trackparse_edits *edits = trackparse_edits_new();trackparse_edits_set_time(edits, fix->line, fix->seconds);trackparse_edits_shift_from(edits, 45, 5);trackparse_edit_result *result = trackparse_apply_edits(text, TRACKPARSE_NUL_TERMINATED, edits);trackparse_edits_free(edits);trackparse_tracklist_free(list);
for (size_t i = 0; i < result->changed_count; i++) { printf("%s%lld", i ? " " : "", result->changed[i]);}printf("\n"); // 5 6 7 8 9for (size_t i = 0; i < result->warnings_count; i++) { const trackparse_edit_warning *w = &result->warnings[i]; printf("%s %zu %lld\n", trackparse_edit_warning_code_name(w->code), w->edit, *w->line);}// style-upgraded 1 5const char *from = result->text.ptr;for (int n = 0; n < 4; n++) from = strstr(from, "\r\n") + 2; /* skip lines 1 to 4 */for (const char *c = from; *c; c++) { if (*c != '\r') putchar(*c);}putchar('\n');// 01:00:16 Alix Perez - Fade Away// 01:03:55 Interview with Hybris// 01:08:29 Imanu - Sacrifice// 01:11:47 Current Value feat. Teddy Killerz - Payload// 01:16:21 Hybris - Spinning Toptrackparse_edit_result_free(result);In JavaScript an edit is an object with an op. In Python it is a plain dict with the same keys,
"from" included. In Rust it is a value of the Edit enum, made
with a function per edit: Edit::set_time(line, seconds), Edit::shift_from(seconds, from). In Go
it is an Edit struct, made with a function per edit as well:
trackparse.SetTime(line, seconds), trackparse.ShiftFrom(seconds, from).
In C the edits go into a trackparse_edits list from trackparse_edits_new(), one call per edit
(trackparse_edits_set_time(edits, line, seconds), trackparse_edits_shift_from(edits, seconds, from)),
declared in trackparse.h.
The list copies the strings it is given, and the result owns its memory, so trackparse_edits_free
can run as soon as trackparse_apply_edits returns; the result is freed with
trackparse_edit_result_free.
Lines 1 to 4 are untouched, CRLF included (R13.9.1). Edits run in order, each on the text the previous one produced, with line numbers as that text has them. The playground does the same with buttons: the suggested fix, a shift and a sort, with undo.
The edits
Section titled “The edits”There are six edits. JavaScript and Python write them the same way, as an object or a dict. Rust
and Go have a constructor for each, and C a function that appends it to a trackparse_edits list;
the tabs under the table give those names:
| JavaScript and Python | Effect |
|---|---|
{ op: "set-time", line, seconds } |
Rewrite the time on that line |
{ op: "shift", seconds } |
Add seconds (negative to go back) to every timed line |
{ op: "shift", seconds, from } |
The same, for every timed line from line from on |
{ op: "shift", seconds, lines } |
The same, for the listed lines only |
{ op: "sort-by-time" } |
Reorder by time. Untimed lines move with the timed line above them, and lines before the first time stay on top |
{ op: "set-rest", line, rest } |
Replace everything after the time, for example with format(track) (Python and Rust format_track, Go Format, C trackparse_format) |
{ op: "insert", after, text } |
Insert a line after line after (0 puts it at the top). It gets the text's usual line ending |
{ op: "delete", line } |
Remove a line |
An edit is an object with an op and the fields in the table.
An edit is a dict with the same keys, quoted: {"op": "set-time", "line": 3, "seconds": 90}.
Each edit has a constructor on Edit.
| Edit | Rust |
|---|---|
set-time |
Edit::set_time(line, seconds) |
shift |
Edit::shift(seconds) |
shift with from |
Edit::shift_from(seconds, from) |
shift with lines |
Edit::shift_lines(seconds, lines) |
sort-by-time |
Edit::sort_by_time() |
set-rest |
Edit::set_rest(line, rest) |
insert |
Edit::insert(after, text) |
delete |
Edit::delete(line) |
Each edit has a constructor in the package, such as trackparse.SetTime.
| Edit | Go |
|---|---|
set-time |
SetTime(line, seconds) |
shift |
Shift(seconds) |
shift with from |
ShiftFrom(seconds, from) |
shift with lines |
ShiftLines(seconds, lines...) |
sort-by-time |
SortByTime() |
set-rest |
SetRest(line, rest) |
insert |
Insert(after, text) |
delete |
Delete(line) |
Each edit is a function that appends it to a trackparse_edits list.
| Edit | C |
|---|---|
set-time |
trackparse_edits_set_time(e, line, seconds) |
shift |
trackparse_edits_shift(e, seconds) |
shift with from |
trackparse_edits_shift_from(e, seconds, from) |
shift with lines |
trackparse_edits_shift_lines(e, seconds, lines, count) |
sort-by-time |
trackparse_edits_sort_by_time(e) |
set-rest |
trackparse_edits_set_rest(e, line, rest, len) |
insert |
trackparse_edits_insert(e, after, text, len) |
delete |
trackparse_edits_delete(e, line) |
Line numbers start at 1. Sorting is R13.9.4, and inserting and deleting R13.9.5.
This example sorts a list, rewrites one line's track text with
format, inserts a line and deletes one:
import { format } from "trackparse";
const messy = "04:10 Mefjus ft. Phace - Sleepless\n00:00 Noisia - Stigma\n09:30 Outro";const rest = format(parseTracklist(messy).entries[0].track, { joiners: "canonical" });// 'Mefjus feat. Phace - Sleepless'
const tidy = applyEdits(messy, [ { op: "sort-by-time" }, { op: "set-rest", line: 2, rest }, { op: "insert", after: 2, text: "06:45 Phace - Psycho" }, { op: "delete", line: 4 },]);
tidy.text; // '00:00 Noisia - Stigma\n04:10 Mefjus feat. Phace - Sleepless\n06:45 Phace - Psycho'tidy.changed; // [ 1, 2, 3 ]from trackparse import format_track
messy = "04:10 Mefjus ft. Phace - Sleepless\n00:00 Noisia - Stigma\n09:30 Outro"rest = format_track(parse_tracklist(messy).entries[0].track, joiners="canonical")# 'Mefjus feat. Phace - Sleepless'
tidy = apply_edits(messy, [ {"op": "sort-by-time"}, {"op": "set-rest", "line": 2, "rest": rest}, {"op": "insert", "after": 2, "text": "06:45 Phace - Psycho"}, {"op": "delete", "line": 4},])
tidy.text # '00:00 Noisia - Stigma\n04:10 Mefjus feat. Phace - Sleepless\n06:45 Phace - Psycho'tidy.changed # (1, 2, 3)use trackparse::{format_track_with, FormatOptions, JoinerStyle};
let messy = "04:10 Mefjus ft. Phace - Sleepless\n00:00 Noisia - Stigma\n09:30 Outro";let list = parse_tracklist(messy);let track = list.entries[0].track.as_ref().unwrap();let rest = format_track_with(track, &FormatOptions::new().joiners(JoinerStyle::Canonical));// "Mefjus feat. Phace - Sleepless"
let tidy = apply_edits(messy, &[ Edit::sort_by_time(), Edit::set_rest(2, rest), Edit::insert(2, "06:45 Phace - Psycho"), Edit::delete(4),]);
&tidy.text; // "00:00 Noisia - Stigma\n04:10 Mefjus feat. Phace - Sleepless\n06:45 Phace - Psycho"&tidy.changed; // [1, 2, 3]messy := "04:10 Mefjus ft. Phace - Sleepless\n00:00 Noisia - Stigma\n09:30 Outro"track := *trackparse.ParseTracklist(messy).Entries[0].Trackrest := trackparse.FormatWith(track, trackparse.FormatOptions{Joiners: trackparse.JoinersCanonical})fmt.Printf("%q\n", rest) // "Mefjus feat. Phace - Sleepless"
tidy := trackparse.ApplyEdits(messy, []trackparse.Edit{ trackparse.SortByTime(), trackparse.SetRest(2, rest), trackparse.Insert(2, "06:45 Phace - Psycho"), trackparse.Delete(4),})
fmt.Printf("%q\n", tidy.Text) // "00:00 Noisia - Stigma\n04:10 Mefjus feat. Phace - Sleepless\n06:45 Phace - Psycho"fmt.Println(tidy.Changed) // [1 2 3]const char *messy = "04:10 Mefjus ft. Phace - Sleepless\n00:00 Noisia - Stigma\n09:30 Outro";list = trackparse_parse_tracklist(messy, TRACKPARSE_NUL_TERMINATED);trackparse_format_options *canonical = trackparse_format_options_new();trackparse_format_options_set_joiners(canonical, TRACKPARSE_JOINERS_CANONICAL);trackparse_string *rest = trackparse_format_with(list->entries[0].track, canonical);printf("%s\n", rest->str.ptr); // Mefjus feat. Phace - Sleepless
edits = trackparse_edits_new();trackparse_edits_sort_by_time(edits);trackparse_edits_set_rest(edits, 2, rest->str.ptr, rest->str.len);trackparse_edits_insert(edits, 2, "06:45 Phace - Psycho", TRACKPARSE_NUL_TERMINATED);trackparse_edits_delete(edits, 4);trackparse_edit_result *tidy = trackparse_apply_edits(messy, TRACKPARSE_NUL_TERMINATED, edits);
printf("%s\n", tidy->text.ptr);// 00:00 Noisia - Stigma// 04:10 Mefjus feat. Phace - Sleepless// 06:45 Phace - Psychofor (size_t i = 0; i < tidy->changed_count; i++) { printf("%s%lld", i ? " " : "", tidy->changed[i]);}printf("\n"); // 1 2 3trackparse_edit_result_free(tidy);trackparse_edits_free(edits);trackparse_string_free(rest);trackparse_format_options_free(canonical);trackparse_tracklist_free(list);Each edit sees the text the one before it produced. After the sort, the Mefjus line is line 2, and after the insert the outro is line 4.
Times keep their style
Section titled “Times keep their style”A rewritten time looks like its neighbors. Hours or no hours, digit widths and fraction digits are
all kept, so 52:47 plus 45 seconds is 53:32, not 00:53:32
(R13.9.2).
When a time without hours crosses the hour, it gains hours in the style the rest of the list uses
(59:31 → 01:00:16, or 1:00:16 if the list writes one-digit hours). The edit reports
style-upgraded so you know
the line got longer.
Bullets and Markdown stay
Section titled “Bullets and Markdown stay”A list bullet or Markdown emphasis around a time sits in lead and gap
(parts), so a rewritten time keeps it. Only the
digits change:
const text = "**[**52:47**] Mefjus - Bandwagon**\n**[**59:31**] Alix Perez - Fade Away**";const result = applyEdits(text, [{ op: "shift", seconds: 45 }]);
result.text;// '**[**53:32**] Mefjus - Bandwagon**\n**[**01:00:16**] Alix Perez - Fade Away**'result.warnings; // [ { code: 'style-upgraded', edit: 0, line: 2 } ]text = "**[**52:47**] Mefjus - Bandwagon**\n**[**59:31**] Alix Perez - Fade Away**"result = apply_edits(text, [{"op": "shift", "seconds": 45}])
result.text# '**[**53:32**] Mefjus - Bandwagon**\n**[**01:00:16**] Alix Perez - Fade Away**'result.warnings # (EditWarning(code='style-upgraded', edit=0, line=2),)let text = "**[**52:47**] Mefjus - Bandwagon**\n**[**59:31**] Alix Perez - Fade Away**";let result = apply_edits(text, &[Edit::shift(45.0)]);
&result.text;// "**[**53:32**] Mefjus - Bandwagon**\n**[**01:00:16**] Alix Perez - Fade Away**"&result.warnings; // [EditWarning { code: StyleUpgraded, edit: 0, line: Some(2) }]text = "**[**52:47**] Mefjus - Bandwagon**\n**[**59:31**] Alix Perez - Fade Away**"result = trackparse.ApplyEdits(text, []trackparse.Edit{trackparse.Shift(45)})
fmt.Printf("%q\n", result.Text)// "**[**53:32**] Mefjus - Bandwagon**\n**[**01:00:16**] Alix Perez - Fade Away**"for _, w := range result.Warnings { fmt.Println(w.Code, w.Edit, *w.Line)}// style-upgraded 0 2text = "**[**52:47**] Mefjus - Bandwagon**\n**[**59:31**] Alix Perez - Fade Away**";edits = trackparse_edits_new();trackparse_edits_shift(edits, 45);result = trackparse_apply_edits(text, TRACKPARSE_NUL_TERMINATED, edits);
printf("%s\n", result->text.ptr);// **[**53:32**] Mefjus - Bandwagon**// **[**01:00:16**] Alix Perez - Fade Away**for (size_t i = 0; i < result->warnings_count; i++) { const trackparse_edit_warning *w = &result->warnings[i]; printf("%s %zu %lld\n", trackparse_edit_warning_code_name(w->code), w->edit, *w->line);}// style-upgraded 0 2trackparse_edit_result_free(result);trackparse_edits_free(edits);set-rest replaces everything after the gap, including a closing ** at the end of the line. The
time is still read afterwards, because emphasis next to a time doesn't need a partner. A layered
w/ line without a time moves with the timed line above it when you sort.
When an edit can't apply
Section titled “When an edit can't apply”Edits never throw or panic. A bad one changes nothing and reports why
(R13.9.6). In Rust the codes are the enum
EditWarningCode (EditInvalid, TimeClamped,
StyleUpgraded), in Go the string type EditWarningCode
(trackparse.EditWarningCodeEditInvalid and so on), in C the enum trackparse_edit_warning_code
(TRACKPARSE_EDIT_WARNING_EDIT_INVALID and so on):
| Code | When |
|---|---|
edit-invalid |
missing line, no time on the line, NaN or infinity, a time too large to write, a line break inside text or rest, an unknown op |
time-clamped |
the time would go below zero, so it was set to zero (R13.9.3) |
style-upgraded |
a time gained hours (see above) |
Each edit warning is { code, edit, line }, where edit is the index in your edits array and
line is null (None in Python and Rust, a nil *int in Go, a NULL const long long * in C) when the edit doesn't name a line.
Warnings shows how to check them in each language.
Guarantees
Section titled “Guarantees”The property tests check these on random text and random edit lists:
applyEdits(t, [])returnst.- An edit never touches a line it doesn't name.
shift(+s)followed byshift(-s)gives back the original text, unless something was clamped or upgraded on the way (R13.9.7).
See also
Section titled “See also”- Tracklist API for the result
fields and the
Edittype in each language. - Tracklists for parsing, line kinds and checks.
- Warnings for the edit warnings in each language.
- How it's tested for the property tests behind the guarantees.
- Specification, R13.9 for the exact edit rules.