From c9d61c3a9cb7ad6fd8690e17d3c752de0aab7279 Mon Sep 17 00:00:00 2001 From: niksedk Date: Mon, 23 Oct 2023 11:06:59 +0200 Subject: [PATCH] Fix hang in CsvNuendo - thx Dvid :) --- SubtitleEdit.sln.DotSettings | 1 + src/libse/SubtitleFormats/CsvNuendo.cs | 44 +++++++++++++++----------- 2 files changed, 27 insertions(+), 18 deletions(-) diff --git a/SubtitleEdit.sln.DotSettings b/SubtitleEdit.sln.DotSettings index 50eb52b41..c03f75681 100644 --- a/SubtitleEdit.sln.DotSettings +++ b/SubtitleEdit.sln.DotSettings @@ -35,6 +35,7 @@ True True True + True True True True diff --git a/src/libse/SubtitleFormats/CsvNuendo.cs b/src/libse/SubtitleFormats/CsvNuendo.cs index b64028def..33117d208 100644 --- a/src/libse/SubtitleFormats/CsvNuendo.cs +++ b/src/libse/SubtitleFormats/CsvNuendo.cs @@ -8,10 +8,10 @@ namespace Nikse.SubtitleEdit.Core.SubtitleFormats { public class CsvNuendo : SubtitleFormat { - private static readonly Regex CsvLine = new Regex("^(\"(.*)\")*,\\d+:\\d+:\\d+:\\d+,\\d+:\\d+:\\d+:\\d+,(\"(.*)\")*", RegexOptions.Compiled); - private static readonly Regex CsvLineNoQuotes = new Regex("[^\"][^,]+[^\"],\\d+:\\d+:\\d+:\\d+,\\d+:\\d+:\\d+:\\d+,[^\"][^,]+[^\"]", RegexOptions.Compiled); - private static readonly Regex CsvLineNoQuotesFirst = new Regex("[^\"][^,]+[^\"],\\d+:\\d+:\\d+:\\d+,\\d+:\\d+:\\d+:\\d+,(\"(.*)\")*", RegexOptions.Compiled); - private static readonly Regex CsvLineAllQuotes = new Regex("^(\"(.*)\")*,\"\\d+:\\d+:\\d+:\\d+\",\"\\d+:\\d+:\\d+:\\d+\",(\"(.*)\")*", RegexOptions.Compiled); + private static readonly Regex CsvLine = new Regex("^(\"(.*)\")*,\\d+:\\d+:\\d+:\\d+,\\d+:\\d+:\\d+:\\d+,(\"(.*)\")*", RegexOptions.Compiled, TimeSpan.FromSeconds(2)); + private static readonly Regex CsvLineNoQuotes = new Regex("[^\"][^,]+[^\"],\\d+:\\d+:\\d+:\\d+,\\d+:\\d+:\\d+:\\d+,[^\"][^,]+[^\"]", RegexOptions.Compiled, TimeSpan.FromSeconds(2)); + private static readonly Regex CsvLineNoQuotesFirst = new Regex("[^\"][^,]+[^\"],\\d+:\\d+:\\d+:\\d+,\\d+:\\d+:\\d+:\\d+,(\"(.*)\")*", RegexOptions.Compiled, TimeSpan.FromSeconds(2)); + private static readonly Regex CsvLineAllQuotes = new Regex("^(\"(.*)\")*,\"\\d+:\\d+:\\d+:\\d+\",\"\\d+:\\d+:\\d+:\\d+\",(\"(.*)\")*", RegexOptions.Compiled, TimeSpan.FromSeconds(2)); private const string LineFormat = "{1}{0}{2}{0}{3}{0}{4}"; private static readonly string Header = string.Format(LineFormat, ",", "\"Character\"", "\"Timecode In\"", "\"Timecode Out\"", "\"Dialogue\""); @@ -27,7 +27,7 @@ namespace Nikse.SubtitleEdit.Core.SubtitleFormats for (var index = 0; index < lines.Count; index++) { var line = lines[index]; - if (index == 0 && + if (index == 0 && (line.StartsWith(" 0 && - (CsvLine.IsMatch(line) || - CsvLineNoQuotes.IsMatch(line) || - CsvLineAllQuotes.IsMatch(line) || - CsvLineNoQuotesFirst.IsMatch(line))) + + try { - fine++; - } - else - { - errors++; - if (fine == 0 && errors > 10) + if (line.IndexOf(':') > 0 && + (CsvLine.IsMatch(line) || + CsvLineNoQuotes.IsMatch(line) || + CsvLineAllQuotes.IsMatch(line) || + CsvLineNoQuotesFirst.IsMatch(line))) { - return false; + fine++; } + else + { + errors++; + if (fine == 0 && errors > 10) + { + return false; + } + } + } + catch + { + return false; } } @@ -79,7 +87,7 @@ namespace Nikse.SubtitleEdit.Core.SubtitleFormats Paragraph p = null; foreach (var line in lines) { - if (line.Contains(':') && + if (line.Contains(':') && CsvLine.IsMatch(line) || CsvLineNoQuotes.IsMatch(line) || CsvLineAllQuotes.IsMatch(line) ||