Handle Unicode control chars in "Remove empty lines" - thx igenaya :)

Fix  #5034
This commit is contained in:
Nikolaj Olsson 2021-05-13 14:12:22 +02:00
parent 4b1e5b7385
commit b45e3dd680
3 changed files with 20 additions and 9 deletions

View File

@ -49,6 +49,7 @@
* Add "Slovenian" to language auto detect - thx Milenko
* Update Tesseract 5 Alpha to "2021-05-06" - thx iGom
* Add French letters in OCR char inspect - thx tormento
* Handle Unicode control char in "Remove empty lines" - thx igenaya
* FIXED:
* Fix for reading bdsup position - thx akaicat
* Fix a few crashes in translation engine - thx howblast/Laís

View File

@ -1270,6 +1270,16 @@ namespace Test.FixCommonErrors
}
}
public void FixEmptyLinesTest3()
{
using (var target = GetFixCommonErrorsLib())
{
InitializeFixCommonErrorsLine(target, "\\U202C");
new FixEmptyLines().Fix(_subtitle, new EmptyFixCallback());
Assert.AreEqual(0, _subtitle.Paragraphs.Count);
}
}
#endregion Fix EmptyLines
#region Fix missing periods at end of line

View File

@ -31,7 +31,7 @@ namespace Nikse.SubtitleEdit.Core.Forms.FixCommonErrors
for (int i = subtitle.Paragraphs.Count - 1; i >= 0; i--)
{
Paragraph p = subtitle.Paragraphs[i];
var p = subtitle.Paragraphs[i];
if (!string.IsNullOrEmpty(p.Text))
{
string text = p.Text.Trim(' ');
@ -87,15 +87,15 @@ namespace Nikse.SubtitleEdit.Core.Forms.FixCommonErrors
}
}
if (callbacks.AllowFix(p, fixAction1) && text.StartsWith(Environment.NewLine, StringComparison.Ordinal))
if (callbacks.AllowFix(p, fixAction1) && text.TrimStart(StringExtensions.UnicodeControlChars).StartsWith(Environment.NewLine, StringComparison.Ordinal))
{
if (pre.Length > 0)
{
text = pre + text.TrimStart(Utilities.NewLineChars);
text = pre + text.TrimStart(StringExtensions.UnicodeControlChars).TrimStart(Utilities.NewLineChars);
}
else
{
text = text.TrimStart(Utilities.NewLineChars);
text = text.TrimStart(StringExtensions.UnicodeControlChars).TrimStart(Utilities.NewLineChars);
}
p.Text = text;
@ -107,15 +107,15 @@ namespace Nikse.SubtitleEdit.Core.Forms.FixCommonErrors
text = pre + text;
}
if (callbacks.AllowFix(p, fixAction2) && text.EndsWith(Environment.NewLine, StringComparison.Ordinal))
if (callbacks.AllowFix(p, fixAction2) && text.TrimEnd(StringExtensions.UnicodeControlChars).EndsWith(Environment.NewLine, StringComparison.Ordinal))
{
if (post.Length > 0)
{
text = text.TrimEnd(Utilities.NewLineChars) + post;
text = text.TrimEnd(StringExtensions.UnicodeControlChars).TrimEnd(Utilities.NewLineChars) + post;
}
else
{
text = text.TrimEnd(Utilities.NewLineChars);
text = text.TrimEnd(StringExtensions.UnicodeControlChars).TrimEnd(Utilities.NewLineChars);
}
p.Text = text;
@ -142,9 +142,9 @@ namespace Nikse.SubtitleEdit.Core.Forms.FixCommonErrors
// this must be the very last action done, or line numbers will be messed up!!!
for (int i = subtitle.Paragraphs.Count - 1; i >= 0; i--)
{
Paragraph p = subtitle.Paragraphs[i];
var p = subtitle.Paragraphs[i];
var text = HtmlUtil.RemoveHtmlTags(p.Text, true).Trim();
if (callbacks.AllowFix(p, fixAction0) && string.IsNullOrEmpty(text.RemoveControlCharacters()))
if (callbacks.AllowFix(p, fixAction0) && string.IsNullOrEmpty(text.RemoveControlCharacters().RemoveChar(StringExtensions.UnicodeControlChars)))
{
subtitle.Paragraphs.RemoveAt(i);
emptyLinesRemoved++;