From 7f5beac94a6071cca791242523ff2ddb0012ebd8 Mon Sep 17 00:00:00 2001 From: Jaroslav Kordula Date: Thu, 18 Oct 2018 21:26:02 +0200 Subject: [PATCH] Relpaces Regex usages with optimized syntax Based on mentione article those changes were implemented: - met tags are inserted using simple string replace - html tag replace by html amp tag using simple replace; there should be always one - cannot think of any scenario where html tag is included mutiple times - usages of static Regex methods replaced by instance calls (based on measurement in the article) + one of the Regex instance re-used --- Kentico.AcceleratedMobilePages/AmpFilter.cs | 24 ++++++++++----------- Kentico.AcceleratedMobilePages/Constants.cs | 7 ++++-- 2 files changed, 16 insertions(+), 15 deletions(-) diff --git a/Kentico.AcceleratedMobilePages/AmpFilter.cs b/Kentico.AcceleratedMobilePages/AmpFilter.cs index 84b7a88..9b5094a 100644 --- a/Kentico.AcceleratedMobilePages/AmpFilter.cs +++ b/Kentico.AcceleratedMobilePages/AmpFilter.cs @@ -78,7 +78,7 @@ private string AppendAmpHtmlLink(string finalHtml) GetDocumentPath(); string metaTag = String.Format(Constants.AMP_AMP_HTML_LINK, ampLink) + Constants.NEW_LINE; // Insert meta tag - finalHtml = Regex.Replace(finalHtml, "", metaTag + ""); + finalHtml = finalHtml.Replace("", metaTag + ""); return finalHtml; } @@ -108,21 +108,20 @@ private void RemoveRestrictedElements(HtmlDocument doc) /// Final HTML string private string PerformRegexCorrections(string finalHtml) { - // Initial HTML amp tag - replace only first occurrence - var regex = new Regex(Constants.HTML_TAG); - finalHtml = regex.Replace(finalHtml, Constants.HTML_REPLACEMENT, 1); + // Initial HTML amp tag + finalHtml = finalHtml.Replace(Constants.HTML_TAG, Constants.HTML_REPLACEMENT); // Remove conditional comments - finalHtml = Regex.Replace(finalHtml, Constants.REGEX_CONDITIONAL_COMMENTS, ""); + finalHtml = new Regex(Constants.REGEX_CONDITIONAL_COMMENTS).Replace(finalHtml, ""); // Remove restricted attributes - finalHtml = Regex.Replace(finalHtml, Constants.REGEX_ATTR_ONANY_SUFFIX, ""); - finalHtml = Regex.Replace(finalHtml, Constants.REGEX_ATTR_XML_ATTRIBUTES, ""); - finalHtml = Regex.Replace(finalHtml, Constants.REGEX_ATTR_IAMPANY_SUFFIX, ""); + finalHtml = new Regex(Constants.REGEX_ATTR_ONANY_SUFFIX).Replace(finalHtml, ""); + finalHtml = new Regex(Constants.REGEX_ATTR_XML_ATTRIBUTES).Replace(finalHtml, ""); + finalHtml = new Regex(Constants.REGEX_ATTR_IAMPANY_SUFFIX).Replace(finalHtml, ""); // Remove restricted attribute's values - finalHtml = Regex.Replace(finalHtml, Constants.REGEX_NAME_CLASS, ""); - finalHtml = Regex.Replace(finalHtml, Constants.REGEX_NAME_ID, ""); + finalHtml = new Regex(Constants.REGEX_NAME_CLASS).Replace(finalHtml, ""); + finalHtml = new Regex(Constants.REGEX_NAME_ID).Replace(finalHtml, ""); return finalHtml; } @@ -226,7 +225,7 @@ private string InsertCompulsoryMarkupAndCss(string finalHtml) { // Save the original tag before replacement later String headTag = ""; - Match m = Regex.Match(finalHtml, Constants.REGEX_HEAD); + Match m = Constants.HeadRegex.Match(finalHtml); if (m.Success) { headTag = m.Value; @@ -253,8 +252,7 @@ private string InsertCompulsoryMarkupAndCss(string finalHtml) Constants.AMP_BOILERPLATE_CODE + Constants.NEW_LINE + String.Format(Constants.AMP_CUSTOM_STYLE, styles) + Constants.NEW_LINE; - finalHtml = Regex.Replace(finalHtml, Constants.REGEX_HEAD, headTag); - return finalHtml; + return Constants.HeadRegex.Replace(finalHtml, headTag); } diff --git a/Kentico.AcceleratedMobilePages/Constants.cs b/Kentico.AcceleratedMobilePages/Constants.cs index e785ef0..30b8e28 100644 --- a/Kentico.AcceleratedMobilePages/Constants.cs +++ b/Kentico.AcceleratedMobilePages/Constants.cs @@ -1,4 +1,6 @@ -namespace Kentico.AcceleratedMobilePages +using System.Text.RegularExpressions; + +namespace Kentico.AcceleratedMobilePages { public class Constants { @@ -34,7 +36,8 @@ public class Constants public const string HTML_REPLACEMENT = "