avatarr-parser 0.1.0

Release-name parser ported from Sonarr v4.0.17.2952
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
// Ported from Sonarr v4.0.17.2952 (97e85a90):
//   src/NzbDrone.Core/Parser/LanguageParser.cs (lines 217-335 + regex
//   declarations 32-34, 36).
//   src/NzbDrone.Core/Parser/Model/SubtitleTitleInfo.cs (return-type fields).
//
// Public entry points:
//   - [`parse_subtitle_language`] mirrors C# `ParseSubtitleLanguage`.
//   - [`parse_basic_subtitle`] mirrors C# `ParseBasicSubtitle`.
//   - [`parse_subtitle_language_information`] mirrors C# `ParseSubtitleLanguageInformation`.
//   - [`parse_language_tags`] mirrors C# `ParseLanguageTags`.
//
// **Plan-spec / Rust-regex divergences resolved in favour of C# semantics
// (per standing rule #1):**
//
// 1. Rust's `regex` crate forbids duplicate named groups. C#
//    `SubtitleLanguageRegex` declares `(?<tags>...)` twice (once before
//    iso_code, once after) and relies on `match.Groups["tags"].Captures`
//    enumerating BOTH. The Rust port drops both `(?<tags>...)` captures
//    from the structural regex; tag-token positions are recovered by
//    walking outward from the iso_code position post-match, applying
//    the same vocabulary (`forced|foreign|default|cc|psdh|sdh`). The
//    walking algorithm is faithful to C#'s greedy `(?:[-_. ]<tag>)*` /
//    lazy `.+?` interaction: tags are only collected when they are
//    contiguous with the iso_code via tag-or-iso-only segments.
//
// 2. Same problem in `SubtitleLanguageTitleRegex`: the C# pattern names
//    `iso_code` twice (pre-title and post-title) AND `tags1`/`tags2`
//    repeat inside their respective `\.(...)*` clusters. The Rust port
//    captures only the structural anchor (the title group) and walks
//    the pre-title and post-title segment lists in code to enumerate
//    iso_codes + tags. This mirrors C#'s `Captures.Count` semantics
//    (the "exactly one iso_code" gate at line 267).
//
// 3. C# `SubtitleTitleRegex` (line 36) uses `(?<!\d+)\d{1,3}(?!\d+)`
//    for the copy-number tail. Rust regex supports neither lookahead
//    nor lookbehind; the port relaxes the regex to `\d{1,3}` and
//    rejects matches whose copy-number is adjacent to other digits.
//
// 4. C# uses `Path.GetFileNameWithoutExtension` to strip the final
//    extension before matching. We reproduce that with a string-based
//    helper that takes everything before the last `.` (or the whole
//    string if no `.`). This avoids `std::path::Path` rewrites of
//    forward/backward slashes that would conflict with C# `Path`
//    semantics on POSIX hosts.

use crate::language::{Language, iso_languages};
use once_cell::sync::Lazy;
use regex::Regex;

/// Tag vocabulary matching the alternation in `SubtitleLanguageRegex`
/// and `SubtitleLanguageTitleRegex`. Order matters only for negative
/// regression checks; lookups are case-insensitive in all consumers.
const TAG_VOCAB: &[&str] = &["forced", "foreign", "default", "cc", "psdh", "sdh"];

/// Outcome of [`parse_basic_subtitle`] / mirrors C# `SubtitleTitleInfo`
/// minimum surface (the fields `ParseBasicSubtitle` actually populates).
///
/// `ParseBasicSubtitle` only sets `TitleFirst` (always `false`),
/// `LanguageTags`, and `Language`. `RawTitle`, `Title`, `Copy` are
/// left at their default values (`None`, `None`, `0`).
#[derive(Debug, Clone, PartialEq, Eq, Default)]
pub struct SubtitleTitleInfo {
    /// Mirrors C# `LanguageTags`. Always lowercased per
    /// `tag.Value.ToLower()` in `ParseSubtitleLanguageInformation`.
    pub language_tags: Vec<String>,
    /// Mirrors C# `Language`. `Language::Unknown` when the file
    /// carried no resolvable language code.
    pub language: Language,
    /// Mirrors C# `RawTitle`. `None` when no title segment was parsed
    /// (e.g. `ParseBasicSubtitle` short-circuit, or
    /// `ParseSubtitleLanguageInformation` falling back to
    /// `ParseBasicSubtitle`).
    pub raw_title: Option<String>,
    /// Mirrors C# `Title`. Either the bare `RawTitle` or the
    /// `(?<title>.+)` capture from `SubtitleTitleRegex` once the
    /// trailing copy-number is stripped.
    pub title: Option<String>,
    /// Mirrors C# `Copy`. Zero when no trailing copy-number suffix
    /// was found.
    pub copy: i32,
    /// Mirrors C# `TitleFirst`. `true` when the parsed title segment
    /// precedes any tag/iso_code segment in the filename (i.e. when
    /// `tags1` is empty in C#'s regex).
    pub title_first: bool,
}

