using System.Text;
using BenchmarkDotNet.Attributes;
using Tui = Terminal.Gui;
namespace Terminal.Gui.Benchmarks.Text.TextFormatter;
///
/// Benchmarks for performance fine-tuning.
///
[MemoryDiagnoser]
[BenchmarkCategory (nameof(Tui.TextFormatter))]
public class RemoveHotKeySpecifier
{
// Omit from summary table.
private static readonly Rune HotkeySpecifier = (Rune)'_';
///
/// Benchmark for previous implementation.
///
[Benchmark]
[ArgumentsSource (nameof (DataSource))]
public string Previous (string text, int hotPos)
{
return StringConcatLoop (text, hotPos, HotkeySpecifier);
}
///
/// Benchmark for current implementation with stackalloc char buffer and fallback to rented array.
///
[Benchmark (Baseline = true)]
[ArgumentsSource (nameof (DataSource))]
public string Current (string text, int hotPos)
{
return Tui.TextFormatter.RemoveHotKeySpecifier (text, hotPos, HotkeySpecifier);
}
///
/// Previous implementation with string concatenation in a loop.
///
public static string StringConcatLoop (string text, int hotPos, Rune hotKeySpecifier)
{
if (string.IsNullOrEmpty (text))
{
return text;
}
// Scan
var start = string.Empty;
var i = 0;
foreach (Rune c in text.EnumerateRunes ())
{
if (c == hotKeySpecifier && i == hotPos)
{
i++;
continue;
}
start += c;
i++;
}
return start;
}
public IEnumerable