From 1caa4782d754eef6b5d1b4f8c3d0dc435e5b03df Mon Sep 17 00:00:00 2001 From: Nikolaj Olsson Date: Sun, 14 Feb 2016 20:23:11 +0100 Subject: [PATCH] Added some refact from #1566 fix #1566 --- libse/SubtitleFormats/MicroDvd.cs | 21 ++++++++++--------- .../SonyDVDArchitectLineAndDuration.cs | 18 ++-------------- libse/SubtitleFormats/SubViewer20.cs | 4 ++-- libse/TimeCode.cs | 6 +++--- libse/Utilities.cs | 3 ++- 5 files changed, 20 insertions(+), 32 deletions(-) diff --git a/libse/SubtitleFormats/MicroDvd.cs b/libse/SubtitleFormats/MicroDvd.cs index b9b50725d..7bec053d5 100644 --- a/libse/SubtitleFormats/MicroDvd.cs +++ b/libse/SubtitleFormats/MicroDvd.cs @@ -246,23 +246,23 @@ namespace Nikse.SubtitleEdit.Core.SubtitleFormats } string text = lineSb.ToString(); int noOfLines = Utilities.CountTagInText(text, '|') + 1; - if (Utilities.CountTagInText(text, "{y:i}") == noOfLines && text.StartsWith("{y:i}")) + if (Utilities.CountTagInText(text, "{y:i}") == noOfLines && text.StartsWith("{y:i}", StringComparison.Ordinal)) text = "{Y:i}" + text.Replace("{y:i}", string.Empty); - else if (Utilities.CountTagInText(text, "{y:b}") == noOfLines && text.StartsWith("{y:b}")) + else if (Utilities.CountTagInText(text, "{y:b}") == noOfLines && text.StartsWith("{y:b}", StringComparison.Ordinal)) text = "{Y:b}" + text.Replace("{y:b}", string.Empty); - else if (Utilities.CountTagInText(text, "{y:u}") == noOfLines && text.StartsWith("{y:u}")) + else if (Utilities.CountTagInText(text, "{y:u}") == noOfLines && text.StartsWith("{y:u}", StringComparison.Ordinal)) text = "{Y:u}" + text.Replace("{y:u}", string.Empty); - else if (Utilities.CountTagInText(text, "{y:u}{y:i}") == noOfLines && text.StartsWith("{y:u}{y:i}")) + else if (Utilities.CountTagInText(text, "{y:u}{y:i}") == noOfLines && text.StartsWith("{y:u}{y:i}", StringComparison.Ordinal)) text = "{Y:u}{Y:i}" + text.Replace("{y:u}", string.Empty).Replace("{y:i}", string.Empty); - else if (Utilities.CountTagInText(text, "{y:i}{y:u}") == noOfLines && text.StartsWith("{y:i}{y:u}")) + else if (Utilities.CountTagInText(text, "{y:i}{y:u}") == noOfLines && text.StartsWith("{y:i}{y:u}", StringComparison.Ordinal)) text = "{Y:i}{Y:u}" + text.Replace("{y:i}", string.Empty).Replace("{y:u}", string.Empty); - else if (Utilities.CountTagInText(text, "{y:i}{y:b}") == noOfLines && text.StartsWith("{y:i}{y:b}")) + else if (Utilities.CountTagInText(text, "{y:i}{y:b}") == noOfLines && text.StartsWith("{y:i}{y:b}", StringComparison.Ordinal)) text = "{Y:i}{Y:b}" + text.Replace("{y:i}", string.Empty).Replace("{y:b}", string.Empty); - else if (Utilities.CountTagInText(text, "{y:b}{y:i}") == noOfLines && text.StartsWith("{y:b}{y:i}")) + else if (Utilities.CountTagInText(text, "{y:b}{y:i}") == noOfLines && text.StartsWith("{y:b}{y:i}", StringComparison.Ordinal)) text = "{Y:b}{Y:i}" + text.Replace("{y:i}", string.Empty).Replace("{y:b}", string.Empty); - else if (Utilities.CountTagInText(text, "{y:b}{y:u}") == noOfLines && text.StartsWith("{y:b}{y:u}")) + else if (Utilities.CountTagInText(text, "{y:b}{y:u}") == noOfLines && text.StartsWith("{y:b}{y:u}", StringComparison.Ordinal)) text = "{Y:b}{Y:u}" + text.Replace("{y:b}", string.Empty).Replace("{y:u}", string.Empty); - else if (Utilities.CountTagInText(text, "{y:u}{y:b}") == noOfLines && text.StartsWith("{y:u}{y:b}")) + else if (Utilities.CountTagInText(text, "{y:u}{y:b}") == noOfLines && text.StartsWith("{y:u}{y:b}", StringComparison.Ordinal)) text = "{Y:u}{Y:b}" + text.Replace("{y:u}", string.Empty).Replace("{y:b}", string.Empty); if (Utilities.CountTagInText(text, "{y:i}") == 1 && noOfLines == 1) @@ -285,6 +285,7 @@ namespace Nikse.SubtitleEdit.Core.SubtitleFormats _lineNumber = 0; var lineSb = new StringBuilder(); var pre = new StringBuilder(); + char[] splitChar = { '|' }; foreach (string line in lines) { _lineNumber++; @@ -304,7 +305,7 @@ namespace Nikse.SubtitleEdit.Core.SubtitleFormats int endFrame = int.Parse(frames[1]); string post = string.Empty; - string[] parts = text.Split(new[] { '|' }, StringSplitOptions.RemoveEmptyEntries); + string[] parts = text.Split(splitChar, StringSplitOptions.RemoveEmptyEntries); int count = 0; lineSb.Clear(); diff --git a/libse/SubtitleFormats/SonyDVDArchitectLineAndDuration.cs b/libse/SubtitleFormats/SonyDVDArchitectLineAndDuration.cs index 4f5e1b89a..d69485ee7 100644 --- a/libse/SubtitleFormats/SonyDVDArchitectLineAndDuration.cs +++ b/libse/SubtitleFormats/SonyDVDArchitectLineAndDuration.cs @@ -92,8 +92,8 @@ namespace Nikse.SubtitleEdit.Core.SubtitleFormats subtitle.Paragraphs.Add(lastParagraph); var arr = line.Split('\t'); - TimeCode start = DecodeTimeCode(arr[1]); - TimeCode end = DecodeTimeCode(arr[2]); + TimeCode start = DecodeTimeCodeFrames(arr[1], SplitCharColon); + TimeCode end = DecodeTimeCodeFrames(arr[2], SplitCharColon); lastParagraph = new Paragraph(start, end, string.Empty); success = true; } @@ -113,19 +113,5 @@ namespace Nikse.SubtitleEdit.Core.SubtitleFormats subtitle.Renumber(); } - private static TimeCode DecodeTimeCode(string s) - { - var parts = s.Split(':'); - - //00:00:07:12 - string hour = parts[0]; - string minutes = parts[1]; - string seconds = parts[2]; - string frames = parts[3]; - - TimeCode tc = new TimeCode(int.Parse(hour), int.Parse(minutes), int.Parse(seconds), FramesToMillisecondsMax999(int.Parse(frames))); - return tc; - } - } } diff --git a/libse/SubtitleFormats/SubViewer20.cs b/libse/SubtitleFormats/SubViewer20.cs index 6e0345b07..369124287 100644 --- a/libse/SubtitleFormats/SubViewer20.cs +++ b/libse/SubtitleFormats/SubViewer20.cs @@ -101,13 +101,13 @@ namespace Nikse.SubtitleEdit.Core.SubtitleFormats var paragraph = new Paragraph(); ExpectingLine expecting = ExpectingLine.TimeCodes; _errorCount = 0; - + char[] splitChars = { ':', ',', '.' }; subtitle.Paragraphs.Clear(); foreach (string line in lines) { if (regexTimeCodes.IsMatch(line)) { - string[] parts = line.Split(new[] { ':', ',', '.' }, StringSplitOptions.RemoveEmptyEntries); + string[] parts = line.Split(splitChars, StringSplitOptions.RemoveEmptyEntries); if (parts.Length == 8) { try diff --git a/libse/TimeCode.cs b/libse/TimeCode.cs index b7052a646..b1d3df2d7 100644 --- a/libse/TimeCode.cs +++ b/libse/TimeCode.cs @@ -209,11 +209,11 @@ namespace Nikse.SubtitleEdit.Core public string ToShortStringHHMMSSFF() { string s = ToHHMMSSFF(); - if (s.StartsWith("0:00:")) + if (s.StartsWith("0:00:", StringComparison.Ordinal)) s = s.Remove(0, 5); - if (s.StartsWith("00:")) + if (s.StartsWith("00:", StringComparison.Ordinal)) s = s.Remove(0, 3); - if (s.StartsWith("00:")) + if (s.StartsWith("00:", StringComparison.Ordinal)) s = s.Remove(0, 3); return s; } diff --git a/libse/Utilities.cs b/libse/Utilities.cs index 459db4861..1ef411b79 100644 --- a/libse/Utilities.cs +++ b/libse/Utilities.cs @@ -2072,6 +2072,7 @@ namespace Nikse.SubtitleEdit.Core if (!string.IsNullOrEmpty(matroskaSubtitleInfo.CodecPrivate)) { bool footerOn = false; + char[] splitChars = { ':', '.' }; foreach (string line in lines) { if (footerOn) @@ -2093,7 +2094,7 @@ namespace Nikse.SubtitleEdit.Core var arr = line.Split(','); if (arr.Length > 3) { - arr = arr[1].Split(new[] { ':', '.' }); + arr = arr[1].Split(splitChars); if (arr.Length == 4) { int hour;