Skip to content

Commit 95bf141

Browse files
authored
Remove regex use in DataProtection (#30855)
1 parent c0a2024 commit 95bf141

File tree

1 file changed

+22
-3
lines changed

1 file changed

+22
-3
lines changed

src/DataProtection/DataProtection/src/TypeForwardingActivator.cs

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
33

44
using System;
5-
using System.Text.RegularExpressions;
65
using Microsoft.Extensions.Logging;
76
using Microsoft.Extensions.Logging.Abstractions;
87

@@ -13,7 +12,6 @@ internal class TypeForwardingActivator : SimpleActivator
1312
private const string OldNamespace = "Microsoft.AspNet.DataProtection";
1413
private const string CurrentNamespace = "Microsoft.AspNetCore.DataProtection";
1514
private readonly ILogger _logger;
16-
private static readonly Regex _versionPattern = new Regex(@",\s?Version=[0-9]+(\.[0-9]+){0,3}", RegexOptions.Compiled, TimeSpan.FromSeconds(2));
1715

1816
public TypeForwardingActivator(IServiceProvider services)
1917
: this(services, NullLoggerFactory.Instance)
@@ -64,6 +62,27 @@ internal object CreateInstance(Type expectedBaseType, string originalTypeName, o
6462
}
6563

6664
protected string RemoveVersionFromAssemblyName(string forwardedTypeName)
67-
=> _versionPattern.Replace(forwardedTypeName, "");
65+
{
66+
// Type, Assembly, Version={Version}, Culture={Culture}, PublicKeyToken={Token}
67+
68+
var versionStartIndex = forwardedTypeName.IndexOf(", Version=", StringComparison.Ordinal);
69+
while (versionStartIndex != -1)
70+
{
71+
var versionEndIndex = forwardedTypeName.IndexOf(',', versionStartIndex + ", Version=".Length);
72+
73+
if (versionEndIndex == -1)
74+
{
75+
// No end index, so are done and can remove the rest
76+
return forwardedTypeName.Substring(0, versionStartIndex);
77+
}
78+
79+
forwardedTypeName = forwardedTypeName.Remove(versionStartIndex, versionEndIndex - versionStartIndex);
80+
versionStartIndex = forwardedTypeName.IndexOf(", Version=", StringComparison.Ordinal);
81+
}
82+
83+
// No version left
84+
return forwardedTypeName;
85+
86+
}
6887
}
6988
}

0 commit comments

Comments
 (0)