Improve code/tests regarding "Change casing"

Also related to  #2664
This commit is contained in:
Nikolaj Olsson 2018-01-02 21:38:03 +01:00
parent 6e82151468
commit bd29b30e80
2 changed files with 130 additions and 42 deletions

View File

@ -120,7 +120,7 @@ namespace Nikse.SubtitleEdit.Core
int idName = 0;
foreach (string name in nameList)
{
int start = lower.IndexOf(name, StringComparison.OrdinalIgnoreCase);
int start = lower.IndexOf(name.ToLowerInvariant(), StringComparison.Ordinal);
while (start >= 0 && start < lower.Length)
{
bool startOk = (start == 0) || (lower[start - 1] == ' ') || (lower[start - 1] == '-') ||
@ -204,6 +204,19 @@ namespace Nikse.SubtitleEdit.Core
!StrippedText.StartsWith("http", StringComparison.OrdinalIgnoreCase))
{
StrippedText = char.ToUpper(StrippedText[0]) + StrippedText.Substring(1);
if (StrippedText.StartsWith("_@", StringComparison.Ordinal))
{
for (int i = 0; i < replaceIds.Count; i++)
{
string id = $"_@{i}_";
if (StrippedText.StartsWith(id, StringComparison.Ordinal))
{
if (!string.IsNullOrEmpty(originalNames[i]))
originalNames[i] = char.ToUpper(originalNames[i][0]) + originalNames[i].Remove(0, 1);
break;
}
}
}
}
}
}
@ -212,7 +225,7 @@ namespace Nikse.SubtitleEdit.Core
{
const string breakAfterChars = @".!?:;)]}([{";
const string expectedChars = "\"`´'()<>!?.- \r\n";
var sb = new StringBuilder();
var sb = new StringBuilder(StrippedText.Length);
bool lastWasBreak = false;
for (int i = 0; i < StrippedText.Length; i++)
{
@ -246,6 +259,21 @@ namespace Nikse.SubtitleEdit.Core
{
lastWasBreak = false;
sb.Append(char.ToUpper(s));
if (StrippedText.Substring(i).StartsWith("_@", StringComparison.Ordinal))
{
var ks = StrippedText.Substring(i);
for (int k = 0; k < replaceIds.Count; k++)
{
string id = $"_@{k}_";
if (ks.StartsWith(id, StringComparison.Ordinal))
{
if (!string.IsNullOrEmpty(originalNames[k]))
originalNames[k] = char.ToUpper(originalNames[k][0]) + originalNames[k].Remove(0, 1);
break;
}
}
}
}
}
}
@ -258,9 +286,17 @@ namespace Nikse.SubtitleEdit.Core
if (s == ']' && idx > 1)
{ // I [Motor roaring] love you!
string temp = sb.ToString(0, idx - 1).Trim();
if (temp.Length > 0 && !char.IsLower(temp[temp.Length - 1]))
if (temp.Length > 0 && !char.IsLetterOrDigit(temp[temp.Length - 1]))
lastWasBreak = true;
}
else if (s == ']' && idx == -1 && Pre.Contains('['))
{ // [ Motor roaring ] Hallo!
lastWasBreak = true;
}
else if (s == ':') // seems to be the rule (in subtitles) to nearly always capitalize first letter efter semicolon
{
lastWasBreak = true;
}
else
{
idx = sb.ToString().LastIndexOf(' ');
@ -270,6 +306,25 @@ namespace Nikse.SubtitleEdit.Core
}
}
}
else if (s == '-' && Pre.Contains("-"))
{
if (sb.ToString().EndsWith(Environment.NewLine + "-"))
{
var prevLine = HtmlUtil.RemoveHtmlTags(sb.ToString().Substring(0, sb.Length - 2).TrimEnd());
if (prevLine.EndsWith('.') ||
prevLine.EndsWith('!') ||
prevLine.EndsWith('?') ||
prevLine.EndsWith(". ♪", StringComparison.Ordinal) ||
prevLine.EndsWith("! ♪", StringComparison.Ordinal) ||
prevLine.EndsWith("? ♪", StringComparison.Ordinal) ||
prevLine.EndsWith(']') ||
prevLine.EndsWith(')') ||
prevLine.EndsWith(':'))
{
lastWasBreak = true;
}
}
}
}
}
StrippedText = sb.ToString();

