Refactor - fix minor issues from codacy

This commit is contained in:
Nikolaj Olsson 2019-01-28 20:13:54 +01:00
parent 1b3314ccdf
commit 1f8daa07cd
3 changed files with 50 additions and 29 deletions

View File

@ -252,9 +252,9 @@ namespace Nikse.SubtitleEdit.Core.SubtitleFormats
return sb.ToString();
}
internal static string EncodeStyles(string text)
internal static string EncodeStyles(string input)
{
text = Utilities.RemoveSsaTags(text);
var text = Utilities.RemoveSsaTags(input);
text = text.Replace("<I>", "<i>").Replace("</I>", "</i>");
bool allItalic = text.StartsWith("<i>", StringComparison.Ordinal) && text.EndsWith("</i>", StringComparison.Ordinal) && Utilities.CountTagInText(text, "<i>") == 1;
bool allBold = text.StartsWith("<b>", StringComparison.Ordinal) && text.EndsWith("</b>", StringComparison.Ordinal) && Utilities.CountTagInText(text, "<b>") == 1;
@ -281,19 +281,23 @@ namespace Nikse.SubtitleEdit.Core.SubtitleFormats
{
return text.Replace(Environment.NewLine, "^U^B^I|^I^B^U");
}
else if (allBoldItalic)
if (allBoldItalic)
{
return text.Replace(Environment.NewLine, "^U^B^I|^I^B^U");
}
else if (allItalic)
if (allItalic)
{
return text.Replace(Environment.NewLine, "^I|^I");
}
else if (allBold)
if (allBold)
{
return text.Replace(Environment.NewLine, "^B|^B");
}
else if (allUnderline)
if (allUnderline)
{
return text.Replace(Environment.NewLine, "^U|^U");
}
@ -305,7 +309,7 @@ namespace Nikse.SubtitleEdit.Core.SubtitleFormats
{
try
{
string[] timeParts = timeString.Split(':', ';');
var timeParts = timeString.Split(':', ';');
timeCode.Hours = int.Parse(timeParts[0]);
timeCode.Minutes = int.Parse(timeParts[1]);
timeCode.Seconds = int.Parse(timeParts[2]);

View File

@ -396,9 +396,9 @@ namespace Nikse.SubtitleEdit.Core
return text;
}
private static string ReInsertHtmlTagsAndCleanUp(string s, Dictionary<int, string> htmlTags)
private static string ReInsertHtmlTagsAndCleanUp(string input, Dictionary<int, string> htmlTags)
{
s = ReInsertHtmlTags(s, htmlTags);
var s = ReInsertHtmlTags(input, htmlTags);
s = s.Replace(" " + Environment.NewLine, Environment.NewLine);
s = s.Replace(Environment.NewLine + " ", Environment.NewLine);
s = s.Replace(Environment.NewLine + "</i>", "</i>" + Environment.NewLine);
@ -2032,17 +2032,17 @@ namespace Nikse.SubtitleEdit.Core
/// <summary>
/// Remove unneeded spaces
/// </summary>
/// <param name="text">text string to remove unneeded spaces from</param>
/// <param name="input">text string to remove unneeded spaces from</param>
/// <param name="language">two letter language id string</param>
/// <returns>text with unneeded spaces removed</returns>
public static string RemoveUnneededSpaces(string text, string language)
public static string RemoveUnneededSpaces(string input, string language)
{
const char zeroWidthSpace = '\u200B';
const char zeroWidthNoBreakSpace = '\uFEFF';
const char noBreakSpace = '\u00A0';
const char operatingSystemCommand = '\u009D';
text = text.Trim();
var text = input.Trim();
int len = text.Length;
int count = 0;
char[] textChars = new char[len];
@ -2256,19 +2256,36 @@ namespace Nikse.SubtitleEdit.Core
}
else if (before.Length > 0)
{
if ((language == "en" && (after.Equals("and", StringComparison.OrdinalIgnoreCase) || after.Equals("or", StringComparison.OrdinalIgnoreCase))) ||
(language == "es" && (after.Equals("y", StringComparison.OrdinalIgnoreCase) || after.Equals("o", StringComparison.OrdinalIgnoreCase))) ||
(language == "da" && (after.Equals("og", StringComparison.OrdinalIgnoreCase) || after.Equals("eller", StringComparison.OrdinalIgnoreCase))) ||
(language == "de" && (after.Equals("und", StringComparison.OrdinalIgnoreCase) || after.Equals("oder", StringComparison.OrdinalIgnoreCase))) ||
(language == "fi" && (after.Equals("ja", StringComparison.OrdinalIgnoreCase) || after.Equals("tai", StringComparison.OrdinalIgnoreCase))) ||
(language == "fr" && (after.Equals("et", StringComparison.OrdinalIgnoreCase) || after.Equals("ou", StringComparison.OrdinalIgnoreCase))) ||
(language == "it" && (after.Equals("e", StringComparison.OrdinalIgnoreCase) || after.Equals("o", StringComparison.OrdinalIgnoreCase))) ||
(language == "nl" && (after.Equals("en", StringComparison.OrdinalIgnoreCase) || after.Equals("of", StringComparison.OrdinalIgnoreCase))) ||
(language == "pl" && (after.Equals("i", StringComparison.OrdinalIgnoreCase) || after.Equals("czy", StringComparison.OrdinalIgnoreCase))) ||
(language == "pt" && (after.Equals("e", StringComparison.OrdinalIgnoreCase) || after.Equals("ou", StringComparison.OrdinalIgnoreCase))))
{
}
else
if ((language != "en" ||
!after.Equals("and", StringComparison.OrdinalIgnoreCase) &&
!after.Equals("or", StringComparison.OrdinalIgnoreCase)) &&
(language != "es" ||
!after.Equals("y", StringComparison.OrdinalIgnoreCase) &&
!after.Equals("o", StringComparison.OrdinalIgnoreCase)) &&
(language != "da" ||
!after.Equals("og", StringComparison.OrdinalIgnoreCase) &&
!after.Equals("eller", StringComparison.OrdinalIgnoreCase)) &&
(language != "de" ||
!after.Equals("und", StringComparison.OrdinalIgnoreCase) &&
!after.Equals("oder", StringComparison.OrdinalIgnoreCase)) &&
(language != "fi" ||
!after.Equals("ja", StringComparison.OrdinalIgnoreCase) &&
!after.Equals("tai", StringComparison.OrdinalIgnoreCase)) &&
(language != "fr" ||
!after.Equals("et", StringComparison.OrdinalIgnoreCase) &&
!after.Equals("ou", StringComparison.OrdinalIgnoreCase)) &&
(language != "it" ||
!after.Equals("e", StringComparison.OrdinalIgnoreCase) &&
!after.Equals("o", StringComparison.OrdinalIgnoreCase)) &&
(language != "nl" ||
!after.Equals("en", StringComparison.OrdinalIgnoreCase) &&
!after.Equals("of", StringComparison.OrdinalIgnoreCase)) &&
(language != "pl" ||
!after.Equals("i", StringComparison.OrdinalIgnoreCase) &&
!after.Equals("czy", StringComparison.OrdinalIgnoreCase)) &&
(language != "pt" ||
!after.Equals("e", StringComparison.OrdinalIgnoreCase) &&
!after.Equals("ou", StringComparison.OrdinalIgnoreCase)))
{
text = text.Remove(idx + 1, 1);
}

View File

@ -6500,15 +6500,15 @@ namespace Nikse.SubtitleEdit.Forms.Ocr
return line;
}
private static string FixItalics(string s)
private static string FixItalics(string input)
{
int italicStartCount = Utilities.CountTagInText(s, "<i>");
int italicStartCount = Utilities.CountTagInText(input, "<i>");
if (italicStartCount == 0)
{
return s;
return input;
}
s = s.Replace(Environment.NewLine + " ", Environment.NewLine);
var s = input.Replace(Environment.NewLine + " ", Environment.NewLine);
s = s.Replace(Environment.NewLine + " ", Environment.NewLine);
s = s.Replace(" " + Environment.NewLine, Environment.NewLine);
s = s.Replace(" " + Environment.NewLine, Environment.NewLine);