Open jhudsoncedaron opened 2 years ago
I couldn't figure out the best area label to add to this issue. If you have write-permissions please help me learn by adding exactly one area label.
Tagging subscribers to this area: @dotnet/area-system-runtime See info in area-owners.md if you want to be subscribed.
Author: | jhudsoncedaron |
---|---|
Assignees: | - |
Labels: | `api-suggestion`, `area-System.Runtime`, `untriaged` |
Milestone: | - |
StringBuilder Append(StringBuilder? value); // For symmetry
That already exists: https://docs.microsoft.com/en-us/dotnet/api/system.text.stringbuilder.append?view=net-6.0#system-text-stringbuilder-append(system-text-stringbuilder)
I will now investigate why I didn't find it. Ah. Only .Append(StringBuilder)
exists, not .Insert(int, Stringbuilder)
.
@stephentoub, do you think this is worth exposing? If so I'll mark as api-ready-for-review
I don't have a strong opinion about it. We added Append(StringBuilder), so if we believe there were scenarios for that, I can see wanting an Insert(int, StringBuilder) as well.
(Are there other argument types for which we have either Append or Insert but not the other?)
I didn't look. I only feed string
, char
, char[]
, Span<char>
, ReadOnlySpan<char>
, or another StringBuilder
to StringBuilder
. Everything else has to be converted via IFormatProvider
first.
There is an AppendFormat
but no InsertFormat
though. I haven't reached the optimization threshold where I have to tighten .Append(3.ToString(CultureInfo.something))
to .AppendFormat
yet.
Background and motivation
Had this appear where we are doing some really complex string building. There's no good way to feed one string builder into another, and partwise-insertion of strings with StringBuilder.Insert() results in way too many calls to MakeRoom and a very fragmented StringBuilder.
Analysis turned up a simple algorithm using two levels of StringBuilder; but it has extra copies because there is no
StringBuilder.Insert(int, StringBuilder)
API Proposal
Logically this would do
StringBuilder.Insert(int index, StringBuilder? value) => Insert(index, value?.ToString())
; but running inside StringBuilder itself there would be two fewer copies.API Usage
Alternative Designs
I have implemented a
StringBuilder
-like thing that builds up in a single array so that it can useStringBuilder.Insert(int, ReadOnlySpan<char>)
Risks
None