Skip to content

Commit

Permalink
external_files: detect locales with a region like en-US
Browse files Browse the repository at this point in the history
This loads subtitle files like foo.en-US.srt with --sub-auto=exact.

To preserve the case of these locales and not convert them to e.g.
en-us, stop lower casing filenames, and instead use case insensitive
functions to check if the media filename is contained in the external
filenames. Extensions, whitelisted cover art filenames and idx files
were already being compared case insensitively.

This allows locales of any number of characters because there are
locales like gsw-u-sd-chzh.

Fixes #12372, fixes #13251.
  • Loading branch information
guidocella committed Apr 18, 2024
1 parent 352d325 commit 262e0f2
Showing 1 changed file with 4 additions and 8 deletions.
12 changes: 4 additions & 8 deletions player/external_files.c
Original file line number Diff line number Diff line change
Expand Up @@ -128,15 +128,13 @@ static struct bstr guess_lang_from_filename(struct bstr name, int *lang_start)
i--;
}

while (i >= 0 && mp_isalpha(name.start[i])) {
while (i >= 0 && (mp_isalpha(name.start[i]) || name.start[i] == '-')) {
lang_length++;
if (lang_length > 3)
return (struct bstr){NULL, 0};
i--;
}

if (lang_length < 2 || i == 0 || name.start[i] != delimiter)
return (struct bstr){NULL, 0};
return (struct bstr){0};

*lang_start = i;
return (struct bstr){name.start + i + 1, lang_length};
Expand All @@ -162,7 +160,6 @@ static void append_dir_subtitles(struct mpv_global *global, struct MPOpts *opts,
struct bstr f_fname = mp_iconv_to_utf8(log, f_fbname,
"UTF-8-MAC", MP_NO_LATIN1_FALLBACK);
struct bstr f_fname_noext = bstrdup(tmpmem, bstr_strip_ext(f_fname));
bstr_lower(f_fname_noext);
struct bstr f_fname_trim = bstr_strip(f_fname_noext);

if (f_fbname.start != f_fname.start)
Expand All @@ -185,7 +182,6 @@ static void append_dir_subtitles(struct mpv_global *global, struct MPOpts *opts,
"UTF-8-MAC", MP_NO_LATIN1_FALLBACK);
// retrieve various parts of the filename
struct bstr tmp_fname_noext = bstrdup(tmpmem2, bstr_strip_ext(dename));
bstr_lower(tmp_fname_noext);
struct bstr tmp_fname_ext = bstr_get_ext(dename);
struct bstr tmp_fname_trim = bstr_strip(tmp_fname_noext);

Expand Down Expand Up @@ -217,13 +213,13 @@ static void append_dir_subtitles(struct mpv_global *global, struct MPOpts *opts,
// higher prio -> auto-selection may prefer it (0 = not loaded)
int prio = 0;

if (bstrcmp(tmp_fname_trim, f_fname_trim) == 0)
if (bstrcasecmp(tmp_fname_trim, f_fname_trim) == 0)
prio |= 32; // exact movie name match

bstr lang = {0};
int start = 0;
lang = guess_lang_from_filename(tmp_fname_trim, &start);
if (bstr_startswith(tmp_fname_trim, f_fname_trim)) {
if (bstr_case_startswith(tmp_fname_trim, f_fname_trim)) {
if (lang.len && start == f_fname_trim.len)
prio |= 16; // exact movie name + followed by lang

Expand Down

0 comments on commit 262e0f2

Please sign in to comment.