From 36ec3d1fd3d0940e76f81482b9a834265155c5a3 Mon Sep 17 00:00:00 2001 From: Phil Scott Date: Sun, 4 Apr 2021 12:56:04 -0400 Subject: [PATCH] Adds rune width caching for cell length calculations --- src/Spectre.Console/Internal/Cell.cs | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/src/Spectre.Console/Internal/Cell.cs b/src/Spectre.Console/Internal/Cell.cs index e30fe2a..3bdfd9d 100644 --- a/src/Spectre.Console/Internal/Cell.cs +++ b/src/Spectre.Console/Internal/Cell.cs @@ -4,11 +4,14 @@ namespace Spectre.Console { internal static class Cell { + private static readonly int?[] _runeWidthCache = new int?[char.MaxValue]; + public static int GetCellLength(string text) { var sum = 0; - foreach (var rune in text) + for (var index = 0; index < text.Length; index++) { + var rune = text[index]; sum += GetCellLength(rune); } @@ -28,7 +31,7 @@ namespace Spectre.Console return 1; } - return UnicodeCalculator.GetWidth(rune); + return _runeWidthCache[rune] ??= UnicodeCalculator.GetWidth(rune); } } }