Improve "RemoveChar" + fix total pixel width label

This commit is contained in:
Nikolaj Olsson 2021-02-06 21:01:38 +01:00
parent adb37cef18
commit 20b4314d21
21 changed files with 90 additions and 72 deletions

View File

@ -200,6 +200,22 @@ namespace Test.Core
Assert.AreEqual("Halloworld!", res);
}
[TestMethod]
public void RemoveChar3()
{
string input = " Hallo world! ";
var res = input.RemoveChar(' ', '!');
Assert.AreEqual("Halloworld", res);
}
[TestMethod]
public void RemoveChar4()
{
string input = " Hallo world! ";
var res = input.RemoveChar(' ', '!', 'H');
Assert.AreEqual("alloworld", res);
}
[TestMethod]
public void CountLetters1()
{

View File

@ -368,9 +368,8 @@ namespace Nikse.SubtitleEdit.Core.Common
public static bool ContainsLetters(string line)
{
if (string.IsNullOrWhiteSpace(line.Replace("0", string.Empty).Replace("1", string.Empty).Replace("2", string.Empty).Replace("3", string.Empty).Replace("4", string.Empty).Replace("5", string.Empty).Replace("6", string.Empty)
.Replace("7", string.Empty).Replace("8", string.Empty).Replace("9", string.Empty).RemoveChar(':').RemoveChar('.').RemoveChar(',').
RemoveChar('-').RemoveChar('>').RemoveChar('/')))
if (string.IsNullOrWhiteSpace(line
.RemoveChar('0', '1', '2', '3', '4', '5', '6', '7', '8', '9', ':', '.', ',', '-', '>', '/')))
{
return false;
}

View File

@ -1,6 +1,7 @@
using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Text;
namespace Nikse.SubtitleEdit.Core.Common
@ -452,6 +453,38 @@ namespace Nikse.SubtitleEdit.Core.Common
return new string(array, 0, arrayIndex);
}
public static string RemoveChar(this string value, char charToRemove, char charToRemove2)
{
char[] array = new char[value.Length];
int arrayIndex = 0;
for (int i = 0; i < value.Length; i++)
{
char ch = value[i];
if (ch != charToRemove && ch != charToRemove2)
{
array[arrayIndex++] = ch;
}
}
return new string(array, 0, arrayIndex);
}
public static string RemoveChar(this string value, params char[] charsToRemove)
{
char[] array = new char[value.Length];
int arrayIndex = 0;
for (int i = 0; i < value.Length; i++)
{
char ch = value[i];
if (!charsToRemove.Contains(ch))
{
array[arrayIndex++] = ch;
}
}
return new string(array, 0, arrayIndex);
}
/// <summary>
/// Count characters excl. white spaces, ssa-tags, html-tags, control-characters, normal spaces and
/// Arabic diacritics depending on parameter.
@ -548,7 +581,7 @@ namespace Nikse.SubtitleEdit.Core.Common
var last = s[s.Length - 1];
return last == '.' || last == '!' || last == '?' || last == ']' || last == ')' || last == '…' || last == '♪' || last == '؟' ||
twoLetterLanguageCode == "el" && last == ';' || twoLetterLanguageCode == "el" && last == '\u037E' ||
last == '-' && s.Length > 3 && s.EndsWith("--", StringComparison.Ordinal) && char.IsLetter(s[s.Length-3]) ||
last == '-' && s.Length > 3 && s.EndsWith("--", StringComparison.Ordinal) && char.IsLetter(s[s.Length - 3]) ||
last == '—' && s.Length > 2 && char.IsLetter(s[s.Length - 2]);
}
}

View File

