From e45a57fa074c1ca6c5ad66b82d5d4aa0b81f8217 Mon Sep 17 00:00:00 2001 From: Ivandro Ismael Date: Sat, 19 Nov 2016 23:51:00 +0000 Subject: [PATCH 1/2] [FixUnneededPeriods] - Update/Optimize. --- .../FixCommonErrors/FixUnneededPeriods.cs | 72 +++++++++---------- 1 file changed, 34 insertions(+), 38 deletions(-) diff --git a/libse/Forms/FixCommonErrors/FixUnneededPeriods.cs b/libse/Forms/FixCommonErrors/FixUnneededPeriods.cs index 9d8913509..02b67a020 100644 --- a/libse/Forms/FixCommonErrors/FixUnneededPeriods.cs +++ b/libse/Forms/FixCommonErrors/FixUnneededPeriods.cs @@ -4,57 +4,53 @@ namespace Nikse.SubtitleEdit.Core.Forms.FixCommonErrors { public class FixUnneededPeriods : IFixCommonError { - public void Fix(Subtitle subtitle, IFixCallbacks callbacks) { var language = Configuration.Settings.Language.FixCommonErrors; string fixAction = language.UnneededPeriod; - int unneededPeriodsFixed = 0; + int removedCount = 0; for (int i = 0; i < subtitle.Paragraphs.Count; i++) { Paragraph p = subtitle.Paragraphs[i]; - var oldText = p.Text; if (callbacks.AllowFix(p, fixAction)) { - if (p.Text.Contains("!." + Environment.NewLine)) + // Returns processed text. + string procText = RemoveDotAfterPunctuation(p.Text); + int diff = p.Text.Length - procText.Length; + if (diff > 0) { - p.Text = p.Text.Replace("!." + Environment.NewLine, "!" + Environment.NewLine); - unneededPeriodsFixed++; + // Calculate total removed dots. + removedCount += diff; + callbacks.AddFixToListView(p, fixAction, p.Text, procText); + p.Text = procText; } - if (p.Text.Contains("?." + Environment.NewLine)) - { - p.Text = p.Text.Replace("?." + Environment.NewLine, "?" + Environment.NewLine); - unneededPeriodsFixed++; - } - if (p.Text.EndsWith("!.", StringComparison.Ordinal)) - { - p.Text = p.Text.TrimEnd('.'); - unneededPeriodsFixed++; - } - if (p.Text.EndsWith("?.", StringComparison.Ordinal)) - { - p.Text = p.Text.TrimEnd('.'); - unneededPeriodsFixed++; - } - - var len = p.Text.Length; - if (p.Text.Contains("!. ")) - { - p.Text = p.Text.Replace("!. ", "! "); - unneededPeriodsFixed += len - p.Text.Length; - len = p.Text.Length; - } - if (p.Text.Contains("?. ")) - { - p.Text = p.Text.Replace("?. ", "? "); - unneededPeriodsFixed += len - p.Text.Length; - } - - if (p.Text != oldText) - callbacks.AddFixToListView(p, fixAction, oldText, p.Text); } } - callbacks.UpdateFixStatus(unneededPeriodsFixed, language.RemoveUnneededPeriods, string.Format(language.XUnneededPeriodsRemoved, unneededPeriodsFixed)); + callbacks.UpdateFixStatus(removedCount, language.RemoveUnneededPeriods, string.Format(language.XUnneededPeriodsRemoved, removedCount)); } + + public static string RemoveDotAfterPunctuation(string inp) + { + for (int i = inp.Length - 1; i > 0; i--) + { + // Expecting pre characters: [?!] + if (inp[i] == '.' && (inp[i - 1] == '?' || inp[i - 1] == '!')) + { + int j = i; + // Fix recursive dot after ?/! + while (j + 1 < inp.Length && inp[j + 1] == '.') + { + j++; + } + // Expecting post characters: [\r\n ] + if (j + 1 == inp.Length || inp[j + 1] == ' ' || inp[j + 1] == '\r' || inp[j + 1] == '\n') + { + inp = inp.Remove(i, j - i + 1); + } + } + } + return inp; + } + } } From 25f75cdec47e5cc7cfeeb7ebc48d56748c7851c5 Mon Sep 17 00:00:00 2001 From: Ivandro Ismael Date: Sat, 19 Nov 2016 23:51:18 +0000 Subject: [PATCH 2/2] [FixUnneededPeriods] - Add Unit-Test. --- src/Test/FixCommonErrorsTest.cs | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) diff --git a/src/Test/FixCommonErrorsTest.cs b/src/Test/FixCommonErrorsTest.cs index 53c31f8b2..6becbc776 100644 --- a/src/Test/FixCommonErrorsTest.cs +++ b/src/Test/FixCommonErrorsTest.cs @@ -1693,5 +1693,32 @@ namespace Test #endregion + #region << Fix unneeded periods after [?!] >> + + [TestMethod] + public void FixUnneededPeriodsTest1() + { + string processedText = FixUnneededPeriods.RemoveDotAfterPunctuation("Foobar?.\r\nFoobar!.\r\nFoobar"); + Assert.AreEqual("Foobar?\r\nFoobar!\r\nFoobar", processedText); + } + + [TestMethod] + public void FixUnneededPeriodsTest2() + { + string processedText = FixUnneededPeriods.RemoveDotAfterPunctuation("Foobar?."); + Assert.AreEqual("Foobar?", processedText); + + processedText = FixUnneededPeriods.RemoveDotAfterPunctuation("Foobar!."); + Assert.AreEqual("Foobar!", processedText); + } + + [TestMethod] + public void FixUnneededPeriodsTest3() + { + string processedText = FixUnneededPeriods.RemoveDotAfterPunctuation("Foobar?. Foobar!.... Foobar"); + Assert.AreEqual("Foobar? Foobar! Foobar", processedText); + } + + #endregion } }