From: Ben Adams Date: Sun, 5 Feb 2017 08:08:56 +0000 (+0000) Subject: Use StringBuilderCache for Enum.InternalFlagsFormat (#9340) X-Git-Tag: accepted/tizen/base/20180629.140029~2372 X-Git-Url: http://review.tizen.org/git/?a=commitdiff_plain;h=bb8c87648f47940f7924e249c34b620bc19a9165;p=platform%2Fupstream%2Fcoreclr.git Use StringBuilderCache for Enum.InternalFlagsFormat (#9340) --- diff --git a/src/mscorlib/src/System/Enum.cs b/src/mscorlib/src/System/Enum.cs index 91b580c..b3efc76 100644 --- a/src/mscorlib/src/System/Enum.cs +++ b/src/mscorlib/src/System/Enum.cs @@ -157,7 +157,7 @@ namespace System Debug.Assert(names.Length == values.Length); int index = values.Length - 1; - StringBuilder retval = new StringBuilder(); + StringBuilder sb = StringBuilderCache.Acquire(); bool firstTime = true; ulong saveResult = result; @@ -173,31 +173,40 @@ namespace System { result -= values[index]; if (!firstTime) - retval.Insert(0, enumSeparatorString); + sb.Insert(0, enumSeparatorString); - retval.Insert(0, names[index]); + sb.Insert(0, names[index]); firstTime = false; } index--; } - // We were unable to represent this number as a bitwise or of valid flags + string returnString; if (result != 0) - return null; // return null so the caller knows to .ToString() the input - - // For the case when we have zero - if (saveResult == 0) { + // We were unable to represent this number as a bitwise or of valid flags + returnString = null; // return null so the caller knows to .ToString() the input + } + else if (saveResult == 0) + { + // For the cases when we have zero if (values.Length > 0 && values[0] == 0) - return names[0]; // Zero was one of the enum values. + { + returnString = names[0]; // Zero was one of the enum values. + } else - return "0"; + { + returnString = "0"; + } } else { - return retval.ToString(); // Return the string representation + returnString = sb.ToString(); // Return the string representation } + + StringBuilderCache.Release(sb); + return returnString; } internal static ulong ToUInt64(Object value)