Skip to content
Merged
Show file tree
Hide file tree
Changes from 5 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
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,11 @@ public static void Free(StringBuilder builder)

public static string ReturnAndFree(StringBuilder builder)
{
SharedPools.Default<StringBuilder>();
return builder.ToString();
string result = builder.ToString();

StringBuilderPool.Free(builder);

return result;
}
}
}
88 changes: 82 additions & 6 deletions StyleCop.Analyzers/StyleCop.Analyzers/Helpers/XmlCommentHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ namespace StyleCop.Analyzers.Helpers
{
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Xml.Linq;
using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.CSharp.Syntax;
Expand Down Expand Up @@ -268,20 +267,97 @@ internal static string GetText(XmlTextSyntax textElement, bool normalizeWhitespa
return null;
}

StringBuilder stringBuilder = StringBuilderPool.Allocate();
bool lastWhitespace = false;

string single = string.Empty;

StringBuilder stringBuilder = null;

foreach (var item in textElement.TextTokens)
{
stringBuilder.Append(item);
if (single.Length == 0)
{
single = item.ToString();
}
else
{
if (stringBuilder == null)
{
stringBuilder = StringBuilderPool.Allocate();
stringBuilder.AppendNormalize(single, normalizeWhitespace, ref lastWhitespace);
}

stringBuilder.AppendNormalize(item.ToString(), normalizeWhitespace, ref lastWhitespace);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

❔ Is it common for this line to be hit when lastWhitespace is true and item is pure whitespace?

}
}

string result = StringBuilderPool.ReturnAndFree(stringBuilder);
if (stringBuilder == null)
{
if (normalizeWhitespace)
{
stringBuilder = StringBuilderPool.Allocate();

if (!stringBuilder.AppendNormalize(single, normalizeWhitespace, ref lastWhitespace))
{
StringBuilderPool.Free(stringBuilder);

// No change is needed, return original string.
return single;
}
}
else
{
return single;
}
}

return StringBuilderPool.ReturnAndFree(stringBuilder);
}

/// <summary>
/// Append to StringBuilder and perform white space normalization.
/// </summary>
/// <returns>True if output is different.</returns>
internal static bool AppendNormalize(this StringBuilder builder, string text, bool normalizeWhitespace, ref bool lastWhitespace)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

❔ Can we reasonably omit the normalizeWhitespace parameter, and update callers that passed false to call Append instead?

{
bool diff = false;

if (normalizeWhitespace)
{
result = Regex.Replace(result, @"\s+", " ");
foreach (char ch in text)
{
if (char.IsWhiteSpace(ch))
{
if (lastWhitespace)
{
diff = true;
}
else
{
if (ch != ' ')
{
diff = true;
}

builder.Append(' ');
}

lastWhitespace = true;
}
else
{
builder.Append(ch);

lastWhitespace = false;
}
}
}
else
{
builder.Append(text);
}

return result;
return diff;
}

internal static T GetFirstAttributeOrDefault<T>(XmlNodeSyntax nodeSyntax)
Expand Down