Skip to content

Commit

Permalink
fix(matcher): allow whitespace as a matching suffix (#75)
Browse files Browse the repository at this point in the history
  • Loading branch information
qwqcode authored Jan 10, 2025
1 parent 312f38e commit 224b389
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 9 deletions.
4 changes: 2 additions & 2 deletions SubRenamer.Core/MatcherDiff.cs
Original file line number Diff line number Diff line change
Expand Up @@ -76,14 +76,14 @@ private static string FindCommonSuffix(string a, string b)

return "";

// Skip [a-z], [A-Z], [0-9], and whitespace. Which is not allowed as a suffix.
// Skip [a-z], [A-Z], [0-9]. Which is not allowed as a suffix.
// Because it may be a part of the `Key` (Episode Number).
// Such as "file [01A] end" and "file [01B] end".
//
// But allows Chinese character as a suffix.
// Such as "file 01 話" and "file 02 話".
// @see https://github.com/qwqcode/SubRenamer/pull/45
bool Skip(char c) => char.IsAsciiLetterOrDigit(c) || c == ' ';
bool Skip(char c) => char.IsAsciiLetterOrDigit(c);
}

public static string ExtractMatchKeyByDiff(DiffResult? diff, string filename)
Expand Down
12 changes: 6 additions & 6 deletions SubRenamer.Tests/MatcherTests/FindCommonSuffixTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ public void Basic()
{
Assert.That(FindCommonSuffix("01]", "02]"), Is.EqualTo("]"));
Assert.That(FindCommonSuffix("01]end", "02]end"), Is.EqualTo("]"));
Assert.That(FindCommonSuffix(" 01 ] end", " 02 ] end"), Is.EqualTo("]"));
Assert.That(FindCommonSuffix(" 01 ] end", " 02 ] end"), Is.EqualTo(" "));
}

[Test]
Expand All @@ -30,15 +30,15 @@ public void SkipMatchCharacter()
Assert.That(FindCommonSuffix("01a", "02a"), Is.EqualTo(""), "should skip Lowercase Letter, then no match");
Assert.That(FindCommonSuffix("01a]", "02a]"), Is.EqualTo("]"), "should skip Lowercase Letter, then match");

Assert.That(FindCommonSuffix("01 ", "02 "), Is.EqualTo(""), "should skip Whitespace, then no match");
Assert.That(FindCommonSuffix("01 ]", "02 ]"), Is.EqualTo("]"), "should skip Whitespace, then match");
Assert.That(FindCommonSuffix("01 ", "02 "), Is.EqualTo(" "), "should not skip Whitespace, then match");
Assert.That(FindCommonSuffix("01 ]", "02 ]"), Is.EqualTo(" "), "should not skip Whitespace, then match");
}

[Test]
public void NoCommon()
{
Assert.That(FindCommonSuffix("01$", "02]"), Is.EqualTo(""));
Assert.That(FindCommonSuffix("01 abc", "02 def"), Is.EqualTo(""));
Assert.That(FindCommonSuffix("01abc", "02def"), Is.EqualTo(""));
}

[Test]
Expand All @@ -65,9 +65,9 @@ public void MatchChineseCharacter()
{
// @see https://github.com/qwqcode/SubRenamer/pull/45
Assert.That(FindCommonSuffix("01話", "02話"), Is.EqualTo("話"));
Assert.That(FindCommonSuffix("01B 集", "02B 集"), Is.EqualTo(""));
Assert.That(FindCommonSuffix("01B 集", "02B 集"), Is.EqualTo(" "));

var result = FindCommonSuffix("01B 話 番外篇", "02B 話 番外篇");
var result = FindCommonSuffix("01B話 番外篇", "02B話 番外篇");
Assert.That(result.Length, Is.EqualTo(1), "should match only single character");
Assert.That(result, Is.EqualTo("話"));
}
Expand Down
15 changes: 14 additions & 1 deletion SubRenamer.Tests/MatcherTests/TopLevelTests.json
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,19 @@
{"Key": "10", "Video": "[Kamigami] Haikyuu!! S2 - 10 [1920x1080 HEVC AAC Sub(Chs,Cht,Jap)].mkv", "Subtitle": "[YYDM-11FANS][Haikyuu!!][10][BDRIP][720P][X264-10bit_AAC][6FDEFD72].tc.ass"}
]
},
{
"Name": "轻音少女 (Whitespace as Suffix)",
"Input": [
{"Key": "", "Video": "轻音少女 S01E01 1080p.VCB-Studio.mkv", "Subtitle": ""},
{"Key": "", "Video": "轻音少女 S01E02 1080p.VCB-Studio.mkv", "Subtitle": ""},
{"Key": "", "Video": "", "Subtitle": "轻音少女S01E01.ass"},
{"Key": "", "Video": "", "Subtitle": "轻音少女S01E02.ass"}
],
"Output": [
{"Key": "1", "Video": "轻音少女 S01E01 1080p.VCB-Studio.mkv", "Subtitle": "轻音少女S01E01.ass"},
{"Key": "2", "Video": "轻音少女 S01E02 1080p.VCB-Studio.mkv", "Subtitle": "轻音少女S01E02.ass"}
]
},
{
"Name": "SquareBrackets",
"Input": [
Expand Down Expand Up @@ -121,7 +134,7 @@
]
},
{
"Name": "WhiteSpaceEnd",
"Name": "WhiteSpaceAsSuffix",
"Input": [
{"Key": "", "Video": "视频 1 xyz.mov", "Subtitle": ""},
{"Key": "", "Video": "视频 77 test xyz.mov", "Subtitle": ""},
Expand Down

0 comments on commit 224b389

Please sign in to comment.