From 551ec98a6d2c0f36807fdf5e995d491f5470e1ff Mon Sep 17 00:00:00 2001 From: qwqcode Date: Fri, 10 Jan 2025 23:49:06 +0800 Subject: [PATCH] fix(matcher): allow whitespace as a matching suffix --- SubRenamer.Core/MatcherDiff.cs | 4 ++-- .../MatcherTests/FindCommonSuffixTests.cs | 12 ++++++------ SubRenamer.Tests/MatcherTests/TopLevelTests.json | 15 ++++++++++++++- 3 files changed, 22 insertions(+), 9 deletions(-) diff --git a/SubRenamer.Core/MatcherDiff.cs b/SubRenamer.Core/MatcherDiff.cs index b163a04..5c15b1f 100644 --- a/SubRenamer.Core/MatcherDiff.cs +++ b/SubRenamer.Core/MatcherDiff.cs @@ -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) diff --git a/SubRenamer.Tests/MatcherTests/FindCommonSuffixTests.cs b/SubRenamer.Tests/MatcherTests/FindCommonSuffixTests.cs index ca58db6..efc77a1 100644 --- a/SubRenamer.Tests/MatcherTests/FindCommonSuffixTests.cs +++ b/SubRenamer.Tests/MatcherTests/FindCommonSuffixTests.cs @@ -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] @@ -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] @@ -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("話")); } diff --git a/SubRenamer.Tests/MatcherTests/TopLevelTests.json b/SubRenamer.Tests/MatcherTests/TopLevelTests.json index e1be1d9..81f1f90 100644 --- a/SubRenamer.Tests/MatcherTests/TopLevelTests.json +++ b/SubRenamer.Tests/MatcherTests/TopLevelTests.json @@ -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": [ @@ -121,7 +134,7 @@ ] }, { - "Name": "WhiteSpaceEnd", + "Name": "WhiteSpaceAsSuffix", "Input": [ {"Key": "", "Video": "视频 1 xyz.mov", "Subtitle": ""}, {"Key": "", "Video": "视频 77 test xyz.mov", "Subtitle": ""},