Fix crash in "Fix common errors - FixMissingOpenBracket" - thx Joe :)

This commit is contained in:
Nikolaj Olsson 2024-03-22 22:36:05 +01:00
parent c17975b64e
commit 853e6333b4
3 changed files with 61 additions and 15 deletions

View File

@ -9,6 +9,7 @@
* Improve EBU STL preview regarding box - thx BlueMeow7 * Improve EBU STL preview regarding box - thx BlueMeow7
* FIXED: * FIXED:
* Fix for Whisper Post-processing - thx Purfview/cvrle77 * Fix for Whisper Post-processing - thx Purfview/cvrle77
* Fix crash in "Fix common errors" - thx Joe
4.0.4 (17th March 2024) 4.0.4 (17th March 2024)

View File

@ -3574,6 +3574,7 @@ namespace Test.FixCommonErrors
engine.Fix(sub, new EmptyFixCallback()); engine.Fix(sub, new EmptyFixCallback());
Assert.AreEqual( "- (Foobar bar zzz).\n- Foo bar Zz", sub.Paragraphs.First().Text); Assert.AreEqual( "- (Foobar bar zzz).\n- Foo bar Zz", sub.Paragraphs.First().Text);
} }
[TestMethod] [TestMethod]
public void FixMissingOpenBracketFourTest() public void FixMissingOpenBracketFourTest()
{ {
@ -3584,6 +3585,16 @@ namespace Test.FixCommonErrors
Assert.AreEqual( "Foobar (THIS IS A NOISE)", sub.Paragraphs.First().Text); 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() private static Subtitle GetGenericSub()
{ {
return new Subtitle() return new Subtitle()

View File

@ -25,20 +25,32 @@ namespace Nikse.SubtitleEdit.Core.Forms.FixCommonErrors
while (i < len && input[i] == '{') while (i < len && input[i] == '{')
{ {
i = input.IndexOf('}', i + 1) + 1; 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 // skip html tags
while (i < len && input[i] == '<') while (i < len && input[i] == '<')
{ {
i = input.IndexOf('>', i + 1) + 1; i = input.IndexOf('>', i + 1) + 1;
if (i == 0) break; if (i == 0)
{
break;
}
} }
// skip anything that is not a letter or digit // 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; return i;
} }
@ -46,34 +58,56 @@ namespace Nikse.SubtitleEdit.Core.Forms.FixCommonErrors
private static string RestoreMissingOpenParenthesis(string input) private static string RestoreMissingOpenParenthesis(string input)
{ {
var len = input.Length; var len = input.Length;
// empty string // empty string
if (len == 0) return input; if (len == 0)
{
return input;
}
var closeTags = new[] { ']', ')' }; var closeTags = new[] { ']', ')' };
// ignore line if contains opening // 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); var ci = input.IndexOfAny(closeTags);
// invalid position // invalid position
if (ci < 1) return input; if (ci < 1)
{
return input;
}
var k = ci - 1; var k = ci - 1;
// jump backward if uppercase or any one of the ignorable chars // 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" // note if we have case like: "Hey, FOO)." then we want to insert the open before the "Hey"
if (k > 0 && input[k] == ',') if (k > 0 && input[k] == ',')
{ {
k--; k--;
while (k > 0 && char.IsLetterOrDigit(input[k])) k--; while (k > 0 && char.IsLetterOrDigit(input[k]))
{
k--;
}
} }
// try landing on white-space char // 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 // 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) // FO) => (FO)
if (ci - k > 1) if (ci - k > 1)
{ {