Skip to content

Commit

Permalink
Fix #6440
Browse files Browse the repository at this point in the history
  • Loading branch information
niksedk committed Nov 21, 2022
1 parent 126dff0 commit 7002fe9
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 47 deletions.
11 changes: 11 additions & 0 deletions src/Test/FixCommonErrors/FixCommonErrorsTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -563,6 +563,17 @@ public void FixMissingSpacesItalicEnd()
}
}

[TestMethod]
public void FixMissingSpacesNotWhenHyphenBeforeQuote()
{
using (var target = GetFixCommonErrorsLib())
{
InitializeFixCommonErrorsLine(target, "There is a pre-\"do it\" conversation about price.");
new FixMissingSpaces().Fix(_subtitle, new EmptyFixCallback());
Assert.AreEqual("There is a pre-\"do it\" conversation about price.", _subtitle.Paragraphs[0].Text);
}
}

[TestMethod]
public void FixMissingSpacesBeforePeriod1()
{
Expand Down
95 changes: 48 additions & 47 deletions src/libse/Forms/FixCommonErrors/FixMissingSpaces.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,17 +23,17 @@ public static class Language

public void Fix(Subtitle subtitle, IFixCallbacks callbacks)
{
string languageCode = callbacks.Language;
string fixAction = Language.FixMissingSpace;
int missingSpaces = 0;
var languageCode = callbacks.Language;
var fixAction = Language.FixMissingSpace;
var missingSpaces = 0;
var dialogHelper = new DialogSplitMerge
{
DialogStyle = Configuration.Settings.General.DialogStyle,
TwoLetterLanguageCode = callbacks.Language,
ContinuationStyle = Configuration.Settings.General.ContinuationStyle
};
const string expectedChars = @"""”<.";
for (int i = 0; i < subtitle.Paragraphs.Count; i++)
for (var i = 0; i < subtitle.Paragraphs.Count; i++)
{
var p = subtitle.Paragraphs[i];

Expand All @@ -44,14 +44,14 @@ public void Fix(Subtitle subtitle, IFixCallbacks callbacks)
!p.Text.Contains("http://", StringComparison.OrdinalIgnoreCase) &&
!Url.IsMatch(p.Text)) // Skip urls.
{
string newText = FixSpaceAfter(p.Text, '؟');
var newText = FixSpaceAfter(p.Text, '؟');
newText = FixSpaceAfter(newText, '\u060C'); // Arabic comma
newText = FixSpaceAfter(newText, '.');
newText = FixSpaceAfter(newText, '!');
if (newText != p.Text && callbacks.AllowFix(p, fixAction))
{
missingSpaces++;
string oldText = p.Text;
var oldText = p.Text;
p.Text = newText;
callbacks.AddFixToListView(p, fixAction, oldText, p.Text);
}
Expand All @@ -65,7 +65,7 @@ public void Fix(Subtitle subtitle, IFixCallbacks callbacks)
var match = FixMissingSpacesReComma.Match(p.Text);
while (match.Success)
{
bool doFix = !expectedChars.Contains(p.Text[match.Index + 2]);
var doFix = !expectedChars.Contains(p.Text[match.Index + 2]);

if (doFix && languageCode == "el" &&
(p.Text.Substring(match.Index).StartsWith("ό,τι", StringComparison.Ordinal) ||
Expand All @@ -80,14 +80,14 @@ public void Fix(Subtitle subtitle, IFixCallbacks callbacks)
if (doFix && callbacks.AllowFix(p, fixAction))
{
missingSpaces++;
string oldText = p.Text;
var oldText = p.Text;
p.Text = p.Text.Replace(match.Value, match.Value[0] + ", " + match.Value[match.Value.Length - 1]);
callbacks.AddFixToListView(p, fixAction, oldText, p.Text);
}
match = match.NextMatch();
}

bool allowFix = callbacks.AllowFix(p, fixAction);
var allowFix = callbacks.AllowFix(p, fixAction);

// missing space after "?"
match = FixMissingSpacesReQuestionMark.Match(p.Text);
Expand All @@ -96,7 +96,7 @@ public void Fix(Subtitle subtitle, IFixCallbacks callbacks)
if (allowFix && !@"""<".Contains(p.Text[match.Index + 2]))
{
missingSpaces++;
string oldText = p.Text;
var oldText = p.Text;
p.Text = p.Text.Replace(match.Value, match.Value[0] + "? " + match.Value[match.Value.Length - 1]);
callbacks.AddFixToListView(p, fixAction, oldText, p.Text);
}
Expand All @@ -110,7 +110,7 @@ public void Fix(Subtitle subtitle, IFixCallbacks callbacks)
if (allowFix && !@"""<".Contains(p.Text[match.Index + 2]))
{
missingSpaces++;
string oldText = p.Text;
var oldText = p.Text;
p.Text = p.Text.Replace(match.Value, match.Value[0] + "! " + match.Value[match.Value.Length - 1]);
callbacks.AddFixToListView(p, fixAction, oldText, p.Text);
}
Expand All @@ -121,22 +121,22 @@ public void Fix(Subtitle subtitle, IFixCallbacks callbacks)
match = FixMissingSpacesReColon.Match(p.Text);
while (match.Success)
{
int start = match.Index;
var start = match.Index;
start -= 4;
if (start < 0)
{
start = 0;
}

int indexOfStartCodeTag = p.Text.IndexOf('{', start);
int indexOfEndCodeTag = p.Text.IndexOf('}', start);
var indexOfStartCodeTag = p.Text.IndexOf('{', start);
var indexOfEndCodeTag = p.Text.IndexOf('}', start);
if (indexOfStartCodeTag >= 0 && indexOfEndCodeTag >= 0 && indexOfStartCodeTag < match.Index)
{
// we are inside a tag: like indexOfEndCodeTag "{y:i}Is this italic?"
}
else if (allowFix && !@"""<".Contains(p.Text[match.Index + 2]))
{
bool skipSwedishOrFinish = false;
var skipSwedishOrFinish = false;
if (languageCode == "sv" || languageCode == "fi")
{
var m = FixMissingSpacesReColonWithAfter.Match(p.Text, match.Index);
Expand All @@ -145,7 +145,7 @@ public void Fix(Subtitle subtitle, IFixCallbacks callbacks)
if (!skipSwedishOrFinish)
{
missingSpaces++;
string oldText = p.Text;
var oldText = p.Text;
p.Text = p.Text.Replace(match.Value, match.Value[0] + ": " + match.Value[match.Value.Length - 1]);
callbacks.AddFixToListView(p, fixAction, oldText, p.Text);
}
Expand All @@ -161,9 +161,9 @@ public void Fix(Subtitle subtitle, IFixCallbacks callbacks)
!p.Text.Contains("http://", StringComparison.OrdinalIgnoreCase) &&
!Url.IsMatch(p.Text)) // Skip urls.
{
bool isMatchAbbreviation = false;
var isMatchAbbreviation = false;

string word = GetWordFromIndex(p.Text, match.Index);
var word = GetWordFromIndex(p.Text, match.Index);
if (Utilities.CountTagInText(word, '.') > 1)
{
isMatchAbbreviation = true;
Expand All @@ -182,7 +182,7 @@ public void Fix(Subtitle subtitle, IFixCallbacks callbacks)
if (!isMatchAbbreviation && callbacks.AllowFix(p, fixAction))
{
missingSpaces++;
string oldText = p.Text;
var oldText = p.Text;
p.Text = p.Text.Replace(match.Value, match.Value.Replace(".", ". "));
callbacks.AddFixToListView(p, fixAction, oldText, p.Text);
}
Expand All @@ -196,7 +196,7 @@ public void Fix(Subtitle subtitle, IFixCallbacks callbacks)
if (newText != p.Text && callbacks.AllowFix(p, fixAction))
{
missingSpaces++;
string oldText = p.Text;
var oldText = p.Text;
p.Text = newText;
callbacks.AddFixToListView(p, fixAction, oldText, p.Text);
}
Expand All @@ -205,16 +205,17 @@ public void Fix(Subtitle subtitle, IFixCallbacks callbacks)
//fix missing spaces before/after quotes - Get a"get out of jail free"card. -> Get a "get out of jail free" card.
if (Utilities.CountTagInText(p.Text, '"') == 2)
{
int start = p.Text.IndexOf('"');
int end = p.Text.LastIndexOf('"');
string quote = p.Text.Substring(start, end - start + 1);
var start = p.Text.IndexOf('"');
var end = p.Text.LastIndexOf('"');
var quote = p.Text.Substring(start, end - start + 1);
if (!quote.Contains(Environment.NewLine))
{
string newText = p.Text;
int indexOfFontTag = newText.IndexOf("<font ", StringComparison.OrdinalIgnoreCase);
bool isAfterAssTag = newText.Contains("{\\") && start > 0 && newText[start - 1] == '}';
bool isAfterEllipsis = start >= 3 && newText.Substring(start - 3, 3) == "...";
if (!isAfterAssTag && !isAfterEllipsis && start > 0 && !(Environment.NewLine + @" >[(♪♫¿").Contains(p.Text[start - 1]))
var newText = p.Text;
var indexOfFontTag = newText.IndexOf("<font ", StringComparison.OrdinalIgnoreCase);
var isAfterAssTag = newText.Contains("{\\") && start > 0 && newText[start - 1] == '}';
var isAfterHyphen = start > 0 && newText[start - 1] == '-';
var isAfterEllipsis = start >= 3 && newText.Substring(start - 3, 3) == "...";
if (!isAfterAssTag && !isAfterEllipsis && !isAfterHyphen && start > 0 && !(Environment.NewLine + @" >[(♪♫¿").Contains(p.Text[start - 1]))
{
if (indexOfFontTag < 0 || start > newText.IndexOf('>', indexOfFontTag)) // font tags can contain "
{
Expand All @@ -232,7 +233,7 @@ public void Fix(Subtitle subtitle, IFixCallbacks callbacks)
if (newText != p.Text && callbacks.AllowFix(p, fixAction))
{
missingSpaces++;
string oldText = p.Text;
var oldText = p.Text;
p.Text = newText;
callbacks.AddFixToListView(p, fixAction, oldText, p.Text);
}
Expand Down Expand Up @@ -260,7 +261,7 @@ public void Fix(Subtitle subtitle, IFixCallbacks callbacks)
}
}
}
string newText = string.Join(Environment.NewLine, lines);
var newText = string.Join(Environment.NewLine, lines);
if (newText != p.Text && callbacks.AllowFix(p, fixAction))
{
missingSpaces++;
Expand All @@ -271,10 +272,10 @@ public void Fix(Subtitle subtitle, IFixCallbacks callbacks)
}

//fix missing spaces in "Hey...move it!" to "Hey... move it!"
int index = p.Text.IndexOf("...", StringComparison.Ordinal);
var index = p.Text.IndexOf("...", StringComparison.Ordinal);
if (index > 0 && p.Text.Length > 5)
{
string newText = p.Text;
var newText = p.Text;
while (index != -1)
{
if (newText.Length > index + 4 && index >= 1)
Expand All @@ -290,7 +291,7 @@ public void Fix(Subtitle subtitle, IFixCallbacks callbacks)
if (newText != p.Text && callbacks.AllowFix(p, fixAction))
{
missingSpaces++;
string oldText = p.Text;
var oldText = p.Text;
p.Text = newText;
callbacks.AddFixToListView(p, fixAction, oldText, p.Text);
}
Expand All @@ -300,7 +301,7 @@ public void Fix(Subtitle subtitle, IFixCallbacks callbacks)
index = p.Text.IndexOf("<i>", StringComparison.OrdinalIgnoreCase);
if (index >= 0 && p.Text.Length > 5)
{
string newText = p.Text;
var newText = p.Text;
while (index != -1)
{
if (newText.Length > index + 6 && index > 1)
Expand All @@ -316,7 +317,7 @@ public void Fix(Subtitle subtitle, IFixCallbacks callbacks)
if (newText != p.Text && callbacks.AllowFix(p, fixAction))
{
missingSpaces++;
string oldText = p.Text;
var oldText = p.Text;
p.Text = newText;
callbacks.AddFixToListView(p, fixAction, oldText, p.Text);
}
Expand All @@ -326,7 +327,7 @@ public void Fix(Subtitle subtitle, IFixCallbacks callbacks)
index = p.Text.IndexOf("</i>", StringComparison.OrdinalIgnoreCase);
if (index > 3 && p.Text.Length > 5)
{
string newText = p.Text;
var newText = p.Text;
while (index != -1)
{
if (newText.Length > index + 6 && index > 1)
Expand All @@ -342,7 +343,7 @@ public void Fix(Subtitle subtitle, IFixCallbacks callbacks)
if (newText != p.Text && callbacks.AllowFix(p, fixAction))
{
missingSpaces++;
string oldText = p.Text;
var oldText = p.Text;
p.Text = newText;
callbacks.AddFixToListView(p, fixAction, oldText, p.Text);
}
Expand All @@ -357,7 +358,7 @@ public void Fix(Subtitle subtitle, IFixCallbacks callbacks)
while (matchFontStart.Success && jumpOut < 10)
{
p.Text = p.Text.Insert(matchFontStart.Index + 1, " ");
matchFontStart = regexFontStart.Match(p.Text, matchFontStart.Index+1);
matchFontStart = regexFontStart.Match(p.Text, matchFontStart.Index + 1);
jumpOut++;
}

Expand All @@ -370,8 +371,8 @@ public void Fix(Subtitle subtitle, IFixCallbacks callbacks)

if (callbacks.Language == "fr") // special rules for French
{
string newText = p.Text;
int j = 1;
var newText = p.Text;
var j = 1;
while (j < newText.Length)
{
if (@"!?:;".Contains(newText[j]) && char.IsLetter(newText[j - 1]))
Expand All @@ -383,7 +384,7 @@ public void Fix(Subtitle subtitle, IFixCallbacks callbacks)
if (newText != p.Text && callbacks.AllowFix(p, fixAction))
{
missingSpaces++;
string oldText = p.Text;
var oldText = p.Text;
p.Text = newText;
callbacks.AddFixToListView(p, fixAction, oldText, p.Text);
}
Expand All @@ -403,10 +404,10 @@ private static string FixSpaceAfter(string input, char fixSpaceAfterChar)
return text;
}

bool skip = false;
var skip = false;
if (fixSpaceAfterChar == '.')
{
string word = GetWordFromIndex(text, idx);
var word = GetWordFromIndex(text, idx);
if (word.Contains('@')) // skip emails
{
skip = true;
Expand Down Expand Up @@ -509,8 +510,8 @@ private static string GetWordFromIndex(string text, int index)
return string.Empty;
}

int endIndex = index;
for (int i = index; i < text.Length; i++)
var endIndex = index;
for (var i = index; i < text.Length; i++)
{
if ((@" " + Environment.NewLine).Contains(text[i]))
{
Expand All @@ -520,8 +521,8 @@ private static string GetWordFromIndex(string text, int index)
endIndex = i;
}

int startIndex = index;
for (int i = index; i >= 0; i--)
var startIndex = index;
for (var i = index; i >= 0; i--)
{
if ((@" " + Environment.NewLine).Contains(text[i]))
{
Expand Down

0 comments on commit 7002fe9

Please sign in to comment.