mirror of
https://github.com/PowerShell/PowerShell
synced 2026-06-08 12:12:50 +00:00
Formatting: Use cache for dash padding strings (#6625)
This commit is contained in:
@@ -180,7 +180,7 @@ namespace Microsoft.PowerShell.Commands.Internal.Format
|
||||
// NOTE: we can do this because "-" is a single cell character
|
||||
// on all devices. If changed to some other character, this assumption
|
||||
// would be invalidated
|
||||
breakLine[k] = new string('-', count);
|
||||
breakLine[k] = StringUtil.DashPadding(count);
|
||||
}
|
||||
GenerateRow(breakLine, lo, false, null, lo.DisplayCells);
|
||||
}
|
||||
|
||||
@@ -81,6 +81,24 @@ namespace System.Management.Automation.Internal
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
private const int DashCacheMax = 120;
|
||||
private static readonly string[] DashCache = new string[DashCacheMax];
|
||||
internal static string DashPadding(int count)
|
||||
{
|
||||
if (count >= DashCacheMax)
|
||||
return new string('-', count);
|
||||
|
||||
var result = DashCache[count];
|
||||
|
||||
if (result == null)
|
||||
{
|
||||
Interlocked.CompareExchange(ref DashCache[count], new string('-', count), null);
|
||||
result = DashCache[count];
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
}
|
||||
} // namespace
|
||||
|
||||
|
||||
Reference in New Issue
Block a user