Adds rune width caching for cell length calculations

This commit is contained in:
Phil Scott 2021-04-04 12:56:04 -04:00 committed by Patrik Svensson
parent fe5096dceb
commit 36ec3d1fd3

View File

@ -4,11 +4,14 @@ namespace Spectre.Console
{ {
internal static class Cell internal static class Cell
{ {
private static readonly int?[] _runeWidthCache = new int?[char.MaxValue];
public static int GetCellLength(string text) public static int GetCellLength(string text)
{ {
var sum = 0; var sum = 0;
foreach (var rune in text) for (var index = 0; index < text.Length; index++)
{ {
var rune = text[index];
sum += GetCellLength(rune); sum += GetCellLength(rune);
} }
@ -28,7 +31,7 @@ namespace Spectre.Console
return 1; return 1;
} }
return UnicodeCalculator.GetWidth(rune); return _runeWidthCache[rune] ??= UnicodeCalculator.GetWidth(rune);
} }
} }
} }