Skip to content

Remove regex use in DataProtection #30855

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Mar 16, 2021
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 22 additions & 3 deletions src/DataProtection/DataProtection/src/TypeForwardingActivator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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)
Expand Down Expand Up @@ -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, so are done and can remove the rest
return forwardedTypeName.Substring(0, versionStartIndex);
}

forwardedTypeName = forwardedTypeName.Remove(versionStartIndex, versionEndIndex - versionStartIndex);
versionStartIndex = forwardedTypeName.IndexOf(", Version=", StringComparison.Ordinal);
}

// No version left
return forwardedTypeName;

}
}
}