diff --git a/Changelog.txt b/Changelog.txt index 1ad048804..73b0b35e5 100644 --- a/Changelog.txt +++ b/Changelog.txt @@ -9,6 +9,7 @@ * Improve EBU STL preview regarding box - thx BlueMeow7 * FIXED: * Fix for Whisper Post-processing - thx Purfview/cvrle77 + * Fix crash in "Fix common errors" - thx Joe 4.0.4 (17th March 2024) diff --git a/src/Test/FixCommonErrors/FixCommonErrorsTest.cs b/src/Test/FixCommonErrors/FixCommonErrorsTest.cs index 01ebbd2b7..7f4a78a34 100644 --- a/src/Test/FixCommonErrors/FixCommonErrorsTest.cs +++ b/src/Test/FixCommonErrors/FixCommonErrorsTest.cs @@ -3574,6 +3574,7 @@ namespace Test.FixCommonErrors engine.Fix(sub, new EmptyFixCallback()); Assert.AreEqual( "- (Foobar bar zzz).\n- Foo bar Zz", sub.Paragraphs.First().Text); } + [TestMethod] public void FixMissingOpenBracketFourTest() { @@ -3584,6 +3585,16 @@ namespace Test.FixCommonErrors Assert.AreEqual( "Foobar (THIS IS A NOISE)", sub.Paragraphs.First().Text); } + [TestMethod] + public void FixMissingOpenBracketFiveTest() + { + var engine = new FixMissingOpenBracket(); + var sub = GetGenericSub(); + sub.Paragraphs.First().Text = "- ]..."; + engine.Fix(sub, new EmptyFixCallback()); + Assert.AreEqual("- ]...", sub.Paragraphs.First().Text); + } + private static Subtitle GetGenericSub() { return new Subtitle() diff --git a/src/libse/Forms/FixCommonErrors/FixMissingOpenBracket.cs b/src/libse/Forms/FixCommonErrors/FixMissingOpenBracket.cs index 4603be7b3..0fadd4596 100644 --- a/src/libse/Forms/FixCommonErrors/FixMissingOpenBracket.cs +++ b/src/libse/Forms/FixCommonErrors/FixMissingOpenBracket.cs @@ -25,20 +25,32 @@ namespace Nikse.SubtitleEdit.Core.Forms.FixCommonErrors while (i < len && input[i] == '{') { i = input.IndexOf('}', i + 1) + 1; - if (i == 0) break; + if (i == 0) + { + break; + } } - while (i < len && IsIgnorable(input[i])) i++; + while (i < len && IsIgnorable(input[i])) + { + i++; + } // skip html tags while (i < len && input[i] == '<') { i = input.IndexOf('>', i + 1) + 1; - if (i == 0) break; + if (i == 0) + { + break; + } } // skip anything that is not a letter or digit - while (i < len && !char.IsLetterOrDigit(input[i])) i++; + while (i < len && !char.IsLetterOrDigit(input[i])) + { + i++; + } return i; } @@ -46,34 +58,56 @@ namespace Nikse.SubtitleEdit.Core.Forms.FixCommonErrors private static string RestoreMissingOpenParenthesis(string input) { var len = input.Length; - + // empty string - if (len == 0) return input; - + if (len == 0) + { + return input; + } + var closeTags = new[] { ']', ')' }; // ignore line if contains opening - if (input.Any(ch => ch == '(' || ch == '[')) return input; + if (input.Any(ch => ch == '(' || ch == '[')) + { + return input; + } var ci = input.IndexOfAny(closeTags); // invalid position - if (ci < 1) return input; + if (ci < 1) + { + return input; + } var k = ci - 1; // jump backward if uppercase or any one of the ignorable chars - while (k > 0 && char.IsUpper(input[k]) || IsIgnorable(input[k])) k--; - + while (k > 0 && (char.IsUpper(input[k]) || IsIgnorable(input[k]))) + { + k--; + } + // note if we have case like: "Hey, FOO)." then we want to insert the open before the "Hey" if (k > 0 && input[k] == ',') { k--; - while (k > 0 && char.IsLetterOrDigit(input[k])) k--; + while (k > 0 && char.IsLetterOrDigit(input[k])) + { + k--; + } } // try landing on white-space char - if (k >= 0 && k + 1 < len && input[k] != ' ' && input[k + 1] == ' ') k++; + if (k >= 0 && k + 1 < len && input[k] != ' ' && input[k + 1] == ' ') + { + k++; + } + // try finding first valid char (this is used to not insert '(' or '[') in to left of a white-space - while (k < ci && char.IsWhiteSpace(input[k])) k++; - + while (k < ci && char.IsWhiteSpace(input[k])) + { + k++; + } + // FO) => (FO) if (ci - k > 1) {