mirror of
https://github.com/icsharpcode/ILSpy.git
synced 2026-10-12 02:15:58 +00:00
SplitTypeParameterCountFromReflectionName allocated the digits after the backtick only to feed int.TryParse and discard them, and the TopLevelTypeName constructor cut the name part twice for generic types (once at the dot, again at the backtick), dropping the first cut. Both run for every generic reflection name parsed, e.g. for typeof-valued attribute arguments and string-switch metadata. The arity is now parsed in place with a digit loop (netstandard2.0 has no span int.TryParse) and each final string is cut exactly once. The digit loop only accepts plain ASCII digits, so suffixes like `+1 that int.TryParse tolerated are now rejected; such names are not legal reflection names. New unit tests pin the parse edge cases. Assisted-by: Claude:claude-fable-5:Claude Code
152 lines
4.7 KiB
C#
152 lines
4.7 KiB
C#
// Copyright (c) 2010-2013 AlphaSierraPapa for the SharpDevelop Team
|
|
//
|
|
// Permission is hereby granted, free of charge, to any person obtaining a copy of this
|
|
// software and associated documentation files (the "Software"), to deal in the Software
|
|
// without restriction, including without limitation the rights to use, copy, modify, merge,
|
|
// publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons
|
|
// to whom the Software is furnished to do so, subject to the following conditions:
|
|
//
|
|
// The above copyright notice and this permission notice shall be included in all copies or
|
|
// substantial portions of the Software.
|
|
//
|
|
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
|
|
// INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
|
|
// PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE
|
|
// FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
|
|
// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
|
|
// DEALINGS IN THE SOFTWARE.
|
|
|
|
using System;
|
|
using System.Collections.Generic;
|
|
using System.Text;
|
|
|
|
namespace ICSharpCode.Decompiler.TypeSystem
|
|
{
|
|
/// <summary>
|
|
/// Holds the name of a top-level type.
|
|
/// This struct cannot refer to nested classes.
|
|
/// </summary>
|
|
[Serializable]
|
|
public readonly struct TopLevelTypeName : IEquatable<TopLevelTypeName>
|
|
{
|
|
readonly string namespaceName;
|
|
readonly string name;
|
|
readonly int typeParameterCount;
|
|
|
|
public TopLevelTypeName(string namespaceName, string name, int typeParameterCount = 0)
|
|
{
|
|
if (namespaceName == null)
|
|
throw new ArgumentNullException(nameof(namespaceName));
|
|
if (name == null)
|
|
throw new ArgumentNullException(nameof(name));
|
|
this.namespaceName = namespaceName;
|
|
this.name = name;
|
|
this.typeParameterCount = typeParameterCount;
|
|
}
|
|
|
|
public TopLevelTypeName(string reflectionName)
|
|
{
|
|
// Locate both separators up front so that namespaceName and name are each cut
|
|
// exactly once, without an intermediate string still carrying the arity suffix.
|
|
int pos = reflectionName.LastIndexOf('.');
|
|
int tick = reflectionName.LastIndexOf('`');
|
|
if (tick > pos && ReflectionHelper.TryParseTypeParameterCount(reflectionName, tick + 1, out typeParameterCount))
|
|
{
|
|
name = reflectionName.Substring(pos + 1, tick - pos - 1);
|
|
}
|
|
else
|
|
{
|
|
typeParameterCount = 0;
|
|
name = reflectionName.Substring(pos + 1);
|
|
}
|
|
namespaceName = pos < 0 ? string.Empty : reflectionName.Substring(0, pos);
|
|
}
|
|
|
|
public string Namespace {
|
|
get { return namespaceName; }
|
|
}
|
|
|
|
public string Name {
|
|
get { return name; }
|
|
}
|
|
|
|
public int TypeParameterCount {
|
|
get { return typeParameterCount; }
|
|
}
|
|
|
|
public string ReflectionName {
|
|
get {
|
|
StringBuilder b = new StringBuilder();
|
|
if (!string.IsNullOrEmpty(namespaceName))
|
|
{
|
|
b.Append(namespaceName);
|
|
b.Append('.');
|
|
}
|
|
b.Append(name);
|
|
if (typeParameterCount > 0)
|
|
{
|
|
b.Append('`');
|
|
b.Append(typeParameterCount);
|
|
}
|
|
return b.ToString();
|
|
}
|
|
}
|
|
|
|
public override string ToString()
|
|
{
|
|
return this.ReflectionName;
|
|
}
|
|
|
|
public override bool Equals(object obj)
|
|
{
|
|
return (obj is TopLevelTypeName) && Equals((TopLevelTypeName)obj);
|
|
}
|
|
|
|
public bool Equals(TopLevelTypeName other)
|
|
{
|
|
return this.namespaceName == other.namespaceName && this.name == other.name && this.typeParameterCount == other.typeParameterCount;
|
|
}
|
|
|
|
public override int GetHashCode()
|
|
{
|
|
return (name != null ? name.GetHashCode() : 0) ^ (namespaceName != null ? namespaceName.GetHashCode() : 0) ^ typeParameterCount;
|
|
}
|
|
|
|
public static bool operator ==(TopLevelTypeName lhs, TopLevelTypeName rhs)
|
|
{
|
|
return lhs.Equals(rhs);
|
|
}
|
|
|
|
public static bool operator !=(TopLevelTypeName lhs, TopLevelTypeName rhs)
|
|
{
|
|
return !lhs.Equals(rhs);
|
|
}
|
|
}
|
|
|
|
[Serializable]
|
|
public sealed class TopLevelTypeNameComparer : IEqualityComparer<TopLevelTypeName>
|
|
{
|
|
public static readonly TopLevelTypeNameComparer Ordinal = new TopLevelTypeNameComparer(StringComparer.Ordinal);
|
|
public static readonly TopLevelTypeNameComparer OrdinalIgnoreCase = new TopLevelTypeNameComparer(StringComparer.OrdinalIgnoreCase);
|
|
|
|
public readonly StringComparer NameComparer;
|
|
|
|
public TopLevelTypeNameComparer(StringComparer nameComparer)
|
|
{
|
|
this.NameComparer = nameComparer;
|
|
}
|
|
|
|
public bool Equals(TopLevelTypeName x, TopLevelTypeName y)
|
|
{
|
|
return x.TypeParameterCount == y.TypeParameterCount
|
|
&& NameComparer.Equals(x.Name, y.Name)
|
|
&& NameComparer.Equals(x.Namespace, y.Namespace);
|
|
}
|
|
|
|
public int GetHashCode(TopLevelTypeName obj)
|
|
{
|
|
return NameComparer.GetHashCode(obj.Name) ^ NameComparer.GetHashCode(obj.Namespace) ^ obj.TypeParameterCount;
|
|
}
|
|
}
|
|
}
|