From cfa0aece783f2f7028537dcdd53afb138bf3b580 Mon Sep 17 00:00:00 2001 From: Ivandro Jao Date: Sat, 11 May 2024 14:23:05 +0100 Subject: [PATCH] Refactor tag replacement in HtmlUtil The revision refactors the replacement of invalid italic tags in the HtmlUtil FixInvalidItalicTags method. The separate string replace statements were replaced with loops that iterate over arrays of predefined invalid tag variations. This change leads to cleaner, more readable, and maintainable code. Signed-off-by: Ivandro Jao --- src/libse/Common/HtmlUtil.cs | 45 +++++++++++++----------------------- 1 file changed, 16 insertions(+), 29 deletions(-) diff --git a/src/libse/Common/HtmlUtil.cs b/src/libse/Common/HtmlUtil.cs index 788319616..180a1f341 100644 --- a/src/libse/Common/HtmlUtil.cs +++ b/src/libse/Common/HtmlUtil.cs @@ -633,6 +633,14 @@ namespace Nikse.SubtitleEdit.Core.Common return false; } + + private static readonly string[] BeginTagVariations = { "< i >", "< i>", "", "< I >", "< I>", "", "" }; + + private static readonly string[] EndTagVariations = + { + "< / i >", "< /i>", "", "< /i >", "", "", + "< / i>", "", "< / I >", "< /I>", "", "< /I >", "", "", "< / I>", "" + }; public static string FixInvalidItalicTags(string input) { @@ -651,36 +659,15 @@ namespace Nikse.SubtitleEdit.Core.Common const string beginTag = ""; const string endTag = ""; + foreach (var beginTagVariation in BeginTagVariations) + { + text = text.Replace(beginTagVariation, beginTag); + } - text = text.Replace("< i >", beginTag); - text = text.Replace("< i>", beginTag); - text = text.Replace("", beginTag); - text = text.Replace("< I >", beginTag); - text = text.Replace("< I>", beginTag); - text = text.Replace("", beginTag); - text = text.Replace("", endTag); - text = text.Replace("< /i>", endTag); - text = text.Replace("", endTag); - text = text.Replace("< /i >", endTag); - text = text.Replace("", endTag); - text = text.Replace("", endTag); - text = text.Replace("< / i>", endTag); - text = text.Replace("", endTag); - text = text.Replace("< / I >", endTag); - text = text.Replace("< /I>", endTag); - text = text.Replace("", endTag); - text = text.Replace("< /I >", endTag); - text = text.Replace("", endTag); - text = text.Replace("", endTag); - text = text.Replace("< / I>", endTag); - text = text.Replace("", beginTag); - text = text.Replace("", endTag); + foreach (var endTagVariation in EndTagVariations) + { + text = text.Replace(endTagVariation, endTag); + } text = text.Replace(" ", "_@_"); text = text.Replace(" _@_", "_@_");