mirror of
https://github.com/kilyabin/psysonic.git
synced 2026-07-21 22:15:40 +00:00
fix(library): avoid UTF-8 panic in artist article strip (#1152)
This commit is contained in:
@@ -8,9 +8,10 @@ pub fn strip_leading_articles(name: &str, ignored_articles: &str) -> String {
|
|||||||
let trimmed = name.trim();
|
let trimmed = name.trim();
|
||||||
for article in ignored_articles.split(' ').filter(|s| !s.is_empty()) {
|
for article in ignored_articles.split(' ').filter(|s| !s.is_empty()) {
|
||||||
let prefix = format!("{} ", article);
|
let prefix = format!("{} ", article);
|
||||||
if trimmed.len() >= prefix.len()
|
// `prefix` is ASCII; use `get` so we never slice inside a multibyte rune
|
||||||
&& trimmed[..prefix.len()].eq_ignore_ascii_case(&prefix)
|
// (e.g. "Elə…" must not panic when probing the "El " article).
|
||||||
{
|
let head = trimmed.get(0..prefix.len());
|
||||||
|
if head.is_some_and(|h| h.eq_ignore_ascii_case(&prefix)) {
|
||||||
return trimmed[prefix.len()..].trim_start().to_string();
|
return trimmed[prefix.len()..].trim_start().to_string();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -60,4 +61,12 @@ mod tests {
|
|||||||
"beatles"
|
"beatles"
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn does_not_panic_when_article_prefix_aligns_with_multibyte_rune() {
|
||||||
|
// Regression: byte slice `trimmed[..prefix.len()]` panicked on "Elə…"
|
||||||
|
// when probing the "El " ignored article (ə spans bytes 2..4).
|
||||||
|
let key = sort_key_for_display_name("Eləmir", DEFAULT_IGNORED_ARTICLES);
|
||||||
|
assert_eq!(key, "eləmir");
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user