@ -487,7 +487,7 @@ namespace Nikse.SubtitleEdit.Core.Common
line = line.Replace(matches[0].ToString(), string.Empty);
line = line.Trim().TrimStart(']');
if (Utilities.IsInteger(line.RemoveChar('[').RemoveChar(']')))
if (Utilities.IsInteger(line.RemoveChar('[', ']')))
{
line = string.Empty;
}
@ -522,7 +522,7 @@ namespace Nikse.SubtitleEdit.Core.Common
line = line.Replace(matches[0].ToString(), string.Empty);
line = line.Replace(matches[1].ToString(), string.Empty);
line = line.Trim().TrimStart(']');
if (Utilities.IsInteger(line.RemoveChar('[').RemoveChar(']')))
if (Utilities.IsInteger(line.RemoveChar('[', ']')))
{
line = string.Empty;
}
@ -905,7 +905,7 @@ namespace Nikse.SubtitleEdit.Core.Common
private static string GetLineWithPerhapsOnlyNumbers(string line)
{
return line.RemoveChar(' ').RemoveChar('.').RemoveChar(',').RemoveChar('\t').RemoveChar(':').RemoveChar(';').RemoveChar('{').RemoveChar('}').RemoveChar('[').RemoveChar(']').RemoveChar('-').RemoveChar('>').RemoveChar('<');
return line.RemoveChar(' ', '.', ',', '\t', ':', ';', '{', '}', '[', ']', '-', '>', '<');
}
private static TimeCode DecodeTime(string[] parts)

View File

