From ff8a832f1b01830080d76087d576d84232e35c8a Mon Sep 17 00:00:00 2001 From: Nikolaj Olsson Date: Wed, 29 Jun 2016 19:22:06 +0200 Subject: [PATCH] Some minor optimizations inspired by #1814 - thx invandrofly :) Fix #1814 --- .../FixMissingPeriodsAtEndOfLine.cs | 6 ++--- libse/Forms/RemoveTextForHI.cs | 4 +-- libse/StripableText.cs | 2 +- src/Forms/ImportText.cs | 2 +- src/Forms/SpellCheck.cs | 2 +- src/Logic/OCR/OcrFixEngine.cs | 25 ++++++++++--------- 6 files changed, 21 insertions(+), 20 deletions(-) diff --git a/libse/Forms/FixCommonErrors/FixMissingPeriodsAtEndOfLine.cs b/libse/Forms/FixCommonErrors/FixMissingPeriodsAtEndOfLine.cs index 1caff0230..9aee26ec3 100644 --- a/libse/Forms/FixCommonErrors/FixMissingPeriodsAtEndOfLine.cs +++ b/libse/Forms/FixCommonErrors/FixMissingPeriodsAtEndOfLine.cs @@ -56,7 +56,7 @@ namespace Nikse.SubtitleEdit.Core.Forms.FixCommonErrors } else if (!string.IsNullOrEmpty(nextText) && next != null && next.Text.Length > 0 && - Utilities.UppercaseLetters.Contains(nextText[0]) && + char.IsUpper(nextText[0]) && tempNoHtml.Length > 0 && !ExpectedString1.Contains(tempNoHtml[tempNoHtml.Length - 1])) { @@ -103,7 +103,7 @@ namespace Nikse.SubtitleEdit.Core.Forms.FixCommonErrors { var st = new StripableText(next.Text); if (st.StrippedText.Length > 0 && st.StrippedText != st.StrippedText.ToUpper() && - Utilities.UppercaseLetters.Contains(st.StrippedText[0])) + char.IsUpper(st.StrippedText[0])) { if (callbacks.AllowFix(p, fixAction)) { @@ -134,7 +134,7 @@ namespace Nikse.SubtitleEdit.Core.Forms.FixCommonErrors indexOfNewLine = p.Text.IndexOf(Environment.NewLine + "-", 3, StringComparison.Ordinal); if (indexOfNewLine < 0) indexOfNewLine = p.Text.IndexOf(Environment.NewLine + " -", 3, StringComparison.Ordinal); - if (indexOfNewLine > 0 && Configuration.Settings.General.UppercaseLetters.Contains(char.ToUpper(p.Text[indexOfNewLine - 1])) && callbacks.AllowFix(p, fixAction)) + if (indexOfNewLine > 0 && char.IsUpper(char.ToUpper(p.Text[indexOfNewLine - 1])) && callbacks.AllowFix(p, fixAction)) { string oldText = p.Text; diff --git a/libse/Forms/RemoveTextForHI.cs b/libse/Forms/RemoveTextForHI.cs index b114b67c1..cdf61559b 100644 --- a/libse/Forms/RemoveTextForHI.cs +++ b/libse/Forms/RemoveTextForHI.cs @@ -330,10 +330,10 @@ namespace Nikse.SubtitleEdit.Core.Forms if (arr0.Length > 0 && arr1.Length > 1) { // line continuation? - if (Utilities.LowercaseLetters.Contains(arr1[0])) // second line starts with lower case letter + if (char.IsLower(arr1[0])) // second line starts with lower case letter { char c = arr0[arr0.Length - 1]; - if (Utilities.LowercaseLetters.Contains(c) || c == ',') // first line ends with comma or lower case letter + if (char.IsLower(c) || c == ',') // first line ends with comma or lower case letter { if (!arr1Stripable.Pre.Contains("...")) { diff --git a/libse/StripableText.cs b/libse/StripableText.cs index 13411efb8..400880178 100644 --- a/libse/StripableText.cs +++ b/libse/StripableText.cs @@ -257,7 +257,7 @@ 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 && !Utilities.LowercaseLetters.Contains(temp[temp.Length - 1])) + if (temp.Length > 0 && !char.IsLower(temp[temp.Length - 1])) lastWasBreak = true; } else diff --git a/src/Forms/ImportText.cs b/src/Forms/ImportText.cs index d2ada8625..9bff13561 100644 --- a/src/Forms/ImportText.cs +++ b/src/Forms/ImportText.cs @@ -227,7 +227,7 @@ namespace Nikse.SubtitleEdit.Forms if (merge && (p.Text.TrimEnd().EndsWith('!') || p.Text.TrimEnd().EndsWith('.'))) { var st = new StripableText(p.Text); - if (st.StrippedText.Length > 0 && Utilities.UppercaseLetters.Contains(st.StrippedText[0].ToString(CultureInfo.InvariantCulture))) + if (st.StrippedText.Length > 0 && char.IsUpper(st.StrippedText[0])) merge = false; } diff --git a/src/Forms/SpellCheck.cs b/src/Forms/SpellCheck.cs index 62344754e..76bbba8ad 100644 --- a/src/Forms/SpellCheck.cs +++ b/src/Forms/SpellCheck.cs @@ -603,7 +603,7 @@ namespace Nikse.SubtitleEdit.Forms suggestions = DoSuggest(_currentWord); // TODO: 0.9.6 fails on "Lt'S" if (_languageName.StartsWith("fr_", StringComparison.Ordinal) && (_currentWord.StartsWith("I'", StringComparison.Ordinal) || _currentWord.StartsWith("I’", StringComparison.Ordinal))) { - if (_currentWord.Length > 3 && Utilities.LowercaseLetters.Contains(_currentWord[2]) && _currentSpellCheckWord.Index > 3) + if (_currentWord.Length > 3 && char.IsLower(_currentWord[2]) && _currentSpellCheckWord.Index > 3) { string ending = _currentParagraph.Text.Substring(0, _currentSpellCheckWord.Index - 1).Trim(); if (ending.Length > 1 && !".!?".Contains(ending[ending.Length - 1])) diff --git a/src/Logic/OCR/OcrFixEngine.cs b/src/Logic/OCR/OcrFixEngine.cs index cd6a0974d..d8217f1d0 100644 --- a/src/Logic/OCR/OcrFixEngine.cs +++ b/src/Logic/OCR/OcrFixEngine.cs @@ -449,22 +449,22 @@ namespace Nikse.SubtitleEdit.Logic.Ocr lastLine.EndsWith(".", StringComparison.Ordinal) || lastLine.EndsWith("!", StringComparison.Ordinal) || lastLine.EndsWith("?", StringComparison.Ordinal); if (text.StartsWith(tag.TrimStart(), StringComparison.Ordinal) && text.Length > 3) { - if (endingBeforeThis || Utilities.UppercaseLetters.Contains(text[2])) + if (endingBeforeThis || char.IsUpper(text[2])) { text = @"L" + text.Substring(1); } - else if (Utilities.LowercaseLetters.Contains(text[2])) + else if (char.IsLower(text[2])) { text = @"l" + text.Substring(1); } } else if (text.StartsWith("" + tag.TrimStart(), StringComparison.Ordinal) && text.Length > 6) { - if (endingBeforeThis || Utilities.UppercaseLetters.Contains(text[5])) + if (endingBeforeThis || char.IsUpper(text[5])) { text = text.Remove(3, 1).Insert(3, "L"); } - else if (Utilities.LowercaseLetters.Contains(text[5])) + else if (char.IsLower(text[5])) { text = text.Remove(3, 1).Insert(3, "l"); } @@ -532,11 +532,11 @@ namespace Nikse.SubtitleEdit.Logic.Ocr endingBeforeThis = string.IsNullOrEmpty(lastLine) || lastLine.EndsWith('.') || lastLine.EndsWith('!') || lastLine.EndsWith('?') || lastLine.EndsWith(".", StringComparison.Ordinal); if (start < text.Length - 8) { - if (endingBeforeThis || Utilities.UppercaseLetters.Contains(text[start + 5 + Environment.NewLine.Length])) + if (endingBeforeThis || char.IsUpper(text[start + 5 + Environment.NewLine.Length])) { text = text.Remove(start + Environment.NewLine.Length + 3, 1).Insert(start + Environment.NewLine.Length + 3, "L"); } - else if (Utilities.LowercaseLetters.Contains(text[start + 5 + Environment.NewLine.Length])) + else if (char.IsLower(text[start + 5 + Environment.NewLine.Length])) { text = text.Remove(start + Environment.NewLine.Length + 3, 1).Insert(start + Environment.NewLine.Length + 3, "l"); } @@ -783,24 +783,24 @@ namespace Nikse.SubtitleEdit.Logic.Ocr input = input.Remove(0, 1); } - if (input.Length > 2 && input[0] == '-' && Utilities.UppercaseLetters.Contains(input[1])) + if (input.Length > 2 && input[0] == '-' && char.IsUpper(input[1])) { input = input.Insert(1, " "); } - if (input.Length > 5 && input.StartsWith("-", StringComparison.Ordinal) && Utilities.UppercaseLetters.Contains(input[4])) + if (input.Length > 5 && input.StartsWith("-", StringComparison.Ordinal) && char.IsUpper(input[4])) { input = input.Insert(4, " "); } int idx = input.IndexOf(Environment.NewLine + "-", StringComparison.Ordinal); - if (idx > 0 && idx + Environment.NewLine.Length + 1 < input.Length && Utilities.UppercaseLetters.Contains(input[idx + Environment.NewLine.Length + 1])) + if (idx > 0 && idx + Environment.NewLine.Length + 1 < input.Length && char.IsUpper(input[idx + Environment.NewLine.Length + 1])) { input = input.Insert(idx + Environment.NewLine.Length + 1, " "); } idx = input.IndexOf(Environment.NewLine + "-", StringComparison.Ordinal); - if (idx > 0 && Utilities.UppercaseLetters.Contains(input[idx + Environment.NewLine.Length + 4])) + if (idx > 0 && char.IsUpper(input[idx + Environment.NewLine.Length + 4])) { input = input.Insert(idx + Environment.NewLine.Length + 4, " "); } @@ -1154,9 +1154,10 @@ namespace Nikse.SubtitleEdit.Logic.Ocr int uppercase = 0; for (int i = 0; i < word.Length; i++) { - if (Utilities.LowercaseLetters.Contains(word[i])) + var ch = word[i]; + if (char.IsLower(ch)) lowercase++; - else if (Utilities.UppercaseLetters.Contains(word[i])) + else if (char.IsUpper(ch)) uppercase++; } if (uppercase > lowercase)