/// Regex constants ported from `LanguageParser.cs:32-34, 36`. Each
/// `Lazy<Regex>` corresponds to a `private static readonly Regex`
/// in C#. Where the C# pattern uses a feature Rust regex lacks
/// (duplicate named groups, lookarounds), the Rust regex is the
/// structural superset and a sibling helper applies the missing
/// constraint post-match. See the file header for the complete list.
pub mod regexes {
    use super::{Lazy, Regex};

    /// `SubtitleLanguageRegex` from `LanguageParser.cs:32`.
    ///
    /// C# pattern (with two `(?<tags>...)` groups whose `Captures`
    /// collection C# concatenates):
    /// `.+?([-_. ](?<tags>forced|foreign|default|cc|psdh|sdh))*[-_. ](?<iso_code>[a-z]{2,3})([-_. ](?<tags>forced|foreign|default|cc|psdh|sdh))*$`
    ///
    /// Rust port drops the inner `(?<tags>...)` captures (so the regex
    /// is just structural validation) and lets [`super::collect_tags_around`]
    /// recover the tag tokens from positions adjacent to `iso_code`.
    pub static SUBTITLE_LANGUAGE_REGEX: Lazy<Regex> = Lazy::new(|| {
        Regex::new(
            r"(?i)^.+?(?:[-_. ](?:forced|foreign|default|cc|psdh|sdh))*[-_. ](?P<iso_code>[a-z]{2,3})(?:[-_. ](?:forced|foreign|default|cc|psdh|sdh))*$",
        )
        .expect("SUBTITLE_LANGUAGE_REGEX must compile")
    });

    /// `SubtitleLanguageTitleRegex` from `LanguageParser.cs:34`.
    ///
    /// C# pattern (with duplicate `iso_code` groups):
    /// `.+?(\.((?<tags1>forced|...)|(?<iso_code>[a-z]{2,3})))*[-_. ](?<title>[^.]*)(\.((?<tags2>forced|...)|(?<iso_code>[a-z]{2,3})))*$`
    ///
    /// Rust port retains the structure but drops the inner alternation
    /// captures; consumers walk the segments to enumerate iso_codes
    /// (gated to exactly one) and tags. The `title` group is the
    /// only structural capture.
    pub static SUBTITLE_LANGUAGE_TITLE_REGEX: Lazy<Regex> = Lazy::new(|| {
        Regex::new(
            r"(?i)^.+?(?:\.(?:forced|foreign|default|cc|psdh|sdh|[a-z]{2,3}))*[-_. ](?P<title>[^.]*)(?:\.(?:forced|foreign|default|cc|psdh|sdh|[a-z]{2,3}))*$",
        )
        .expect("SUBTITLE_LANGUAGE_TITLE_REGEX must compile")
    });

    /// `SubtitleTitleRegex` from `LanguageParser.cs:36`.
    ///
    /// C# pattern: `^((?<title>.+) - )?(?<copy>(?<!\d+)\d{1,3}(?!\d+))$`.
    /// The lookarounds gate against multi-digit-adjacency (e.g. a
    /// 4-digit year, where `\d{1,3}` would otherwise capture three of
    /// the four digits). Rust regex supports neither lookahead nor
    /// lookbehind; the port relaxes the regex and applies the gate
    /// in [`super::is_valid_copy_match`].
    pub static SUBTITLE_TITLE_REGEX: Lazy<Regex> = Lazy::new(|| {
        Regex::new(r"^(?:(?P<title>.+) - )?(?P<copy>\d{1,3})$")
            .expect("SUBTITLE_TITLE_REGEX must compile")
    });
}

/// Public entry: mirror C# `LanguageParser.ParseSubtitleLanguage`
/// (lines 217-250).
///
/// Algorithm:
/// 1. `simple_filename = strip_last_extension(file_name)`.
/// 2. Match `SUBTITLE_LANGUAGE_REGEX` against `simple_filename`.
///    On success, look up `iso_code` via `iso_languages::find`; that
///    maps to `Language::Unknown` if the code is unknown.
/// 3. Otherwise, walk `Language::All` checking
///    `simple_filename.ends_with(language.name())` case-insensitively.
///    First hit wins.
/// 4. Otherwise return `Language::Unknown`.
pub fn parse_subtitle_language(file_name: &str) -> Language {
    let simple = strip_last_extension(file_name);

    if let Some(caps) = regexes::SUBTITLE_LANGUAGE_REGEX.captures(simple)
        && let Some(iso) = caps.name("iso_code")
    {
        // C# `IsoLanguages.Find` is the same lookup wired in T10; a
        // miss yields `Language::Unknown` (matching C#'s
        // `isoLanguage?.Language ?? Language.Unknown`).
        return iso_languages::find(&iso.as_str().to_ascii_lowercase())
            .unwrap_or(Language::Unknown);
    }

    // Fallback: ends_with(language.name()) for every Language variant,
    // case-insensitive. C# loops over `Language.All`; we mirror that
    // ordering (C# sets `All` to the contiguous list plus `Original`).
    for language in ALL_LANGUAGES_FOR_ENDS_WITH {
        let name = language.name();
        if ends_with_ignore_ascii_case(simple, name) {
            return *language;
        }
    }

    Language::Unknown
}

