From 8bbe1b7592c1ce5deb599e84cdf09b63661b2f0d Mon Sep 17 00:00:00 2001 From: Hao Kung Date: Thu, 11 Mar 2021 15:10:48 -0800 Subject: [PATCH 1/5] Remove regex use --- .../src/TypeForwardingActivator.cs | 25 ++++++++++++++++--- 1 file changed, 22 insertions(+), 3 deletions(-) diff --git a/src/DataProtection/DataProtection/src/TypeForwardingActivator.cs b/src/DataProtection/DataProtection/src/TypeForwardingActivator.cs index bf3113eadacf..b2f02dbde67c 100644 --- a/src/DataProtection/DataProtection/src/TypeForwardingActivator.cs +++ b/src/DataProtection/DataProtection/src/TypeForwardingActivator.cs @@ -2,7 +2,6 @@ // Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. using System; -using System.Text.RegularExpressions; using Microsoft.Extensions.Logging; using Microsoft.Extensions.Logging.Abstractions; @@ -13,7 +12,6 @@ internal class TypeForwardingActivator : SimpleActivator private const string OldNamespace = "Microsoft.AspNet.DataProtection"; private const string CurrentNamespace = "Microsoft.AspNetCore.DataProtection"; private readonly ILogger _logger; - private static readonly Regex _versionPattern = new Regex(@",\s?Version=[0-9]+(\.[0-9]+){0,3}", RegexOptions.Compiled, TimeSpan.FromSeconds(2)); public TypeForwardingActivator(IServiceProvider services) : this(services, NullLoggerFactory.Instance) @@ -64,6 +62,27 @@ internal object CreateInstance(Type expectedBaseType, string originalTypeName, o } protected string RemoveVersionFromAssemblyName(string forwardedTypeName) - => _versionPattern.Replace(forwardedTypeName, ""); + { + // Type, Assembly, Version={Version}, Culture={Culture}, PublicKeyToken={Token} + + var versionStartIndex = forwardedTypeName.IndexOf("Version=", StringComparison.Ordinal); + while (versionStartIndex != -1) + { + var versionEndIndex = forwardedTypeName.IndexOf(',', versionStartIndex + "Version=".Length); + + if (versionEndIndex == -1) + { + // No end index? + return forwardedTypeName; + } + + forwardedTypeName = forwardedTypeName.Substring(0, versionStartIndex - 1) + forwardedTypeName.Substring(versionEndIndex + 1); + versionStartIndex = forwardedTypeName.IndexOf("Version=", StringComparison.Ordinal); + } + + // No version left + return forwardedTypeName; + + } } } From d71aa023ab989379fc06817d41ee5f405e8b6a5a Mon Sep 17 00:00:00 2001 From: Hao Kung Date: Mon, 15 Mar 2021 10:56:31 -0700 Subject: [PATCH 2/5] Use remove --- .../DataProtection/src/TypeForwardingActivator.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/DataProtection/DataProtection/src/TypeForwardingActivator.cs b/src/DataProtection/DataProtection/src/TypeForwardingActivator.cs index b2f02dbde67c..eb5eaaf2b008 100644 --- a/src/DataProtection/DataProtection/src/TypeForwardingActivator.cs +++ b/src/DataProtection/DataProtection/src/TypeForwardingActivator.cs @@ -76,7 +76,7 @@ protected string RemoveVersionFromAssemblyName(string forwardedTypeName) return forwardedTypeName; } - forwardedTypeName = forwardedTypeName.Substring(0, versionStartIndex - 1) + forwardedTypeName.Substring(versionEndIndex + 1); + forwardedTypeName = forwardedTypeName.Remove(versionStartIndex, versionEndIndex - versionStartIndex); versionStartIndex = forwardedTypeName.IndexOf("Version=", StringComparison.Ordinal); } From 9256b9318a6b5f84d0d5458d35525f3211ddcd53 Mon Sep 17 00:00:00 2001 From: Hao Kung Date: Mon, 15 Mar 2021 14:35:57 -0700 Subject: [PATCH 3/5] Fix count --- .../DataProtection/src/TypeForwardingActivator.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/DataProtection/DataProtection/src/TypeForwardingActivator.cs b/src/DataProtection/DataProtection/src/TypeForwardingActivator.cs index eb5eaaf2b008..f752ab8e6a3c 100644 --- a/src/DataProtection/DataProtection/src/TypeForwardingActivator.cs +++ b/src/DataProtection/DataProtection/src/TypeForwardingActivator.cs @@ -76,7 +76,7 @@ protected string RemoveVersionFromAssemblyName(string forwardedTypeName) return forwardedTypeName; } - forwardedTypeName = forwardedTypeName.Remove(versionStartIndex, versionEndIndex - versionStartIndex); + forwardedTypeName = forwardedTypeName.Remove(versionStartIndex, versionEndIndex - versionStartIndex + 2); versionStartIndex = forwardedTypeName.IndexOf("Version=", StringComparison.Ordinal); } From c498e08aefcdc1fbd10a8e9714324daa5fe34644 Mon Sep 17 00:00:00 2001 From: Hao Kung Date: Mon, 15 Mar 2021 15:45:02 -0700 Subject: [PATCH 4/5] Throw when no end index found --- .../DataProtection/src/TypeForwardingActivator.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/DataProtection/DataProtection/src/TypeForwardingActivator.cs b/src/DataProtection/DataProtection/src/TypeForwardingActivator.cs index f752ab8e6a3c..a4f8289bf4c6 100644 --- a/src/DataProtection/DataProtection/src/TypeForwardingActivator.cs +++ b/src/DataProtection/DataProtection/src/TypeForwardingActivator.cs @@ -73,7 +73,7 @@ protected string RemoveVersionFromAssemblyName(string forwardedTypeName) if (versionEndIndex == -1) { // No end index? - return forwardedTypeName; + throw new InvalidOperationException("Unexpected end of version string."); } forwardedTypeName = forwardedTypeName.Remove(versionStartIndex, versionEndIndex - versionStartIndex + 2); From cab77ec8244f96262d5ca71cb4f06a6dbc03e8f8 Mon Sep 17 00:00:00 2001 From: Hao Kung Date: Mon, 15 Mar 2021 18:06:02 -0700 Subject: [PATCH 5/5] Fix tests --- .../DataProtection/src/TypeForwardingActivator.cs | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/src/DataProtection/DataProtection/src/TypeForwardingActivator.cs b/src/DataProtection/DataProtection/src/TypeForwardingActivator.cs index a4f8289bf4c6..6f9e1bf02280 100644 --- a/src/DataProtection/DataProtection/src/TypeForwardingActivator.cs +++ b/src/DataProtection/DataProtection/src/TypeForwardingActivator.cs @@ -65,19 +65,19 @@ protected string RemoveVersionFromAssemblyName(string forwardedTypeName) { // Type, Assembly, Version={Version}, Culture={Culture}, PublicKeyToken={Token} - var versionStartIndex = forwardedTypeName.IndexOf("Version=", StringComparison.Ordinal); + var versionStartIndex = forwardedTypeName.IndexOf(", Version=", StringComparison.Ordinal); while (versionStartIndex != -1) { - var versionEndIndex = forwardedTypeName.IndexOf(',', versionStartIndex + "Version=".Length); + var versionEndIndex = forwardedTypeName.IndexOf(',', versionStartIndex + ", Version=".Length); if (versionEndIndex == -1) { - // No end index? - throw new InvalidOperationException("Unexpected end of version string."); + // No end index, so are done and can remove the rest + return forwardedTypeName.Substring(0, versionStartIndex); } - forwardedTypeName = forwardedTypeName.Remove(versionStartIndex, versionEndIndex - versionStartIndex + 2); - versionStartIndex = forwardedTypeName.IndexOf("Version=", StringComparison.Ordinal); + forwardedTypeName = forwardedTypeName.Remove(versionStartIndex, versionEndIndex - versionStartIndex); + versionStartIndex = forwardedTypeName.IndexOf(", Version=", StringComparison.Ordinal); } // No version left