C
C#7mo 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
Angius7mo ago
Char.ConvertFromUtf32(Int32)?
wasabi
wasabiOP7mo 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
reflectronic7mo 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
wasabiOP7mo ago
I basically did that though without copying it.
reflectronic
reflectronic7mo ago
well. that is probably the most efficient way of doing it
wasabi
wasabiOP7mo ago
Figured.
Sehra
Sehra7mo 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?