@ -791,21 +791,7 @@ namespace Nikse.SubtitleEdit.Core.Common
if (s.StartsWith("m ", StringComparison.Ordinal))
{
var test = s.Remove(0, 2)
.RemoveChar('0')
.RemoveChar('1')
.RemoveChar('2')
.RemoveChar('3')
.RemoveChar('4')
.RemoveChar('5')
.RemoveChar('6')
.RemoveChar('7')
.RemoveChar('8')
.RemoveChar('9')
.RemoveChar('-')
.RemoveChar('l')
.RemoveChar('m')
.RemoveChar(' ')
.RemoveChar('.');
.RemoveChar('0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '-', 'l', 'm', ' ', '.');
if (test.Length == 0)
{
return string.Empty;

View File

@ -247,9 +247,7 @@ namespace Nikse.SubtitleEdit.Core.Forms.FixCommonErrors
for (var lineIndex = 0; lineIndex < lines.Count; lineIndex++)
{
var lineNoHtmlAndMusicTags = HtmlUtil.RemoveHtmlTags(lines[lineIndex], true)
.RemoveChar('#')
.RemoveChar('♪')
.RemoveChar('♫');
.RemoveChar('#', '♪', '♫');
if (lineNoHtmlAndMusicTags.Length > 1)
{
foreach (var musicSymbol in musicSymbols)

View File

@ -398,12 +398,12 @@ namespace Nikse.SubtitleEdit.Core.Forms
if (lines.Count == 2)
{
if (string.IsNullOrWhiteSpace(lines[1].RemoveChar('.').RemoveChar('?').RemoveChar('!').RemoveChar('-').RemoveChar('—')))
if (string.IsNullOrWhiteSpace(lines[1].RemoveChar('.', '?', '!', '-', '—')))
{
text = lines[0];
lines = text.SplitToLines();
}
else if (string.IsNullOrWhiteSpace(lines[0].RemoveChar('.').RemoveChar('?').RemoveChar('!').RemoveChar('-').RemoveChar('—')))
else if (string.IsNullOrWhiteSpace(lines[0].RemoveChar('.', '?', '!', '-', '—')))
{
text = lines[1];
lines = text.SplitToLines();

View File

@ -1599,21 +1599,7 @@ Format: Layer, Start, End, Style, Name, MarginL, MarginR, MarginV, Effect, Text"
if (noTags.StartsWith("m ", StringComparison.Ordinal))
{
var test = noTags.Remove(0, 2)
.RemoveChar('0')
.RemoveChar('1')
.RemoveChar('2')
.RemoveChar('3')
.RemoveChar('4')
.RemoveChar('5')
.RemoveChar('6')
.RemoveChar('7')
.RemoveChar('8')
.RemoveChar('9')
.RemoveChar('-')
.RemoveChar('l')
.RemoveChar('m')
.RemoveChar(' ')
.RemoveChar('.');
.RemoveChar('0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '-', 'l', 'm', ' ', '.');
if (test.Length == 0)
{
p.Text = string.Empty;

View File

@ -105,8 +105,8 @@ namespace Nikse.SubtitleEdit.Core.SubtitleFormats
{
string text = s.Remove(0, arr[0].Length + arr[1].Length + arr[2].Length + 2).Trim();
if (string.IsNullOrWhiteSpace(text.Replace("0", string.Empty).Replace("1", string.Empty).Replace("2", string.Empty).Replace("3", string.Empty).Replace("4", string.Empty).Replace("5", string.Empty).
Replace("6", string.Empty).Replace("7", string.Empty).Replace("8", string.Empty).Replace("9", string.Empty).RemoveChar('.').RemoveChar(':').RemoveChar(',')))
if (string.IsNullOrWhiteSpace(text
.RemoveChar('0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '.', ':', ',')))
{
_errorCount++;
}

View File

@ -54,8 +54,8 @@ namespace Nikse.SubtitleEdit.Core.SubtitleFormats
var parts = line.Split(',');
if (parts.Length >= 3)
{
var start = parts[0].RemoveChar(' ').RemoveChar('-');
var end = parts[1].RemoveChar(' ').RemoveChar('-');
var start = parts[0].RemoveChar(' ', '-');
var end = parts[1].RemoveChar(' ', '-');
string text = line.Remove(0, parts[0].Length + 1 + parts[1].Length).Replace("///", Environment.NewLine).Replace("\"", string.Empty).Trim();
if (text.StartsWith(",", StringComparison.Ordinal))
{

View File

@ -54,8 +54,8 @@ namespace Nikse.SubtitleEdit.Core.SubtitleFormats
var parts = line.Split(',');
if (parts.Length >= 3)
{
var start = parts[0].RemoveChar(' ').RemoveChar('-');
var end = parts[1].RemoveChar(' ').RemoveChar('-');
var start = parts[0].RemoveChar(' ', '-');
var end = parts[1].RemoveChar(' ', '-');
string text = line.Remove(0, parts[0].Length + 1 + parts[1].Length).Replace("///", Environment.NewLine).Replace("\"", string.Empty).Trim();
if (text.StartsWith(",", StringComparison.Ordinal))
{

View File

@ -140,11 +140,11 @@ namespace Nikse.SubtitleEdit.Core.SubtitleFormats
}
else if (line != null && line.TrimStart().StartsWith("$VertAlign", StringComparison.OrdinalIgnoreCase))
{
verticalAlign = line.RemoveChar(' ').RemoveChar('\t');
verticalAlign = line.RemoveChar(' ', '\t');
}
else if (line != null && line.TrimStart().StartsWith("$HorzAlign", StringComparison.OrdinalIgnoreCase))
{
horizontalAlign = line.RemoveChar(' ').RemoveChar('\t');
horizontalAlign = line.RemoveChar(' ', '\t');
}
else if (line.Replace(" ", string.Empty).Equals("$Italic=True", StringComparison.OrdinalIgnoreCase))
{

View File

@ -89,11 +89,11 @@ namespace Nikse.SubtitleEdit.Core.SubtitleFormats
}
else if (line != null && line.TrimStart().StartsWith("$VertAlign", StringComparison.OrdinalIgnoreCase))
{
verticalAlign = line.RemoveChar(' ').RemoveChar('\t');
verticalAlign = line.RemoveChar(' ', '\t');
}
else if (line != null && line.TrimStart().StartsWith("$HorzAlign", StringComparison.OrdinalIgnoreCase))
{
horizontalAlign = line.RemoveChar(' ').RemoveChar('\t');
horizontalAlign = line.RemoveChar(' ', '\t');
}
else if (line.Replace(" ", string.Empty).Equals("$Italic=True", StringComparison.OrdinalIgnoreCase))
{

View File

@ -73,11 +73,11 @@ namespace Nikse.SubtitleEdit.Core.SubtitleFormats
}
else if (line != null && line.TrimStart().StartsWith("$VertAlign", StringComparison.OrdinalIgnoreCase))
{
verticalAlign = line.RemoveChar(' ').RemoveChar('\t');
verticalAlign = line.RemoveChar(' ', '\t');
}
else if (line != null && line.TrimStart().StartsWith("$HorzAlign", StringComparison.OrdinalIgnoreCase))
{
horizontalAlign = line.RemoveChar(' ').RemoveChar('\t');
horizontalAlign = line.RemoveChar(' ', '\t');
}
}
}

View File

@ -88,11 +88,11 @@ namespace Nikse.SubtitleEdit.Core.SubtitleFormats
}
else if (line != null && line.TrimStart().StartsWith("$VertAlign", StringComparison.OrdinalIgnoreCase))
{
verticalAlign = line.RemoveChar(' ').RemoveChar('\t');
verticalAlign = line.RemoveChar(' ', '\t');
}
else if (line != null && line.TrimStart().StartsWith("$HorzAlign", StringComparison.OrdinalIgnoreCase))
{
horizontalAlign = line.RemoveChar(' ').RemoveChar('\t');
horizontalAlign = line.RemoveChar(' ', '\t');
}
else if (line.Replace(" ", string.Empty).Equals("$Italic=True", StringComparison.OrdinalIgnoreCase))
{

View File

@ -94,11 +94,11 @@ namespace Nikse.SubtitleEdit.Core.SubtitleFormats
}
else if (line.TrimStart().StartsWith("$VertAlign", StringComparison.OrdinalIgnoreCase))
{
verticalAlign = line.RemoveChar(' ').RemoveChar('\t');
verticalAlign = line.RemoveChar(' ', '\t');
}
else if (line.TrimStart().StartsWith("$HorzAlign", StringComparison.OrdinalIgnoreCase))
{
horizontalAlign = line.RemoveChar(' ').RemoveChar('\t');
horizontalAlign = line.RemoveChar(' ', '\t');
}
else if (line.Replace(" ", string.Empty).Equals("$Italic=True", StringComparison.OrdinalIgnoreCase))
{

View File

@ -133,11 +133,11 @@ $ColorIndex4 = 3
}
else if (line.TrimStart().StartsWith("$VertAlign", StringComparison.OrdinalIgnoreCase))
{
verticalAlign = line.RemoveChar(' ').RemoveChar('\t');
verticalAlign = line.RemoveChar(' ', '\t');
}
else if (line.TrimStart().StartsWith("$HorzAlign", StringComparison.OrdinalIgnoreCase))
{
horizontalAlign = line.RemoveChar(' ').RemoveChar('\t');
horizontalAlign = line.RemoveChar(' ', '\t');
}
else if (!string.IsNullOrWhiteSpace(line) && !line.StartsWith("//", StringComparison.Ordinal) && !line.StartsWith('$'))
{

View File

@ -158,11 +158,11 @@ $TapeOffset = FALSE
}
else if (line.TrimStart().StartsWith("$VertAlign", StringComparison.OrdinalIgnoreCase))
{
verticalAlign = line.RemoveChar(' ').RemoveChar('\t');
verticalAlign = line.RemoveChar(' ', '\t');
}
else if (line.TrimStart().StartsWith("$HorzAlign", StringComparison.OrdinalIgnoreCase))
{
horizontalAlign = line.RemoveChar(' ').RemoveChar('\t');
horizontalAlign = line.RemoveChar(' ', '\t');
}
else if (!string.IsNullOrWhiteSpace(line) && !line.StartsWith("//", StringComparison.Ordinal) && !line.StartsWith('$'))
{

View File

@ -170,7 +170,7 @@ namespace Nikse.SubtitleEdit.Controls
return new TimeCode();
}
if (string.IsNullOrWhiteSpace(maskedTextBox1.Text.RemoveChar('.').Replace(CultureInfo.CurrentCulture.NumberFormat.NumberDecimalSeparator, string.Empty).RemoveChar(',').RemoveChar(':')))
if (string.IsNullOrWhiteSpace(maskedTextBox1.Text.RemoveChar('.').Replace(CultureInfo.CurrentCulture.NumberFormat.NumberDecimalSeparator, string.Empty).RemoveChar(',', ':')))
{
return new TimeCode(TimeCode.MaxTimeTotalMilliseconds);
}

View File

@ -9241,7 +9241,7 @@ namespace Nikse.SubtitleEdit.Forms
{
if (Configuration.Settings.Tools.ListViewSyntaxColorWideLines)
{
var totalLengthPixels = TextWidth.CalcPixelWidth(text);
var totalLengthPixels = TextWidth.CalcPixelWidth(text.RemoveChar('\r', '\n'));
lineTotal.Text = string.Format(_languageGeneral.TotalLengthX, string.Format("{0} {1}", len, totalLengthPixels));
}
else
@ -9260,7 +9260,7 @@ namespace Nikse.SubtitleEdit.Forms
{
if (Configuration.Settings.Tools.ListViewSyntaxColorWideLines)
{
var totalLengthPixels = TextWidth.CalcPixelWidth(text);
var totalLengthPixels = TextWidth.CalcPixelWidth(text.RemoveChar('\r', '\n'));
lineTotal.Text = string.Format(_languageGeneral.TotalLengthX, string.Format("{0} {1}", len, totalLengthPixels));
}
else
@ -9276,7 +9276,7 @@ namespace Nikse.SubtitleEdit.Forms
{
if (Configuration.Settings.Tools.ListViewSyntaxColorWideLines)
{
var totalLengthPixels = TextWidth.CalcPixelWidth(text);
var totalLengthPixels = TextWidth.CalcPixelWidth(text.RemoveChar('\r', '\n'));
lineTotal.Text = string.Format(_languageGeneral.TotalLengthXSplitLine, string.Format("{0} {1}", len, totalLengthPixels));
}
else
@ -9291,7 +9291,7 @@ namespace Nikse.SubtitleEdit.Forms
{
if (Configuration.Settings.Tools.ListViewSyntaxColorWideLines)
{
var totalLengthPixels = TextWidth.CalcPixelWidth(text);
var totalLengthPixels = TextWidth.CalcPixelWidth(text.RemoveChar('\r', '\n'));
lineTotal.Text = string.Format(_languageGeneral.TotalLengthX, string.Format("{0} {1}", len, totalLengthPixels));
}
else
@ -9315,7 +9315,7 @@ namespace Nikse.SubtitleEdit.Forms
{
if (Configuration.Settings.Tools.ListViewSyntaxColorWideLines)
{
var totalLengthPixels = TextWidth.CalcPixelWidth(s);
var totalLengthPixels = TextWidth.CalcPixelWidth(s.RemoveChar('\r', '\n'));
lineTotal.Text = string.Format(_languageGeneral.TotalLengthX, string.Format("{0} {1}", len, totalLengthPixels));
}
else
@ -26054,7 +26054,7 @@ namespace Nikse.SubtitleEdit.Forms
if (Configuration.Settings.Tools.ListViewSyntaxColorWideLines)
{
var totalLengthPixels = TextWidth.CalcPixelWidth(textNoHtml);
var totalLengthPixels = TextWidth.CalcPixelWidth(textNoHtml.RemoveChar('\r', '\n'));
totalL = " " + string.Format(_languageGeneral.TotalLengthX, string.Format("{0} {1}", totalLength, totalLengthPixels));
}
else

View File

@ -8877,7 +8877,7 @@ namespace Nikse.SubtitleEdit.Forms.Ocr
return;
}
s = s.RemoveChar('?').RemoveChar('/').RemoveChar('*').RemoveChar('\\');
s = s.RemoveChar('?', '/', '*', '\\');
if (string.IsNullOrEmpty(s))
{
return;