Skip to content

Commit

Permalink
update bpm remove regex and early exit
Browse files Browse the repository at this point in the history
  • Loading branch information
Esgrove committed Jan 30, 2025
1 parent ab50981 commit ad3d384
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 7 deletions.
2 changes: 1 addition & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "track-rename"
version = "1.26.1"
version = "1.27.0"
edition = "2021"
authors = ["Esgrove <[email protected]>"]
description = "CLI tool for formatting and renaming audio tracks"
Expand Down
20 changes: 15 additions & 5 deletions src/formatting.rs
Original file line number Diff line number Diff line change
Expand Up @@ -188,9 +188,10 @@ static RE_BPM_WITH_KEY: LazyLock<Regex> =

// Matches BPM followed by two or three letters (likely denoting key or mode),
// formatted within parentheses at the end of a string
static RE_BPM_WITH_TEXT: LazyLock<Regex> = LazyLock::new(|| Regex::new(r"\b\d{2,3}bpm\)$").unwrap());
static RE_BPM_WITH_TEXT_PARENTHESES: LazyLock<Regex> =
LazyLock::new(|| Regex::new(r"\s\(\d{2,3}\s?[a-zA-Z]{2,3}\)$").unwrap());
static RE_BPM_WITH_TEXT: LazyLock<Regex> = LazyLock::new(|| Regex::new(r"\b\d{2,3}\s?[a-zA-Z]{2,3}\)$").unwrap());
static RE_BPM_WITH_EXTRA_TEXT: LazyLock<Regex> = LazyLock::new(|| Regex::new(r"\b\d{2,3}\s?[a-zA-Z]{2,3}\)$").unwrap());

// Matches any text within parentheses that contains a dash, separating it into two groups
static RE_DASH_IN_PARENTHESES: LazyLock<Regex> = LazyLock::new(|| Regex::new(r"\((.*?) - (.*?)\)").unwrap());
Expand Down Expand Up @@ -521,10 +522,19 @@ fn remove_bpm_in_parentheses_from_end(text: &mut String) {
}

let mut result = (*text).to_string();
result = RE_BPM_IN_PARENTHESES.replace_all(&result, "").to_string();
result = RE_BPM_WITH_KEY.replace_all(&result, "").to_string();
result = RE_BPM_WITH_TEXT_PARENTHESES.replace_all(&result, "").to_string();
result = RE_BPM_WITH_TEXT.replace_all(&result, "").to_string();
let regexes = [
&RE_BPM_IN_PARENTHESES,
&RE_BPM_WITH_TEXT,
&RE_BPM_WITH_KEY,
&RE_BPM_WITH_TEXT_PARENTHESES,
&RE_BPM_WITH_EXTRA_TEXT,
];
for re in regexes {
if re.is_match(&result) {
result = re.replace_all(&result, "").to_string();
break;
}
}

*text = result;
}
Expand Down
6 changes: 6 additions & 0 deletions tests/test_formatting.rs
Original file line number Diff line number Diff line change
Expand Up @@ -495,6 +495,12 @@ static REMOVE_BPM_AND_KEY_TEST_DATA: &[FormattingTestData] = &[
title: "People (Trayze Remix) (113 4b)",
correct_title: "People (Trayze Remix)",
},
FormattingTestData {
artist: "Donna Summer",
correct_artist: "Donna Summer",
title: "Hot Stuff [The Reflex Revision] 120bpm",
correct_title: "Hot Stuff (The Reflex Revision)",
},
];

static WHITESPACE_TEST_DATA: &[FormattingTestData] = &[
Expand Down

0 comments on commit ad3d384

Please sign in to comment.