/// Public entry: mirror C# `LanguageParser.ParseBasicSubtitle`
/// (lines 252-260).
///
/// Composes `parse_language_tags` and `parse_subtitle_language` into a
/// minimal `SubtitleTitleInfo` (only `language_tags`, `language`, and
/// `title_first = false` are populated; `raw_title`/`title`/`copy`
/// stay at defaults).
pub fn parse_basic_subtitle(file_name: &str) -> SubtitleTitleInfo {
    SubtitleTitleInfo {
        title_first: false,
        language_tags: parse_language_tags(file_name),
        language: parse_subtitle_language(file_name),
        ..Default::default()
    }
}

/// Public entry: mirror C# `LanguageParser.ParseSubtitleLanguageInformation`
/// (lines 262-297).
///
/// Tries the richer `SUBTITLE_LANGUAGE_TITLE_REGEX` first. On match the
/// `iso_code` count must be exactly one (C# line 267 gate). On failure
/// or gate-violation, falls back to `parse_basic_subtitle`.
pub fn parse_subtitle_language_information(file_name: &str) -> SubtitleTitleInfo {
    let simple = strip_last_extension(file_name);

    let Some(caps) = regexes::SUBTITLE_LANGUAGE_TITLE_REGEX.captures(simple) else {
        return parse_basic_subtitle(file_name);
    };

    let title_match = match caps.name("title") {
        Some(m) => m,
        None => return parse_basic_subtitle(file_name),
    };

    // Walk the dot-prefixed segments BEFORE the title separator and
    // AFTER the title to recover iso_codes and tags. The title's own
    // separator is `[-_. ]` (any of dash/underscore/dot/space) but the
    // surrounding segments are `\.<token>` (literal dot prefix).
    let title_start = title_match.start();
    let title_end = title_match.end();

    // C# `[-_. ](?<title>...)` consumes one separator immediately
    // before the title's first byte. Find its byte index so the
    // pre-segments scan stops at the right place.
    let title_sep_idx = title_start.saturating_sub(1);
    let pre_section = &simple[..title_sep_idx];
    let post_section = &simple[title_end..];

    let (pre_iso_codes, pre_tags) = scan_dot_segments_from_right(pre_section);
    let (post_iso_codes, post_tags) = scan_dot_segments_from_left(post_section);

    let iso_count = pre_iso_codes.len() + post_iso_codes.len();
    if iso_count != 1 {
        return parse_basic_subtitle(file_name);
    }

    let iso_code = pre_iso_codes
        .first()
        .or_else(|| post_iso_codes.first())
        .expect("iso_count==1 guarantees at least one iso_code");

    let language = iso_languages::find(&iso_code.to_ascii_lowercase()).unwrap_or(Language::Unknown);

    // C# `tags1.Captures.Union(tags2.Captures).Cast<Capture>()` uses
    // `Capture`'s default reference-equality comparer (Capture has no
    // `Equals`/`GetHashCode` override), so distinct Capture objects
    // are NEVER deduped even when their `Value` matches. The Rust
    // port mirrors that with a plain concatenate-and-lowercase. (No
    // current test fixture exercises the dup case, but
    // standing rule #1 says C# fidelity wins.)
    let language_tags: Vec<String> = pre_tags
        .iter()
        .chain(post_tags.iter())
        .filter(|s| !s.is_empty())
        .map(|s| s.to_ascii_lowercase())
        .collect();

    // C# `RawTitle = matchTitle.Groups["title"].Value`. Always
    // `Some(...)` (possibly empty string) when the outer regex
    // matched; `[^.]*` always succeeds (zero or more), so
    // `Group.Success` is true and `.Value` is at minimum `""`.
    let raw_title = caps.name("title").map(|m| m.as_str().to_string());
    // C# `TitleFirst = matchTitle.Groups["tags1"].Captures.Empty()` -- it
    // checks tags1 only, NOT iso_code. A pre-iso segment (e.g.
    // `.eng.testtitle.forced.ass`) leaves tags1 empty and so still has
    // `TitleFirst = true` in C#.
    let title_first = pre_tags.is_empty();

    let mut info = SubtitleTitleInfo {
        title_first,
        language_tags,
        raw_title,
        language,
        ..Default::default()
    };
    update_title_and_copy_from_title(&mut info);
    info
}

/// Public entry: mirror C# `LanguageParser.ParseLanguageTags`
/// (lines 318-335).
///
/// Returns the lowercased tag tokens captured around `iso_code` by
/// `SUBTITLE_LANGUAGE_REGEX`. Returns an empty list when the regex
/// does not match (matching C#'s empty `Captures` enumeration).
pub fn parse_language_tags(file_name: &str) -> Vec<String> {
    let simple = strip_last_extension(file_name);

    let Some(caps) = regexes::SUBTITLE_LANGUAGE_REGEX.captures(simple) else {
        return Vec::new();
    };
    let Some(iso) = caps.name("iso_code") else {
        return Vec::new();
    };

    // The iso_code group span doesn't include its leading separator;
    // `iso_start` points at the iso_code's first byte. Walk from
    // `iso_start - 1` leftward (the separator chars are 1 byte each)
    // collecting tag tokens, then from `iso.end()` rightward.
    collect_tags_around(simple, iso.start(), iso.end())
}

