More code quality improvements

This commit is contained in:
Martijn van Berkel (Flitskikker) 2020-04-11 12:08:03 +02:00
parent e8495f7f8b
commit b4aad6385b
5 changed files with 41 additions and 14 deletions

View File

@ -13,10 +13,10 @@ namespace Nikse.SubtitleEdit.Core
private static readonly string SingleQuotes = "'"; private static readonly string SingleQuotes = "'";
private static readonly string DoubleQuotes = "''"; private static readonly string DoubleQuotes = "''";
public static List<string> Prefixes = new List<string> { "...", "..", "-", "", "", "—", "…" }; public static readonly List<string> Prefixes = new List<string> { "...", "..", "-", "", "", "—", "…" };
public static List<string> Suffixes = new List<string> { "...", "..", "-", "", "", "—", "…" }; public static readonly List<string> Suffixes = new List<string> { "...", "..", "-", "", "", "—", "…" };
public static string SanitizeString(string input, bool removeDashes = true, bool removeHtml = true) public static string SanitizeString(string input, bool removeDashes)
{ {
if (string.IsNullOrEmpty(input)) if (string.IsNullOrEmpty(input))
{ {
@ -24,10 +24,7 @@ namespace Nikse.SubtitleEdit.Core
} }
string checkString = input; string checkString = input;
if (removeHtml) checkString = Regex.Replace(checkString, "<.*?>", string.Empty);
{
checkString = Regex.Replace(checkString, "<.*?>", string.Empty);
}
checkString = Regex.Replace(checkString, "\\(.*?\\)", string.Empty); checkString = Regex.Replace(checkString, "\\(.*?\\)", string.Empty);
checkString = Regex.Replace(checkString, "\\[.*?\\]", string.Empty); checkString = Regex.Replace(checkString, "\\[.*?\\]", string.Empty);
checkString = Regex.Replace(checkString, "\\{.*?\\}", string.Empty); checkString = Regex.Replace(checkString, "\\{.*?\\}", string.Empty);
@ -96,7 +93,12 @@ namespace Nikse.SubtitleEdit.Core
return checkString; return checkString;
} }
public static string ExtractParagraphOnly(string input, bool removeDashes = true) public static string SanitizeString(string input)
{
return SanitizeString(input, true);
}
public static string ExtractParagraphOnly(string input, bool removeDashes)
{ {
string checkString = input; string checkString = input;
checkString = Regex.Replace(checkString, "\\{.*?\\}", string.Empty); checkString = Regex.Replace(checkString, "\\{.*?\\}", string.Empty);
@ -111,6 +113,11 @@ namespace Nikse.SubtitleEdit.Core
return checkString; return checkString;
} }
public static string ExtractParagraphOnly(string input)
{
return ExtractParagraphOnly(input, true);
}
public static string ReplaceFirstOccurrence(string source, string find, string replace) public static string ReplaceFirstOccurrence(string source, string find, string replace)
{ {
int place = source.IndexOf(find, StringComparison.Ordinal); int place = source.IndexOf(find, StringComparison.Ordinal);
@ -137,7 +144,7 @@ namespace Nikse.SubtitleEdit.Core
return result; return result;
} }
public static bool ShouldAddSuffix(string input, ContinuationProfile profile, bool sanitize = true) public static bool ShouldAddSuffix(string input, ContinuationProfile profile, bool sanitize)
{ {
string text = sanitize ? SanitizeString(input) : input; string text = sanitize ? SanitizeString(input) : input;
@ -149,6 +156,11 @@ namespace Nikse.SubtitleEdit.Core
return false; return false;
} }
public static bool ShouldAddSuffix(string input, ContinuationProfile profile)
{
return ShouldAddSuffix(input, profile, true);
}
public static string AddSuffixIfNeeded(string originalText, ContinuationProfile profile, bool gap) public static string AddSuffixIfNeeded(string originalText, ContinuationProfile profile, bool gap)
{ {
// Get last word // Get last word
@ -223,7 +235,7 @@ namespace Nikse.SubtitleEdit.Core
return ReplaceFirstOccurrence(originalText, firstWord, newFirstWord); return ReplaceFirstOccurrence(originalText, firstWord, newFirstWord);
} }
public static string RemoveSuffix(string originalText, ContinuationProfile profile, bool addComma = false) public static string RemoveSuffix(string originalText, ContinuationProfile profile, bool addComma)
{ {
// Get last word // Get last word
string text = SanitizeString(originalText); string text = SanitizeString(originalText);
@ -249,6 +261,11 @@ namespace Nikse.SubtitleEdit.Core
return ReplaceLastOccurrence(originalText, lastWord, newLastWord); return ReplaceLastOccurrence(originalText, lastWord, newLastWord);
} }
public static string RemoveSuffix(string originalText, ContinuationProfile profile)
{
return RemoveSuffix(originalText, profile, false);
}
public static string RemovePrefix(string originalText, ContinuationProfile profile) public static string RemovePrefix(string originalText, ContinuationProfile profile)
{ {
// Get first word of the next paragraph // Get first word of the next paragraph

View File

@ -13,7 +13,11 @@ namespace Nikse.SubtitleEdit.Core.Forms.FixCommonErrors
return true; return true;
} }
public void AddFixToListView(Paragraph p, string action, string before, string after, bool isChecked = true) public void AddFixToListView(Paragraph p, string action, string before, string after)
{
}
public void AddFixToListView(Paragraph p, string action, string before, string after, bool isChecked)
{ {
} }

View File

@ -250,7 +250,7 @@ namespace Nikse.SubtitleEdit.Core.Forms.FixCommonErrors
callbacks.UpdateFixStatus(fixCount, language.FixUnnecessaryLeadingDots, language.XFixContinuationStyle); callbacks.UpdateFixStatus(fixCount, language.FixUnnecessaryLeadingDots, language.XFixContinuationStyle);
} }
private bool IsPreviewStep(IFixCallbacks callbacks) private static bool IsPreviewStep(IFixCallbacks callbacks)
{ {
return callbacks.AllowFix(new Paragraph { Number = -1 }, string.Empty); return callbacks.AllowFix(new Paragraph { Number = -1 }, string.Empty);
} }

View File

@ -7,7 +7,8 @@ namespace Nikse.SubtitleEdit.Core.Interfaces
public interface IFixCallbacks : IDoSpell public interface IFixCallbacks : IDoSpell
{ {
bool AllowFix(Paragraph p, string action); bool AllowFix(Paragraph p, string action);
void AddFixToListView(Paragraph p, string action, string before, string after, bool isChecked = true); void AddFixToListView(Paragraph p, string action, string before, string after);
void AddFixToListView(Paragraph p, string action, string before, string after, bool isChecked);
void LogStatus(string sender, string message); void LogStatus(string sender, string message);
void LogStatus(string sender, string message, bool isImportant); void LogStatus(string sender, string message, bool isImportant);
void UpdateFixStatus(int fixes, string message, string xMessage); void UpdateFixStatus(int fixes, string message, string xMessage);

View File

@ -525,7 +525,12 @@ namespace Nikse.SubtitleEdit.Forms
listView1.Items.Add(item); listView1.Items.Add(item);
} }
public void AddFixToListView(Paragraph p, string action, string before, string after, bool isChecked = true) public void AddFixToListView(Paragraph p, string action, string before, string after)
{
AddFixToListView(p, action, before, after, true);
}
public void AddFixToListView(Paragraph p, string action, string before, string after, bool isChecked)
{ {
if (_onlyListFixes) if (_onlyListFixes)
{ {