StrBuilder

Source: lib/StrBuilder.cs

This page is generated from the source by scripts/gen-reference.py.

StrBuilder: growable string buffer backed by an Arena, with $"..." support.

StrBuilder sb = StrBuilder.Create(arena);
sb.Append("total: ");
sb.Append($"{count} items, {price:F2} each");
Out.Line(sb.AsSpan());

Like List, a StrBuilder is a handle to a header in the arena, so copies share the same contents, and default(StrBuilder) is "not created": it reads as empty and Append returns false. Append also returns false when the arena is full. Growing extends the buffer in place while it is still the arena's most recent allocation; otherwise it copies into a fresh buffer and the old one stays allocated until the arena is reset.

The buffer is laid out like a string object, MethodTable*[chars], so Str.Format(arena, $"...") turns a finished buffer into a string without copying.

Members

Create

public static StrBuilder Create(Arena arena, int initialCapacity = 64)

Returns a new builder in arena, or a not-created one if it is out of memory.

IsCreated

public bool IsCreated

Length

public int Length

AsSpan

public ReadOnlySpan<char> AsSpan()

The text so far. Valid until the next Append or until the arena is reset.

Append

public bool Append(ReadOnlySpan<char> text)
public bool Append(char value)
public bool Append(long value)
public bool Append(ref InterpolatedHandler handler)

Appends $"..." directly, without an intermediate string.

Clear

public void Clear()

ToArenaString

public string ToArenaString()

Copies the text into a new string in the builder's own arena. Returns null if it is out of memory.

public string ToArenaString(Arena target)

Copies the text into a new string in target: build in a scratch arena, keep the result in a longer-lived one.