/// Mirror C# `UpdateTitleAndCopyFromTitle` (lines 299-316). Operates
/// in-place on the partially-filled `SubtitleTitleInfo`.
///
/// Matches `SUBTITLE_TITLE_REGEX` against `info.raw_title`. On match
/// the optional `(?<title>.+)` capture (may be `None` when only a
/// copy-number is present) becomes `info.title`, and `(?<copy>\d{1,3})`
/// becomes `info.copy`. On no-match, `info.title = info.raw_title`
/// and `info.copy = 0`.
fn update_title_and_copy_from_title(info: &mut SubtitleTitleInfo) {
    let Some(raw) = info.raw_title.as_deref() else {
        info.title = None;
        info.copy = 0;
        return;
    };

    if let Some(caps) = regexes::SUBTITLE_TITLE_REGEX.captures(raw)
        && let Some(copy) = caps.name("copy")
    {
        // Reapply the C# lookarounds: copy must NOT be adjacent
        // to other digits inside `raw`. With the regex anchored
        // start-to-end, "adjacency" can only mean: the digit run
        // is longer than 3 (the regex would still match since
        // `\d{1,3}` is greedy at end with $). Concretely, if
        // RawTitle contains a 4+ digit run, the regex either
        // fails (because part of the run lands inside `title`
        // and breaks the ` - ` separator) or backtracks to a
        // valid 1-3 digit copy with title preceding.
        //
        // For robustness, validate the C# lookaround logic
        // explicitly: characters immediately before `copy.start()`
        // and after `copy.end()` (within `raw`) must not be ASCII
        // digits.
        if !is_valid_copy_match(raw, copy.start(), copy.end()) {
            info.title = Some(raw.to_string());
            info.copy = 0;
            return;
        }
        info.title = caps.name("title").map(|m| m.as_str().to_string());
        info.copy = copy.as_str().parse().unwrap_or(0);
        return;
    }

    info.title = Some(raw.to_string());
    info.copy = 0;
}

/// Reapplies the C# `(?<!\d+)\d{1,3}(?!\d+)` lookarounds. Returns
/// `true` if `raw[copy_start..copy_end]` is a valid copy-number
/// span (no immediately adjacent digit on either side).
fn is_valid_copy_match(raw: &str, copy_start: usize, copy_end: usize) -> bool {
    let bytes = raw.as_bytes();
    if copy_start > 0 && bytes[copy_start - 1].is_ascii_digit() {
        return false;
    }
    if copy_end < bytes.len() && bytes[copy_end].is_ascii_digit() {
        return false;
    }
    true
}

/// Strip the last `.<ext>` from a filename, mirroring
/// `Path.GetFileNameWithoutExtension`. First strips any leading
/// directory components (everything up to and including the final
/// `/` or `\`), then strips the last `.<ext>`. Returns the input
/// unchanged when no `.` is present; returns `""` for a leading-dot
/// name like `.bashrc` (C# treats those as having an empty stem).
fn strip_last_extension(file_name: &str) -> &str {
    // Step 1: drop any leading directory portion. C# `Path.GetFileName`
    // splits on either `/` or `\` (the latter for legacy Windows paths).
    let bare = match file_name.rfind(['/', '\\']) {
        Some(idx) => &file_name[idx + 1..],
        None => file_name,
    };
    // Step 2: drop the last `.<ext>` if any.
    match bare.rfind('.') {
        None => bare,
        Some(idx) => &bare[..idx],
    }
}

/// Case-insensitive `ends_with` over ASCII bytes.
///
/// C# uses `StringComparison.OrdinalIgnoreCase` which is byte-equal
/// modulo ASCII case. Language names are pure ASCII (e.g. "Portuguese
/// (Brazil)") so a byte-level compare suffices.
fn ends_with_ignore_ascii_case(haystack: &str, needle: &str) -> bool {
    if needle.len() > haystack.len() {
        return false;
    }
    let tail = &haystack.as_bytes()[haystack.len() - needle.len()..];
    tail.eq_ignore_ascii_case(needle.as_bytes())
}

/// Walk leftward from `iso_start - 1` (i.e. the iso_code's leading
/// separator byte) collecting `[-_. ]<tag>` segments while each
/// segment's token is in the tag vocabulary. Then walk rightward
/// from `iso_end` collecting the same. Returns concatenated tag
/// tokens lowercased, in left-to-right source order.
///
/// This recovers the `(?<tags>...)*` captures C# would have collected
/// before and after iso_code, despite Rust regex's no-duplicate-name
/// constraint. Faithfulness check: stops at the first non-tag-vocab
/// segment because C#'s `(?:[-_. ]<tag>)*` would also stop the loop
/// at that point (the tag alternation would fail, ending the `*`
/// repetition).
fn collect_tags_around(simple: &str, iso_start: usize, iso_end: usize) -> Vec<String> {
    let mut left_tags = Vec::new();
    let mut cursor = iso_start;
    while cursor > 0 {
        let Some((tag, new_cursor)) = pop_left_tag_segment(simple, cursor) else {
            break;
        };
        left_tags.push(tag);
        cursor = new_cursor;
    }
    left_tags.reverse();

    let mut right_tags = Vec::new();
    let mut cursor = iso_end;
    while cursor < simple.len() {
        let Some((tag, new_cursor)) = pop_right_tag_segment(simple, cursor) else {
            break;
        };
        right_tags.push(tag);
        cursor = new_cursor;
    }

    left_tags.append(&mut right_tags);
    left_tags
}

