From 020e95c5b2f43c2ec9bd6793e01d3a8de2bb8bc6 Mon Sep 17 00:00:00 2001 From: niksedk Date: Mon, 31 Aug 2015 20:33:40 +0200 Subject: [PATCH] Testing removal of control characters - issue #1211 and more... --- libse/StringExtensions.cs | 23 +++++++++++++++++++++++ libse/SubtitleFormats/TimedText200604.cs | 4 ++-- src/Forms/MultipleReplace.cs | 7 ++++--- 3 files changed, 29 insertions(+), 5 deletions(-) diff --git a/libse/StringExtensions.cs b/libse/StringExtensions.cs index 248a31fa8..afb469750 100644 --- a/libse/StringExtensions.cs +++ b/libse/StringExtensions.cs @@ -162,5 +162,28 @@ namespace Nikse.SubtitleEdit.Core } return false; } + + public static string RemoveControlCharacters(this string s) + { + var sb = new StringBuilder(s.Length); + foreach (var ch in s) + { + if (!Char.IsControl(ch)) + sb.Append(ch); + } + return sb.ToString(); + } + + public static string RemoveControlCharactersButWhiteSpace(this string s) + { + var sb = new StringBuilder(s.Length); + foreach (var ch in s) + { + if (!Char.IsControl(ch) || ch == '\u000d' || ch == '\u000a' || ch == '\u0009') + sb.Append(ch); + } + return sb.ToString(); + } + } } \ No newline at end of file diff --git a/libse/SubtitleFormats/TimedText200604.cs b/libse/SubtitleFormats/TimedText200604.cs index 74e061c1f..d1bec1137 100644 --- a/libse/SubtitleFormats/TimedText200604.cs +++ b/libse/SubtitleFormats/TimedText200604.cs @@ -42,7 +42,7 @@ namespace Nikse.SubtitleEdit.Core.SubtitleFormats var xml = new XmlDocument { XmlResolver = null }; try { - xml.LoadXml(xmlAsString); + xml.LoadXml(xmlAsString.RemoveControlCharactersButWhiteSpace()); var nsmgr = new XmlNamespaceManager(xml.NameTable); nsmgr.AddNamespace("ttaf1", xml.DocumentElement.NamespaceURI); @@ -152,7 +152,7 @@ namespace Nikse.SubtitleEdit.Core.SubtitleFormats var sb = new StringBuilder(); lines.ForEach(line => sb.AppendLine(line)); var xml = new XmlDocument { XmlResolver = null }; - xml.LoadXml(sb.ToString().Trim().Replace("http://www.w3.org/2006/04/ttaf1#styling\"xml:lang", "http://www.w3.org/2006/04/ttaf1#styling\" xml:lang")); + xml.LoadXml(sb.ToString().RemoveControlCharactersButWhiteSpace().Trim().Replace("http://www.w3.org/2006/04/ttaf1#styling\"xml:lang", "http://www.w3.org/2006/04/ttaf1#styling\" xml:lang")); var nsmgr = new XmlNamespaceManager(xml.NameTable); nsmgr.AddNamespace("ttaf1", xml.DocumentElement.NamespaceURI); diff --git a/src/Forms/MultipleReplace.cs b/src/Forms/MultipleReplace.cs index 3da5f07b2..802f5435c 100644 --- a/src/Forms/MultipleReplace.cs +++ b/src/Forms/MultipleReplace.cs @@ -124,7 +124,8 @@ namespace Nikse.SubtitleEdit.Forms private void ButtonAddClick(object sender, EventArgs e) { - if (textBoxFind.Text.Length > 0) + string findText = textBoxFind.Text.RemoveControlCharacters(); + if (findText.Length > 0) { string searchType = SearchTypeNormal; if (radioButtonCaseSensitive.Checked) @@ -132,7 +133,7 @@ namespace Nikse.SubtitleEdit.Forms else if (radioButtonRegEx.Checked) { searchType = SearchTypeRegularExpression; - if (!Utilities.IsValidRegex(textBoxFind.Text)) + if (!Utilities.IsValidRegex(findText)) { MessageBox.Show(Configuration.Settings.Language.General.RegularExpressionIsNotValid); textBoxFind.Select(); @@ -140,7 +141,7 @@ namespace Nikse.SubtitleEdit.Forms } } - AddToReplaceListView(true, textBoxFind.Text, textBoxReplace.Text, EnglishSearchTypeToLocal(searchType)); + AddToReplaceListView(true, findText, textBoxReplace.Text.RemoveControlCharacters(), EnglishSearchTypeToLocal(searchType)); textBoxFind.Text = string.Empty; textBoxReplace.Text = string.Empty; GeneratePreview();