Skip to content

Commit c1d6dd3

Browse files
authored
Fix ordering of checks in AppendInterpolatedStringHandler.AppendFormatted (#79539)
We want to check for IsEnum first to avoid boxing the enum via ISpanFormattable.TryFormat. This just swaps the two blocks.
1 parent c03de20 commit c1d6dd3

File tree

1 file changed

+12
-12
lines changed

1 file changed

+12
-12
lines changed

src/libraries/System.Private.CoreLib/src/System/Text/StringBuilder.cs

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2539,7 +2539,18 @@ public void AppendFormatted<T>(T value)
25392539
// if it only implements IFormattable, we come out even: only if it implements both do we
25402540
// end up paying for an extra interface check.
25412541

2542-
if (value is ISpanFormattable)
2542+
if (typeof(T).IsEnum)
2543+
{
2544+
if (Enum.TryFormatUnconstrained(value, _stringBuilder.RemainingCurrentChunk, out int charsWritten))
2545+
{
2546+
_stringBuilder.m_ChunkLength += charsWritten;
2547+
}
2548+
else
2549+
{
2550+
AppendFormattedWithTempSpace(value, 0, format: null);
2551+
}
2552+
}
2553+
else if (value is ISpanFormattable)
25432554
{
25442555
Span<char> destination = _stringBuilder.RemainingCurrentChunk;
25452556
if (((ISpanFormattable)value).TryFormat(destination, out int charsWritten, default, _provider)) // constrained call avoiding boxing for value types
@@ -2560,17 +2571,6 @@ public void AppendFormatted<T>(T value)
25602571
AppendFormattedWithTempSpace(value, 0, format: null);
25612572
}
25622573
}
2563-
else if (typeof(T).IsEnum)
2564-
{
2565-
if (Enum.TryFormatUnconstrained(value, _stringBuilder.RemainingCurrentChunk, out int charsWritten))
2566-
{
2567-
_stringBuilder.m_ChunkLength += charsWritten;
2568-
}
2569-
else
2570-
{
2571-
AppendFormattedWithTempSpace(value, 0, format: null);
2572-
}
2573-
}
25742574
else
25752575
{
25762576
_stringBuilder.Append(((IFormattable)value).ToString(format: null, _provider)); // constrained call avoiding boxing for value types

0 commit comments

Comments
 (0)