/// If the bytes ending at `cursor` form `[-_. ]<tag>`, return
/// `(tag.lowercased(), new_cursor)` where `new_cursor` points one
/// byte past the separator (i.e. the position the next leftward step
/// should start from). Returns `None` if the segment isn't a tag.
fn pop_left_tag_segment(simple: &str, cursor: usize) -> Option<(String, usize)> {
    // Look back for a separator. The C# regex uses `[-_. ]` as
    // separator both before and after iso_code in
    // `SubtitleLanguageRegex`.
    let bytes = simple.as_bytes();
    if cursor == 0 {
        return None;
    }
    // `cursor` is currently the byte index of the iso_code's first
    // byte (or the previously-popped tag's separator byte). The
    // separator immediately before `cursor` is at `cursor - 1`.
    let sep_idx = cursor.checked_sub(1)?;
    let sep = bytes[sep_idx];
    if !is_subtitle_separator(sep) {
        return None;
    }
    // The token starts at the byte AFTER the previous separator (or 0)
    // and runs up to `sep_idx`. Walk leftward from `sep_idx - 1` until
    // we find another separator or reach the start.
    let mut tok_start = sep_idx;
    while tok_start > 0 && !is_subtitle_separator(bytes[tok_start - 1]) {
        tok_start -= 1;
    }
    let token = &simple[tok_start..sep_idx];
    if !is_tag_token(token) {
        return None;
    }
    Some((token.to_ascii_lowercase(), tok_start))
}

/// Mirror of [`pop_left_tag_segment`] for rightward walks. Returns
/// `(tag, new_cursor)` where `new_cursor` is the byte index one past
/// the consumed token.
fn pop_right_tag_segment(simple: &str, cursor: usize) -> Option<(String, usize)> {
    let bytes = simple.as_bytes();
    if cursor >= bytes.len() {
        return None;
    }
    let sep = bytes[cursor];
    if !is_subtitle_separator(sep) {
        return None;
    }
    // Token starts after the separator, runs until next separator or
    // end of string.
    let tok_start = cursor + 1;
    let mut tok_end = tok_start;
    while tok_end < bytes.len() && !is_subtitle_separator(bytes[tok_end]) {
        tok_end += 1;
    }
    let token = &simple[tok_start..tok_end];
    if !is_tag_token(token) {
        return None;
    }
    Some((token.to_ascii_lowercase(), tok_end))
}

/// Walk a pre-title section `\.<token>` rightward. Returns
/// `(iso_codes, tags)` accumulated. The last segment ends at the
/// section's end (the title separator is consumed by the caller).
///
/// Ordering: tokens appear left-to-right (which is also the order
/// C# would enumerate `Captures`).
fn scan_dot_segments_from_right(section: &str) -> (Vec<String>, Vec<String>) {
    // Walk the section breaking on every `.` boundary.
    let mut iso_codes = Vec::new();
    let mut tags = Vec::new();
    if section.is_empty() {
        return (iso_codes, tags);
    }
    let bytes = section.as_bytes();

    // Walk RIGHT-TO-LEFT collecting trailing `\.<token>` segments
    // until a segment fails to match either tag vocab or 2-3 letter
    // ISO. This mirrors C#'s greedy `(\.(...))*` cluster anchored
    // against the title separator: the cluster stops at the first
    // segment that doesn't fit either alternation.
    let mut consumed: Vec<(String, bool)> = Vec::new();
    let mut cursor = bytes.len();
    while cursor > 0 {
        // Find the rightmost `.` strictly inside [..cursor].
        let Some(dot) = section[..cursor].rfind('.') else {
            break;
        };
        let token = &section[dot + 1..cursor];
        if token.is_empty() {
            break;
        }
        let is_iso = is_iso_code_token(token);
        let is_tag = is_tag_token(token);
        if !is_iso && !is_tag {
            break;
        }
        // Tags take precedence over iso_code in C# (alternation order).
        if is_tag {
            consumed.push((token.to_ascii_lowercase(), false));
        } else {
            consumed.push((token.to_string(), true));
        }
        cursor = dot;
    }
    // We collected right-to-left; reverse for source order.
    consumed.reverse();
    for (tok, is_iso_flag) in consumed {
        if is_iso_flag {
            iso_codes.push(tok);
        } else {
            tags.push(tok);
        }
    }
    (iso_codes, tags)
}

/// Walk a post-title section `\.<token>` left-to-right. Returns
/// `(iso_codes, tags)` accumulated.
fn scan_dot_segments_from_left(section: &str) -> (Vec<String>, Vec<String>) {
    let mut iso_codes = Vec::new();
    let mut tags = Vec::new();
    if section.is_empty() {
        return (iso_codes, tags);
    }
    if !section.starts_with('.') {
        // C# regex requires `\.<token>`; without a leading dot the
        // cluster matches zero times.
        return (iso_codes, tags);
    }
    // Split on '.' and walk left-to-right. The leading empty part
    // before the first `.` is dropped.
    for token in section.split('.').skip(1) {
        if token.is_empty() {
            break;
        }
        let is_iso = is_iso_code_token(token);
        let is_tag = is_tag_token(token);
        if !is_iso && !is_tag {
            break;
        }
        if is_tag {
            tags.push(token.to_ascii_lowercase());
        } else {
            iso_codes.push(token.to_string());
        }
    }
    (iso_codes, tags)
}

