-
An overload to Here's an extension method that can be used as a workaround, and is how it could be implemented: public static void AppendRune(this StringBuilder StringBuilder, Rune Rune) {
Span<char> Chars = stackalloc char[2];
int CharsWritten = Rune.EncodeToUtf16(Chars);
StringBuilder.Append(Chars[..CharsWritten]);
} The benefit of this is that, currently, using Benchmarks: See codeusing BenchmarkDotNet.Attributes;
using BenchmarkDotNet.Running;
using System.Text;
namespace RuneStringBuilderBenchmarks;
public class Program {
public static void Main() {
BenchmarkRunner.Run<Benchmarks>();
}
}
[MemoryDiagnoser]
public class Benchmarks {
private const int IterationCount = 10_000;
[Benchmark]
public void AppendRune() {
StringBuilder StringBuilder = new();
Rune Rune = (Rune)'a';
for (int Counter = 0; Counter < IterationCount; Counter++) {
StringBuilder.AppendRune(Rune);
}
}
[Benchmark]
public void AppendObject() {
StringBuilder StringBuilder = new();
Rune Rune = (Rune)'a';
for (int Counter = 0; Counter < IterationCount; Counter++) {
StringBuilder.Append(Rune);
}
}
[Benchmark]
public void AppendRuneSurrogatePair() {
StringBuilder StringBuilder = new();
Rune Rune = new('\uD83D', '\uDE00'); // 😀 (grinning face emoji)
for (int Counter = 0; Counter < IterationCount; Counter++) {
StringBuilder.AppendRune(Rune);
}
}
[Benchmark]
public void AppendObjectSurrogatePair() {
StringBuilder StringBuilder = new();
Rune Rune = new('\uD83D', '\uDE00'); // 😀 (grinning face emoji)
for (int Counter = 0; Counter < IterationCount; Counter++) {
StringBuilder.Append(Rune);
}
}
}
public static class Extensions {
public static void AppendRune(this StringBuilder StringBuilder, Rune Rune) {
Span<char> Chars = stackalloc char[2];
int CharsWritten = Rune.EncodeToUtf16(Chars);
StringBuilder.Append(Chars[..CharsWritten]);
}
} Specs:
Results:
|
Beta Was this translation helpful? Give feedback.
Answered by
MihaZupan
Jan 7, 2025
Replies: 1 comment
-
public partial sealed class StringBuilder
{
public StringBuilder Append(Rune value);
} This API has already been approved in #27912, just not yet implemented. |
Beta Was this translation helpful? Give feedback.
0 replies
Answer selected by
MihaZupan
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This API has already been approved in #27912, just not yet implemented.