From 3fe480d1d03a853cab08f926486d5d62b96410b6 Mon Sep 17 00:00:00 2001 From: Nikolaj Olsson Date: Sun, 19 Apr 2020 07:37:52 +0200 Subject: [PATCH] Fix date postfixes in "remove spaces" - thx Boulder08 :) Fix #4129 --- libse/StringExtensions.cs | 20 ++++++++++++++++++++ libse/Utilities.cs | 15 +++++++++++++++ src/Test/Logic/UtilitiesTest.cs | 9 ++++++++- 3 files changed, 43 insertions(+), 1 deletion(-) diff --git a/libse/StringExtensions.cs b/libse/StringExtensions.cs index e0bccbdcc..6ac3d9ccd 100644 --- a/libse/StringExtensions.cs +++ b/libse/StringExtensions.cs @@ -297,6 +297,26 @@ namespace Nikse.SubtitleEdit.Core return false; } + public static bool ContainsNumber(this string s) + { + if (s == null) + { + return false; + } + + int max = s.Length; + for (int index = 0; index < max; index++) + { + var ch = s[index]; + if (char.IsNumber(ch)) + { + return true; + } + } + + return false; + } + public static string RemoveControlCharacters(this string s) { int max = s.Length; diff --git a/libse/Utilities.cs b/libse/Utilities.cs index 8b06f4a93..b4a1dc831 100644 --- a/libse/Utilities.cs +++ b/libse/Utilities.cs @@ -2115,6 +2115,21 @@ namespace Nikse.SubtitleEdit.Core text = text.Replace(" ." + Environment.NewLine, "." + Environment.NewLine); } + if (language == "en" && text.ContainsNumber()) + { + // 1 st => 1st + text = new Regex(@"(1) (st)\b").Replace(text, "$1$2"); + + // 2 nd => 2nd + text = new Regex(@"(2) (nd)\b").Replace(text, "$1$2"); + + // 3 rd => 2rd + text = new Regex(@"(3) (rd)\b").Replace(text, "$1$2"); + + // 4 th => 4th + text = new Regex(@"([0456789]) (th)\b").Replace(text, "$1$2"); + } + if (language != "fr") // special rules for French { if (text.Contains(" !")) diff --git a/src/Test/Logic/UtilitiesTest.cs b/src/Test/Logic/UtilitiesTest.cs index 21a715672..5ca383420 100644 --- a/src/Test/Logic/UtilitiesTest.cs +++ b/src/Test/Logic/UtilitiesTest.cs @@ -594,7 +594,14 @@ namespace Test.Logic string s = Utilities.RemoveUnneededSpaces("The time is 8. 40.", "en"); Assert.AreEqual("The time is 8.40.", s); } - + + [TestMethod] + public void RemoveUnneededSpacesBetweenNumbersDates() + { + string s = Utilities.RemoveUnneededSpaces("The 4 th and 1 st 3 rd and 2 nd.", "en"); + Assert.AreEqual("The 4th and 1st 3rd and 2nd.", s); + } + [TestMethod] public void CountTagInTextStringOneLetterString() {