/// `[a-z]{2,3}` case-insensitive ASCII test mirroring the iso_code
/// branch of the alternation. Pure-ASCII because the C# pattern is
/// `[a-z]` not `[\p{L}]`.
fn is_iso_code_token(token: &str) -> bool {
    let n = token.len();
    if n != 2 && n != 3 {
        return false;
    }
    token.bytes().all(|b| b.is_ascii_alphabetic())
}

/// Tag vocabulary membership test (case-insensitive ASCII).
fn is_tag_token(token: &str) -> bool {
    TAG_VOCAB
        .iter()
        .any(|t| t.len() == token.len() && t.eq_ignore_ascii_case(token))
}

/// `[-_. ]` separator class matching the C# alternation. ASCII bytes
/// only (matches the C# class verbatim).
fn is_subtitle_separator(b: u8) -> bool {
    matches!(b, b'-' | b'_' | b'.' | b' ')
}

/// `Language.All` ordered list for the `EndsWith` fallback in
/// `parse_subtitle_language`. Mirrors C# `Language.All` ordering
/// (the contiguous-ID list plus `Original` last). `Unknown` is
/// excluded because the fallback should produce `Unknown` only via
/// the final `else`.
const ALL_LANGUAGES_FOR_ENDS_WITH: &[Language] = &[
    Language::English,
    Language::French,
    Language::Spanish,
    Language::German,
    Language::Italian,
    Language::Danish,
    Language::Dutch,
    Language::Japanese,
    Language::Icelandic,
    Language::Chinese,
    Language::Russian,
    Language::Polish,
    Language::Vietnamese,
    Language::Swedish,
    Language::Norwegian,
    Language::Finnish,
    Language::Turkish,
    Language::Portuguese,
    Language::Flemish,
    Language::Greek,
    Language::Korean,
    Language::Hungarian,
    Language::Hebrew,
    Language::Lithuanian,
    Language::Czech,
    Language::Arabic,
    Language::Hindi,
    Language::Bulgarian,
    Language::Malayalam,
    Language::Ukrainian,
    Language::Slovak,
    Language::Thai,
    Language::PortugueseBrazil,
    Language::SpanishLatino,
    Language::Romanian,
    Language::Latvian,
    Language::Persian,
    Language::Catalan,
    Language::Croatian,
    Language::Serbian,
    Language::Bosnian,
    Language::Estonian,
    Language::Tamil,
    Language::Indonesian,
    Language::Macedonian,
    Language::Slovenian,
    Language::Original,
];

#[cfg(test)]
mod tests {
    use super::*;

    // ----- Plan-required tests -------------------------------------------

    #[test]
    fn parse_subtitle_extracts_eng_iso_code() {
        let lang = parse_subtitle_language("Show.S01E01.eng.srt");
        assert_eq!(lang, Language::English);
    }

    #[test]
    fn parse_subtitle_extracts_fre_iso_code() {
        // "fre" -> "fra" via the B-to-T map wired in T10.
        assert_eq!(
            parse_subtitle_language("Show.S01E01.fre.srt"),
            Language::French
        );
    }

    #[test]
    fn parse_basic_subtitle_returns_lang_and_tags() {
        let r = parse_basic_subtitle("Show.S01E01.eng.forced.srt");
        assert_eq!(r.language, Language::English);
        assert!(r.language_tags.contains(&"forced".to_string()));
    }

    #[test]
    fn parse_language_tags_extracts_sdh() {
        let tags = parse_language_tags("Show.S01E01.eng.sdh.srt");
        assert!(tags.contains(&"sdh".to_string()));
    }

    // ----- C# LanguageParserFixture parity -------------------------------

    #[test]
    fn fixture_subtitle_unknown_when_no_iso_code() {
        // C# fixture line 12: `"Series Title - S01E01 - Pilot.sub"` -> Unknown.
        assert_eq!(
            parse_subtitle_language("Series Title - S01E01 - Pilot.sub"),
            Language::Unknown
        );
    }

    #[test]
    fn fixture_subtitle_english_via_iso_codes_and_endswith() {
        // C# fixture lines 19-29: every variant resolves to English.
        let cases = [
            "Series Title - S01E01 - Pilot.en.sub",
            "Series Title - S01E01 - Pilot.EN.sub",
            "Series Title - S01E01 - Pilot.eng.sub",
            "Series Title - S01E01 - Pilot.ENG.sub",
            "Series Title - S01E01 - Pilot.English.sub",
            "Series Title - S01E01 - Pilot.english.sub",
            "Series Title - S01E01 - Pilot.en.cc.sub",
            "Series Title - S01E01 - Pilot.en.sdh.sub",
            "Series Title - S01E01 - Pilot.en.forced.sub",
            "Series Title - S01E01 - Pilot.en.sdh.forced.sub",
        ];
        for case in cases {
            assert_eq!(
                parse_subtitle_language(case),
                Language::English,
                "case = {case}"
            );
        }
    }

