Skip to content

Commit

Permalink
Remove regex use in DataProtection (#30855)
Browse files Browse the repository at this point in the history
  • Loading branch information
HaoK authored Mar 16, 2021
1 parent c0a2024 commit 95bf141
Showing 1 changed file with 22 additions and 3 deletions.
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;

}
}
}

0 comments on commit 95bf141

Please sign in to comment.