-
Notifications
You must be signed in to change notification settings - Fork 513
Make XmlCommentHelper faster #3651
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 5 commits
0a9f7f0
51e4f00
69c87bd
a7d4102
bf9df99
7b8c31d
589f040
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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; | ||
|
@@ -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); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ❔ Is it common for this line to be hit when |
||
} | ||
} | ||
|
||
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) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ❔ Can we reasonably omit the |
||
{ | ||
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) | ||
|
Uh oh!
There was an error while loading. Please reload this page.