    #[test]
    fn fixture_parse_subtitle_language_information_eng_with_default_forced() {
        // C# fixture line 465.
        let info = parse_subtitle_language_information(
            "Name (2020) - S01E20 - [AAC 2.0].testtitle.default.eng.forced.ass",
        );
        assert_eq!(info.language, Language::English);
        assert_eq!(
            info.language_tags,
            vec!["default".to_string(), "forced".to_string()]
        );
        assert_eq!(info.title.as_deref(), Some("testtitle"));
    }

    #[test]
    fn fixture_parse_subtitle_language_information_fra_with_default_forced() {
        // C# fixture line 471.
        let info = parse_subtitle_language_information(
            "Name (2020) - S01E20 - [AAC 2.0].testtitle.default.fra.forced.ass",
        );
        assert_eq!(info.language, Language::French);
        assert_eq!(
            info.language_tags,
            vec!["default".to_string(), "forced".to_string()]
        );
        assert_eq!(info.title.as_deref(), Some("testtitle"));
    }

    #[test]
    fn fixture_parse_subtitle_language_information_ru_dashed_title() {
        // C# fixture line 477: `ru-something-else` -> language Russian,
        // title `something-else`, tags empty.
        let info = parse_subtitle_language_information(
            "Name (2020) - S01E20 - [AAC 2.0].ru-something-else.srt",
        );
        assert_eq!(info.language, Language::Russian);
        assert!(
            info.language_tags.is_empty(),
            "got {:?}",
            info.language_tags
        );
        assert_eq!(info.title.as_deref(), Some("something-else"));
    }

    #[test]
    fn fixture_parse_subtitle_language_information_full_subtitles_title() {
        // C# fixture line 478.
        let info = parse_subtitle_language_information(
            "Name (2020) - S01E20 - [AAC 2.0].Full Subtitles.eng.ass",
        );
        assert_eq!(info.language, Language::English);
        assert!(
            info.language_tags.is_empty(),
            "got {:?}",
            info.language_tags
        );
        assert_eq!(info.title.as_deref(), Some("Full Subtitles"));
    }

    #[test]
    fn fixture_parse_subtitle_language_information_dash_one_copy_strip() {
        // C# fixture line 479: title "mytitle - 1" => after
        // SubtitleTitleRegex => Title="mytitle", Copy=1.
        let info = parse_subtitle_language_information(
            "Name (2020) - S01E20 - [AAC 2.0].mytitle - 1.en.ass",
        );
        assert_eq!(info.language, Language::English);
        assert_eq!(info.title.as_deref(), Some("mytitle"));
        assert_eq!(info.copy, 1);
    }

    #[test]
    fn fixture_parse_subtitle_language_information_space_one_no_strip() {
        // C# fixture line 480: title "mytitle 1" => Title="mytitle 1",
        // Copy=0 (no ` - ` separator before the digit).
        let info = parse_subtitle_language_information(
            "Name (2020) - S01E20 - [AAC 2.0].mytitle 1.en.ass",
        );
        assert_eq!(info.language, Language::English);
        assert_eq!(info.title.as_deref(), Some("mytitle 1"));
        assert_eq!(info.copy, 0);
    }

    #[test]
    fn fixture_parse_subtitle_language_information_no_copy() {
        // C# fixture line 481: title "mytitle" -> Title="mytitle", Copy=0.
        let info =
            parse_subtitle_language_information("Name (2020) - S01E20 - [AAC 2.0].mytitle.en.ass");
        assert_eq!(info.language, Language::English);
        assert_eq!(info.title.as_deref(), Some("mytitle"));
        assert_eq!(info.copy, 0);
    }

    #[test]
    fn fixture_parse_subtitle_language_information_no_iso_code_falls_back_to_basic() {
        // C# fixture line 491-494: zero or two iso_codes => fallback
        // to ParseBasicSubtitle => Language=Unknown, LanguageTags=empty,
        // RawTitle=null.
        let cases = [
            "Name (2020) - S01E20 - [AAC 2.0].default.forced.ass",
            "Name (2020) - S01E20 - [AAC 2.0].default.ass",
            "Name (2020) - S01E20 - [AAC 2.0].ass",
            "Name (2020) - S01E20 - [AAC 2.0].testtitle.ass",
        ];
        for case in cases {
            let info = parse_subtitle_language_information(case);
            assert_eq!(info.language, Language::Unknown, "case = {case}");
            assert!(info.language_tags.is_empty(), "case = {case}");
            assert!(info.raw_title.is_none(), "case = {case}");
        }
    }

    // ----- Edge cases that pin our helper-function logic -------------------

    #[test]
    fn parse_subtitle_endswith_after_extension_strip() {
        // C# `Path.GetFileNameWithoutExtension` strips one extension.
        // `Show - Pilot.English.sub` -> simple `Show - Pilot.English`
        // which neither matches `SUBTITLE_LANGUAGE_REGEX` (no `[a-z]{2,3}`
        // tail) nor ends with a Language name except via a trailing
        // language-name component. Fixture line 23 already covers the
        // `.English.sub` -> English case via the EndsWith fallback.
        // Pin the same path via `Pilot.German.sub` as a positive test
        // and `Pilot.NotALanguage.sub` as a negative.
        assert_eq!(
            parse_subtitle_language("Show - Pilot.German.sub"),
            Language::German
        );
        assert_eq!(
            parse_subtitle_language("Show - Pilot.NotALanguage.sub"),
            Language::Unknown
        );
    }

