Don't add dashes if there are already dashes, use safe substring, add some unit tests

This commit is contained in:
Martijn van Berkel (Flitskikker) 2022-09-07 17:22:05 +02:00
parent af58d46763
commit 57705c12d2
3 changed files with 392 additions and 40 deletions

View File

@ -0,0 +1,322 @@
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Nikse.SubtitleEdit.Core.Common;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Test.Logic
{
[TestClass]
public class ConvertColorsToDialogTest
{
[TestMethod]
public void TestDialog1()
{
Configuration.Settings.General.DialogStyle = Nikse.SubtitleEdit.Core.Enums.DialogType.DashBothLinesWithSpace;
var subtitle = new Subtitle(new List<Paragraph>() { new Paragraph("<font color=\"#ffff00\">That was really delicious.</font>" + Environment.NewLine +
"I know.", 0, 2000) });
ConvertColorsToDialogUtils.ConvertColorsToDialogInSubtitle(subtitle, true, false, false);
var result = subtitle.Paragraphs.First().Text;
Assert.AreEqual("- That was really delicious." + Environment.NewLine + "- I know.", result);
}
[TestMethod]
public void TestDialog2()
{
Configuration.Settings.General.DialogStyle = Nikse.SubtitleEdit.Core.Enums.DialogType.DashBothLinesWithSpace;
var subtitle = new Subtitle(new List<Paragraph>() { new Paragraph("That's it!" + Environment.NewLine +
"<font color=\"#ffff00\">..sped to victory.</font>", 0, 2000) });
ConvertColorsToDialogUtils.ConvertColorsToDialogInSubtitle(subtitle, true, false, false);
var result = subtitle.Paragraphs.First().Text;
Assert.AreEqual("- That's it!" + Environment.NewLine + "- ..sped to victory.", result);
}
[TestMethod]
public void TestDialog2Alt()
{
Configuration.Settings.General.DialogStyle = Nikse.SubtitleEdit.Core.Enums.DialogType.DashSecondLineWithoutSpace;
var subtitle = new Subtitle(new List<Paragraph>() { new Paragraph("That's it!" + Environment.NewLine +
"<font color=\"#ffff00\">..sped to victory.</font>", 0, 2000) });
ConvertColorsToDialogUtils.ConvertColorsToDialogInSubtitle(subtitle, true, false, false);
var result = subtitle.Paragraphs.First().Text;
Assert.AreEqual("That's it!" + Environment.NewLine + "-..sped to victory.", result);
}
[TestMethod]
public void TestDialog3()
{
Configuration.Settings.General.DialogStyle = Nikse.SubtitleEdit.Core.Enums.DialogType.DashBothLinesWithSpace;
var subtitle = new Subtitle(new List<Paragraph>() { new Paragraph("<font color=\"#ff0000\">That's it!</font>" + Environment.NewLine +
"<font color=\"#ffff00\">..sped to victory.</font>", 0, 2000) });
ConvertColorsToDialogUtils.ConvertColorsToDialogInSubtitle(subtitle, true, false, false);
var result = subtitle.Paragraphs.First().Text;
Assert.AreEqual("- That's it!" + Environment.NewLine + "- ..sped to victory.", result);
}
[TestMethod]
public void TestDialog3Alt()
{
Configuration.Settings.General.DialogStyle = Nikse.SubtitleEdit.Core.Enums.DialogType.DashSecondLineWithoutSpace;
var subtitle = new Subtitle(new List<Paragraph>() { new Paragraph("<font color=\"#ff0000\">That's it!</font>" + Environment.NewLine +
"<font color=\"#ffff00\">..sped to victory.</font>", 0, 2000) });
ConvertColorsToDialogUtils.ConvertColorsToDialogInSubtitle(subtitle, true, false, false);
var result = subtitle.Paragraphs.First().Text;
Assert.AreEqual("That's it!" + Environment.NewLine + "-..sped to victory.", result);
}
[TestMethod]
public void TestDialog4()
{
Configuration.Settings.General.DialogStyle = Nikse.SubtitleEdit.Core.Enums.DialogType.DashBothLinesWithSpace;
var subtitle = new Subtitle(new List<Paragraph>() { new Paragraph("We are going to Ibiza." + Environment.NewLine +
"<font color=\"#ffff00\">To where?</font> Ibiza. <font color=\"#ffff00\">Okay.</font>", 0, 2000) });
ConvertColorsToDialogUtils.ConvertColorsToDialogInSubtitle(subtitle, true, false, false);
var result = subtitle.Paragraphs.First().Text;
Assert.AreEqual("- We are going to Ibiza." + Environment.NewLine + "- To where? - Ibiza. - Okay.", result);
}
[TestMethod]
public void TestDialog4Alt()
{
Configuration.Settings.General.DialogStyle = Nikse.SubtitleEdit.Core.Enums.DialogType.DashSecondLineWithoutSpace;
var subtitle = new Subtitle(new List<Paragraph>() { new Paragraph("We are going to Ibiza." + Environment.NewLine +
"<font color=\"#ffff00\">To where?</font> Ibiza. <font color=\"#ffff00\">Okay.</font>", 0, 2000) });
ConvertColorsToDialogUtils.ConvertColorsToDialogInSubtitle(subtitle, true, false, false);
var result = subtitle.Paragraphs.First().Text;
Assert.AreEqual("We are going to Ibiza." + Environment.NewLine + "-To where? -Ibiza. -Okay.", result);
}
[TestMethod]
public void TestDialogAddNewLinesOff()
{
Configuration.Settings.General.DialogStyle = Nikse.SubtitleEdit.Core.Enums.DialogType.DashSecondLineWithoutSpace;
var subtitle = new Subtitle(new List<Paragraph>() { new Paragraph("Keep on going. <font color=\"#ffff00\">Okay.</font>", 0, 2000) });
ConvertColorsToDialogUtils.ConvertColorsToDialogInSubtitle(subtitle, true, false, false);
var result = subtitle.Paragraphs.First().Text;
Assert.AreEqual("Keep on going. -Okay.", result);
}
[TestMethod]
public void TestDialogAddNewLinesOn()
{
Configuration.Settings.General.DialogStyle = Nikse.SubtitleEdit.Core.Enums.DialogType.DashSecondLineWithoutSpace;
var subtitle = new Subtitle(new List<Paragraph>() { new Paragraph("Keep on going. <font color=\"#ffff00\">Okay.</font>", 0, 2000) });
ConvertColorsToDialogUtils.ConvertColorsToDialogInSubtitle(subtitle, true, true, false);
var result = subtitle.Paragraphs.First().Text;
Assert.AreEqual("Keep on going." + Environment.NewLine + "-Okay.", result);
}
[TestMethod]
public void TestDialogThreeLines1()
{
Configuration.Settings.General.DialogStyle = Nikse.SubtitleEdit.Core.Enums.DialogType.DashBothLinesWithSpace;
var subtitle = new Subtitle(new List<Paragraph>() { new Paragraph("<font color=\"#00ffff\">Got a sinking feeling.</font>" + Environment.NewLine +
"<font color=\"#00ff00\">\"Evacuate from</font>" + Environment.NewLine +
"<font color=\"#00ff00\">an underwater chopper.\"</font> ", 0, 2000) });
ConvertColorsToDialogUtils.ConvertColorsToDialogInSubtitle(subtitle, true, false, false);
var result = subtitle.Paragraphs.First().Text;
Assert.AreEqual("- Got a sinking feeling." + Environment.NewLine + "- \"Evacuate from" + Environment.NewLine + "an underwater chopper.\"", result);
}
[TestMethod]
public void TestDialogThreeLines2()
{
Configuration.Settings.General.DialogStyle = Nikse.SubtitleEdit.Core.Enums.DialogType.DashBothLinesWithSpace;
var subtitle = new Subtitle(new List<Paragraph>() { new Paragraph("<font color=\"#00ffff\">Got a sinking feeling</font>" + Environment.NewLine +
"<font color=\"#00ffff\">about all of this.</font>" + Environment.NewLine +
"<font color=\"#00ff00\">Don't worry.</font> ", 0, 2000) });
ConvertColorsToDialogUtils.ConvertColorsToDialogInSubtitle(subtitle, true, false, false);
var result = subtitle.Paragraphs.First().Text;
Assert.AreEqual("- Got a sinking feeling" + Environment.NewLine + "about all of this." + Environment.NewLine + "- Don't worry.", result);
}
[TestMethod]
public void TestDialogThreeLines2ReBreak()
{
Configuration.Settings.General.DialogStyle = Nikse.SubtitleEdit.Core.Enums.DialogType.DashBothLinesWithSpace;
var subtitle = new Subtitle(new List<Paragraph>() { new Paragraph("<font color=\"#00ffff\">Got a sinking feeling</font>" + Environment.NewLine +
"<font color=\"#00ffff\">about all of this.</font>" + Environment.NewLine +
"<font color=\"#00ff00\">Don't worry.</font> ", 0, 2000) });
ConvertColorsToDialogUtils.ConvertColorsToDialogInSubtitle(subtitle, true, false, true);
var result = subtitle.Paragraphs.First().Text;
Assert.AreEqual("- Got a sinking feeling about all of this." + Environment.NewLine + "- Don't worry.", result);
}
[TestMethod]
public void TestNoChange1()
{
Configuration.Settings.General.DialogStyle = Nikse.SubtitleEdit.Core.Enums.DialogType.DashBothLinesWithSpace;
var subtitle = new Subtitle(new List<Paragraph>() { new Paragraph("<font color=\"#ffff00\">That's it!</font>" + Environment.NewLine +
"<font color=\"#ffff00\">..sped to victory.</font>", 0, 2000) });
ConvertColorsToDialogUtils.ConvertColorsToDialogInSubtitle(subtitle, true, false, false);
var result = subtitle.Paragraphs.First().Text;
Assert.AreEqual("That's it!" + Environment.NewLine + "..sped to victory.", result);
}
[TestMethod]
public void TestNoChange2()
{
Configuration.Settings.General.DialogStyle = Nikse.SubtitleEdit.Core.Enums.DialogType.DashBothLinesWithSpace;
var subtitle = new Subtitle(new List<Paragraph>() { new Paragraph("<font color=\"#ffff00\">That's it!</font> <font color=\"#ffff00\">Sped to victory.</font>", 0, 2000) });
ConvertColorsToDialogUtils.ConvertColorsToDialogInSubtitle(subtitle, true, false, false);
var result = subtitle.Paragraphs.First().Text;
Assert.AreEqual("That's it! Sped to victory.", result);
}
[TestMethod]
public void TestNoChange3()
{
Configuration.Settings.General.DialogStyle = Nikse.SubtitleEdit.Core.Enums.DialogType.DashBothLinesWithSpace;
var subtitle = new Subtitle(new List<Paragraph>() { new Paragraph("<font color=\"#ffff00\">- That was really delicious.</font>" + Environment.NewLine +
"- I know.", 0, 2000) });
ConvertColorsToDialogUtils.ConvertColorsToDialogInSubtitle(subtitle, false, false, false);
var result = subtitle.Paragraphs.First().Text;
Assert.AreEqual("<font color=\"#ffff00\">- That was really delicious.</font>" + Environment.NewLine + "- I know.", result);
}
[TestMethod]
public void TestNoChange3NoColor()
{
Configuration.Settings.General.DialogStyle = Nikse.SubtitleEdit.Core.Enums.DialogType.DashBothLinesWithSpace;
var subtitle = new Subtitle(new List<Paragraph>() { new Paragraph("<font color=\"#ffff00\">- That was really delicious.</font>" + Environment.NewLine +
"- I know.", 0, 2000) });
ConvertColorsToDialogUtils.ConvertColorsToDialogInSubtitle(subtitle, true, false, false);
var result = subtitle.Paragraphs.First().Text;
Assert.AreEqual("- That was really delicious." + Environment.NewLine + "- I know.", result);
}
[TestMethod]
public void TestNoChange4()
{
Configuration.Settings.General.DialogStyle = Nikse.SubtitleEdit.Core.Enums.DialogType.DashBothLinesWithSpace;
var subtitle = new Subtitle(new List<Paragraph>() { new Paragraph("- <font color=\"#ffff00\">That was really delicious.</font>" + Environment.NewLine +
"- I know.", 0, 2000) });
ConvertColorsToDialogUtils.ConvertColorsToDialogInSubtitle(subtitle, false, false, false);
var result = subtitle.Paragraphs.First().Text;
Assert.AreEqual("- <font color=\"#ffff00\">That was really delicious.</font>" + Environment.NewLine + "- I know.", result);
}
[TestMethod]
public void TestNoChange4NoColor()
{
Configuration.Settings.General.DialogStyle = Nikse.SubtitleEdit.Core.Enums.DialogType.DashBothLinesWithSpace;
var subtitle = new Subtitle(new List<Paragraph>() { new Paragraph("- <font color=\"#ffff00\">That was really delicious.</font>" + Environment.NewLine +
"- I know.", 0, 2000) });
ConvertColorsToDialogUtils.ConvertColorsToDialogInSubtitle(subtitle, true, false, false);
var result = subtitle.Paragraphs.First().Text;
Assert.AreEqual("- That was really delicious." + Environment.NewLine + "- I know.", result);
}
[TestMethod]
public void TestNoChange5()
{
Configuration.Settings.General.DialogStyle = Nikse.SubtitleEdit.Core.Enums.DialogType.DashBothLinesWithSpace;
var subtitle = new Subtitle(new List<Paragraph>() { new Paragraph("- No, don't touch that-- - <font color=\"#ffff00\">That was stupid.</font>" + Environment.NewLine +
"- I know.", 0, 2000) });
ConvertColorsToDialogUtils.ConvertColorsToDialogInSubtitle(subtitle, false, false, false);
var result = subtitle.Paragraphs.First().Text;
Assert.AreEqual("- No, don't touch that-- - <font color=\"#ffff00\">That was stupid.</font>" + Environment.NewLine + "- I know.", result);
}
[TestMethod]
public void TestNoChange5NoColor()
{
Configuration.Settings.General.DialogStyle = Nikse.SubtitleEdit.Core.Enums.DialogType.DashBothLinesWithSpace;
var subtitle = new Subtitle(new List<Paragraph>() { new Paragraph("- No, don't touch that-- - <font color=\"#ffff00\">That was stupid.</font>" + Environment.NewLine +
"- I know.", 0, 2000) });
ConvertColorsToDialogUtils.ConvertColorsToDialogInSubtitle(subtitle, true, false, false);
var result = subtitle.Paragraphs.First().Text;
Assert.AreEqual("- No, don't touch that-- - That was stupid." + Environment.NewLine + "- I know.", result);
}
[TestMethod]
public void TestDialogInterruption()
{
Configuration.Settings.General.DialogStyle = Nikse.SubtitleEdit.Core.Enums.DialogType.DashBothLinesWithSpace;
var subtitle = new Subtitle(new List<Paragraph>() { new Paragraph("No, don't touch that-- <font color=\"#ffff00\">That was stupid.</font>" + Environment.NewLine +
"I know.", 0, 2000) });
ConvertColorsToDialogUtils.ConvertColorsToDialogInSubtitle(subtitle, false, false, false);
var result = subtitle.Paragraphs.First().Text;
Assert.AreEqual("- No, don't touch that-- - <font color=\"#ffff00\">That was stupid.</font>" + Environment.NewLine + "- I know.", result);
}
[TestMethod]
public void TestDialogInterruptionNoColor()
{
Configuration.Settings.General.DialogStyle = Nikse.SubtitleEdit.Core.Enums.DialogType.DashBothLinesWithSpace;
var subtitle = new Subtitle(new List<Paragraph>() { new Paragraph("No, don't touch that-- <font color=\"#ffff00\">That was stupid.</font>" + Environment.NewLine +
"I know.", 0, 2000) });
ConvertColorsToDialogUtils.ConvertColorsToDialogInSubtitle(subtitle, true, false, false);
var result = subtitle.Paragraphs.First().Text;
Assert.AreEqual("- No, don't touch that-- - That was stupid." + Environment.NewLine + "- I know.", result);
}
}
}