View File

@ -1,5 +1,6 @@
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Nikse.SubtitleEdit.Core;
using System;
namespace Test.Logic
{
@ -11,108 +12,108 @@ namespace Test.Logic
public void StrippableTextItalic()
{
var st = new StrippableText("<i>Hi!</i>");
Assert.AreEqual(st.Pre, "<i>");
Assert.AreEqual(st.Post, "!</i>");
Assert.AreEqual(st.StrippedText, "Hi");
Assert.AreEqual("<i>", st.Pre);
Assert.AreEqual("!</i>", st.Post);
Assert.AreEqual("Hi", st.StrippedText);
}
[TestMethod]
public void StrippableTextAss()
{
var st = new StrippableText("{\\an9}Hi!");
Assert.AreEqual(st.Pre, "{\\an9}");
Assert.AreEqual(st.Post, "!");
Assert.AreEqual(st.StrippedText, "Hi");
Assert.AreEqual("{\\an9}", st.Pre);
Assert.AreEqual("!", st.Post);
Assert.AreEqual("Hi", st.StrippedText);
}
[TestMethod]
public void StrippableTextFont()
{
var st = new StrippableText("<font color=\"red\">Hi!</font>");
Assert.AreEqual(st.Pre, "<font color=\"red\">");
Assert.AreEqual(st.Post, "!</font>");
Assert.AreEqual(st.StrippedText, "Hi");
Assert.AreEqual("<font color=\"red\">", st.Pre);
Assert.AreEqual("!</font>", st.Post);
Assert.AreEqual("Hi", st.StrippedText);
}
[TestMethod]
public void StrippableTextItalic2()
{
var st = new StrippableText("<i>O</i>");
Assert.AreEqual(st.Pre, "<i>");
Assert.AreEqual(st.Post, "</i>");
Assert.AreEqual(st.StrippedText, "O");
Assert.AreEqual("<i>", st.Pre);
Assert.AreEqual("</i>", st.Post);
Assert.AreEqual("O", st.StrippedText);
}
[TestMethod]
public void StrippableTextItalic3()
{
var st = new StrippableText("<i>Hi!");
Assert.AreEqual(st.Pre, "<i>");
Assert.AreEqual(st.Post, "!");
Assert.AreEqual(st.StrippedText, "Hi");
Assert.AreEqual("<i>", st.Pre);
Assert.AreEqual("!", st.Post);
Assert.AreEqual("Hi", st.StrippedText);
}
[TestMethod]
public void StrippableTextFontDontTouch()
{
var st = new StrippableText("{MAN} Hi, how are you today!");
Assert.AreEqual(st.Pre, "");
Assert.AreEqual(st.Post, "!");
Assert.AreEqual(st.StrippedText, "{MAN} Hi, how are you today");
Assert.AreEqual("", st.Pre);
Assert.AreEqual("!", st.Post);
Assert.AreEqual("{MAN} Hi, how are you today", st.StrippedText);
}
[TestMethod]
public void StrippableOnlyPre()
{
var st = new StrippableText("(");
Assert.AreEqual(st.Pre, "(");
Assert.AreEqual(st.Post, "");
Assert.AreEqual(st.StrippedText, "");
Assert.AreEqual("(", st.Pre);
Assert.AreEqual("", st.Post);
Assert.AreEqual("", st.StrippedText);
}
[TestMethod]
public void StrippableOnlyPre2()
{
var st = new StrippableText("<");
Assert.AreEqual(st.Pre, "");
Assert.AreEqual(st.Post, "");
Assert.AreEqual(st.StrippedText, "<");
Assert.AreEqual("", st.Pre);
Assert.AreEqual("", st.Post);
Assert.AreEqual("<", st.StrippedText);
}
[TestMethod]
public void StrippableOnlyPre3()
{
var st = new StrippableText("<i>");
Assert.AreEqual(st.Pre, "<i>");
Assert.AreEqual(st.Post, "");
Assert.AreEqual(st.StrippedText, "");
Assert.AreEqual("<i>", st.Pre);
Assert.AreEqual("", st.Post);
Assert.AreEqual("", st.StrippedText);
}
[TestMethod]
public void StrippableOnlyText()
{
var st = new StrippableText("H");
Assert.AreEqual(st.Pre, "");
Assert.AreEqual(st.Post, "");
Assert.AreEqual(st.StrippedText, "H");
Assert.AreEqual("", st.Pre);
Assert.AreEqual("", st.Post);
Assert.AreEqual("H", st.StrippedText);
}
[TestMethod]
public void StrippableTextItalicAndFont()
{
var st = new StrippableText("<i><font color=\"red\">Hi!</font></i>");
Assert.AreEqual(st.Pre, "<i><font color=\"red\">");
Assert.AreEqual(st.Post, "!</font></i>");
Assert.AreEqual(st.StrippedText, "Hi");
Assert.AreEqual("<i><font color=\"red\">", st.Pre);
Assert.AreEqual("!</font></i>", st.Post);
Assert.AreEqual("Hi", st.StrippedText);
}
[TestMethod]
public void StrippableTextItalicAndMore()
{
var st = new StrippableText("<i>...<b>Hi!</b></i>");
Assert.AreEqual(st.Pre, "<i>...<b>");
Assert.AreEqual(st.Post, "!</b></i>");
Assert.AreEqual(st.StrippedText, "Hi");
Assert.AreEqual("<i>...<b>", st.Pre);
Assert.AreEqual("!</b></i>", st.Post);
Assert.AreEqual("Hi", st.StrippedText);
}
[TestMethod]
@ -120,7 +121,7 @@ namespace Test.Logic
{
var st = new StrippableText("this is for www.nikse.dk. thank you.");
st.FixCasing(new System.Collections.Generic.List<string>(), false, true, true, "Bye.");
Assert.AreEqual(st.MergedString, "This is for www.nikse.dk. Thank you.");
Assert.AreEqual("This is for www.nikse.dk. Thank you.", st.MergedString);
}
[TestMethod]
@ -128,7 +129,7 @@ namespace Test.Logic
{
var st = new StrippableText("this is for www.nikse.dk! thank you.");
st.FixCasing(new System.Collections.Generic.List<string>(), false, true, true, "Bye.");
Assert.AreEqual(st.MergedString, "This is for www.nikse.dk! Thank you.");
Assert.AreEqual("This is for www.nikse.dk! Thank you.", st.MergedString);
}
[TestMethod]
@ -136,7 +137,39 @@ namespace Test.Logic
{
var st = new StrippableText("www.nikse.dk");
st.FixCasing(new System.Collections.Generic.List<string>(), false, true, true, "Bye.");
Assert.AreEqual(st.MergedString, "www.nikse.dk");
Assert.AreEqual("www.nikse.dk", st.MergedString);
}
[TestMethod]
public void StrippableTextChangeCasing4()
{
var st = new StrippableText("- hi joe!" + Environment.NewLine + "- hi jane.");
st.FixCasing(new System.Collections.Generic.List<string>(), false, true, true, "Bye.");
Assert.AreEqual("- Hi joe!" + Environment.NewLine + "- Hi jane.", st.MergedString);
}
[TestMethod]
public void StrippableTextChangeCasing6()
{
var st = new StrippableText("- hi joe!" + Environment.NewLine + "- hi jane.");
st.FixCasing(new System.Collections.Generic.List<string> { "Joe", "Jane" }, true, true, true, "Bye.");
Assert.AreEqual("- Hi Joe!" + Environment.NewLine + "- Hi Jane.", st.MergedString);
}
[TestMethod]
public void StrippableTextChangeCasing7()
{
var st = new StrippableText("[ newsreel narrator ] ominous clouds of war.");
st.FixCasing(new System.Collections.Generic.List<string> { "Joe", "Jane" }, true, true, true, "Bye.");
Assert.AreEqual("[ Newsreel narrator ] Ominous clouds of war.", st.MergedString);
}
[TestMethod]
public void StrippableTextChangeCasing8()
{
var st = new StrippableText("andy: dad!");
st.FixCasing(new System.Collections.Generic.List<string> { "Joe", "Jane" }, true, true, true, "Bye.");
Assert.AreEqual("Andy: Dad!", st.MergedString);
}
}