    #[test]
    fn parse_subtitle_endswith_portuguese_brazil() {
        // `Language::PortugueseBrazil.name() == "Portuguese (Brazil)"`.
        // The EndsWith fallback uses the literal Display name. Strip
        // one extension first, so the test's simple filename ends in
        // `Portuguese (Brazil)`.
        assert_eq!(
            parse_subtitle_language("Show - Pilot.Portuguese (Brazil).sub"),
            Language::PortugueseBrazil
        );
    }

    #[test]
    fn parse_language_tags_empty_when_no_match() {
        assert!(parse_language_tags("Pilot.sub").is_empty());
        assert!(parse_language_tags("file_with_no_extension").is_empty());
    }

    #[test]
    fn parse_language_tags_handles_pre_iso_tags() {
        // `Pilot.forced.eng.srt` -> simple `Pilot.forced.eng`.
        // Regex matches: pre-tag `forced`, iso `eng`, post-tags none.
        let tags = parse_language_tags("Pilot.forced.eng.srt");
        assert_eq!(tags, vec!["forced".to_string()]);
    }

    #[test]
    fn parse_language_tags_handles_post_iso_tags() {
        let tags = parse_language_tags("Pilot.eng.sdh.forced.srt");
        assert_eq!(tags, vec!["sdh".to_string(), "forced".to_string()]);
    }

    #[test]
    fn parse_language_tags_drops_non_vocab_words() {
        // `Pilot.junk.eng.srt` -> simple `Pilot.junk.eng`. The C#
        // regex would match `iso=eng` with `.+? = Pilot.junk`. Tags
        // would be empty (junk is not a tag and gets absorbed by .+?).
        let tags = parse_language_tags("Pilot.junk.eng.srt");
        assert!(tags.is_empty(), "got {tags:?}");
    }

    #[test]
    fn parse_basic_subtitle_title_first_is_false() {
        // C# `ParseBasicSubtitle` always sets TitleFirst = false.
        let r = parse_basic_subtitle("Show.S01E01.eng.srt");
        assert!(!r.title_first);
    }

    #[test]
    fn parse_subtitle_language_information_title_first_true_when_no_pre_segments() {
        // `testtitle.eng.forced` -> title is the first segment after
        // the title separator, no pre-iso/pre-tag => TitleFirst=true.
        let info = parse_subtitle_language_information(
            "Name (2020) - S01E20 - [AAC 2.0].testtitle.eng.forced.ass",
        );
        assert!(info.title_first, "got {info:?}");
    }

    #[test]
    fn parse_subtitle_language_information_title_first_true_with_pre_iso_only() {
        // `eng.testtitle.forced` -> pre-iso fires but tags1 stays
        // empty. C# `TitleFirst = tags1.Captures.Empty()` is true.
        let info = parse_subtitle_language_information(
            "Name (2020) - S01E20 - [AAC 2.0].eng.testtitle.forced.ass",
        );
        assert!(info.title_first, "got {info:?}");
    }

    #[test]
    fn parse_subtitle_language_information_title_first_false_with_pre_tag() {
        // `default.eng.testtitle.forced` -> pre-tag fires (tags1 has
        // `default`), TitleFirst=false.
        let info = parse_subtitle_language_information(
            "Name (2020) - S01E20 - [AAC 2.0].default.eng.testtitle.forced.ass",
        );
        assert!(!info.title_first, "got {info:?}");
    }

    #[test]
    fn parse_subtitle_unknown_for_pure_garbage() {
        assert_eq!(
            parse_subtitle_language("garbage_no_match"),
            Language::Unknown
        );
    }

    #[test]
    fn strip_last_extension_handles_empty_and_dotfiles() {
        assert_eq!(strip_last_extension(""), "");
        // C# `Path.GetFileNameWithoutExtension(".bashrc")` returns "":
        // a leading-dot name has an empty stem.
        assert_eq!(strip_last_extension(".bashrc"), "");
        assert_eq!(strip_last_extension("a.b.c"), "a.b");
        assert_eq!(strip_last_extension("foo"), "foo");
        assert_eq!(strip_last_extension("foo.bar"), "foo");
    }

    #[test]
    fn strip_last_extension_strips_directories() {
        // C# `Path.GetFileNameWithoutExtension` first calls `GetFileName`
        // (drops directory portion), then strips the last extension.
        assert_eq!(strip_last_extension("/foo/bar.zip/baz.eng.srt"), "baz.eng");
        assert_eq!(strip_last_extension("/tmp/Pilot.eng.srt"), "Pilot.eng");
        assert_eq!(strip_last_extension(r"C:\tmp\Pilot.eng.srt"), "Pilot.eng");
    }

    #[test]
    fn ends_with_ignore_ascii_case_basic() {
        assert!(ends_with_ignore_ascii_case("FooEnglish", "english"));
        assert!(ends_with_ignore_ascii_case("FooEnglish", "ENGLISH"));
        assert!(!ends_with_ignore_ascii_case("Foo", "FooBar"));
    }
}