View File

@ -65,6 +65,7 @@
<Compile Include="Assa\ResamplerTest.cs" />
<Compile Include="Dictionaries\StringWithoutSpaceSplitToWordsTest.cs" />
<Compile Include="LanguageFiles\LanguageFileTest.cs" />
<Compile Include="Logic\ConvertColorsToDialogTest.cs" />
<Compile Include="Logic\NetflixHelperTest.cs" />
<Compile Include="Logic\SubtitleFormats\PacTest.cs" />
<Compile Include="Core\UUEncodingTest.cs" />

View File

@ -29,10 +29,10 @@ namespace Nikse.SubtitleEdit.Core.Common
while (index < p.Text.Length)
{
if (index + "<font color".Length <= p.Text.Length && p.Text.Substring(index, "<font color".Length).ToLowerInvariant() == "<font color")
if (index + "<font color".Length <= p.Text.Length && p.Text.SafeSubstring(index, "<font color".Length).ToLowerInvariant() == "<font color")
{
// New color
newColor = p.Text.Substring(p.Text.IndexOf("=", index) + 1, p.Text.IndexOf(">", index) - p.Text.IndexOf("=", index) - 1).Replace("\"", "");
newColor = p.Text.SafeSubstring(p.Text.IndexOf("=", index) + 1, p.Text.IndexOf(">", index) - p.Text.IndexOf("=", index) - 1).Replace("\"", "");
if (currentColor == null)
{
@ -40,28 +40,33 @@ namespace Nikse.SubtitleEdit.Core.Common
}
else if (currentColor != newColor)
{
if (dashFirstLine && !firstLineAdded)
// Don't insert dash if there is already a dash, but DO insert a dash if it is an interruption
if (p.Text.SafeSubstring(index, 1) != "-" && p.Text.SafeSubstring(index - 1, 1) != "-"
&& (p.Text.SafeSubstring(index - 2, 2) != "- " || p.Text.SafeSubstring(index - 3, 3) == "-- "))
{
p.Text = dash + p.Text;
if (dashFirstLine && !firstLineAdded)
{
p.Text = dash + p.Text;
index += dash.Length;
firstLineAdded = true;
}
if (!addNewLines && p.Text.SafeSubstring(index - 1, 1) != " " && p.Text.SafeSubstring(index - 1, 1) != "\r" && p.Text.SafeSubstring(index - 1, 1) != "\n")
{
p.Text = p.Text.SafeSubstring(0, index) + " " + p.Text.SafeSubstring(index);
index += 1;
}
else if (addNewLines && p.Text.SafeSubstring(index - 1, 1) != "\r" && p.Text.SafeSubstring(index - 1, 1) != "\n")
{
p.Text = p.Text.SafeSubstring(0, index) + Environment.NewLine + p.Text.SafeSubstring(index);
index += Environment.NewLine.Length;
}
p.Text = p.Text.SafeSubstring(0, index) + dash + p.Text.SafeSubstring(index);
index += dash.Length;
firstLineAdded = true;
}
if (!addNewLines && p.Text.Substring(index - 1, 1) != " " && p.Text.Substring(index - 1, 1) != "\r" && p.Text.Substring(index - 1, 1) != "\n")
{
p.Text = p.Text.Substring(0, index) + " " + p.Text.Substring(index);
index += 1;
}
else if (addNewLines && p.Text.Substring(index - 1, 1) != "\r" && p.Text.Substring(index - 1, 1) != "\n")
{
p.Text = p.Text.Substring(0, index) + Environment.NewLine + p.Text.Substring(index);
index += Environment.NewLine.Length;
}
p.Text = p.Text.Substring(0, index) + dash + p.Text.Substring(index);
index += dash.Length;
currentColor = newColor;
}
@ -69,14 +74,14 @@ namespace Nikse.SubtitleEdit.Core.Common
endOfColor = false;
}
else if (index + "</font>".Length <= p.Text.Length && p.Text.Substring(index, "</font>".Length).ToLowerInvariant() == "</font>")
else if (index + "</font>".Length <= p.Text.Length && p.Text.SafeSubstring(index, "</font>".Length).ToLowerInvariant() == "</font>")
{
// End of color
endOfColor = true;
index += "</font>".Length;
}
else if (index + 1 <= p.Text.Length && p.Text.Substring(index, 1) == " " || p.Text.Substring(index, 1) == "\r" || p.Text.Substring(index, 1) == "\n")
else if (index + 1 <= p.Text.Length && p.Text.SafeSubstring(index, 1) == " " || p.Text.SafeSubstring(index, 1) == "\r" || p.Text.SafeSubstring(index, 1) == "\n")
{
// Whitespace, ignore
index += 1;
@ -96,28 +101,33 @@ namespace Nikse.SubtitleEdit.Core.Common
if (currentColor != newColor)
{
if (dashFirstLine && !firstLineAdded)
// Don't insert dash if there is already a dash, but DO insert a dash if it is an interruption
if (p.Text.SafeSubstring(index, 1) != "-" && p.Text.SafeSubstring(index - 1, 1) != "-"
&& (p.Text.SafeSubstring(index - 2, 2) != "- " || p.Text.SafeSubstring(index - 3, 3) == "-- "))
{
p.Text = dash + p.Text;
if (dashFirstLine && !firstLineAdded)
{
p.Text = dash + p.Text;
index += dash.Length;
firstLineAdded = true;
}
if (!addNewLines && p.Text.SafeSubstring(index - 1, 1) != " " && p.Text.SafeSubstring(index - 1, 1) != "\r" && p.Text.SafeSubstring(index - 1, 1) != "\n")
{
p.Text = p.Text.SafeSubstring(0, index) + " " + p.Text.SafeSubstring(index);
index += 1;
}
else if (addNewLines && p.Text.SafeSubstring(index - 1, 1) != "\r" && p.Text.SafeSubstring(index - 1, 1) != "\n")
{
p.Text = p.Text.SafeSubstring(0, index) + Environment.NewLine + p.Text.SafeSubstring(index);
index += Environment.NewLine.Length;
}
p.Text = p.Text.SafeSubstring(0, index) + dash + p.Text.SafeSubstring(index);
index += dash.Length;
firstLineAdded = true;
}
if (!addNewLines && p.Text.Substring(index - 1, 1) != " " && p.Text.Substring(index - 1, 1) != "\r" && p.Text.Substring(index - 1, 1) != "\n")
{
p.Text = p.Text.Substring(0, index) + " " + p.Text.Substring(index);
index += 1;
}
else if (addNewLines && p.Text.Substring(index - 1, 1) != "\r" && p.Text.Substring(index - 1, 1) != "\n")
{
p.Text = p.Text.Substring(0, index) + Environment.NewLine + p.Text.Substring(index);
index += Environment.NewLine.Length;
}
p.Text = p.Text.Substring(0, index) + dash + p.Text.Substring(index);
index += dash.Length;
currentColor = newColor;
}
}
@ -147,6 +157,25 @@ namespace Nikse.SubtitleEdit.Core.Common
}
}
private static string SafeSubstring(this string value, int startIndex, int length = -1, string defaultValue = "")
{
try
{
if (length >= 0)
{
return value.Substring(startIndex, length);
}
else
{
return value.Substring(startIndex);
}
}
catch (ArgumentOutOfRangeException)
{
return defaultValue;
}
}
public static void ConvertColorsToDialogInSubtitle(Subtitle subtitle, bool removeColorTags, bool addNewLines, bool reBreakLines)
{
var language = LanguageAutoDetect.AutoDetectGoogleLanguage(subtitle);