C
C#2mo ago
wasabi

Appending to StringBuilder by Unicode code point?

What might be the most efficient way to append a unicode code point (int32) to a StringBuilder, taking into account I need it to work on Framework.
7 Replies
Angius
Angius2mo ago
Char.ConvertFromUtf32(Int32)?
wasabi
wasabiOP2mo ago
That allocates a string[] Which would be very heavy when working with ... large amounts of text. I kinda just ended up, so far, doing it myself, writing 1 or 2 chars to a Span<char>, then appending that.
reflectronic
reflectronic2mo ago
https://github.com/microsoft/referencesource/blob/master/mscorlib/system/char.cs#L921-L940 copy this but replace the array with char* surrogate = stackalloc char[2]; then use Append(char*, int)
wasabi
wasabiOP2mo ago
I basically did that though without copying it.
reflectronic
reflectronic2mo ago
well. that is probably the most efficient way of doing it
wasabi
wasabiOP2mo ago
Figured.
Sehra
Sehra2mo ago
void Append(StringBuilder sb, int cp)
{
if (cp > 0xFFFF)
{
sb.Append((char)((cp - 0x10000) / 0x400 + 0xD800));
sb.Append((char)((cp - 0x10000) % 0x400 + 0xDC00));
}
else
{
sb.Append((char)cp);
}
}
void Append(StringBuilder sb, int cp)
{
if (cp > 0xFFFF)
{
sb.Append((char)((cp - 0x10000) / 0x400 + 0xD800));
sb.Append((char)((cp - 0x10000) % 0x400 + 0xDC00));
}
else
{
sb.Append((char)cp);
}
}

Did you find this page helpful?