Skip to content

Commit 224b389

Browse files
authored
fix(matcher): allow whitespace as a matching suffix (#75)
1 parent 312f38e commit 224b389

File tree

3 files changed

+22
-9
lines changed

3 files changed

+22
-9
lines changed

SubRenamer.Core/MatcherDiff.cs

+2-2
Original file line numberDiff line numberDiff line change
@@ -76,14 +76,14 @@ private static string FindCommonSuffix(string a, string b)
7676

7777
return "";
7878

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

8989
public static string ExtractMatchKeyByDiff(DiffResult? diff, string filename)

SubRenamer.Tests/MatcherTests/FindCommonSuffixTests.cs

+6-6
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ public void Basic()
1616
{
1717
Assert.That(FindCommonSuffix("01]", "02]"), Is.EqualTo("]"));
1818
Assert.That(FindCommonSuffix("01]end", "02]end"), Is.EqualTo("]"));
19-
Assert.That(FindCommonSuffix(" 01 ] end", " 02 ] end"), Is.EqualTo("]"));
19+
Assert.That(FindCommonSuffix(" 01 ] end", " 02 ] end"), Is.EqualTo(" "));
2020
}
2121

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

33-
Assert.That(FindCommonSuffix("01 ", "02 "), Is.EqualTo(""), "should skip Whitespace, then no match");
34-
Assert.That(FindCommonSuffix("01 ]", "02 ]"), Is.EqualTo("]"), "should skip Whitespace, then match");
33+
Assert.That(FindCommonSuffix("01 ", "02 "), Is.EqualTo(" "), "should not skip Whitespace, then match");
34+
Assert.That(FindCommonSuffix("01 ]", "02 ]"), Is.EqualTo(" "), "should not skip Whitespace, then match");
3535
}
3636

3737
[Test]
3838
public void NoCommon()
3939
{
4040
Assert.That(FindCommonSuffix("01$", "02]"), Is.EqualTo(""));
41-
Assert.That(FindCommonSuffix("01 abc", "02 def"), Is.EqualTo(""));
41+
Assert.That(FindCommonSuffix("01abc", "02def"), Is.EqualTo(""));
4242
}
4343

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

70-
var result = FindCommonSuffix("01B 話 番外篇", "02B 話 番外篇");
70+
var result = FindCommonSuffix("01B話 番外篇", "02B話 番外篇");
7171
Assert.That(result.Length, Is.EqualTo(1), "should match only single character");
7272
Assert.That(result, Is.EqualTo("話"));
7373
}

SubRenamer.Tests/MatcherTests/TopLevelTests.json

+14-1
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,19 @@
9494
{"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"}
9595
]
9696
},
97+
{
98+
"Name": "轻音少女 (Whitespace as Suffix)",
99+
"Input": [
100+
{"Key": "", "Video": "轻音少女 S01E01 1080p.VCB-Studio.mkv", "Subtitle": ""},
101+
{"Key": "", "Video": "轻音少女 S01E02 1080p.VCB-Studio.mkv", "Subtitle": ""},
102+
{"Key": "", "Video": "", "Subtitle": "轻音少女S01E01.ass"},
103+
{"Key": "", "Video": "", "Subtitle": "轻音少女S01E02.ass"}
104+
],
105+
"Output": [
106+
{"Key": "1", "Video": "轻音少女 S01E01 1080p.VCB-Studio.mkv", "Subtitle": "轻音少女S01E01.ass"},
107+
{"Key": "2", "Video": "轻音少女 S01E02 1080p.VCB-Studio.mkv", "Subtitle": "轻音少女S01E02.ass"}
108+
]
109+
},
97110
{
98111
"Name": "SquareBrackets",
99112
"Input": [
@@ -121,7 +134,7 @@
121134
]
122135
},
123136
{
124-
"Name": "WhiteSpaceEnd",
137+
"Name": "WhiteSpaceAsSuffix",
125138
"Input": [
126139
{"Key": "", "Video": "视频 1 xyz.mov", "Subtitle": ""},
127140
{"Key": "", "Video": "视频 77 test xyz.mov", "Subtitle": ""},

0 commit comments

Comments
 (0)