Skip to content

Commit

Permalink
Apply optimizations inside AppendDisplayName
Browse files Browse the repository at this point in the history
(Note: The changed method's logic for dealing with generic type names
is way too simplistic. For example, it'll fail badly with nested types
or with types where the generic argument is yet another generic type.
Changing this isn't trivial, but perhaps should be done some time.)
  • Loading branch information
stakx committed Nov 3, 2017
1 parent 0068af4 commit c864081
Showing 1 changed file with 23 additions and 29 deletions.
52 changes: 23 additions & 29 deletions Source/ExpressionStringBuilder.cs
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
using System;
using System.Collections;
using System.Collections.Generic;
using System.Globalization;
using System.Collections.ObjectModel;
using System.Diagnostics;
using System.Linq;
using System.Linq.Expressions;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Text;
using System.Collections.ObjectModel;

using Moq.Properties;

namespace Moq
Expand Down Expand Up @@ -629,45 +630,38 @@ internal static string ToStringOperator(ExpressionType nodeType)
}
}

internal static class StringExtensions
{
public static string AsCommaSeparatedValues(this IEnumerable<string> source)
{
if (source == null)
{
return string.Empty;
}
var result = new StringBuilder(100);
bool appendComma = false;
foreach (var value in source)
{
if (appendComma)
{
result.Append(", ");
}
result.Append(value);
appendComma = true;
}
return result.ToString();
}
}

internal static class StringBuilderExtensions
{
public static StringBuilder AppendDisplayName(this StringBuilder builder, Type source, Func<Type, string> getName)
{
if (source == null)
Debug.Assert(source != null);

var name = getName(source);
var backtickIndex = name.IndexOf('`');
if (backtickIndex >= 0)
{
throw new ArgumentNullException("source");
builder.Append(name, 0, backtickIndex);
}
else
{
builder.Append(name);
}

builder.Append(getName(source).Split('`').First());
if (source.GetTypeInfo().IsGenericType)
{
var genericArguments = source.GetGenericArguments();
builder.Append('<');
builder.Append(source.GetGenericArguments().Select(t => getName(t)).AsCommaSeparatedValues());
for (int i = 0, n = genericArguments.Length; i < n; ++i)
{
if (i > 0)
{
builder.Append(", ");
}
builder.Append(getName(genericArguments[i]));
}
builder.Append('>');
}

return builder;
}
}
Expand Down

0 comments on commit c864081

Please sign in to comment.