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 ReplaceCRLFWithSpace
{
///
/// Benchmark for previous implementation.
///
[Benchmark]
[ArgumentsSource (nameof (DataSource))]
public string Previous (string str)
{
return ToRuneListReplaceImplementation (str);
}
///
/// Benchmark for current implementation.
///
[Benchmark (Baseline = true)]
[ArgumentsSource (nameof (DataSource))]
public string Current (string str)
{
return Tui.TextFormatter.ReplaceCRLFWithSpace (str);
}
///
/// Previous implementation with intermediate rune list.
///
///
///
private static string ToRuneListReplaceImplementation (string str)
{
var runes = str.ToRuneList ();
for (int i = 0; i < runes.Count; i++)
{
switch (runes [i].Value)
{
case '\n':
runes [i] = (Rune)' ';
break;
case '\r':
if ((i + 1) < runes.Count && runes [i + 1].Value == '\n')
{
runes [i] = (Rune)' ';
runes.RemoveAt (i + 1);
i++;
}
else
{
runes [i] = (Rune)' ';
}
break;
}
}
return Tui.StringExtensions.ToString (runes);
}
public IEnumerable